forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListMethodImplementor.java
236 lines (206 loc) · 11.7 KB
/
ListMethodImplementor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package io.quarkus.rest.data.panache.deployment.methods;
import static io.quarkus.gizmo.MethodDescriptor.ofMethod;
import static io.quarkus.rest.data.panache.deployment.utils.PaginationImplementor.DEFAULT_PAGE_INDEX;
import static io.quarkus.rest.data.panache.deployment.utils.PaginationImplementor.DEFAULT_PAGE_SIZE;
import static io.quarkus.rest.data.panache.deployment.utils.SignatureMethodCreator.ofType;
import java.util.List;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.FieldDescriptor;
import io.quarkus.gizmo.MethodCreator;
import io.quarkus.gizmo.ResultHandle;
import io.quarkus.gizmo.TryBlock;
import io.quarkus.panache.common.Page;
import io.quarkus.panache.common.Sort;
import io.quarkus.rest.data.panache.RestDataResource;
import io.quarkus.rest.data.panache.deployment.Constants;
import io.quarkus.rest.data.panache.deployment.ResourceMetadata;
import io.quarkus.rest.data.panache.deployment.properties.ResourceProperties;
import io.quarkus.rest.data.panache.deployment.utils.PaginationImplementor;
import io.quarkus.rest.data.panache.deployment.utils.SignatureMethodCreator;
import io.quarkus.rest.data.panache.deployment.utils.SortImplementor;
import io.quarkus.rest.data.panache.deployment.utils.UniImplementor;
import io.smallrye.mutiny.Uni;
public final class ListMethodImplementor extends StandardMethodImplementor {
private static final String METHOD_NAME = "list";
private static final String RESOURCE_METHOD_NAME = "list";
private static final String EXCEPTION_MESSAGE = "Failed to list the entities";
private static final String REL = "list";
private final PaginationImplementor paginationImplementor;
private final SortImplementor sortImplementor = new SortImplementor();
public ListMethodImplementor(boolean isResteasyClassic, boolean isReactivePanache) {
super(isResteasyClassic, isReactivePanache);
this.paginationImplementor = new PaginationImplementor();
}
/**
* Generate JAX-RS GET method.
*
* The RESTEasy Classic version exposes {@link RestDataResource#list(Page, Sort)}
* and the generated pseudo-code with enabled pagination is shown below. If pagination is disabled pageIndex and pageSize
* query parameters are skipped and null {@link Page} instance is used.
*
* <pre>
* {@code
* @GET
* @Path("")
* @Produces({"application/json"})
* @LinkResource(
* rel = "list",
* entityClassName = "com.example.Entity"
* )
* public Response list(@QueryParam("page") @DefaultValue("0") int pageIndex,
* @QueryParam("size") @DefaultValue("20") int pageSize,
* @QueryParam("sort") String sortQuery) {
* Page page = Page.of(pageIndex, pageSize);
* Sort sort = ...; // Build a sort instance by parsing a query param
* try {
* List<Entity> entities = resource.getAll(page, sort);
* // Get the page count, and build first, last, next, previous page instances
* Response.ResponseBuilder responseBuilder = Response.status(200);
* responseBuilder.entity(entities);
* // Add headers with first, last, next and previous page URIs if they exist
* return responseBuilder.build();
* } catch (Throwable t) {
* throw new RestDataPanacheException(t);
* }
* }
* }
* </pre>
*
* The RESTEasy Reactive version exposes {@link io.quarkus.rest.data.panache.ReactiveRestDataResource#list(Page, Sort)}
* and the generated code looks more or less like this:
*
* <pre>
* {@code
* @GET
* @Path("")
* @Produces({"application/json"})
* @LinkResource(
* rel = "list",
* entityClassName = "com.example.Entity"
* )
* public Uni<Response> list(@QueryParam("page") @DefaultValue("0") int pageIndex,
* @QueryParam("size") @DefaultValue("20") int pageSize,
* @QueryParam("sort") String sortQuery) {
* Page page = Page.of(pageIndex, pageSize);
* Sort sort = ...; // Build a sort instance by parsing a query param
* try {
* return resource.getAll(page, sort).map(entities -> {
* // Get the page count, and build first, last, next, previous page instances
* Response.ResponseBuilder responseBuilder = Response.status(200);
* responseBuilder.entity(entities);
* // Add headers with first, last, next and previous page URIs if they exist
* return responseBuilder.build();
* });
*
* } catch (Throwable t) {
* throw new RestDataPanacheException(t);
* }
* }
* }
* </pre>
*/
@Override
protected void implementInternal(ClassCreator classCreator, ResourceMetadata resourceMetadata,
ResourceProperties resourceProperties, FieldDescriptor resourceField) {
if (resourceProperties.isPaged()) {
implementPaged(classCreator, resourceMetadata, resourceProperties, resourceField);
} else {
implementNotPaged(classCreator, resourceMetadata, resourceProperties, resourceField);
}
}
@Override
protected String getResourceMethodName() {
return RESOURCE_METHOD_NAME;
}
private void implementPaged(ClassCreator classCreator, ResourceMetadata resourceMetadata,
ResourceProperties resourceProperties, FieldDescriptor resourceField) {
// Method parameters: sort strings, page index, page size, uri info
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(METHOD_NAME, classCreator,
isNotReactivePanache() ? ofType(Response.class) : ofType(Uni.class, resourceMetadata.getEntityType()),
List.class, int.class, int.class, UriInfo.class);
// Add method annotations
addGetAnnotation(methodCreator);
addPathAnnotation(methodCreator, resourceProperties.getPath(RESOURCE_METHOD_NAME));
addProducesAnnotation(methodCreator, APPLICATION_JSON);
addLinksAnnotation(methodCreator, resourceMetadata.getEntityType(), REL);
addSortQueryParamValidatorAnnotation(methodCreator);
addQueryParamAnnotation(methodCreator.getParameterAnnotations(0), "sort");
addQueryParamAnnotation(methodCreator.getParameterAnnotations(1), "page");
addDefaultValueAnnotation(methodCreator.getParameterAnnotations(1), Integer.toString(DEFAULT_PAGE_INDEX));
addQueryParamAnnotation(methodCreator.getParameterAnnotations(2), "size");
addDefaultValueAnnotation(methodCreator.getParameterAnnotations(2), Integer.toString(DEFAULT_PAGE_SIZE));
addContextAnnotation(methodCreator.getParameterAnnotations(3));
ResultHandle resource = methodCreator.readInstanceField(resourceField, methodCreator.getThis());
ResultHandle sortQuery = methodCreator.getMethodParam(0);
ResultHandle sort = sortImplementor.getSort(methodCreator, sortQuery);
ResultHandle pageIndex = methodCreator.getMethodParam(1);
ResultHandle pageSize = methodCreator.getMethodParam(2);
ResultHandle page = paginationImplementor.getPage(methodCreator, pageIndex, pageSize);
ResultHandle uriInfo = methodCreator.getMethodParam(3);
if (isNotReactivePanache()) {
TryBlock tryBlock = implementTryBlock(methodCreator, EXCEPTION_MESSAGE);
ResultHandle pageCount = tryBlock.invokeVirtualMethod(
ofMethod(resourceMetadata.getResourceClass(), Constants.PAGE_COUNT_METHOD_PREFIX + RESOURCE_METHOD_NAME,
int.class, Page.class),
resource, page);
ResultHandle links = paginationImplementor.getLinks(tryBlock, uriInfo, page, pageCount);
ResultHandle entities = tryBlock.invokeVirtualMethod(
ofMethod(resourceMetadata.getResourceClass(), RESOURCE_METHOD_NAME, List.class, Page.class, Sort.class),
resource, page, sort);
// Return response
tryBlock.returnValue(responseImplementor.ok(tryBlock, entities, links));
tryBlock.close();
} else {
ResultHandle uniPageCount = methodCreator.invokeVirtualMethod(
ofMethod(resourceMetadata.getResourceClass(), Constants.PAGE_COUNT_METHOD_PREFIX + RESOURCE_METHOD_NAME,
Uni.class, Page.class),
resource, page);
methodCreator.returnValue(UniImplementor.flatMap(methodCreator, uniPageCount, EXCEPTION_MESSAGE,
(body, pageCount) -> {
ResultHandle pageCountAsInt = body.checkCast(pageCount, Integer.class);
ResultHandle links = paginationImplementor.getLinks(body, uriInfo, page, pageCountAsInt);
ResultHandle uniEntities = body.invokeVirtualMethod(
ofMethod(resourceMetadata.getResourceClass(), RESOURCE_METHOD_NAME, Uni.class, Page.class,
Sort.class),
resource, page, sort);
body.returnValue(UniImplementor.map(body, uniEntities, EXCEPTION_MESSAGE,
(listBody, list) -> listBody.returnValue(responseImplementor.ok(listBody, list, links))));
}));
}
methodCreator.close();
}
private void implementNotPaged(ClassCreator classCreator, ResourceMetadata resourceMetadata,
ResourceProperties resourceProperties, FieldDescriptor resourceFieldDescriptor) {
MethodCreator methodCreator = SignatureMethodCreator.getMethodCreator(METHOD_NAME, classCreator,
isNotReactivePanache() ? ofType(Response.class) : ofType(Uni.class, resourceMetadata.getEntityType()),
List.class);
// Add method annotations
addGetAnnotation(methodCreator);
addPathAnnotation(methodCreator, resourceProperties.getPath(RESOURCE_METHOD_NAME));
addProducesAnnotation(methodCreator, APPLICATION_JSON);
addLinksAnnotation(methodCreator, resourceMetadata.getEntityType(), REL);
addQueryParamAnnotation(methodCreator.getParameterAnnotations(0), "sort");
ResultHandle sortQuery = methodCreator.getMethodParam(0);
ResultHandle sort = sortImplementor.getSort(methodCreator, sortQuery);
ResultHandle resource = methodCreator.readInstanceField(resourceFieldDescriptor, methodCreator.getThis());
if (isNotReactivePanache()) {
TryBlock tryBlock = implementTryBlock(methodCreator, EXCEPTION_MESSAGE);
ResultHandle entities = tryBlock.invokeVirtualMethod(
ofMethod(resourceMetadata.getResourceClass(), RESOURCE_METHOD_NAME,
List.class, Page.class, Sort.class),
resource, tryBlock.loadNull(), sort);
tryBlock.returnValue(responseImplementor.ok(tryBlock, entities));
tryBlock.close();
} else {
ResultHandle uniEntities = methodCreator.invokeVirtualMethod(
ofMethod(resourceMetadata.getResourceClass(), RESOURCE_METHOD_NAME,
Uni.class, Page.class, Sort.class),
resource, methodCreator.loadNull(), sort);
methodCreator.returnValue(UniImplementor.map(methodCreator, uniEntities, EXCEPTION_MESSAGE,
(body, entities) -> body.returnValue(responseImplementor.ok(body, entities))));
}
methodCreator.close();
}
}