Search in sources :

Example 26 with RequestContext

use of com.linkedin.r2.message.RequestContext 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 27 with RequestContext

use of com.linkedin.r2.message.RequestContext 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 28 with RequestContext

use of com.linkedin.r2.message.RequestContext 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 29 with RequestContext

use of com.linkedin.r2.message.RequestContext 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 30 with RequestContext

use of com.linkedin.r2.message.RequestContext 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)249 Test (org.testng.annotations.Test)201 RestRequest (com.linkedin.r2.message.rest.RestRequest)148 URI (java.net.URI)139 RestResponse (com.linkedin.r2.message.rest.RestResponse)106 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)102 HashMap (java.util.HashMap)73 ByteString (com.linkedin.data.ByteString)66 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)63 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)55 FutureCallback (com.linkedin.common.callback.FutureCallback)51 ArrayList (java.util.ArrayList)41 Callback (com.linkedin.common.callback.Callback)39 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)39 RestException (com.linkedin.r2.message.rest.RestException)38 Map (java.util.Map)37 CountDownLatch (java.util.concurrent.CountDownLatch)37 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)36 TrackerClient (com.linkedin.d2.balancer.clients.TrackerClient)31 BeforeTest (org.testng.annotations.BeforeTest)31