Search in sources :

Example 1 with RestFilter

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

the class RestFilterTest method setUp.

@BeforeMethod
public void setUp() throws Exception {
    _filter = new RestFilter() {
    };
    _beforeFilter = new RestCountFilter();
    _afterFilter = new RestCountFilter();
    _fc = FilterChains.createRestChain(_beforeFilter, _filter, _afterFilter);
}
Also used : BaseRestFilter(com.linkedin.r2.filter.message.rest.BaseRestFilter) RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestCountFilter(com.linkedin.r2.testutils.filter.RestCountFilter) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with RestFilter

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

the class TestRestCompressionEcho method compressionEchoData.

@DataProvider
public Object[][] compressionEchoData() {
    EncodingType[] encodings = new EncodingType[] { EncodingType.GZIP, EncodingType.SNAPPY, EncodingType.IDENTITY };
    Object[][] args = new Object[4 * encodings.length * encodings.length][2];
    int cur = 0;
    for (EncodingType requestEncoding : encodings) {
        for (EncodingType acceptEncoding : encodings) {
            RestFilter clientCompressionFilter = new ClientCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), new EncodingType[] { acceptEncoding }, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }));
            TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createRestChain(clientCompressionFilter)).build();
            Client http1Client = new TransportClientAdapter(factory.getClient(getHttp1ClientProperties()), REST_OVER_STREAM);
            Client http2Client = new TransportClientAdapter(factory.getClient(getHttp2ClientProperties()), REST_OVER_STREAM);
            args[cur][0] = http1Client;
            args[cur][1] = LARGE_BYTES_NUM;
            args[cur + 1][0] = http2Client;
            args[cur + 1][1] = LARGE_BYTES_NUM;
            cur += 2;
            _clientFactories.add(factory);
            _clients.add(http1Client);
            _clients.add(http2Client);
        }
    }
    // test data that won't trigger compression
    for (EncodingType requestEncoding : encodings) {
        for (EncodingType acceptEncoding : encodings) {
            RestFilter clientCompressionFilter = new ClientCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), new EncodingType[] { acceptEncoding }, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }));
            TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createRestChain(clientCompressionFilter)).build();
            Client http1Client = new TransportClientAdapter(factory.getClient(getHttp1ClientProperties()), REST_OVER_STREAM);
            Client http2Client = new TransportClientAdapter(factory.getClient(getHttp2ClientProperties()), REST_OVER_STREAM);
            args[cur][0] = http1Client;
            args[cur][1] = SMALL_BYTES_NUM;
            args[cur + 1][0] = http2Client;
            args[cur + 1][1] = SMALL_BYTES_NUM;
            cur += 2;
            _clientFactories.add(factory);
            _clients.add(http1Client);
            _clients.add(http2Client);
        }
    }
    return args;
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) EncodingType(com.linkedin.r2.filter.compression.EncodingType) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) Client(com.linkedin.r2.transport.common.Client) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) ClientCompressionFilter(com.linkedin.r2.filter.compression.ClientCompressionFilter) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Example 3 with RestFilter

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

the class TestRequestCompression method initClass.

@BeforeClass
public void initClass() throws Exception {
    class CheckRequestCompressionFilter implements RestFilter {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Map<String, String> requestHeaders = req.getHeaders();
            if (requestHeaders.containsKey(TEST_HELP_HEADER)) {
                String contentEncodingHeader = requestHeaders.get(HttpConstants.CONTENT_ENCODING);
                if (requestHeaders.get(TEST_HELP_HEADER).equals(EXPECT_COMPRESSION)) {
                    if (contentEncodingHeader == null) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is not compressed when it should be.");
                    } else if (!contentEncodingHeader.equals("x-snappy-framed")) {
                        // which is always snappy in this test.
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is compressed with " + contentEncodingHeader + " instead of x-snappy-framed.");
                    }
                } else {
                    if (contentEncodingHeader != null) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Request is compressed when it shouldn't be.");
                    }
                }
            }
            nextFilter.onRequest(req, requestContext, wireAttrs);
        }
    }
    // Check that Content-Encoding and Content-Length headers are set correctly by ServerCompressionFilter.
    class CheckHeadersFilter implements RestFilter {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            if (req.getHeaders().containsKey(HttpConstants.CONTENT_ENCODING)) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Encoding header not removed.");
            }
            if (req.getEntity().length() != Integer.parseInt(req.getHeader(HttpConstants.CONTENT_LENGTH))) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Content-Length header incorrect.");
            }
            nextFilter.onRequest(req, requestContext, wireAttrs);
        }
    }
    final FilterChain fc = FilterChains.empty().addLastRest(new CheckRequestCompressionFilter()).addLastRest(new ServerCompressionFilter(RestLiIntTestServer.supportedCompression)).addLastRest(new CheckHeadersFilter()).addLastRest(new SimpleLoggingFilter());
    super.init(null, fc, false);
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) NextFilter(com.linkedin.r2.filter.NextFilter) ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterChain(com.linkedin.r2.filter.FilterChain) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) BeforeClass(org.testng.annotations.BeforeClass)

Example 4 with RestFilter

use of com.linkedin.r2.filter.message.rest.RestFilter 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 5 with RestFilter

use of com.linkedin.r2.filter.message.rest.RestFilter 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)

Aggregations

RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)19 Test (org.testng.annotations.Test)16 FilterChain (com.linkedin.r2.filter.FilterChain)15 RequestContext (com.linkedin.r2.message.RequestContext)14 RestRequest (com.linkedin.r2.message.rest.RestRequest)9 Callback (com.linkedin.common.callback.Callback)8 ByteString (com.linkedin.data.ByteString)8 NextFilter (com.linkedin.r2.filter.NextFilter)8 RestResponse (com.linkedin.r2.message.rest.RestResponse)8 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 Map (java.util.Map)8 HashMap (java.util.HashMap)6 RestException (com.linkedin.r2.message.rest.RestException)4 StreamException (com.linkedin.r2.message.stream.StreamException)4 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)4 RetriableRequestException (com.linkedin.r2.RetriableRequestException)3 ServerRetryFilter (com.linkedin.r2.filter.transport.ServerRetryFilter)3 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)2 ClientRetryFilter (com.linkedin.r2.filter.transport.ClientRetryFilter)2 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2