use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestMessages method testToStreamTransportCallbackStreamException.
@Test
public void testToStreamTransportCallbackStreamException() {
TransportCallback<RestResponse> restCallback = response -> {
Assert.assertTrue(response.hasError());
Assert.assertNotNull(response.getError());
Assert.assertTrue(response.getError() instanceof RestException);
Assert.assertNotNull(response.getWireAttributes());
Assert.assertEquals(response.getWireAttributes(), WIRE_ATTR);
};
TransportCallback<StreamResponse> streamCallback = Messages.toStreamTransportCallback(restCallback);
StreamResponseBuilder builder = new StreamResponseBuilder();
StreamResponse streamResponse = builder.build(EntityStreams.newEntityStream(new ByteStringWriter(DATA)));
streamCallback.onResponse(TransportResponseImpl.error(new StreamException(streamResponse, new IllegalStateException()), WIRE_ATTR));
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestServerRetryFilter method testNestedException.
@Test
public void testNestedException() {
String retryMessage = "this is a retry";
ServerRetryFilter retryFilter = new ServerRetryFilter();
RestFilter captureFilter = new RestFilter() {
@Override
public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
}
};
FilterChain filterChain = FilterChains.createRestChain(captureFilter, retryFilter);
Throwable nestedException = new RetriableRequestException(retryMessage);
for (int i = 0; i < 5; i++) {
nestedException = new RuntimeException(nestedException);
}
FilterUtil.fireRestError(filterChain, new RestException(null, nestedException), new HashMap<String, String>());
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class ServerCompressionFilter method onRestRequest.
/**
* Handles compression tasks for incoming requests
*/
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
try {
//Check if the request is compressed, if so, decompress
String requestContentEncoding = req.getHeader(HttpConstants.CONTENT_ENCODING);
if (requestContentEncoding != null) {
//This must be a specific compression type other than *
EncodingType encoding;
try {
encoding = EncodingType.get(requestContentEncoding.trim().toLowerCase());
} catch (IllegalArgumentException ex) {
throw new CompressionException(CompressionConstants.UNSUPPORTED_ENCODING + requestContentEncoding);
}
if (encoding == EncodingType.ANY) {
throw new CompressionException(CompressionConstants.REQUEST_ANY_ERROR + requestContentEncoding);
}
//Process the correct compression types only
if (encoding.hasCompressor()) {
byte[] decompressedContent = encoding.getCompressor().inflate(req.getEntity().asInputStream());
Map<String, String> headers = new HashMap<String, String>(req.getHeaders());
headers.remove(HttpConstants.CONTENT_ENCODING);
headers.put(HttpConstants.CONTENT_LENGTH, Integer.toString(decompressedContent.length));
req = req.builder().setEntity(decompressedContent).setHeaders(headers).build();
}
}
//Get client support for compression and flag compress if need be
String responseAcceptedEncodings = req.getHeader(HttpConstants.ACCEPT_ENCODING);
if (responseAcceptedEncodings == null) {
//Only permit identity
responseAcceptedEncodings = EMPTY;
}
requestContext.putLocalAttr(HttpConstants.ACCEPT_ENCODING, responseAcceptedEncodings);
if (!responseAcceptedEncodings.isEmpty()) {
requestContext.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, _serverCompressionHelper.getResponseCompressionThreshold(req));
}
nextFilter.onRequest(req, requestContext, wireAttrs);
} catch (CompressionException e) {
//If we can't decompress the client's request, we can't do much more with it
LOG.error(e.getMessage(), e.getCause());
RestResponse restResponse = new RestResponseBuilder().setStatus(HttpConstants.UNSUPPORTED_MEDIA_TYPE).build();
nextFilter.onError(new RestException(restResponse, e), requestContext, wireAttrs);
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestServerTimeout method testFilterNotCancelButShouldNotTimeout.
@Test
public void testFilterNotCancelButShouldNotTimeout() throws Exception {
RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, STREAM_EXCEPTION_FILTER_URI)).setEntity(new byte[10240]).build();
_client.restRequest(request);
Future<RestResponse> futureResponse = _client.restRequest(request);
// if server times out, our second request would fail with TimeoutException because it's blocked by first one
try {
futureResponse.get(SERVER_IOHANDLER_TIMEOUT / 2, TimeUnit.MILLISECONDS);
Assert.fail("Should fail with ExecutionException");
} catch (ExecutionException ex) {
Assert.assertTrue(ex.getCause() instanceof RestException);
RestException restException = (RestException) ex.getCause();
Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("StreamException in filter."));
}
}
use of com.linkedin.r2.message.rest.RestException in project rest.li by linkedin.
the class TestServerTimeoutAsyncEvent method testFilterThrowButShouldNotTimeout.
@Test
public void testFilterThrowButShouldNotTimeout() throws Exception {
RestRequest request = new RestRequestBuilder(Bootstrap.createHttpURI(PORT, BUGGY_FILTER_URI)).setEntity(new byte[10240]).build();
_client.restRequest(request);
Future<RestResponse> futureResponse = _client.restRequest(request);
// if server times out, our second request would fail with TimeoutException because it's blocked by first one
try {
futureResponse.get(ASYNC_EVENT_TIMEOUT / 2, TimeUnit.MILLISECONDS);
Assert.fail("Should fail with ExecutionException");
} catch (ExecutionException ex) {
Assert.assertTrue(ex.getCause() instanceof RestException);
RestException restException = (RestException) ex.getCause();
Assert.assertTrue(restException.getResponse().getEntity().asString("UTF8").contains("Buggy filter throws."));
}
}
Aggregations