Search in sources :

Example 1 with CreateResponse

use of com.linkedin.restli.server.CreateResponse 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 2 with CreateResponse

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

the class TestPhotoResource method createPhoto.

private Long createPhoto(String title, PhotoFormats format) {
    final LatLong l = new LatLong().setLatitude(7.0f).setLongitude(27.0f);
    final EXIF e = new EXIF().setIsFlash(true).setLocation(l);
    final Photo p = new Photo().setTitle(title).setFormat(format).setExif(e);
    final CreateResponse cResp = _res.create(p);
    Assert.assertTrue(cResp.hasId());
    return (Long) cResp.getId();
}
Also used : EXIF(com.linkedin.restli.example.EXIF) CreateResponse(com.linkedin.restli.server.CreateResponse) LatLong(com.linkedin.restli.example.LatLong) Photo(com.linkedin.restli.example.Photo) LatLong(com.linkedin.restli.example.LatLong)

Example 3 with CreateResponse

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

the class AnnotatedComplexKeysResource method create.

@RestMethod.Create
public Promise<CreateResponse> create(final Message message) {
    final SettablePromise<CreateResponse> result = Promises.settable();
    final Runnable requestHandler = new Runnable() {

        public void run() {
            ComplexResourceKey<TwoPartKey, TwoPartKey> key = _dataProvider.create(message);
            result.done(new CreateResponse(key));
        }
    };
    _scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);
    return result;
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) CreateResponse(com.linkedin.restli.server.CreateResponse)

Example 4 with CreateResponse

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

the class ExceptionsResource method create.

/**
 * Responds with an error for requests to create insulting greetings, responds
 * with 201 created for all other requests.
 */
@RestMethod.Create
public CreateResponse create(Greeting g) {
    if (g.hasTone() && g.getTone() == Tone.INSULTING) {
        RestLiServiceException notAcceptableException = new RestLiServiceException(HttpStatus.S_406_NOT_ACCEPTABLE, "I will not tolerate your insolence!");
        DataMap details = new DataMap();
        details.put("reason", "insultingGreeting");
        notAcceptableException.setErrorDetails(new EmptyRecord(details));
        notAcceptableException.setServiceErrorCode(999);
        throw notAcceptableException;
    } else {
        return new CreateResponse(g.getId(), HttpStatus.S_201_CREATED);
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) DataMap(com.linkedin.data.DataMap)

Example 5 with CreateResponse

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

the class TestCreateResponseBuilder method testBuilder.

@Test(dataProvider = "testData")
public void testBuilder(ProtocolVersion protocolVersion, String uriString, Object expectedId, String expectedLocation, String expectedHeaderId, String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap) throws URISyntaxException {
    CompoundKey compoundKey = new CompoundKey().append("a", "a").append("b", 1);
    CreateResponse createResponse = new CreateResponse(compoundKey);
    IdResponse<?> expectedIdResponse = new IdResponse<>(expectedId);
    RestRequest restRequest = new RestRequestBuilder(new URI(uriString)).build();
    Map<String, String> headers = ResponseBuilderUtil.getHeaders();
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, protocolVersion.toString());
    // the headers passed in are modified
    Map<String, String> expectedHeaders = new HashMap<>(headers);
    ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
    ServerResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
    RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
    CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
    RestLiResponseData<CreateResponseEnvelope> responseData = createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.emptyList());
    Assert.assertFalse(responseData.getResponseEnvelope().isGetAfterCreate());
    RestLiResponse restLiResponse = createResponseBuilder.buildResponse(routingResult, responseData);
    expectedHeaders.put(RestConstants.HEADER_LOCATION, expectedLocation);
    if (protocolVersion.equals(AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion())) {
        expectedHeaders.put(RestConstants.HEADER_ID, expectedHeaderId);
    } else {
        expectedHeaders.put(RestConstants.HEADER_RESTLI_ID, expectedHeaderId);
    }
    EasyMock.verify(mockContext, mockDescriptor);
    ResponseBuilderUtil.validateHeaders(restLiResponse, expectedHeaders);
    Assert.assertEquals(restLiResponse.getStatus(), HttpStatus.S_201_CREATED);
    Assert.assertEquals(restLiResponse.getEntity(), expectedIdResponse);
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) CompoundKey(com.linkedin.restli.common.CompoundKey) HashMap(java.util.HashMap) CreateResponse(com.linkedin.restli.server.CreateResponse) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) URI(java.net.URI) RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestRequest(com.linkedin.r2.message.rest.RestRequest) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Aggregations

CreateResponse (com.linkedin.restli.server.CreateResponse)25 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)10 Test (org.testng.annotations.Test)9 ArrayList (java.util.ArrayList)7 RestRequest (com.linkedin.r2.message.rest.RestRequest)6 BatchCreateResult (com.linkedin.restli.server.BatchCreateResult)6 DataMap (com.linkedin.data.DataMap)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)5 CreateKVResponse (com.linkedin.restli.server.CreateKVResponse)5 URI (java.net.URI)5 Foo (com.linkedin.pegasus.generator.examples.Foo)4 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)4 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)4 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)4 ResourceContext (com.linkedin.restli.server.ResourceContext)4 RecordTemplate (com.linkedin.data.template.RecordTemplate)3 CompoundKey (com.linkedin.restli.common.CompoundKey)3