use of com.linkedin.restli.server.filter.FilterRequestContext in project rest.li by linkedin.
the class ErrorResponseValidationFilter method onError.
@Override
public CompletableFuture<Void> onError(Throwable throwable, final FilterRequestContext requestContext, final FilterResponseContext responseContext) {
CompletableFuture<Void> future = new CompletableFuture<>();
if (throwable instanceof RestLiServiceException) {
RestLiServiceException restLiServiceException = (RestLiServiceException) throwable;
// do the validation only if the 'code' field is set on RestLiServiceException.
if (restLiServiceException.hasCode()) {
List<ServiceError> methodServiceErrors = requestContext.getMethodServiceErrors();
List<ServiceError> resourceServiceErrors = requestContext.getFilterResourceModel().getServiceErrors();
// nor on the method level skip the validation.
if (methodServiceErrors == null && resourceServiceErrors == null) {
// error details should not be set on RestLiServiceException object.
if (restLiServiceException.getErrorDetailsRecord() != null) {
return completeExceptionallyWithHttp500(future, restLiServiceException);
}
future.completeExceptionally(restLiServiceException);
return future;
}
Set<ServiceError> serviceErrors = new HashSet<>();
if (methodServiceErrors != null) {
serviceErrors.addAll(methodServiceErrors);
}
if (resourceServiceErrors != null) {
serviceErrors.addAll(resourceServiceErrors);
}
// An empty list of codes means that any service error code will result in a Http 500 error response.
if (serviceErrors.isEmpty()) {
return completeExceptionallyWithHttp500(future, restLiServiceException);
}
String errorCode = restLiServiceException.getCode();
Optional<ServiceError> maybeServiceError = serviceErrors.stream().filter(serviceError -> serviceError.code().equals(errorCode)).findFirst();
// convert given throwable to 500_INTERNAL_SERVER_ERROR exception.
if (!maybeServiceError.isPresent()) {
return completeExceptionallyWithHttp500(future, restLiServiceException);
}
ServiceError definedServiceError = maybeServiceError.get();
// Check that the error detail type is valid.
if (restLiServiceException.hasErrorDetails()) {
Class<?> errorResponseErrorDetailType = restLiServiceException.getErrorDetailsRecord().getClass();
Class<?> definedErrorDetailType = definedServiceError.errorDetailType();
if (!errorResponseErrorDetailType.equals(definedErrorDetailType)) {
return completeExceptionallyWithHttp500(future, restLiServiceException);
}
}
// convert given throwable to 500_INTERNAL_SERVER_ERROR exception.
if (definedServiceError.httpStatus() != restLiServiceException.getStatus()) {
return completeExceptionallyWithHttp500(future, restLiServiceException);
}
// TODO: validate error message. What if the defined message in service error has placeholders, which gets filled based on some business logic in the code.
}
}
future.completeExceptionally(throwable);
return future;
}
use of com.linkedin.restli.server.filter.FilterRequestContext in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithFiltersExceptionFromFirstFilterSecondFilterHandlesEx.
@SuppressWarnings("unchecked")
@Test
public void testOnSuccessWithFiltersExceptionFromFirstFilterSecondFilterHandlesEx() throws Exception {
// App stuff.
final RecordTemplate entityFromApp = Foo.createFoo("Key", "One");
RestLiResponseData<CreateResponseEnvelope> appResponseData = ResponseDataBuilderUtil.buildCreateResponseData(HttpStatus.S_200_OK, entityFromApp);
// Filter stuff.
final Map<String, String> errorHeaders = buildErrorHeaders();
final RecordTemplate entityFromFilter = Foo.createFoo("Key", "Two");
RestLiResponse partialFilterErrorResponse = new RestLiResponse.Builder().build();
final Exception exFromFilter = new RuntimeException("Exception From Filter");
// Setup.
when((RestLiResponseData<CreateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, entityFromApp)).thenReturn(appResponseData);
when(_restRequest.getHeaders()).thenReturn(null);
when(_responseHandler.buildPartialResponse(_routingResult, appResponseData)).thenReturn(partialFilterErrorResponse);
// Mock filter behavior.
doThrow(exFromFilter).when(_filter).onResponse(eq(_filterRequestContext), any(FilterResponseContext.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Throwable t = (Throwable) args[0];
FilterRequestContext requestContext = (FilterRequestContext) args[1];
FilterResponseContext responseContext = (FilterResponseContext) args[2];
// The second filter should be invoked with details of the exception thrown by the first
// filter.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
assertNull(responseData.getResponseEnvelope().getRecord());
assertEquals(responseData.getHeaders(), errorHeaders);
assertEquals(responseData.getResponseEnvelope().getException().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
// Modify data.
setStatus(responseContext, HttpStatus.S_402_PAYMENT_REQUIRED);
// The second filter handles the exception thrown by the first filter (i.e.) sets an entity
// response in the response data.
responseData.getResponseEnvelope().setRecord(entityFromFilter, HttpStatus.S_402_PAYMENT_REQUIRED);
responseData.getHeaders().put("error-fixed", "second-filter");
return CompletableFuture.completedFuture(null);
}
}).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
// invoke request filters so cursor is in correct place
when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
_twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
// Invoke.
_twoFilterRestLiCallback.onSuccess(entityFromApp);
// Verify.
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, entityFromApp);
verify(_responseHandler).buildPartialResponse(_routingResult, appResponseData);
verify(_callback).onSuccess(any(RestLiResponse.class));
Map<String, String> expectedHeaders = Maps.newHashMap();
expectedHeaders.put("X-RestLi-Protocol-Version", "1.0.0");
expectedHeaders.put("error-fixed", "second-filter");
verifyNoMoreInteractions(_responseHandler, _callback);
assertFalse(appResponseData.getResponseEnvelope().isErrorResponse());
assertEquals(appResponseData.getResponseEnvelope().getRecord(), entityFromFilter);
assertNotNull(appResponseData);
assertEquals(HttpStatus.S_402_PAYMENT_REQUIRED, appResponseData.getResponseEnvelope().getStatus());
assertEquals(appResponseData.getHeaders(), expectedHeaders);
assertNull(appResponseData.getResponseEnvelope().getException());
}
use of com.linkedin.restli.server.filter.FilterRequestContext in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithFiltersExceptionFromFirstFilterSecondFilterDoesNotHandleEx.
@SuppressWarnings("unchecked")
@Test
public void testOnSuccessWithFiltersExceptionFromFirstFilterSecondFilterDoesNotHandleEx() throws Exception {
// App stuff.
final RecordTemplate entityFromApp = Foo.createFoo("Key", "Two");
RestLiResponseData<CreateResponseEnvelope> appResponseData = ResponseDataBuilderUtil.buildCreateResponseData(HttpStatus.S_200_OK, entityFromApp);
// Filter stuff.
ArgumentCaptor<RestLiServiceException> exFromFilterCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
final Map<String, String> headersFromFilter = Maps.newHashMap();
headersFromFilter.put("Key", "Error from filter");
RestLiServiceException exceptionFromFilter = new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR);
RestLiResponseData<?> responseErrorData = new RestLiResponseDataImpl<>(new CreateResponseEnvelope(exceptionFromFilter, false), headersFromFilter, Collections.emptyList());
RestLiResponse partialFilterErrorResponse = new RestLiResponse.Builder().build();
final Exception exFromFilter = new RuntimeException("Exception From Filter");
Map<String, String> errorHeaders = buildErrorHeaders();
// Common stuff.
RestException finalRestException = new RestException(new RestResponseBuilder().build());
// Setup.
when((RestLiResponseData<CreateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, entityFromApp)).thenReturn(appResponseData);
when(_restRequest.getHeaders()).thenReturn(null);
when(_responseHandler.buildExceptionResponseData(eq(_routingResult), exFromFilterCapture.capture(), anyMap(), anyList())).thenReturn(responseErrorData);
when(_responseHandler.buildPartialResponse(_routingResult, responseErrorData)).thenReturn(partialFilterErrorResponse);
// Mock filter behavior.
doThrow(exFromFilter).when(_filter).onResponse(eq(_filterRequestContext), any(FilterResponseContext.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Throwable t = (Throwable) args[0];
FilterRequestContext requestContext = (FilterRequestContext) args[1];
FilterResponseContext responseContext = (FilterResponseContext) args[2];
// The second filter should be invoked with details of the exception thrown by the first
// filter.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
assertNull(responseData.getResponseEnvelope().getRecord());
assertEquals(responseData.getHeaders(), errorHeaders);
assertEquals(responseData.getResponseEnvelope().getException().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
// Modify data.
setStatus(responseContext, HttpStatus.S_402_PAYMENT_REQUIRED);
return completedFutureWithError(responseData.getResponseEnvelope().getException());
}
}).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
// invoke request filters so cursor is in correct place
when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
_twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
// Invoke.
_twoFilterRestLiCallback.onSuccess(entityFromApp);
// Verify.
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, entityFromApp);
verify(_responseHandler).buildPartialResponse(_routingResult, appResponseData);
ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
verifyNoMoreInteractions(_responseHandler, _callback);
RestLiResponseException restLiResponseException = partialRestResponseExceptionCaptor.getValue();
assertTrue(restLiResponseException.getCause() instanceof RestLiServiceException);
RestLiServiceException restliEx1 = (RestLiServiceException) restLiResponseException.getCause();
assertNotNull(restliEx1);
assertEquals(HttpStatus.S_402_PAYMENT_REQUIRED, restliEx1.getStatus());
// exceptions should not be equal because in new logic we are replacing the exception with new one
assertNotEquals(exFromFilter.getMessage(), restliEx1.getMessage());
assertNotEquals(exFromFilter, restliEx1.getCause());
assertNotNull(appResponseData);
assertEquals(HttpStatus.S_402_PAYMENT_REQUIRED, appResponseData.getResponseEnvelope().getStatus());
assertEquals(appResponseData.getHeaders(), errorHeaders);
assertNull(appResponseData.getResponseEnvelope().getRecord());
}
use of com.linkedin.restli.server.filter.FilterRequestContext in project rest.li by linkedin.
the class TestRestLiCallback method testOnSuccessWithFiltersSuccessful.
@Test
@SuppressWarnings("unchecked")
public void testOnSuccessWithFiltersSuccessful() throws Exception {
String result = "foo";
final RestLiResponseAttachments restLiResponseAttachments = new RestLiResponseAttachments.Builder().build();
final RecordTemplate entityFromApp = Foo.createFoo("Key", "One");
final Map<String, String> headersFromApp = Maps.newHashMap();
headersFromApp.put("Key", "Input");
final RecordTemplate entityFromFilter1 = Foo.createFoo("Key", "Two");
final RecordTemplate entityFromFilter2 = Foo.createFoo("Key", "Three");
final Map<String, String> headersFromFilters = Maps.newHashMap();
headersFromFilters.put("Key", "Output");
RestLiResponseData<CreateResponseEnvelope> appResponseData = new RestLiResponseDataImpl<>(new CreateResponseEnvelope(HttpStatus.S_200_OK, entityFromApp, false), headersFromApp, Collections.emptyList());
RestLiResponse partialResponse = new RestLiResponse.Builder().build();
// Setup.
when((RestLiResponseData<CreateResponseEnvelope>) _responseHandler.buildRestLiResponseData(_restRequest, _routingResult, result)).thenReturn(appResponseData);
when(_responseHandler.buildPartialResponse(_routingResult, appResponseData)).thenReturn(partialResponse);
// Mock the behavior of the first filter.
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
// Verify incoming data.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(HttpStatus.S_200_OK, responseData.getResponseEnvelope().getStatus());
assertEquals(headersFromApp, responseData.getHeaders());
assertEquals(entityFromApp, responseData.getResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
responseData.getResponseEnvelope().setRecord(entityFromFilter1, HttpStatus.S_400_BAD_REQUEST);
responseData.getHeaders().clear();
return CompletableFuture.completedFuture(null);
}
}).doAnswer(new Answer<Object>() {
// Mock the behavior of the second filter.
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
// Verify incoming data.
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(HttpStatus.S_400_BAD_REQUEST, responseData.getResponseEnvelope().getStatus());
assertTrue(responseData.getHeaders().isEmpty());
assertEquals(responseData.getResponseEnvelope().getRecord(), entityFromFilter1);
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
responseData.getResponseEnvelope().setRecord(entityFromFilter2, HttpStatus.S_403_FORBIDDEN);
responseData.getHeaders().putAll(headersFromFilters);
return CompletableFuture.completedFuture(null);
}
}).when(_filter).onResponse(eq(_filterRequestContext), any(FilterResponseContext.class));
// invoke request filters so cursor is in correct place
when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
_twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
// Invoke with some response attachments.
_twoFilterRestLiCallback.onSuccess(result);
// Verify.
assertNotNull(appResponseData);
assertEquals(HttpStatus.S_403_FORBIDDEN, appResponseData.getResponseEnvelope().getStatus());
assertEquals(entityFromFilter2, appResponseData.getResponseEnvelope().getRecord());
assertEquals(headersFromFilters, appResponseData.getHeaders());
verify(_responseHandler).buildRestLiResponseData(_restRequest, _routingResult, result);
verify(_responseHandler).buildPartialResponse(_routingResult, appResponseData);
verify(_callback).onSuccess(partialResponse);
verifyZeroInteractions(_restRequest);
verifyNoMoreInteractions(_responseHandler, _callback);
}
use of com.linkedin.restli.server.filter.FilterRequestContext in project rest.li by linkedin.
the class TestRestLiCallback method testOnErrorWithFiltersExceptionFromFirstFilterSecondFilterDoesNotHandle.
@SuppressWarnings("unchecked")
@Test
public void testOnErrorWithFiltersExceptionFromFirstFilterSecondFilterDoesNotHandle() throws Exception {
// App stuff.
RestLiServiceException exFromApp = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND, "App failure");
RestLiResponseData<CreateResponseEnvelope> responseAppData = ResponseDataBuilderUtil.buildCreateResponseData(exFromApp);
// Filter stuff.
final Exception exFromFirstFilter = new RuntimeException("Runtime exception from first filter");
RestLiResponse partialResponse = new RestLiResponse.Builder().build();
// Setup.
when(_responseHandler.buildExceptionResponseData(eq(_routingResult), any(RestLiServiceException.class), anyMap(), anyList())).thenReturn(responseAppData);
when(_responseHandler.buildPartialResponse(_routingResult, responseAppData)).thenReturn(partialResponse);
when(_restRequest.getHeaders()).thenReturn(null);
Map<String, String> errorHeaders = buildErrorHeaders();
// Mock filter behavior.
doThrow(exFromFirstFilter).doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Throwable t = (Throwable) args[0];
FilterRequestContext requestContext = (FilterRequestContext) args[1];
FilterResponseContext responseContext = (FilterResponseContext) args[2];
// The second filter should be invoked with original exception
RestLiResponseData<CreateResponseEnvelope> responseData = (RestLiResponseData<CreateResponseEnvelope>) responseContext.getResponseData();
assertEquals(responseData.getResponseEnvelope().getStatus(), HttpStatus.S_500_INTERNAL_SERVER_ERROR);
assertNull(responseData.getResponseEnvelope().getRecord());
assertEquals(responseData.getHeaders(), errorHeaders);
assertTrue(responseData.getResponseEnvelope().isErrorResponse());
// Modify data.
setStatus(responseContext, HttpStatus.S_402_PAYMENT_REQUIRED);
// filter.
return completedFutureWithError(responseData.getResponseEnvelope().getException());
}
}).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
// invoke request filters so cursor is in correct place
when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
_twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
// Invoke.
_twoFilterRestLiCallback.onError(exFromApp);
// Verify.
ArgumentCaptor<RestLiServiceException> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
verify(_responseHandler).buildExceptionResponseData(eq(_routingResult), exCapture.capture(), anyMap(), anyList());
verify(_responseHandler).buildPartialResponse(_routingResult, responseAppData);
ArgumentCaptor<RestLiResponseException> partialRestResponseExceptionCaptor = ArgumentCaptor.forClass(RestLiResponseException.class);
verify(_callback).onError(partialRestResponseExceptionCaptor.capture());
verify(_restRequest).getHeaders();
verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
assertNotNull(responseAppData);
assertEquals(HttpStatus.S_402_PAYMENT_REQUIRED, responseAppData.getResponseEnvelope().getStatus());
assertEquals(responseAppData.getHeaders(), errorHeaders);
assertNull(responseAppData.getResponseEnvelope().getRecord());
RestLiServiceException restliEx = exCapture.getAllValues().get(0);
assertNotNull(restliEx);
assertEquals(exFromApp.getStatus(), restliEx.getStatus());
assertEquals(exFromApp.getMessage(), restliEx.getMessage());
RestLiResponseException restLiResponseException = partialRestResponseExceptionCaptor.getValue();
assertEquals(restLiResponseException.getRestLiResponse(), partialResponse);
assertTrue(restLiResponseException.getCause() instanceof RestLiServiceException);
restliEx = (RestLiServiceException) restLiResponseException.getCause();
assertEquals(HttpStatus.S_402_PAYMENT_REQUIRED, restliEx.getStatus());
}
Aggregations