Search in sources :

Example 1 with CollectionResult

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);
    }
}
Also used : CollectionResult(com.linkedin.restli.server.CollectionResult) ArrayList(java.util.ArrayList) DataMap(com.linkedin.data.DataMap)

Example 2 with CollectionResult

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);
}
Also used : ToneFacet(com.linkedin.restli.examples.greetings.api.ToneFacet) Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionResult(com.linkedin.restli.server.CollectionResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Tone(com.linkedin.restli.examples.greetings.api.Tone) ToneFacetArray(com.linkedin.restli.examples.greetings.api.ToneFacetArray) SearchMetadata(com.linkedin.restli.examples.greetings.api.SearchMetadata) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) EmptyMap(com.linkedin.restli.examples.greetings.api.EmptyMap) Map(java.util.Map) StringMap(com.linkedin.data.template.StringMap) Finder(com.linkedin.restli.server.annotations.Finder)

Example 3 with CollectionResult

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
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) CollectionResult(com.linkedin.restli.server.CollectionResult) ArrayList(java.util.ArrayList) List(java.util.List) RestMethod(com.linkedin.restli.server.annotations.RestMethod) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Method(java.lang.reflect.Method) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 4 with CollectionResult

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);
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) CollectionResult(com.linkedin.restli.server.CollectionResult) ArrayList(java.util.ArrayList) Finder(com.linkedin.restli.server.annotations.Finder)

Example 5 with CollectionResult

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);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CreateStatus(com.linkedin.restli.common.CreateStatus) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) URIDetails(com.linkedin.restli.internal.testutils.URIDetails) HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) CollectionResponse(com.linkedin.restli.common.CollectionResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ByteString(com.linkedin.data.ByteString) DataMap(com.linkedin.data.DataMap) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) CollectionResult(com.linkedin.restli.server.CollectionResult) BasicCollectionResult(com.linkedin.restli.server.BasicCollectionResult) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) BasicCollectionResult(com.linkedin.restli.server.BasicCollectionResult) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Aggregations

CollectionResult (com.linkedin.restli.server.CollectionResult)6 DataMap (com.linkedin.data.DataMap)3 ArrayList (java.util.ArrayList)3 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)2 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)2 Finder (com.linkedin.restli.server.annotations.Finder)2 HashMap (java.util.HashMap)2 ByteString (com.linkedin.data.ByteString)1 StringMap (com.linkedin.data.template.StringMap)1 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)1 Foo (com.linkedin.pegasus.generator.examples.Foo)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 CollectionResponse (com.linkedin.restli.common.CollectionResponse)1 CreateStatus (com.linkedin.restli.common.CreateStatus)1 HttpStatus (com.linkedin.restli.common.HttpStatus)1 LinkArray (com.linkedin.restli.common.LinkArray)1 ResourceMethod (com.linkedin.restli.common.ResourceMethod)1 UpdateStatus (com.linkedin.restli.common.UpdateStatus)1 EmptyMap (com.linkedin.restli.examples.greetings.api.EmptyMap)1 SearchMetadata (com.linkedin.restli.examples.greetings.api.SearchMetadata)1