Search in sources :

Example 11 with FilterResponseContext

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);
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 12 with FilterResponseContext

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;
        }
    };
}
Also used : RoutingException(com.linkedin.restli.server.RoutingException) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext) RestLiResponseData(com.linkedin.restli.server.RestLiResponseData) TreeMap(java.util.TreeMap)

Example 13 with FilterResponseContext

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);
}
Also used : FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext)

Example 14 with FilterResponseContext

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);
}
Also used : FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext)

Example 15 with FilterResponseContext

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);
}
Also used : FilterResponseContext(com.linkedin.restli.server.filter.FilterResponseContext)

Aggregations

FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)19 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)13 BeforeTest (org.testng.annotations.BeforeTest)13 Test (org.testng.annotations.Test)13 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)12 RoutingException (com.linkedin.restli.server.RoutingException)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 RestException (com.linkedin.r2.message.rest.RestException)10 RestLiResponseData (com.linkedin.restli.server.RestLiResponseData)10 RecordTemplate (com.linkedin.data.template.RecordTemplate)8 Answer (org.mockito.stubbing.Answer)6 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)4 DataMap (com.linkedin.data.DataMap)3 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)3 List (java.util.List)3 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 CollectionMetadata (com.linkedin.restli.common.CollectionMetadata)2 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)2 RequestExecutionReportBuilder (com.linkedin.restli.server.RequestExecutionReportBuilder)2 ArrayList (java.util.ArrayList)2