Search in sources :

Example 1 with ServerCompressionFilter

use of com.linkedin.r2.filter.compression.ServerCompressionFilter in project incubator-gobblin by apache.

the class EmbeddedRestliServer method startUp.

@Override
protected void startUp() throws Exception {
    RestLiConfig config = new RestLiConfig();
    Set<String> resourceClassNames = Sets.newHashSet();
    for (Class<? extends BaseResource> resClass : this.resources) {
        resourceClassNames.add(resClass.getName());
    }
    config.addResourceClassNames(resourceClassNames);
    config.setServerNodeUri(this.serverUri);
    config.setDocumentationRequestHandler(new DefaultDocumentationRequestHandler());
    config.addFilter(new RestLiValidationFilter());
    ResourceFactory factory = new GuiceInjectResourceFactory(this.injector);
    TransportDispatcher dispatcher = new DelegatingTransportDispatcher(new RestLiServer(config, factory));
    String acceptedFilters = EncodingType.SNAPPY.getHttpName() + "," + EncodingType.GZIP.getHttpName();
    FilterChain filterChain = FilterChains.createRestChain(new ServerCompressionFilter(acceptedFilters));
    this.httpServer = Optional.of(new HttpNettyServerFactory(filterChain).createServer(this.port, dispatcher));
    this.log.info("Starting the {} embedded server at port {}.", this.name, this.port);
    this.httpServer.get().start();
}
Also used : RestLiValidationFilter(com.linkedin.restli.server.validation.RestLiValidationFilter) RestLiServer(com.linkedin.restli.server.RestLiServer) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) HttpNettyServerFactory(com.linkedin.r2.transport.http.server.HttpNettyServerFactory) ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) DefaultDocumentationRequestHandler(com.linkedin.restli.docgen.DefaultDocumentationRequestHandler) FilterChain(com.linkedin.r2.filter.FilterChain) GuiceInjectResourceFactory(com.linkedin.restli.server.guice.GuiceInjectResourceFactory) ResourceFactory(com.linkedin.restli.server.resources.ResourceFactory) DelegatingTransportDispatcher(com.linkedin.restli.server.DelegatingTransportDispatcher) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) RestLiConfig(com.linkedin.restli.server.RestLiConfig) GuiceInjectResourceFactory(com.linkedin.restli.server.guice.GuiceInjectResourceFactory)

Example 2 with ServerCompressionFilter

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

the class RestLiIntegrationTest method init.

public void init(List<? extends Filter> filters) throws IOException {
    final FilterChain fc = FilterChains.empty().addLastRest(new ServerCompressionFilter(RestLiIntTestServer.supportedCompression, new CompressionConfig(0))).addLastRest(new SimpleLoggingFilter());
    init(filters, fc, false);
}
Also used : ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterChain(com.linkedin.r2.filter.FilterChain) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) CompressionConfig(com.linkedin.r2.filter.CompressionConfig)

Example 3 with ServerCompressionFilter

use of com.linkedin.r2.filter.compression.ServerCompressionFilter 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 ServerCompressionFilter

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

the class TestResponseCompression method initClass.

@BeforeClass
public void initClass() throws Exception {
    class TestHelperFilter implements Filter {

        @Override
        public CompletableFuture<Void> onRequest(FilterRequestContext requestContext) {
            Map<String, String> requestHeaders = requestContext.getRequestHeaders();
            if (requestHeaders.containsKey(EXPECTED_ACCEPT_ENCODING)) {
                String expected = requestHeaders.get(EXPECTED_ACCEPT_ENCODING);
                if (expected.equals(NONE)) {
                    if (requestHeaders.containsKey(HttpConstants.ACCEPT_ENCODING)) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Accept-Encoding header should not be present.");
                    }
                } else {
                    if (!expected.equals(requestHeaders.get(HttpConstants.ACCEPT_ENCODING))) {
                        throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Accept-Encoding header should be " + expected + ", but received " + requestHeaders.get(HttpConstants.ACCEPT_ENCODING));
                    }
                }
            }
            if (requestHeaders.containsKey(EXPECTED_COMPRESSION_THRESHOLD)) {
                if (!requestHeaders.get(EXPECTED_COMPRESSION_THRESHOLD).equals(requestHeaders.get(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD))) {
                    throw new RestLiServiceException(HttpStatus.S_400_BAD_REQUEST, "Expected " + HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD + " " + requestHeaders.get(EXPECTED_COMPRESSION_THRESHOLD) + ", but received " + requestHeaders.get(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD));
                }
            }
            return CompletableFuture.completedFuture(null);
        }
    }
    // The default compression threshold is between tiny and huge threshold.
    final FilterChain fc = FilterChains.empty().addLastRest(new TestCompressionServer.SaveContentEncodingHeaderFilter()).addLastRest(new ServerCompressionFilter("x-snappy-framed,snappy,gzip,deflate", new CompressionConfig(10000))).addLastRest(new SimpleLoggingFilter());
    super.init(Arrays.asList(new TestHelperFilter()), fc, false);
}
Also used : ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterChain(com.linkedin.r2.filter.FilterChain) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) SimpleLoggingFilter(com.linkedin.r2.filter.logging.SimpleLoggingFilter) Filter(com.linkedin.restli.server.filter.Filter) ServerCompressionFilter(com.linkedin.r2.filter.compression.ServerCompressionFilter) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) BeforeClass(org.testng.annotations.BeforeClass)

Example 5 with ServerCompressionFilter

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

the class TestServerCompressionFilter method testResponseCompressionRules.

// Test response compression rules where the server has a default threshold of Integer.MAX_VALUE.
@Test(dataProvider = "headersData")
public void testResponseCompressionRules(String acceptEncoding, int compressionThreshold, EncodingType expectedContentEncoding) throws CompressionException, URISyntaxException {
    ServerCompressionFilter serverCompressionFilter = new ServerCompressionFilter(ACCEPT_COMPRESSIONS);
    RequestContext context = new RequestContext();
    context.putLocalAttr(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
    context.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, compressionThreshold);
    int originalLength = 100;
    byte[] entity = new byte[originalLength];
    Arrays.fill(entity, (byte) 'A');
    int compressedLength = (expectedContentEncoding == null) ? originalLength : expectedContentEncoding.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    String expectedContentEncodingName = (expectedContentEncoding == null) ? null : expectedContentEncoding.getHttpName();
    RestResponse restResponse = new RestResponseBuilder().setEntity(entity).build();
    serverCompressionFilter.onRestResponse(restResponse, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncodingName, compressedLength));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Aggregations

FilterChain (com.linkedin.r2.filter.FilterChain)5 ServerCompressionFilter (com.linkedin.r2.filter.compression.ServerCompressionFilter)5 SimpleLoggingFilter (com.linkedin.r2.filter.logging.SimpleLoggingFilter)3 CompressionConfig (com.linkedin.r2.filter.CompressionConfig)2 RequestContext (com.linkedin.r2.message.RequestContext)2 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)2 HttpNettyServerFactory (com.linkedin.r2.transport.http.server.HttpNettyServerFactory)2 DefaultDocumentationRequestHandler (com.linkedin.restli.docgen.DefaultDocumentationRequestHandler)2 DelegatingTransportDispatcher (com.linkedin.restli.server.DelegatingTransportDispatcher)2 RestLiConfig (com.linkedin.restli.server.RestLiConfig)2 RestLiServer (com.linkedin.restli.server.RestLiServer)2 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 ResourceFactory (com.linkedin.restli.server.resources.ResourceFactory)2 BeforeClass (org.testng.annotations.BeforeClass)2 Injector (com.google.inject.Injector)1 NextFilter (com.linkedin.r2.filter.NextFilter)1 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)1