use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiServer method testHandleRequestWithRestLiResponseError.
@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseError(final RestOrStream restOrStream) throws Exception {
final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
EasyMock.expect(statusResource.get(eq(1L))).andReturn(null).once();
replay(statusResource);
Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {
@Override
public void onSuccess(RestLiResponse restLiResponse) {
fail("We should not get a success here. The server should have returned a 404!");
}
@Override
public void onError(Throwable e) {
RestLiResponseException restLiResponseException = (RestLiResponseException) e;
assertEquals(restLiResponseException.getRestLiResponse().getStatus(), HttpStatus.S_404_NOT_FOUND, "We should get a 404 back here!");
EasyMock.verify(statusResource);
EasyMock.reset(statusResource);
}
};
if (restOrStream == RestOrStream.REST) {
RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
_server.handleRequestWithRestLiResponse(request, new RequestContext(), restLiResponseCallback);
} else {
StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
_server.handleRequestWithRestLiResponse(streamRequest, new RequestContext(), restLiResponseCallback);
}
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class FilterChainCallbackImpl method onError.
@Override
public void onError(Throwable th, final RestLiResponseData<?> responseData) {
markOnResponseTimings(_method.getContext().getRawRequestContext());
// The Throwable passed in is not used at all. However, before every invocation, the throwable is wrapped inside
// the RestLiResponseData parameter. This can potentially be refactored.
Throwable error;
try {
RestLiServiceException serviceException = responseData.getResponseEnvelope().getException();
final RestLiResponse response = _responseHandler.buildPartialResponse(_method, responseData);
error = new RestLiResponseException(serviceException, response);
} catch (Throwable throwable) {
LOGGER.error("Unexpected error when processing error response.", responseData.getResponseEnvelope().getException());
error = throwable;
}
_wrappedCallback.onError(error);
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class FilterChainCallbackImpl method buildErrorResponse.
private RestLiResponse buildErrorResponse(Throwable th, RestLiResponseData<?> responseData) {
Map<String, String> responseHeaders = responseData.getHeaders();
responseHeaders.put(HeaderUtil.getErrorResponseHeaderName(responseHeaders), RestConstants.HEADER_VALUE_ERROR);
RestLiServiceException ex;
if (th instanceof RestLiServiceException) {
ex = (RestLiServiceException) th;
} else {
ex = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, th.getMessage(), th);
}
return new RestLiResponse.Builder().headers(responseHeaders).cookies(responseData.getCookies()).status(ex.getStatus()).entity(_errorResponseBuilder.buildErrorResponse(ex)).build();
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiMethodInvocation method checkInvocation.
private void checkInvocation(Object resource, RequestContext requestContext, ResourceMethodDescriptor resourceMethodDescriptor, ResourceMethodConfig resourceMethodConfig, String httpMethod, ProtocolVersion version, String uri, String entityBody, MutablePathKeys pathkeys, final Callback<RestResponse> callback, final boolean isDebugMode, final boolean expectRoutingException, final RestLiAttachmentReader expectedRequestAttachments, final RestLiResponseAttachments expectedResponseAttachments) throws Exception {
assertNotNull(resource);
assertNotNull(resourceMethodDescriptor);
try {
EasyMock.replay(resource);
RestRequestBuilder builder = new RestRequestBuilder(new URI(uri)).setMethod(httpMethod).addHeaderValue("Accept", "application/json").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
if (entityBody != null) {
builder.setEntity(entityBody.getBytes(Data.UTF_8_CHARSET));
}
if (expectedResponseAttachments != null) {
builder.addHeaderValue(RestConstants.HEADER_ACCEPT, RestConstants.HEADER_VALUE_MULTIPART_RELATED);
}
RestRequest request = builder.build();
if (isDebugMode) {
requestContext.putLocalAttr(RestLiMethodInvoker.ATTRIBUTE_PROMISE_LISTENER, new PromiseListener<Object>() {
@Override
public void onResolved(Promise<Object> promise) {
// PromiseListener is invoked with a task.
if (promise instanceof Task) {
requestContext.putLocalAttr(ATTRIBUTE_PARSEQ_TRACE, ((Task<?>) promise).getTrace());
}
}
});
}
final ServerResourceContext resourceContext = new ResourceContextImpl(pathkeys, request, requestContext);
resourceContext.setRequestAttachmentReader(expectedRequestAttachments);
if (expectedResponseAttachments != null) {
resourceContext.setResponseAttachments(expectedResponseAttachments);
}
RoutingResult routingResult = new RoutingResult(resourceContext, resourceMethodDescriptor, resourceMethodConfig);
RestLiArgumentBuilder argumentBuilder = _methodAdapterProvider.getArgumentBuilder(resourceMethodDescriptor.getType());
RestLiRequestData requestData = argumentBuilder.extractRequestData(routingResult, entityBody != null && !entityBody.isEmpty() ? DataMapUtils.readMapWithExceptions(request) : null);
FilterRequestContext filterContext = new FilterRequestContextInternalImpl(routingResult.getContext(), resourceMethodDescriptor, requestData);
final CountDownLatch latch = new CountDownLatch(1);
final CountDownLatch expectedRoutingExceptionLatch = new CountDownLatch(1);
RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler(_methodAdapterProvider, _errorResponseBuilder);
Callback<RestLiResponse> executionCallback = new Callback<RestLiResponse>() {
@Override
public void onError(Throwable e) {
if (e.getCause() != null && e.getCause().getCause() instanceof RoutingException) {
expectedRoutingExceptionLatch.countDown();
}
if (callback != null) {
callback.onError(e);
}
Assert.assertEquals(resourceContext.getRequestAttachmentReader(), expectedRequestAttachments);
Assert.assertEquals(resourceContext.getResponseAttachments(), expectedResponseAttachments);
latch.countDown();
}
@Override
public void onSuccess(final RestLiResponse result) {
if (callback != null) {
callback.onSuccess(ResponseUtils.buildResponse(routingResult, result));
}
Assert.assertEquals(resourceContext.getResponseAttachments(), expectedResponseAttachments);
latch.countDown();
}
};
FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(routingResult, _invoker, argumentBuilder);
FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(routingResult, restLiResponseHandler, executionCallback, _errorResponseBuilder);
final RestLiCallback outerCallback = new RestLiCallback(filterContext, new RestLiFilterResponseContextFactory(request, routingResult, restLiResponseHandler), new RestLiFilterChain(null, filterChainDispatcher, filterChainCallback));
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), Collections.emptySet(), routingResult.getContext());
_invoker.invoke(requestData, routingResult, argumentBuilder, outerCallback);
try {
latch.await();
if (expectRoutingException) {
expectedRoutingExceptionLatch.await();
}
} catch (InterruptedException e) {
// Ignore
}
EasyMock.verify(resource);
Assert.assertEquals((routingResult.getContext()).getResponseMimeType(), "application/json");
} catch (RestLiSyntaxException e) {
throw new RoutingException("syntax exception", 400);
} finally {
EasyMock.reset(resource);
EasyMock.makeThreadSafe(resource, true);
}
}
use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.
the class TestRestLiMethodInvocation method testInvokeWithUnsupportedAcceptMimeType.
@Test
public void testInvokeWithUnsupportedAcceptMimeType() throws Exception {
RestRequestBuilder builder = new RestRequestBuilder(new URI("")).addHeaderValue("Accept", "text/plain").setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, version.toString());
RestRequest request = builder.build();
final RestLiAttachmentReader attachmentReader = new RestLiAttachmentReader(null);
final CountDownLatch latch = new CountDownLatch(1);
RestLiResponseHandler restLiResponseHandler = new RestLiResponseHandler(_methodAdapterProvider, _errorResponseBuilder);
ServerResourceContext resourceContext = new ResourceContextImpl(new PathKeysImpl(), new RestRequestBuilder(URI.create("")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.LATEST_PROTOCOL_VERSION.toString()).build(), new RequestContext());
resourceContext.setRequestAttachmentReader(attachmentReader);
Callback<RestLiResponse> executionCallback = new Callback<RestLiResponse>() {
@Override
public void onError(Throwable e) {
latch.countDown();
Assert.assertTrue(e instanceof RestException);
RestException ex = (RestException) e;
Assert.assertEquals(ex.getResponse().getStatus(), HttpStatus.S_406_NOT_ACCEPTABLE.getCode());
Assert.assertEquals(resourceContext.getRequestAttachmentReader(), attachmentReader);
Assert.assertNull(resourceContext.getResponseAttachments());
}
@Override
public void onSuccess(RestLiResponse result) {
Assert.fail();
}
};
try {
RoutingResult routingResult = new RoutingResult(resourceContext, null);
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), Collections.emptySet(), routingResult.getContext());
FilterChainDispatcher filterChainDispatcher = new FilterChainDispatcherImpl(routingResult, _invoker, null);
FilterChainCallback filterChainCallback = new FilterChainCallbackImpl(null, restLiResponseHandler, executionCallback, _errorResponseBuilder);
final RestLiCallback callback = new RestLiCallback(null, new RestLiFilterResponseContextFactory(request, null, restLiResponseHandler), new RestLiFilterChain(null, filterChainDispatcher, filterChainCallback));
_invoker.invoke(null, routingResult, null, callback);
latch.await();
} catch (Exception e) {
// exception is expected
Assert.assertTrue(e instanceof RestLiServiceException);
}
Assert.assertNull(resourceContext.getResponseMimeType());
}
Aggregations