Search in sources :

Example 21 with ResourceContext

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);
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) GetResult(com.linkedin.restli.server.GetResult) HttpStatus(com.linkedin.restli.common.HttpStatus) RecordTemplate(com.linkedin.data.template.RecordTemplate) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) DataMap(com.linkedin.data.DataMap)

Example 22 with ResourceContext

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");
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 23 with ResourceContext

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.
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) HashMap(java.util.HashMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl) ByteString(com.linkedin.data.ByteString) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 24 with ResourceContext

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;
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) PathKeysImpl(com.linkedin.restli.internal.server.PathKeysImpl)

Example 25 with ResourceContext

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;
}
Also used : PagingContextParam(com.linkedin.restli.server.annotations.PagingContextParam) ArrayList(java.util.ArrayList) PathKeyParam(com.linkedin.restli.server.annotations.PathKeyParam) RestLiAttachmentsParam(com.linkedin.restli.server.annotations.RestLiAttachmentsParam) MetadataProjectionParam(com.linkedin.restli.server.annotations.MetadataProjectionParam) HashSet(java.util.HashSet) PagingContext(com.linkedin.restli.server.PagingContext) ResourceContext(com.linkedin.restli.server.ResourceContext) Context(com.linkedin.parseq.Context) ActionParam(com.linkedin.restli.server.annotations.ActionParam) PathKeysParam(com.linkedin.restli.server.annotations.PathKeysParam) PathKeys(com.linkedin.restli.server.PathKeys) AlternativeKeys(com.linkedin.restli.server.annotations.AlternativeKeys) ParSeqContextParam(com.linkedin.restli.server.annotations.ParSeqContextParam) AssocKeyParam(com.linkedin.restli.server.annotations.AssocKeyParam) UnstructuredDataReactiveReaderParam(com.linkedin.restli.server.annotations.UnstructuredDataReactiveReaderParam) ResourceContextParam(com.linkedin.restli.server.annotations.ResourceContextParam) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException)

Aggregations

ResourceContext (com.linkedin.restli.server.ResourceContext)25 DataMap (com.linkedin.data.DataMap)12 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)11 RecordTemplate (com.linkedin.data.template.RecordTemplate)8 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)8 Test (org.testng.annotations.Test)8 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)7 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)5 ArrayList (java.util.ArrayList)5 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)4 RequestContext (com.linkedin.r2.message.RequestContext)4 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)4 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)4 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)4 CreateResponse (com.linkedin.restli.server.CreateResponse)4 URI (java.net.URI)4 ByteString (com.linkedin.data.ByteString)3 BatchResponse (com.linkedin.restli.common.BatchResponse)3 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)3 CreateKVResponse (com.linkedin.restli.server.CreateKVResponse)3