Search in sources :

Example 6 with ClientStreamCompressionFilter

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

the class TestRequestCompression method noCompressionData.

@DataProvider
public Object[][] noCompressionData() {
    StreamEncodingType[] encodings = new StreamEncodingType[] { StreamEncodingType.GZIP, StreamEncodingType.DEFLATE, StreamEncodingType.SNAPPY_FRAMED, StreamEncodingType.BZIP2, StreamEncodingType.IDENTITY };
    String[] protocols = new String[] { HttpProtocolVersion.HTTP_1_1.name(), HttpProtocolVersion.HTTP_2.name() };
    Object[][] args = new Object[encodings.length * protocols.length][1];
    int cur = 0;
    for (StreamEncodingType requestEncoding : encodings) {
        for (String protocol : protocols) {
            StreamFilter clientCompressionFilter = new ClientStreamCompressionFilter(requestEncoding, new CompressionConfig(THRESHOLD), null, new CompressionConfig(THRESHOLD), Arrays.asList(new String[] { "*" }), _executor);
            TransportClientFactory factory = new HttpClientFactory.Builder().setFilterChain(FilterChains.createStreamChain(clientCompressionFilter)).build();
            HashMap<String, String> properties = new HashMap<>();
            properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, protocol);
            Client client = new TransportClientAdapter(factory.getClient(Collections.<String, String>emptyMap()), true);
            args[cur][0] = client;
            //args[cur][1] = URI.create("/" + requestEncoding.getHttpName());
            cur++;
            _clientFactories.add(factory);
            _clients.add(client);
        }
    }
    return args;
}
Also used : HashMap(java.util.HashMap) TransportDispatcherBuilder(com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) ByteString(com.linkedin.data.ByteString) StreamFilter(com.linkedin.r2.filter.message.stream.StreamFilter) StreamEncodingType(com.linkedin.r2.filter.compression.streaming.StreamEncodingType) ClientStreamCompressionFilter(com.linkedin.r2.filter.compression.ClientStreamCompressionFilter) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) Client(com.linkedin.r2.transport.common.Client) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) DataProvider(org.testng.annotations.DataProvider)

Example 7 with ClientStreamCompressionFilter

use of com.linkedin.r2.filter.compression.ClientStreamCompressionFilter 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)7 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)6 ByteString (com.linkedin.data.ByteString)4 ClientStreamCompressionFilter (com.linkedin.r2.filter.compression.ClientStreamCompressionFilter)4 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)4 StreamFilter (com.linkedin.r2.filter.message.stream.StreamFilter)3 RequestContext (com.linkedin.r2.message.RequestContext)3 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)3 Client (com.linkedin.r2.transport.common.Client)3 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)3 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)3 TransportDispatcherBuilder (com.linkedin.r2.transport.common.bridge.server.TransportDispatcherBuilder)3 URI (java.net.URI)3 DataProvider (org.testng.annotations.DataProvider)3 Test (org.testng.annotations.Test)3 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 Executor (java.util.concurrent.Executor)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1