Search in sources :

Example 11 with NextFilter

use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.

the class ClientStreamCompressionFilter method onStreamError.

@Override
public void onStreamError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
    if (ex instanceof StreamException) {
        Boolean decompressionOff = (Boolean) requestContext.getLocalAttr(R2Constants.RESPONSE_DECOMPRESSION_OFF);
        if (decompressionOff == null || !decompressionOff) {
            StreamException se = (StreamException) ex;
            StreamResponse response = se.getResponse();
            //Check for header encoding
            String compressionHeader = response.getHeader(HttpConstants.CONTENT_ENCODING);
            //decompress if necessary
            if (compressionHeader != null) {
                StreamEncodingType encoding = StreamEncodingType.get(compressionHeader.trim().toLowerCase());
                if (encoding != null) {
                    final StreamingCompressor compressor = encoding.getCompressor(_executor);
                    EntityStream uncompressedStream = compressor.inflate(response.getEntityStream());
                    StreamResponseBuilder builder = response.builder();
                    Map<String, String> headers = stripHeaders(builder.getHeaders(), HttpConstants.CONTENT_ENCODING, HttpConstants.CONTENT_LENGTH);
                    response = builder.setHeaders(headers).build(uncompressedStream);
                    ex = new StreamException(response);
                }
            }
        }
    }
    nextFilter.onError(ex, requestContext, wireAttrs);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) StreamEncodingType(com.linkedin.r2.filter.compression.streaming.StreamEncodingType) StreamException(com.linkedin.r2.message.stream.StreamException)

Example 12 with NextFilter

use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.

the class TestServerRetryFilter method testStreamRetryFilter.

@Test
public void testStreamRetryFilter() {
    String retryMessage = "this is a retry";
    ServerRetryFilter retryFilter = new ServerRetryFilter();
    StreamFilter captureFilter = new StreamFilter() {

        @Override
        public void onStreamError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
            Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
        }
    };
    FilterChain filterChain = FilterChains.createStreamChain(captureFilter, retryFilter);
    FilterUtil.fireRestError(filterChain, new StreamException(null, new RetriableRequestException(retryMessage)), new HashMap<String, String>());
}
Also used : RetriableRequestException(com.linkedin.r2.RetriableRequestException) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RequestContext(com.linkedin.r2.message.RequestContext) StreamFilter(com.linkedin.r2.filter.message.stream.StreamFilter) HashMap(java.util.HashMap) Map(java.util.Map) StreamException(com.linkedin.r2.message.stream.StreamException) Test(org.testng.annotations.Test)

Example 13 with NextFilter

use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.

the class TestStreamFilterAdapters method testRequestFilterAdapterPassThrough.

@Test
public void testRequestFilterAdapterPassThrough() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onRequest(req, requestContext, wireAttrs);
        }
    });
    fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    StreamRequest capturedReq = _afterFilter.getRequest();
    Assert.assertEquals(capturedReq.getURI(), SIMPLE_URI);
    capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("shouldn't have error");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "12345");
        }
    }));
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) ByteString(com.linkedin.data.ByteString) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 14 with NextFilter

use of com.linkedin.r2.filter.NextFilter 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");
        }
    }));
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) NextFilter(com.linkedin.r2.filter.NextFilter) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) StreamException(com.linkedin.r2.message.stream.StreamException) RestException(com.linkedin.r2.message.rest.RestException) StreamException(com.linkedin.r2.message.stream.StreamException) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 15 with NextFilter

use of com.linkedin.r2.filter.NextFilter in project rest.li by linkedin.

the class TestStreamFilterAdapters method testRequestFilterAdapterChangeRequest.

@Test
public void testRequestFilterAdapterChangeRequest() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onRequest(req.builder().setEntity(req.getEntity().asString("UTF8").replace('1', '0').getBytes()).build(), requestContext, wireAttrs);
        }
    });
    fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    StreamRequest capturedReq = _afterFilter.getRequest();
    Assert.assertEquals(capturedReq.getURI(), SIMPLE_URI);
    capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("shouldn't have error");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "02345");
        }
    }));
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) ByteString(com.linkedin.data.ByteString) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Aggregations

RequestContext (com.linkedin.r2.message.RequestContext)23 Test (org.testng.annotations.Test)22 Map (java.util.Map)18 NextFilter (com.linkedin.r2.filter.NextFilter)17 FilterChain (com.linkedin.r2.filter.FilterChain)15 RestResponse (com.linkedin.r2.message.rest.RestResponse)15 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)14 RestRequest (com.linkedin.r2.message.rest.RestRequest)14 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)13 Callback (com.linkedin.common.callback.Callback)10 HashMap (java.util.HashMap)10 ByteString (com.linkedin.data.ByteString)9 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 URI (java.net.URI)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 StreamException (com.linkedin.r2.message.stream.StreamException)7 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)7 RestException (com.linkedin.r2.message.rest.RestException)6