Search in sources :

Example 6 with ResourceContext

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

the class BatchCreateResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    if (result instanceof BatchCreateKVResult) {
        BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
        if (list.getResults() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<BatchCreateResponseEnvelope.CollectionCreateResponseItem>(list.getResults().size());
        for (CreateKVResponse e : list.getResults()) {
            if (e == null) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
            } else {
                Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(e.getId(), routingResult);
                if (e.getError() == null) {
                    final ResourceContext resourceContext = routingResult.getContext();
                    DataMap entityData = e.getEntity() != null ? e.getEntity().data() : null;
                    final DataMap data = RestUtils.projectFields(entityData, resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
                    CreateIdEntityStatus<Object, RecordTemplate> entry = new CreateIdEntityStatus<Object, RecordTemplate>(e.getStatus().getCode(), id, new AnyRecord(data), null, ProtocolVersionUtil.extractProtocolVersion(headers));
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
                } else {
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(e.getError(), id));
                }
            }
        }
        RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, headers, cookies);
        responseData.setResponseEnvelope(new BatchCreateResponseEnvelope(collectionCreateList, true, responseData));
        return responseData;
    } else {
        BatchCreateResult<?, ?> list = (BatchCreateResult<?, ?>) result;
        //Verify that a null list was not passed into the BatchCreateResult. If so, this is a developer error.
        if (list.getResults() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<BatchCreateResponseEnvelope.CollectionCreateResponseItem>(list.getResults().size());
        for (CreateResponse e : list.getResults()) {
            //Verify that a null element was not passed into the BatchCreateResult list. If so, this is a developer error.
            if (e == null) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
            } else {
                Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(e.getId(), routingResult);
                if (e.getError() == null) {
                    CreateIdStatus<Object> entry = new CreateIdStatus<Object>(e.getStatus().getCode(), id, null, ProtocolVersionUtil.extractProtocolVersion(headers));
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
                } else {
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(e.getError(), id));
                }
            }
        }
        RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(HttpStatus.S_200_OK, headers, cookies);
        responseData.setResponseEnvelope(new BatchCreateResponseEnvelope(collectionCreateList, responseData));
        return responseData;
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) BatchCreateResult(com.linkedin.restli.server.BatchCreateResult) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse)

Example 7 with ResourceContext

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

the class JobExecutionInfoResource method batchGet.

@Override
public Map<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> batchGet(Set<ComplexResourceKey<JobExecutionQuery, EmptyRecord>> keys) {
    Map<ComplexResourceKey<JobExecutionQuery, EmptyRecord>, JobExecutionQueryResult> results = Maps.newHashMap();
    for (ComplexResourceKey<JobExecutionQuery, EmptyRecord> key : keys) {
        JobExecutionQueryResult result = get(key);
        if (result != null) {
            results.put(key, get(key));
        }
    }
    ResourceContext rc = this.getContext();
    rc.setResponseHeader("Access-Control-Allow-Origin", "*");
    this.setContext(rc);
    return results;
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) ResourceContext(com.linkedin.restli.server.ResourceContext) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 8 with ResourceContext

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

the class CookieResource method setResponseCookie.

/**
 * Concatonates request cookies and add it as a single response cookie.
 */
private void setResponseCookie() {
    ResourceContext context = getContext();
    List<HttpCookie> requestCookies = context.getRequestCookies();
    if (requestCookies.size() > 0) {
        for (HttpCookie elem : requestCookies) {
            context.addResponseCookie(new HttpCookie(elem.getValue(), elem.getName()));
        }
    } else {
        context.addResponseCookie(new HttpCookie("empty_name", "empty_cookie"));
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) HttpCookie(java.net.HttpCookie)

Example 9 with ResourceContext

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

the class TestArgumentBuilder method testResourceContextParameterType.

@Test
@SuppressWarnings("deprecation")
public void testResourceContextParameterType() {
    String testParamKey = "testParam";
    ServerResourceContext mockResourceContext = EasyMock.createMock(ServerResourceContext.class);
    List<Parameter<?>> parameters = new ArrayList<>();
    Parameter<ResourceContext> param1 = new Parameter<>(testParamKey, ResourceContext.class, null, false, null, Parameter.ParamType.RESOURCE_CONTEXT, false, AnnotationSet.EMPTY);
    Parameter<ResourceContext> param2 = new Parameter<>(testParamKey, ResourceContext.class, null, false, null, Parameter.ParamType.RESOURCE_CONTEXT_PARAM, false, AnnotationSet.EMPTY);
    parameters.add(param1);
    parameters.add(param2);
    EasyMock.expect(mockResourceContext.getRequestAttachmentReader()).andReturn(null);
    Object[] results = ArgumentBuilder.buildArgs(new Object[0], getMockResourceMethod(parameters), mockResourceContext, null, getMockResourceMethodConfig(false));
    Assert.assertEquals(results[0], mockResourceContext);
    Assert.assertEquals(results[1], mockResourceContext);
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ArrayList(java.util.ArrayList) Parameter(com.linkedin.restli.internal.server.model.Parameter) Test(org.testng.annotations.Test)

Example 10 with ResourceContext

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

the class ManualProjectionsResource method get.

@RestMethod.Get
public Greeting get(Long key, @QueryParam("ignoreProjection") @Optional("false") boolean ignoreProjection) {
    ResourceContext context = getContext();
    Greeting greeting = new Greeting();
    context.setProjectionMode(ProjectionMode.MANUAL);
    MaskTree mask = context.getProjectionMask();
    if (mask != null && ignoreProjection == false) {
        if (mask.getOperations().get(Greeting.fields().message()) == MaskOperation.POSITIVE_MASK_OP) {
            greeting.setMessage("Projected message!");
        }
        if (mask.getOperations().get(Greeting.fields().tone()) == MaskOperation.POSITIVE_MASK_OP) {
            greeting.setTone(Tone.FRIENDLY);
        }
        if (mask.getOperations().get(Greeting.fields().id()) == MaskOperation.POSITIVE_MASK_OP) {
            greeting.setId(key);
        }
    } else {
        greeting.setMessage("Full greeting.");
        greeting.setTone(Tone.FRIENDLY);
        greeting.setId(key);
    }
    return greeting;
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ResourceContext(com.linkedin.restli.server.ResourceContext) MaskTree(com.linkedin.data.transform.filter.request.MaskTree)

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