Search in sources :

Example 16 with ResourceContext

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

the class GetResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest 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();
    final DataMap data = RestUtils.projectFields(record.data(), resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(status, headers, cookies);
    responseData.setResponseEnvelope(new GetResponseEnvelope(new AnyRecord(data), responseData));
    return responseData;
}
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) DataMap(com.linkedin.data.DataMap)

Example 17 with ResourceContext

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

the class CreateResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    CreateResponse createResponse = (CreateResponse) result;
    if (createResponse.hasError()) {
        RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getError(), headers, cookies);
        responseData.setResponseEnvelope(new CreateResponseEnvelope(null, responseData));
        return responseData;
    }
    Object id = null;
    if (createResponse.hasId()) {
        id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
        final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
        String stringKey = URIParamUtils.encodeKeyForUri(id, UriComponent.Type.PATH_SEGMENT, protocolVersion);
        UriBuilder uribuilder = UriBuilder.fromUri(request.getURI());
        uribuilder.path(stringKey);
        if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
            // add altkey param to location URI
            uribuilder.queryParam(RestConstants.ALT_KEY_PARAM, routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM));
        }
        headers.put(RestConstants.HEADER_LOCATION, uribuilder.build((Object) null).toString());
        headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForHeader(id, protocolVersion));
    }
    //Verify that a null status was not passed into the CreateResponse. If so, this is a developer error.
    if (createResponse.getStatus() == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: " + routingResult.getResourceMethod());
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getStatus(), headers, cookies);
    CreateResponseEnvelope responseEnvelope;
    if (createResponse instanceof CreateKVResponse) {
        final ResourceContext resourceContext = routingResult.getContext();
        DataMap entityData = ((CreateKVResponse) createResponse).getEntity().data();
        final DataMap data = RestUtils.projectFields(entityData, resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
        responseEnvelope = new CreateResponseEnvelope(new AnyRecord(data), true, responseData);
    } else //Instance of idResponse
    {
        IdResponse<?> idResponse = new IdResponse<Object>(id);
        responseEnvelope = new CreateResponseEnvelope(idResponse, responseData);
    }
    responseData.setResponseEnvelope(responseEnvelope);
    return responseData;
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) CreateResponse(com.linkedin.restli.server.CreateResponse) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) UriBuilder(com.linkedin.jersey.api.uri.UriBuilder) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse)

Example 18 with ResourceContext

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

the class TestBatchUpdateResponseBuilder method testBuilder.

@Test(dataProvider = "testData")
@SuppressWarnings("unchecked")
public void testBuilder(Object results, ProtocolVersion protocolVersion, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, Map<String, UpdateStatus> expectedResults, Map<String, ErrorResponse> expectedErrors) {
    ResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    BatchUpdateResponseBuilder batchUpdateResponseBuilder = new BatchUpdateResponseBuilder(new ErrorResponseBuilder());
    RestLiResponseData responseData = batchUpdateResponseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
    PartialRestResponse restResponse = batchUpdateResponseBuilder.buildResponse(routingResult, responseData);
    BatchResponse<UpdateStatus> batchResponse = (BatchResponse<UpdateStatus>) restResponse.getEntity();
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restResponse, headers);
    Assert.assertEquals(batchResponse.getResults(), expectedResults);
    Assert.assertEquals(batchResponse.getErrors().size(), expectedErrors.size());
    for (Map.Entry<String, ErrorResponse> entry : batchResponse.getErrors().entrySet()) {
        String key = entry.getKey();
        ErrorResponse value = entry.getValue();
        Assert.assertEquals(value.getStatus(), expectedErrors.get(key).getStatus());
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) UpdateStatus(com.linkedin.restli.common.UpdateStatus) BatchResponse(com.linkedin.restli.common.BatchResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.testng.annotations.Test)

Example 19 with ResourceContext

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

the class TestCollectionResponseBuilder method testBuilder.

@SuppressWarnings("unchecked")
@Test(dataProvider = "testData")
public void testBuilder(Object results, DataMap expectedMetadata, List<Foo> expectedElements, CollectionMetadata expectedPaging, MaskTree dataMaskTree, MaskTree metaDataMaskTree, MaskTree pagingMaskTree, ProjectionMode dataProjectionMode, ProjectionMode metadataProjectionMode) throws URISyntaxException {
    for (ResourceMethod resourceMethod : Arrays.asList(ResourceMethod.GET_ALL, ResourceMethod.FINDER)) {
        Map<String, String> headers = ResponseBuilderUtil.getHeaders();
        ResourceContext mockContext = getMockResourceContext(dataMaskTree, metaDataMaskTree, pagingMaskTree, dataProjectionMode, metadataProjectionMode);
        ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(resourceMethod);
        RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
        CollectionResponseBuilder responseBuilder = new CollectionResponseBuilder();
        RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(getRestRequest(), routingResult, results, headers, Collections.<HttpCookie>emptyList());
        PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
        EasyMock.verify(mockContext, mockDescriptor);
        ResponseBuilderUtil.validateHeaders(restResponse, headers);
        CollectionResponse<Foo> actualResults = (CollectionResponse<Foo>) restResponse.getEntity();
        Assert.assertEquals(actualResults.getElements(), expectedElements);
        Assert.assertEquals(actualResults.getMetadataRaw(), expectedMetadata);
        Assert.assertEquals(actualResults.getPaging(), expectedPaging);
        EasyMock.verify(mockContext);
    }
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceContext(com.linkedin.restli.server.ResourceContext) CollectionResponse(com.linkedin.restli.common.CollectionResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) Foo(com.linkedin.pegasus.generator.examples.Foo) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) ResourceMethod(com.linkedin.restli.common.ResourceMethod) Test(org.testng.annotations.Test)

Example 20 with ResourceContext

use of com.linkedin.restli.server.ResourceContext in project incubator-gobblin by apache.

the class JobExecutionInfoResource method get.

@Override
public JobExecutionQueryResult get(ComplexResourceKey<JobExecutionQuery, EmptyRecord> key) {
    JobExecutionQuery query = key.getKey();
    JobExecutionInfoArray jobExecutionInfos = new JobExecutionInfoArray();
    try {
        for (JobExecutionInfo jobExecutionInfo : this.jobHistoryStore.get(query)) {
            jobExecutionInfos.add(jobExecutionInfo);
        }
    } catch (Throwable t) {
        LOGGER.error(String.format("Failed to execute query [id = %s, type = %s]", query.getId(), query.getIdType().name()), t);
        return null;
    }
    JobExecutionQueryResult result = new JobExecutionQueryResult();
    result.setJobExecutions(jobExecutionInfos);
    ResourceContext rc = this.getContext();
    rc.setResponseHeader("Access-Control-Allow-Origin", "*");
    this.setContext(rc);
    return result;
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext)

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