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;
}
}
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();
}
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;
}
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);
}
}
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);
}
Aggregations