use of com.linkedin.restli.server.CreateResponse in project rest.li by linkedin.
the class MixedResource method create.
@Create
public void create(Greeting entity, @CallbackParam final Callback<CreateResponse> callback) {
final Runnable requestHandler = new Runnable() {
public void run() {
callback.onSuccess(new CreateResponse(HttpStatus.S_200_OK));
}
};
scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);
}
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(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 TestBatchCreateResponseBuilder method testCreateResultBuilder.
@Test(dataProvider = "createResultBuilderTestData")
@SuppressWarnings("unchecked")
public void testCreateResultBuilder(String altKeyName, Map<String, AlternativeKey<?, ?>> alternativeKeyMap, List<CreateIdStatus<Object>> expectedStatuses) {
List<CreateResponse> createResponses = Arrays.asList(new CreateResponse(1L), new CreateResponse(2L));
BatchCreateResult<Long, Foo> results = new BatchCreateResult<Long, Foo>(createResponses);
Map<String, String> headers = ResponseBuilderUtil.getHeaders();
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
ResourceContext mockContext = getMockResourceContext(altKeyName);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
BatchCreateResponseBuilder responseBuilder = new BatchCreateResponseBuilder(null);
RestLiResponseData responseData = responseBuilder.buildRestLiResponseData(null, routingResult, results, headers, Collections.<HttpCookie>emptyList());
PartialRestResponse restResponse = responseBuilder.buildResponse(routingResult, responseData);
EasyMock.verify(mockDescriptor);
ResponseBuilderUtil.validateHeaders(restResponse, headers);
Assert.assertFalse(responseData.getBatchCreateResponseEnvelope().isGetAfterCreate());
List<com.linkedin.restli.common.CreateIdStatus<Object>> items = new ArrayList<CreateIdStatus<Object>>();
for (BatchCreateResponseEnvelope.CollectionCreateResponseItem item : responseData.getBatchCreateResponseEnvelope().getCreateResponses()) {
items.add((CreateIdStatus<Object>) item.getRecord());
}
Assert.assertEquals(restResponse.getEntity(), new BatchCreateIdResponse<Object>(items));
Assert.assertEquals(expectedStatuses, items);
Assert.assertEquals(restResponse.getStatus(), HttpStatus.S_200_OK);
}
use of com.linkedin.restli.server.CreateResponse in project rest.li by linkedin.
the class TestCreateResponseBuilder method testCreateResponseException.
@Test
public void testCreateResponseException() throws URISyntaxException {
CreateResponse createResponse = new CreateResponse(new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST));
RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).build();
RestLiResponseData envelope = new CreateResponseBuilder().buildRestLiResponseData(restRequest, null, createResponse, Collections.<String, String>emptyMap(), Collections.<HttpCookie>emptyList());
Assert.assertTrue(envelope.isErrorResponse());
}
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, 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<Object>(expectedId);
RestRequest restRequest = new RestRequestBuilder(new URI("/foo")).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<String, String>(headers);
ResourceMethodDescriptor mockDescriptor = getMockResourceMethodDescriptor(alternativeKeyMap);
ResourceContext mockContext = getMockResourceContext(protocolVersion, altKeyName);
RoutingResult routingResult = new RoutingResult(mockContext, mockDescriptor);
CreateResponseBuilder createResponseBuilder = new CreateResponseBuilder();
RestLiResponseData responseData = createResponseBuilder.buildRestLiResponseData(restRequest, routingResult, createResponse, headers, Collections.<HttpCookie>emptyList());
Assert.assertFalse(responseData.getCreateResponseEnvelope().isGetAfterCreate());
PartialRestResponse partialRestResponse = 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(partialRestResponse, expectedHeaders);
Assert.assertEquals(partialRestResponse.getStatus(), HttpStatus.S_201_CREATED);
Assert.assertEquals(partialRestResponse.getEntity(), expectedIdResponse);
}
Aggregations