Search in sources :

Example 51 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder 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 52 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class UriRewriteProxy method handleRequest.

@Override
public void handleRequest(StreamRequest request, RequestContext requestContext, final Callback<StreamResponse> callback) {
    URI newUri = _uriRewriter.rewrite(request.getURI());
    StreamRequestBuilder builder = new StreamRequestBuilder(request);
    builder.setURI(newUri);
    StreamRequest newRequest = builder.build(request.getEntityStream());
    _client.streamRequest(newRequest, requestContext, callback);
}
Also used : URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Example 53 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestNettyRequestAdapter method testStreamToHttp2HeadersPseudoHeaders.

@Test
public void testStreamToHttp2HeadersPseudoHeaders() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    Assert.assertEquals(headers.authority(), "localhost:8080");
    Assert.assertEquals(headers.method(), "GET");
    Assert.assertEquals(headers.path(), "/foo/bar?q=baz");
    Assert.assertEquals(headers.scheme(), "http");
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 54 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestNettyRequestAdapter method testStreamToHttp2HeadersCookies.

@Test
public void testStreamToHttp2HeadersCookies() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    IntStream.range(0, 10).forEach(i -> streamRequestBuilder.addCookie(ANY_COOKIE));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    List<CharSequence> cookies = headers.getAll(HttpHeaderNames.COOKIE);
    Assert.assertNotNull(cookies);
    Assert.assertEquals(cookies.size(), 10);
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 55 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder in project rest.li by linkedin.

the class TestNettyRequestAdapter method testStreamToHttp2HeadersRegularHeaders.

@Test
public void testStreamToHttp2HeadersRegularHeaders() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    streamRequestBuilder.setHeader("header1", "value1");
    streamRequestBuilder.setHeader("header2", "value2");
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    Assert.assertEquals(headers.get("header1"), "value1");
    Assert.assertEquals(headers.get("header2"), "value2");
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Aggregations

StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)121 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)106 Test (org.testng.annotations.Test)102 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)87 URI (java.net.URI)68 RequestContext (com.linkedin.r2.message.RequestContext)56 CountDownLatch (java.util.concurrent.CountDownLatch)51 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 Callback (com.linkedin.common.callback.Callback)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 ByteString (com.linkedin.data.ByteString)22 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)21 AbstractServiceTest (test.r2.integ.clientserver.providers.AbstractServiceTest)21 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)20 AfterTest (org.testng.annotations.AfterTest)19 BeforeTest (org.testng.annotations.BeforeTest)19 RestRequest (com.linkedin.r2.message.rest.RestRequest)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)18 FutureCallback (com.linkedin.common.callback.FutureCallback)17