use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class TestRestLiFilterResponseContextFactory method testFromThrowable.
@SuppressWarnings("unchecked")
@Test(dataProvider = "provideExceptionsAndStatuses")
public void testFromThrowable(Exception e, HttpStatus status) {
RestLiServiceException serviceException;
if (e instanceof RestLiServiceException) {
serviceException = (RestLiServiceException) e;
} else {
serviceException = new RestLiServiceException(status, e);
}
RestLiServiceException exception = serviceException;
Map<String, String> headers = Collections.emptyMap();
java.util.List<java.net.HttpCookie> cookies = Collections.emptyList();
RestLiResponseData<GetResponseEnvelope> responseData = new RestLiResponseDataImpl<>(new GetResponseEnvelope(exception), headers, cookies);
ArgumentCaptor<RestLiServiceException> exceptionArgumentCaptor = ArgumentCaptor.forClass(RestLiServiceException.class);
// Setup.
when(_responseHandler.buildExceptionResponseData(eq(_routingResult), exceptionArgumentCaptor.capture(), anyMap(), anyList())).thenReturn(responseData);
when(_restRequest.getHeaders()).thenReturn(null);
// Invoke.
FilterResponseContext responseContext = _filterResponseContextFactory.fromThrowable(e);
// Verify.
verify(_responseHandler).buildExceptionResponseData(eq(_routingResult), exceptionArgumentCaptor.capture(), anyMap(), anyList());
verify(_restRequest).getHeaders();
// RestLiCallback should pass the original exception to the response handler.
RestLiServiceException exceptionArgument = exceptionArgumentCaptor.getValue();
assertTrue(exceptionArgument.equals(e) || exceptionArgument.getCause().equals(e));
assertEquals(exceptionArgument.getStatus(), status);
// The end result should also contain the original exception.
assertTrue(responseContext.getResponseData().getResponseEnvelope().isErrorResponse());
assertTrue(responseContext.getResponseData().getResponseEnvelope().getException().equals(e) || responseContext.getResponseData().getResponseEnvelope().getException().getCause().equals(e));
assertEquals(responseContext.getResponseData().getResponseEnvelope().getException().getStatus(), status);
}
use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class RestLiFilterResponseContextFactory method fromThrowable.
/**
* Create a {@link FilterResponseContext} based on the given error.
*
* @param throwable Error obtained from the resource method invocation.
*
* @return {@link FilterResponseContext} corresponding to the given input.
*/
public FilterResponseContext fromThrowable(Throwable throwable) {
RestLiServiceException restLiServiceException;
if (throwable instanceof RestLiServiceException) {
restLiServiceException = (RestLiServiceException) throwable;
} else if (throwable instanceof RoutingException) {
RoutingException routingException = (RoutingException) throwable;
restLiServiceException = new RestLiServiceException(HttpStatus.fromCode(routingException.getStatus()), routingException.getMessage(), routingException);
} else {
restLiServiceException = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, throwable.getMessage(), throwable);
}
Map<String, String> requestHeaders = _request.getHeaders();
Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, ProtocolVersionUtil.extractProtocolVersion(requestHeaders).toString());
headers.put(HeaderUtil.getErrorResponseHeaderName(requestHeaders), RestConstants.HEADER_VALUE_ERROR);
final RestLiResponseData responseData = _responseHandler.buildExceptionResponseData(_request, _method, restLiServiceException, headers, Collections.<HttpCookie>emptyList());
return new FilterResponseContext() {
@Override
public RestLiResponseData getResponseData() {
return responseData;
}
};
}
use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class RestLiCallback method onSuccess.
@Override
public void onSuccess(final T result, RequestExecutionReport executionReport, final RestLiResponseAttachments responseAttachments) {
final FilterResponseContext responseContext;
try {
responseContext = _filterResponseContextFactory.fromResult(result);
} catch (Exception e) {
// Invoke the onError method if we run into any exception while creating the response context from result.
// Note that due to the fact we are in onSuccess(), we assume the application code has absorbed, or is in the
// process of absorbing any request attachments present.
onError(e, executionReport, null, responseAttachments);
return;
}
// Now kick off the responses in the filter chain. Same note as above; we assume that the application code has
// absorbed any request attachments present in the request.
_filterChain.onResponse(_filterRequestContext, responseContext, responseAttachments);
}
use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class RestLiCallback method onError.
public void onError(final Throwable e) {
markPreTimings();
final FilterResponseContext responseContext = _filterResponseContextFactory.fromThrowable(e);
markPostTimings();
// Now kick off the response filters with error
_filterChain.onError(e, _filterRequestContext, responseContext);
}
use of com.linkedin.restli.server.filter.FilterResponseContext in project rest.li by linkedin.
the class RestLiCallback method onSuccess.
public void onSuccess(final Object result) {
markPreTimings();
final FilterResponseContext responseContext;
try {
responseContext = _filterResponseContextFactory.fromResult(result);
} catch (Exception e) {
// Invoke the onError method if we run into any exception while creating the response context from result.
// Note that due to the fact we are in onSuccess(), we assume the application code has absorbed, or is in the
// process of absorbing any request attachments present.
onError(e);
return;
}
markPostTimings();
// Now kick off the responses in the filter chain. Same note as above; we assume that the application code has
// absorbed any request attachments present in the request.
_filterChain.onResponse(_filterRequestContext, responseContext);
}
Aggregations