use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestStreamFilterAdapters method testRequestFilterAdapterCallsOnError.
@Test
public void testRequestFilterAdapterCallsOnError() {
final Exception runTimeException = new RuntimeException();
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onError(runTimeException, requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
Throwable ex = _beforeFilter.getThrowable();
Assert.assertSame(ex, runTimeException);
fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onError(simpleRestException(req.getEntity().asString("UTF8")), requestContext, wireAttrs);
}
});
fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
ex = _beforeFilter.getThrowable();
Assert.assertTrue(ex instanceof StreamException);
StreamResponse errorResponse = ((StreamException) ex).getResponse();
errorResponse.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("should not happen");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "12345");
}
}));
}
use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestStreamFilterAdapters method testResponseFilterAdapterCallsOnErrorInOnResponse.
@Test
public void testResponseFilterAdapterCallsOnErrorInOnResponse() {
FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onError(simpleRestException(res.getEntity().asString("UTF8")), requestContext, wireAttrs);
}
@Override
public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
}
});
fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
Throwable capturedEx = _beforeFilter.getThrowable();
Assert.assertTrue(capturedEx instanceof StreamException);
((StreamException) capturedEx).getResponse().getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {
@Override
public void onError(Throwable e) {
Assert.fail("should not happen");
}
@Override
public void onSuccess(ByteString result) {
Assert.assertEquals(result.asString("UTF8"), "12345");
}
}));
fc = adaptAndCreateFilterChain(new RestFilter() {
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
nextFilter.onError(new IllegalStateException(), requestContext, wireAttrs);
}
@Override
public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
}
});
fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
capturedEx = _beforeFilter.getThrowable();
Assert.assertTrue(capturedEx instanceof IllegalStateException);
}
use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestHttpBridge method testHttpToStreamErrorMessage.
@Test
public void testHttpToStreamErrorMessage() throws TimeoutException, InterruptedException, ExecutionException {
FutureCallback<StreamResponse> futureCallback = new FutureCallback<StreamResponse>();
TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(futureCallback);
TransportCallback<StreamResponse> bridgeCallback = HttpBridge.httpToStreamCallback(callback);
StreamResponse streamResponse = new StreamResponseBuilder().build(EntityStreams.emptyStream());
// Note: FutureCallback will fail if called twice. An exception would be raised on the current
// thread because we begin the callback sequence here in onResponse.
// (test originally added due to bug with double callback invocation)
bridgeCallback.onResponse(TransportResponseImpl.<StreamResponse>error(new StreamException(streamResponse)));
StreamResponse resp = futureCallback.get(30, TimeUnit.SECONDS);
// should have unpacked restResponse from the RestException that we passed in without
// propagating the actual exception
Assert.assertSame(resp, streamResponse);
}
use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class ServerStreamCompressionFilter method onStreamRequest.
/**
* Handles compression tasks for incoming requests
*/
@Override
public void onStreamRequest(StreamRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> 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 *
StreamEncodingType encoding = StreamEncodingType.get(requestContentEncoding.trim().toLowerCase());
if (encoding == null || encoding == StreamEncodingType.ANY) {
throw new CompressionException(CompressionConstants.UNSUPPORTED_ENCODING + requestContentEncoding);
}
//Process the correct content-encoding types only
StreamingCompressor compressor = encoding.getCompressor(_executor);
if (compressor == null) {
throw new CompressionException(CompressionConstants.UNKNOWN_ENCODING + encoding);
}
EntityStream uncompressedStream = compressor.inflate(req.getEntityStream());
Map<String, String> headers = stripHeaders(req.getHeaders(), HttpConstants.CONTENT_ENCODING, HttpConstants.CONTENT_LENGTH);
req = req.builder().setHeaders(headers).build(uncompressedStream);
}
//Get client support for compression and flag compress if need be
String responseCompression = req.getHeader(HttpConstants.ACCEPT_ENCODING);
if (responseCompression == null) {
// per RFC 2616, section 14.3, if no Accept-Encoding field is present in a request,
// server SHOULD use "identity" content-encoding if it is available.
responseCompression = StreamEncodingType.IDENTITY.getHttpName();
}
if (!responseCompression.equalsIgnoreCase(StreamEncodingType.IDENTITY.getHttpName())) {
requestContext.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, _serverCompressionHelper.getResponseCompressionThreshold(req));
}
requestContext.putLocalAttr(HttpConstants.ACCEPT_ENCODING, responseCompression);
nextFilter.onRequest(req, requestContext, wireAttrs);
} catch (CompressionException ex) {
LOG.error(ex.getMessage(), ex.getCause());
StreamResponse streamResponse = new StreamResponseBuilder().setStatus(HttpConstants.UNSUPPORTED_MEDIA_TYPE).build(EntityStreams.emptyStream());
nextFilter.onError(new StreamException(streamResponse, ex), requestContext, wireAttrs);
}
}
use of com.linkedin.r2.message.stream.StreamException in project rest.li by linkedin.
the class TestJetty404 method testJetty404.
// make sure jetty's default behavior will read all the request bytes in case of 404
@Test
public void testJetty404() throws Exception {
BytesWriter writer = new BytesWriter(200 * 1024, (byte) 100);
final AtomicReference<Throwable> exRef = new AtomicReference<Throwable>();
final CountDownLatch latch = new CountDownLatch(1);
_client.streamRequest(new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, URI.create("/wrong-path"))).build(EntityStreams.newEntityStream(writer)), new Callback<StreamResponse>() {
@Override
public void onError(Throwable e) {
exRef.set(e);
latch.countDown();
}
@Override
public void onSuccess(StreamResponse result) {
latch.countDown();
}
});
latch.await(5000, TimeUnit.MILLISECONDS);
Assert.assertTrue(writer.isDone());
Throwable ex = exRef.get();
Assert.assertTrue(ex instanceof StreamException);
StreamResponse response = ((StreamException) ex).getResponse();
Assert.assertEquals(response.getStatus(), RestStatus.NOT_FOUND);
response.getEntityStream().setReader(new DrainReader());
}
Aggregations