use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class GetResponseBuilder method buildRestLiResponseData.
/**
* {@inheritDoc}
*
* @param result The result of a Rest.li GET method. It can be the entity itself, or the entity wrapped in a
* {@link GetResult}.
*/
@Override
public RestLiResponseData<GetResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
final RecordTemplate record;
final HttpStatus status;
if (result instanceof GetResult) {
final GetResult<?> getResult = (GetResult<?>) result;
record = getResult.getValue();
status = getResult.getStatus();
} else {
record = (RecordTemplate) result;
status = HttpStatus.S_200_OK;
}
final ResourceContext resourceContext = routingResult.getContext();
DataMap rawData = record.data();
RecordDataSchema schema = record.schema();
if (resourceContext.isFillInDefaultsRequested()) {
rawData = (DataMap) ResponseUtils.fillInDataDefault(schema, rawData);
}
TimingContextUtil.beginTiming(resourceContext.getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
final DataMap data = RestUtils.projectFields(rawData, resourceContext);
TimingContextUtil.endTiming(resourceContext.getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
return new RestLiResponseDataImpl<>(new GetResponseEnvelope(status, new AnyRecord(data)), headers, cookies);
}
use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class TestResourceContext method testResourceContextGetProjectionMaskWithSyntax.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "projectionMaskWithSyntax")
public void testResourceContextGetProjectionMaskWithSyntax(ProtocolVersion version, String stringUri) throws Exception {
URI uri = URI.create(stringUri);
Map<String, String> headers = new HashMap<>(1);
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
ResourceContext context = new ResourceContextImpl(new PathKeysImpl(), new MockRequest(uri, headers), new RequestContext());
final MaskTree entityMask = context.getProjectionMask();
// "{d={$*={f=1, e=1}}, b={$*={c=1}}, a={$*=1}}"
final DataMap expectedEntityMap = new DataMap();
final DataMap aMap = new DataMap();
aMap.put(FilterConstants.WILDCARD, 1);
expectedEntityMap.put("a", aMap);
final DataMap bMap = new DataMap();
final DataMap bWildcardMap = new DataMap();
bWildcardMap.put("c", 1);
bMap.put(FilterConstants.WILDCARD, bWildcardMap);
expectedEntityMap.put("b", bMap);
final DataMap dMap = new DataMap();
final DataMap dWildcardMap = new DataMap();
dWildcardMap.put("f", 1);
dWildcardMap.put("e", 1);
dMap.put(FilterConstants.WILDCARD, dWildcardMap);
expectedEntityMap.put("d", dMap);
Assert.assertEquals(entityMask.getDataMap(), expectedEntityMap, "The generated DataMap for the MaskTree should be correct");
final MaskTree metadataMask = context.getMetadataProjectionMask();
// "{baz={$*={a=1}}, foo={$*={b=1, c=1, a=1}}, bar={$*=1}}"
final DataMap expectedMetadataMap = new DataMap();
final DataMap barMap = new DataMap();
barMap.put(FilterConstants.WILDCARD, 1);
expectedMetadataMap.put("bar", barMap);
final DataMap fooWilcardMap = new DataMap();
fooWilcardMap.put("b", 1);
fooWilcardMap.put("a", 1);
fooWilcardMap.put("c", 1);
final DataMap fooMap = new DataMap();
fooMap.put(FilterConstants.WILDCARD, fooWilcardMap);
expectedMetadataMap.put("foo", fooMap);
final DataMap bazWildcardMap = new DataMap();
bazWildcardMap.put("a", 1);
final DataMap bazMap = new DataMap();
bazMap.put(FilterConstants.WILDCARD, bazWildcardMap);
expectedMetadataMap.put("baz", bazMap);
Assert.assertEquals(metadataMask.getDataMap(), expectedMetadataMap, "The generated DataMap for the MaskTree should be correct");
final MaskTree pagingMask = context.getPagingProjectionMask();
// "{total=1, count=1, links={$*={rel=1}}}"
final DataMap expectedPagingMap = new DataMap();
expectedPagingMap.put("total", 1);
expectedPagingMap.put("count", 1);
final DataMap linksWildcardMap = new DataMap();
linksWildcardMap.put("rel", 1);
final DataMap linksMap = new DataMap();
linksMap.put(FilterConstants.WILDCARD, linksWildcardMap);
expectedPagingMap.put("links", linksMap);
Assert.assertEquals(pagingMask.getDataMap(), expectedPagingMap, "The generated DataMap for the MaskTree should be correct");
}
use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class TestResourceContext method testResourceContextWithQueryParamsGetProjectionMaskWithMaskSyntax.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "queryParamsProjectionMaskWithSyntax")
public void testResourceContextWithQueryParamsGetProjectionMaskWithMaskSyntax(ProtocolVersion version, String stringUri) throws Exception {
URI uri = URI.create(stringUri);
Map<String, String> headers = new HashMap<>(1);
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
ResourceContext context = new ResourceContextImpl(new PathKeysImpl(), new MockRequest(uri, headers), new RequestContext());
// Assert.assertEquals(mask.toString(), "{location={longitude=1, latitude=1}, locale=1, state=1}");
final MaskTree entityMask = context.getProjectionMask();
final DataMap expectedEntityMap = new DataMap();
expectedEntityMap.put("locale", 1);
expectedEntityMap.put("state", 1);
final DataMap locationMap = new DataMap();
locationMap.put("longitude", 1);
locationMap.put("latitude", 1);
expectedEntityMap.put("location", locationMap);
Assert.assertEquals(entityMask.getDataMap(), expectedEntityMap, "The generated DataMap for the MaskTree should be correct");
// "{region=1, profile={weight=1, height=1}, city=1}"
final MaskTree metadataMask = context.getMetadataProjectionMask();
final DataMap expectedMetadataMap = new DataMap();
expectedMetadataMap.put("city", 1);
expectedMetadataMap.put("region", 1);
final DataMap profileMap = new DataMap();
profileMap.put("weight", 1);
profileMap.put("height", 1);
expectedMetadataMap.put("profile", profileMap);
Assert.assertEquals(metadataMask.getDataMap(), expectedMetadataMap, "The generated DataMap for the MaskTree should be correct");
// Note the lack of a test with paging here. This is because paging (CollectionMetadata) has a LinkArray which
// requires a wildcard path spec in the URI. That behavior is inconsistent with the other projections in this test,
// therefore it will be included in the subsequent test.
}
use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class RestLiArgumentBuilderTestHelper method getMockResourceContext.
public static ResourceContext getMockResourceContext() {
ResourceContext context = createMock(ResourceContext.class);
PathKeysImpl pathKeys = new PathKeysImpl();
expect(context.getPathKeys()).andReturn(pathKeys);
replay(context);
return context;
}
use of com.linkedin.restli.server.ResourceContext in project rest.li by linkedin.
the class RestLiAnnotationReader method getParameters.
@SuppressWarnings("deprecation")
private static List<Parameter<?>> getParameters(final ResourceModel model, final Method method, final ResourceMethod methodType) {
Set<String> paramNames = new HashSet<>();
List<Parameter<?>> queryParameters = new ArrayList<>();
Annotation[][] paramsAnnos = method.getParameterAnnotations();
// Iterate over the method parameters.
for (int idx = 0; idx < paramsAnnos.length; idx++) {
AnnotationSet paramAnnotations = new AnnotationSet(paramsAnnos[idx]);
Class<?> paramType = method.getParameterTypes()[idx];
Parameter<?> param = getPositionalParameter(model, methodType, idx, paramAnnotations);
// if no positional definition, look for custom annotated parameters
if (param == null) {
if (paramAnnotations.contains(QueryParam.class)) {
param = buildQueryParam(method, paramAnnotations, paramType);
} else if (paramAnnotations.contains(ActionParam.class)) {
param = buildActionParam(method, paramAnnotations, paramType);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.AssocKey.class)) {
param = buildAssocKeyParam(model, method, paramAnnotations, paramType, com.linkedin.restli.server.annotations.AssocKey.class);
} else if (paramAnnotations.contains(AssocKeyParam.class)) {
param = buildAssocKeyParam(model, method, paramAnnotations, paramType, AssocKeyParam.class);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.Context.class)) {
param = buildPagingContextParam(paramAnnotations, paramType, com.linkedin.restli.server.annotations.Context.class);
} else if (paramAnnotations.contains(PagingContextParam.class)) {
param = buildPagingContextParam(paramAnnotations, paramType, PagingContextParam.class);
} else if (paramAnnotations.contains(CallbackParam.class)) {
param = buildCallbackParam(method, methodType, idx, paramType, paramAnnotations);
} else if (paramAnnotations.contains(ParSeqContextParam.class)) {
param = buildParSeqContextParam(method, methodType, idx, paramType, paramAnnotations, ParSeqContextParam.class);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.ParSeqContext.class)) {
param = buildParSeqContextParam(method, methodType, idx, paramType, paramAnnotations, com.linkedin.restli.server.annotations.ParSeqContext.class);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.Projection.class)) {
param = buildProjectionParam(paramAnnotations, paramType, Parameter.ParamType.PROJECTION);
} else if (paramAnnotations.contains(ProjectionParam.class)) {
param = buildProjectionParam(paramAnnotations, paramType, Parameter.ParamType.PROJECTION_PARAM);
} else if (paramAnnotations.contains(MetadataProjectionParam.class)) {
param = buildProjectionParam(paramAnnotations, paramType, Parameter.ParamType.METADATA_PROJECTION_PARAM);
} else if (paramAnnotations.contains(PagingProjectionParam.class)) {
param = buildProjectionParam(paramAnnotations, paramType, Parameter.ParamType.PAGING_PROJECTION_PARAM);
} else if (paramAnnotations.contains(com.linkedin.restli.server.annotations.Keys.class)) {
param = buildPathKeysParam(paramAnnotations, paramType, com.linkedin.restli.server.annotations.Keys.class);
} else if (paramAnnotations.contains(PathKeysParam.class)) {
param = buildPathKeysParam(paramAnnotations, paramType, PathKeysParam.class);
} else if (paramAnnotations.contains(PathKeyParam.class)) {
param = buildPathKeyParam(model, paramAnnotations, paramType, PathKeyParam.class);
} else if (paramAnnotations.contains(HeaderParam.class)) {
param = buildHeaderParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(ResourceContextParam.class)) {
param = buildResourceContextParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(ValidatorParam.class)) {
param = buildValidatorParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(RestLiAttachmentsParam.class)) {
param = buildRestLiAttachmentsParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(UnstructuredDataWriterParam.class)) {
param = buildUnstructuredDataWriterParam(paramAnnotations, paramType);
} else if (paramAnnotations.contains(UnstructuredDataReactiveReaderParam.class)) {
param = buildUnstructuredDataReactiveReader(paramAnnotations, paramType);
} else {
throw new ResourceConfigException(buildMethodMessage(method) + " must annotate each parameter with @QueryParam, @ActionParam, @AssocKeyParam, @PagingContextParam, " + "@ProjectionParam, @MetadataProjectionParam, @PagingProjectionParam, @PathKeysParam, @PathKeyParam, " + "@HeaderParam, @CallbackParam, @ResourceContext, @ParSeqContextParam, @ValidatorParam, " + "@RestLiAttachmentsParam, @UnstructuredDataWriterParam, @UnstructuredDataReactiveReaderParam, " + "or @ValidateParam");
}
}
if (param != null) {
validateParameter(method, methodType, paramNames, paramAnnotations, param, paramType);
queryParameters.add(param);
}
}
return queryParameters;
}
Aggregations