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);
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations