Search in sources :

Example 6 with PagingContext

use of com.linkedin.restli.server.PagingContext in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testPagingContextUserTimelineStartAndCount.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusUserTimelineStartAndCount")
public void testPagingContextUserTimelineStartAndCount(ProtocolVersion version, String query) throws Exception {
    ResourceModel statusResourceModel = buildResourceModel(StatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = statusResourceModel.findNamedMethod("user_timeline");
    StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.getUserTimeline(eq(true), eq(new PagingContext(0, 20, true, true)))).andReturn(null).once();
    checkInvocation(statusResource, methodDescriptor, "GET", version, "/statuses" + query);
}
Also used : PagingContext(com.linkedin.restli.server.PagingContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) TaskStatusCollectionResource(com.linkedin.restli.server.twitter.TaskStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) CustomStatusCollectionResource(com.linkedin.restli.server.twitter.CustomStatusCollectionResource) PromiseStatusCollectionResource(com.linkedin.restli.server.twitter.PromiseStatusCollectionResource) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with PagingContext

use of com.linkedin.restli.server.PagingContext in project rest.li by linkedin.

the class TestRestLiMethodInvocation method testFinderOptionalBooleanParam.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusFinderOptionalBooleanParam")
public void testFinderOptionalBooleanParam(ProtocolVersion version, String query) throws Exception {
    ResourceModel statusResourceModel = buildResourceModel(StatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = statusResourceModel.findNamedMethod("user_timeline");
    StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.getUserTimeline(eq(false), (PagingContext) EasyMock.anyObject())).andReturn(null).once();
    checkInvocation(statusResource, methodDescriptor, "GET", version, "/statuses" + query);
}
Also used : PagingContext(com.linkedin.restli.server.PagingContext) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) TaskStatusCollectionResource(com.linkedin.restli.server.twitter.TaskStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) CustomStatusCollectionResource(com.linkedin.restli.server.twitter.CustomStatusCollectionResource) PromiseStatusCollectionResource(com.linkedin.restli.server.twitter.PromiseStatusCollectionResource) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 8 with PagingContext

use of com.linkedin.restli.server.PagingContext in project rest.li by linkedin.

the class RestLiAnnotationReader method buildPagingContextParam.

private static Parameter<?> buildPagingContextParam(final AnnotationSet annotations, final Class<?> paramType, final Class<?> paramAnnotationType) {
    if (!paramType.equals(PagingContext.class)) {
        throw new ResourceConfigException("Incorrect data type for param: @" + PagingContextParam.class.getSimpleName() + " or @" + Context.class.getSimpleName() + " parameter annotation must be of type " + PagingContext.class.getName());
    }
    PagingContext defaultContext = null;
    Parameter.ParamType parameter = null;
    if (paramAnnotationType.equals(PagingContextParam.class)) {
        PagingContextParam pagingContextParam = annotations.get(PagingContextParam.class);
        defaultContext = new PagingContext(pagingContextParam.defaultStart(), pagingContextParam.defaultCount(), false, false);
        parameter = Parameter.ParamType.PAGING_CONTEXT_PARAM;
    } else if (paramAnnotationType.equals(Context.class)) {
        Context contextParam = annotations.get(Context.class);
        defaultContext = new PagingContext(contextParam.defaultStart(), contextParam.defaultCount(), false, false);
        parameter = Parameter.ParamType.CONTEXT;
    } else {
        throw new ResourceConfigException("Param Annotation type must be 'PagingContextParam' or the deprecated 'Context' for PagingContext");
    }
    Optional optional = annotations.get(Optional.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter("", paramType, null, optional != null, defaultContext, parameter, false, annotations);
    return param;
}
Also used : PagingContext(com.linkedin.restli.server.PagingContext) ResourceContext(com.linkedin.restli.server.ResourceContext) ParSeqContext(com.linkedin.restli.server.annotations.ParSeqContext) Context(com.linkedin.restli.server.annotations.Context) PagingContextParam(com.linkedin.restli.server.annotations.PagingContextParam) Optional(com.linkedin.restli.server.annotations.Optional) PagingContext(com.linkedin.restli.server.PagingContext) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Example 9 with PagingContext

use of com.linkedin.restli.server.PagingContext in project rest.li by linkedin.

the class RestUtils method buildMetadata.

public static CollectionMetadata buildMetadata(final URI requestUri, final ResourceContext resourceContext, final ResourceMethodDescriptor methodDescriptor, final List<?> resultElements, final PageIncrement pageIncrement, final Integer totalResults) {
    CollectionMetadata metadata = new CollectionMetadata();
    List<Parameter<?>> pagingContextParams = methodDescriptor.getParametersWithType(Parameter.ParamType.PAGING_CONTEXT_PARAM);
    PagingContext defaultPagingContext = pagingContextParams.isEmpty() ? null : (PagingContext) pagingContextParams.get(0).getDefaultValue();
    PagingContext pagingContext = getPagingContext(resourceContext, defaultPagingContext);
    metadata.setCount(pagingContext.getCount());
    metadata.setStart(pagingContext.getStart());
    if (totalResults != null) {
        metadata.setTotal(totalResults);
    } else {
        metadata.removeTotal();
    }
    LinkArray links = new LinkArray();
    String bestEncoding = RestConstants.HEADER_VALUE_APPLICATION_JSON;
    if (resourceContext.getRawRequest() != null) {
        bestEncoding = pickBestEncoding(resourceContext.getRequestHeaders().get(RestConstants.HEADER_ACCEPT));
    }
    //links use count as the step interval, so links don't make sense with count==0
    if (pagingContext.getCount() > 0) {
        // prev link
        if (pagingContext.getStart() > 0) {
            int prevStart = Math.max(0, pagingContext.getStart() - pagingContext.getCount());
            String prevUri = buildPaginatedUri(requestUri, prevStart, pagingContext.getCount());
            Link prevLink = new Link();
            prevLink.setRel("prev");
            prevLink.setHref(prevUri);
            prevLink.setType(bestEncoding);
            links.add(prevLink);
        }
        // next link if there are more results, or we returned a full page
        Integer nextStart = getNextPageStart(resultElements.size(), totalResults, pagingContext, pageIncrement);
        if (nextStart != null) {
            // R2 doesn't expose host/port => can't build absolute URI (this is ok, as
            // relative URIs internally
            String nextUri = buildPaginatedUri(requestUri, nextStart, pagingContext.getCount());
            Link nextLink = new Link();
            nextLink.setRel("next");
            nextLink.setHref(nextUri);
            nextLink.setType(bestEncoding);
            links.add(nextLink);
        }
        metadata.setLinks(links);
    }
    return metadata;
}
Also used : CollectionMetadata(com.linkedin.restli.common.CollectionMetadata) PagingContext(com.linkedin.restli.server.PagingContext) LinkArray(com.linkedin.restli.common.LinkArray) Parameter(com.linkedin.restli.internal.server.model.Parameter) Link(com.linkedin.restli.common.Link)

Example 10 with PagingContext

use of com.linkedin.restli.server.PagingContext in project rest.li by linkedin.

the class RestUtils method getPagingContext.

public static PagingContext getPagingContext(final ResourceContext context, final PagingContext defaultContext) {
    String startString = ArgumentUtils.argumentAsString(context.getParameter(RestConstants.START_PARAM), RestConstants.START_PARAM);
    String countString = ArgumentUtils.argumentAsString(context.getParameter(RestConstants.COUNT_PARAM), RestConstants.COUNT_PARAM);
    try {
        int defaultStart = defaultContext == null ? RestConstants.DEFAULT_START : defaultContext.getStart();
        int defaultCount = defaultContext == null ? RestConstants.DEFAULT_COUNT : defaultContext.getCount();
        int start = startString == null || StringUtils.isEmpty(startString.trim()) ? defaultStart : Integer.parseInt(startString);
        int count = countString == null || StringUtils.isEmpty(countString.trim()) ? defaultCount : Integer.parseInt(countString);
        if (count < 0 || start < 0) {
            throw new RoutingException("start/count parameters must be non-negative", 400);
        }
        return new PagingContext(start, count, startString != null, countString != null);
    } catch (NumberFormatException e) {
        throw new RoutingException("Invalid (non-integer) start/count parameters", 400);
    }
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) PagingContext(com.linkedin.restli.server.PagingContext)

Aggregations

PagingContext (com.linkedin.restli.server.PagingContext)19 Test (org.testng.annotations.Test)14 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)12 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)11 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)11 AfterTest (org.testng.annotations.AfterTest)11 BeforeTest (org.testng.annotations.BeforeTest)11 PromiseStatusCollectionResource (com.linkedin.restli.server.twitter.PromiseStatusCollectionResource)8 List (java.util.List)7 Parameter (com.linkedin.restli.internal.server.model.Parameter)5 AsyncStatusCollectionResource (com.linkedin.restli.server.twitter.AsyncStatusCollectionResource)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)3 CustomStatusCollectionResource (com.linkedin.restli.server.twitter.CustomStatusCollectionResource)3 PromiseDiscoveredItemsResource (com.linkedin.restli.server.twitter.PromiseDiscoveredItemsResource)3 StatusCollectionResource (com.linkedin.restli.server.twitter.StatusCollectionResource)3 TaskStatusCollectionResource (com.linkedin.restli.server.twitter.TaskStatusCollectionResource)3 Callback (com.linkedin.common.callback.Callback)2 ByteString (com.linkedin.data.ByteString)2 DataMap (com.linkedin.data.DataMap)2 StringDataSchema (com.linkedin.data.schema.StringDataSchema)2