use of com.linkedin.restli.server.CollectionResult in project rest.li by linkedin.
the class ExampleRequestResponseGenerator method buildFinderResult.
private CollectionResult<RecordTemplatePlaceholder, RecordTemplatePlaceholder> buildFinderResult(RecordDataSchema finderMetadataSchema) {
final List<RecordTemplatePlaceholder> results = new ArrayList<RecordTemplatePlaceholder>();
results.add(generateEntity());
results.add(generateEntity());
if (finderMetadataSchema != null) {
DataMap metadataDataMap = (DataMap) _dataGenerator.buildData("metadata", finderMetadataSchema);
RecordTemplatePlaceholder metadata = new RecordTemplatePlaceholder(metadataDataMap, finderMetadataSchema);
return new CollectionResult<RecordTemplatePlaceholder, RecordTemplatePlaceholder>(results, results.size(), metadata);
} else {
return new CollectionResult<RecordTemplatePlaceholder, RecordTemplatePlaceholder>(results);
}
}
use of com.linkedin.restli.server.CollectionResult in project rest.li by linkedin.
the class GreetingsResourceImpl method searchWithFacets.
@Finder("searchWithFacets")
public CollectionResult<Greeting, SearchMetadata> searchWithFacets(@PagingContextParam PagingContext ctx, @QueryParam("tone") @Optional Tone tone) {
List<Greeting> greetings = search(ctx, tone);
Map<Tone, Integer> toneCounts = new HashMap<Tone, Integer>();
for (Greeting g : greetings) {
if (!toneCounts.containsKey(g.getTone())) {
toneCounts.put(g.getTone(), 0);
}
toneCounts.put(g.getTone(), toneCounts.get(g.getTone()) + 1);
}
SearchMetadata metadata = new SearchMetadata();
metadata.setFacets(new ToneFacetArray());
for (Map.Entry<Tone, Integer> entry : toneCounts.entrySet()) {
ToneFacet f = new ToneFacet();
f.setTone(entry.getKey());
f.setCount(entry.getValue());
metadata.getFacets().add(f);
}
return new CollectionResult<Greeting, SearchMetadata>(greetings, null, metadata);
}
use of com.linkedin.restli.server.CollectionResult in project rest.li by linkedin.
the class RestLiAnnotationReader method validateFinderMethod.
private static void validateFinderMethod(final ResourceMethodDescriptor finderMethodDescriptor, final ResourceModel resourceModel) {
Method method = finderMethodDescriptor.getMethod();
Class<?> valueClass = resourceModel.getValueClass();
Class<?> returnType, elementType;
try {
returnType = getLogicalReturnClass(method);
final List<Class<?>> typeArguments;
if (List.class.isAssignableFrom(returnType)) {
typeArguments = ReflectionUtils.getTypeArguments(List.class, returnType.asSubclass(List.class));
} else if (CollectionResult.class.isAssignableFrom(returnType)) {
typeArguments = ReflectionUtils.getTypeArguments(CollectionResult.class, returnType.asSubclass(CollectionResult.class));
} else {
throw new ResourceConfigException("@Finder method '" + method.getName() + "' on class '" + resourceModel.getResourceClass().getName() + "' has an unsupported return type");
}
if (typeArguments == null || typeArguments.get(0) == null) {
// the return type may leave value type as parameterized and specify in runtime
final ParameterizedType collectionType = (ParameterizedType) getLogicalReturnType(method);
elementType = (Class<?>) collectionType.getActualTypeArguments()[0];
} else {
elementType = typeArguments.get(0);
}
} catch (ClassCastException e) {
throw new ResourceConfigException("@Finder method '" + method.getName() + "' on class '" + resourceModel.getResourceClass().getName() + "' has an invalid return or a data template type", e);
}
if (!List.class.isAssignableFrom(returnType) && !CollectionResult.class.isAssignableFrom(returnType)) {
throw new ResourceConfigException("@Finder method '" + method.getName() + "' on class '" + resourceModel.getResourceClass().getName() + "' has an invalid return type '" + returnType.getName() + "'. Expected " + "List<" + valueClass.getName() + "> or CollectionResult<" + valueClass.getName() + ">");
}
String collectionClassName = returnType.getSimpleName();
if (!RecordTemplate.class.isAssignableFrom(elementType) || !resourceModel.getValueClass().equals(elementType)) {
throw new ResourceConfigException("@Finder method '" + method.getName() + "' on class '" + resourceModel.getResourceClass().getName() + "' has an invalid return type. Expected " + collectionClassName + "<" + valueClass.getName() + ">, but found " + collectionClassName + "<" + elementType + '>');
}
ResourceMethodDescriptor existingFinder = resourceModel.findNamedMethod(finderMethodDescriptor.getFinderName());
if (existingFinder != null) {
throw new ResourceConfigException("Found duplicate @Finder method named '" + finderMethodDescriptor.getFinderName() + "' on class '" + resourceModel.getResourceClass().getName() + '\'');
}
// query parameters are checked in getQueryParameters method
}
use of com.linkedin.restli.server.CollectionResult in project rest.li by linkedin.
the class GreetingsResourceImpl method searchWithPostFilter.
@Finder("searchWithPostFilter")
public CollectionResult<Greeting, Empty> searchWithPostFilter(@PagingContextParam PagingContext ctx) {
List<Greeting> greetings = new ArrayList<Greeting>();
int idx = 0;
int start = ctx.getStart();
int stop = start + ctx.getCount();
for (Greeting g : _db.values()) {
if (idx++ >= ctx.getStart()) {
greetings.add(g);
if (idx == stop) {
break;
}
}
}
// for testing, using a post-filter that just removes the first element
if (greetings.size() > 0)
greetings.remove(0);
int total = _db.values().size();
// this is to keep paging consistent even in the presence of a post filter.
return new CollectionResult<Greeting, Empty>(greetings, total, null, PageIncrement.FIXED);
}
use of com.linkedin.restli.server.CollectionResult in project rest.li by linkedin.
the class TestRestLiResponseHandler method testCollections.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testCollections(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
ResourceModel resourceModel = buildResourceModel(StatusCollectionResource.class);
ResourceMethodDescriptor methodDescriptor = resourceModel.findNamedMethod("search");
RestResponse response;
// #1 check datamap/entity structure
ServerResourceContext context = new ResourceContextImpl();
RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, context);
response = _responseHandler.buildResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), new RoutingResult(context, methodDescriptor), buildStatusList(3));
checkResponse(response, 200, 2, acceptTypeData.responseContentType, CollectionResponse.class.getName(), Status.class.getName(), true, errorResponseHeaderName);
String baseUri = "/test?someParam=foo";
// #1.1 using CollectionResult
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(5)), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 5, 0, 5, 1, null, null, null, acceptTypeData);
// #1.1 using CollectionResult (with total)
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(5), 10), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
// using CollectionResult with metadata RecordTemplate
CollectionMetadata metadata = new CollectionMetadata();
metadata.setCount(42);
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, new CollectionResult<Status, CollectionMetadata>(buildStatusList(5), 10, metadata), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 5, 0, 5, 1, 10, null, null, acceptTypeData);
DataMap dataMap = acceptTypeData.dataCodec.readMap(response.getEntity().asInputStream());
CollectionResponse<Status> collectionResponse = new CollectionResponse<Status>(dataMap, Status.class);
assertEquals(new CollectionMetadata(collectionResponse.getMetadataRaw()), metadata);
// #2 pagination: first page, no next
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, buildStatusList(3), acceptTypeData.acceptHeaders, protocolVersion);
checkCollectionResponse(response, 3, 0, 5, 0, null, null, null, acceptTypeData);
// #3 pagination: first page, has next (boundary case)
response = invokeResponseHandler(baseUri + "&start=0&count=5", methodDescriptor, buildStatusList(5), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=5&someParam=foo"
final Map<String, String> queryParamsMap3next = new HashMap<String, String>();
queryParamsMap3next.put("count", "5");
queryParamsMap3next.put("start", "5");
queryParamsMap3next.put("someParam", "foo");
final URIDetails expectedURIDetails3next = new URIDetails(protocolVersion, "/test", null, queryParamsMap3next, null);
checkCollectionResponse(response, 5, 0, 5, 1, null, null, expectedURIDetails3next, acceptTypeData);
// #4 pagination: second page, has prev/ext
response = invokeResponseHandler(baseUri + "&start=5&count=5", methodDescriptor, buildStatusList(5), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=0&someParam=foo", "/test?count=5&start=10&someParam=foo",
final Map<String, String> queryParamsMap4prev = new HashMap<String, String>();
queryParamsMap4prev.put("count", "5");
queryParamsMap4prev.put("start", "0");
queryParamsMap4prev.put("someParam", "foo");
final URIDetails expectedURIDetails4prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap4prev, null);
final Map<String, String> queryParamsMap4next = new HashMap<String, String>();
queryParamsMap4next.put("count", "5");
queryParamsMap4next.put("start", "10");
queryParamsMap4next.put("someParam", "foo");
final URIDetails expectedURIDetails4next = new URIDetails(protocolVersion, "/test", null, queryParamsMap4next, null);
checkCollectionResponse(response, 5, 5, 5, 2, null, expectedURIDetails4prev, expectedURIDetails4next, acceptTypeData);
// #5 pagination:last page, has prev
response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, buildStatusList(4), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=5&someParam=foo"
final Map<String, String> queryParamsMap5prev = new HashMap<String, String>();
queryParamsMap5prev.put("count", "5");
queryParamsMap5prev.put("start", "5");
queryParamsMap5prev.put("someParam", "foo");
final URIDetails expectedURIDetails5prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap5prev, null);
checkCollectionResponse(response, 4, 10, 5, 1, null, expectedURIDetails5prev, null, acceptTypeData);
response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(4), 15), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=5&someParam=foo", "/test?count=5&start=14&someParam=foo"
final Map<String, String> queryParamsMap6prev = new HashMap<String, String>();
queryParamsMap6prev.put("count", "5");
queryParamsMap6prev.put("start", "5");
queryParamsMap6prev.put("someParam", "foo");
final URIDetails expectedURIDetails6prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap6prev, null);
final Map<String, String> queryParamsMap6next = new HashMap<String, String>();
queryParamsMap6next.put("count", "5");
queryParamsMap6next.put("start", "14");
queryParamsMap6next.put("someParam", "foo");
final URIDetails expectedURIDetails6next = new URIDetails(protocolVersion, "/test", null, queryParamsMap6next, null);
checkCollectionResponse(response, 4, 10, 5, 2, 15, expectedURIDetails6prev, expectedURIDetails6next, acceptTypeData);
response = invokeResponseHandler(baseUri + "&start=10&count=5", methodDescriptor, new BasicCollectionResult<Status>(buildStatusList(4), 14), acceptTypeData.acceptHeaders, protocolVersion);
//"/test?count=5&start=5&someParam=foo"
final Map<String, String> queryParamsMap7prev = new HashMap<String, String>();
queryParamsMap7prev.put("count", "5");
queryParamsMap7prev.put("start", "5");
queryParamsMap7prev.put("someParam", "foo");
final URIDetails expectedURIDetails7prev = new URIDetails(protocolVersion, "/test", null, queryParamsMap7prev, null);
checkCollectionResponse(response, 4, 10, 5, 1, 14, expectedURIDetails7prev, null, acceptTypeData);
}
Aggregations