Search in sources :

Example 1 with CompressionOption

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

the class TestClientStreamCompressionFilter method testRequestCompressionRules.

@Test(dataProvider = "requestData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    Executor executor = Executors.newCachedThreadPool();
    ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
    // The entity should be compressible for this test.
    int original = 100;
    byte[] entity = new byte[original];
    Arrays.fill(entity, (byte) 'A');
    StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
    int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    RequestContext context = new RequestContext();
    if (operation != null) {
        context.putLocalAttr(R2Constants.OPERATION, operation);
    }
    context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
    int entityLength = headerShouldBePresent ? compressed : original;
    HeaderCaptureFilter captureFilter = new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, headerShouldBePresent, entityLength);
    clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), captureFilter);
    FutureCallback<ByteString> callback = new FutureCallback<ByteString>();
    FullEntityReader reader = new FullEntityReader(callback);
    captureFilter.getEntityStream().setReader(reader);
    ByteString entityRead = callback.get(10, TimeUnit.SECONDS);
    Assert.assertEquals(entityRead.length(), entityLength);
}
Also used : ByteString(com.linkedin.data.ByteString) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) Executor(java.util.concurrent.Executor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) FutureCallback(com.linkedin.common.callback.FutureCallback) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 2 with CompressionOption

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

the class TestClientCompressionFilter method testResponseCompressionRules.

@Test(dataProvider = "responseCompressionData")
public void testResponseCompressionRules(CompressionConfig responseCompressionConfig, CompressionOption responseCompressionOverride, String expectedAcceptEncoding, String expectedCompressionThreshold, String operation) throws CompressionException, URISyntaxException {
    ClientCompressionFilter clientCompressionFilter = new ClientCompressionFilter(EncodingType.SNAPPY.getHttpName(), new CompressionConfig(Integer.MAX_VALUE), ACCEPT_COMPRESSIONS, responseCompressionConfig, Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR));
    RestRequest restRequest = new RestRequestBuilder(new URI(URI)).build();
    RequestContext context = new RequestContext();
    if (operation != null) {
        context.putLocalAttr(R2Constants.OPERATION, operation);
    }
    context.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, responseCompressionOverride);
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.ACCEPT_ENCODING, expectedAcceptEncoding));
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, expectedCompressionThreshold));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 3 with CompressionOption

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

the class TestClientCompressionFilter method testRequestCompressionRules.

@Test(dataProvider = "requestCompressionData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent) throws CompressionException, URISyntaxException {
    ClientCompressionFilter clientCompressionFilter = new ClientCompressionFilter(EncodingType.SNAPPY.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Collections.<String>emptyList());
    // The entity should be compressible for this test.
    int original = 100;
    byte[] entity = new byte[original];
    Arrays.fill(entity, (byte) 'A');
    RestRequest restRequest = new RestRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).setEntity(entity).build();
    int compressed = EncodingType.SNAPPY.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    RequestContext context = new RequestContext();
    context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
    int entityLength = headerShouldBePresent ? compressed : original;
    String expectedContentEncoding = headerShouldBePresent ? EncodingType.SNAPPY.getHttpName() : null;
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncoding, entityLength));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 4 with CompressionOption

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

the class ClientStreamCompressionFilter method onStreamRequest.

/**
   * Optionally compresses outgoing Stream requests
   * */
public void onStreamRequest(StreamRequest req, final RequestContext requestContext, final Map<String, String> wireAttrs, final NextFilter<StreamRequest, StreamResponse> nextFilter) {
    //Set accepted encoding for compressed response
    String operation = (String) requestContext.getLocalAttr(R2Constants.OPERATION);
    if (!_acceptEncodingHeader.isEmpty() && _helper.shouldCompressResponseForOperation(operation)) {
        CompressionOption responseCompressionOverride = (CompressionOption) requestContext.getLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE);
        req = addResponseCompressionHeaders(responseCompressionOverride, req);
    }
    if (_requestContentEncoding != StreamEncodingType.IDENTITY) {
        final StreamRequest request = req;
        final StreamingCompressor compressor = _requestContentEncoding.getCompressor(_executor);
        CompressionOption option = (CompressionOption) requestContext.getLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE);
        if (option == null || option != CompressionOption.FORCE_OFF) {
            final int threshold = option == CompressionOption.FORCE_ON ? 0 : _requestCompressionConfig.getCompressionThreshold();
            PartialReader reader = new PartialReader(threshold, new Callback<EntityStream[]>() {

                @Override
                public void onError(Throwable ex) {
                    nextFilter.onError(ex, requestContext, wireAttrs);
                }

                @Override
                public void onSuccess(EntityStream[] result) {
                    if (result.length == 1) {
                        StreamRequest uncompressedRequest = request.builder().build(result[0]);
                        nextFilter.onRequest(uncompressedRequest, requestContext, wireAttrs);
                    } else {
                        StreamRequestBuilder builder = request.builder();
                        EntityStream compressedStream = compressor.deflate(EntityStreams.newEntityStream(new CompositeWriter(result)));
                        Map<String, String> headers = stripHeaders(builder.getHeaders(), HttpConstants.CONTENT_LENGTH);
                        StreamRequest compressedRequest = builder.setHeaders(headers).setHeader(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName()).build(compressedStream);
                        nextFilter.onRequest(compressedRequest, requestContext, wireAttrs);
                    }
                }
            });
            req.getEntityStream().setReader(reader);
            return;
        }
    }
    nextFilter.onRequest(req, requestContext, wireAttrs);
}
Also used : CompositeWriter(com.linkedin.r2.message.stream.entitystream.CompositeWriter) PartialReader(com.linkedin.r2.filter.compression.streaming.PartialReader) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) CompressionOption(com.linkedin.r2.filter.CompressionOption) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 5 with CompressionOption

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

the class TestClientStreamCompressionFilter method testAcceptEncodingHeader.

@Test(dataProvider = "requestData")
public void testAcceptEncodingHeader(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
    Executor executor = Executors.newCachedThreadPool();
    ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
    // The entity should be compressible for this test.
    int original = 100;
    byte[] entity = new byte[original];
    Arrays.fill(entity, (byte) 'A');
    StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
    int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    RequestContext context = new RequestContext();
    if (operation != null) {
        context.putLocalAttr(R2Constants.OPERATION, operation);
    }
    context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
    int entityLength = headerShouldBePresent ? compressed : original;
    clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.ACCEPT_ENCODING, true));
}
Also used : Executor(java.util.concurrent.Executor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestContext(com.linkedin.r2.message.RequestContext) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Aggregations

CompressionConfig (com.linkedin.r2.filter.CompressionConfig)4 RequestContext (com.linkedin.r2.message.RequestContext)4 URI (java.net.URI)4 Test (org.testng.annotations.Test)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)3 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CompressionOption (com.linkedin.r2.filter.CompressionOption)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)2 Executor (java.util.concurrent.Executor)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1 ByteString (com.linkedin.data.ByteString)1 PartialReader (com.linkedin.r2.filter.compression.streaming.PartialReader)1 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)1 CompositeWriter (com.linkedin.r2.message.stream.entitystream.CompositeWriter)1 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)1 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)1 Map (java.util.Map)1