Search in sources :

Example 36 with StreamRequestBuilder

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

the class ServletHelper method readStreamRequestHeadersFromServletRequest.

static StreamRequestBuilder readStreamRequestHeadersFromServletRequest(HttpServletRequest req) throws IOException, ServletException, URISyntaxException {
    StringBuilder sb = new StringBuilder();
    sb.append(extractPathInfo(req));
    String query = req.getQueryString();
    if (query != null) {
        sb.append('?');
        sb.append(query);
    }
    URI uri = new URI(sb.toString());
    StreamRequestBuilder rb = new StreamRequestBuilder(uri);
    rb.setMethod(req.getMethod());
    for (Enumeration<String> headerNames = req.getHeaderNames(); headerNames.hasMoreElements(); ) {
        String headerName = headerNames.nextElement();
        if (headerName.equalsIgnoreCase(HttpConstants.REQUEST_COOKIE_HEADER_NAME)) {
            for (Enumeration<String> cookies = req.getHeaders(headerName); cookies.hasMoreElements(); ) {
                rb.addCookie(cookies.nextElement());
            }
        } else {
            for (Enumeration<String> headerValues = req.getHeaders(headerName); headerValues.hasMoreElements(); ) {
                rb.addHeaderValue(headerName, headerValues.nextElement());
            }
        }
    }
    return rb;
}
Also used : ByteString(com.linkedin.data.ByteString) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder)

Example 37 with StreamRequestBuilder

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

the class TestChannelPoolBehavior method testChannelReuse.

@Test
public void testChannelReuse() throws Exception {
    _client2.streamRequest(new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, NOT_FOUND_URI)).build(EntityStreams.newEntityStream(new SlowWriter())), new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            if (e instanceof StreamException) {
                StreamException streamException = (StreamException) e;
                streamException.getResponse().getEntityStream().setReader(new CancelingReader());
            }
            throw new RuntimeException(e);
        }

        @Override
        public void onSuccess(StreamResponse result) {
            result.getEntityStream().setReader(new DrainReader());
        }
    });
    Future<RestResponse> responseFuture = _client2.restRequest(new RestRequestBuilder(Bootstrap.createHttpURI(PORT, NORMAL_URI)).build());
    RestResponse response = responseFuture.get(WRITER_DELAY * 1000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(response.getStatus(), RestStatus.OK);
}
Also used : CancelingReader(com.linkedin.r2.message.stream.entitystream.CancelingReader) RestResponse(com.linkedin.r2.message.rest.RestResponse) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamException(com.linkedin.r2.message.stream.StreamException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 38 with StreamRequestBuilder

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

the class TestCompressionEcho method testResponseCompression.

@Test(dataProvider = "compressionEchoData")
public void testResponseCompression(Client client, long bytes) throws InterruptedException, TimeoutException, ExecutionException {
    StreamRequestBuilder builder = new StreamRequestBuilder((_clientProvider.createHttpURI(_port, ECHO_URI)));
    BytesWriter writer = new BytesWriter(bytes, BYTE);
    StreamRequest request = builder.build(EntityStreams.newEntityStream(writer));
    // add operation to enable sending accept encoding
    RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    final FutureCallback<StreamResponse> callback = new FutureCallback<>();
    client.streamRequest(request, requestContext, callback);
    final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), RestStatus.OK);
    final FutureCallback<None> readerCallback = new FutureCallback<>();
    final BytesReader reader = new BytesReader(BYTE, readerCallback);
    response.getEntityStream().setReader(reader);
    readerCallback.get(60, TimeUnit.SECONDS);
    Assert.assertEquals(reader.getTotalBytes(), bytes);
    Assert.assertTrue(reader.allBytesCorrect());
}
Also used : BytesWriter(test.r2.integ.helper.BytesWriter) BytesReader(test.r2.integ.helper.BytesReader) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RequestContext(com.linkedin.r2.message.RequestContext) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 39 with StreamRequestBuilder

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

the class TestDisruptor method testStreamErrorDisrupt.

@Test
public void testStreamErrorDisrupt() throws Exception {
    final Map<String, Object> properties = new HashMap<>();
    properties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(REQUEST_TIMEOUT));
    final Client client = _clientProvider.createClient(FilterChains.empty(), properties);
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.error(REQUEST_LATENCY));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    client.streamRequest(new StreamRequestBuilder(getHttpURI()).build(EntityStreams.emptyStream()), requestContext, new Callback<StreamResponse>() {

        @Override
        public void onSuccess(StreamResponse result) {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            success.set(e instanceof DisruptedException);
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
    Assert.assertTrue(success.get(), "Unexpected transport response");
}
Also used : HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DisruptedException(com.linkedin.r2.disruptor.DisruptedException) RequestContext(com.linkedin.r2.message.RequestContext) Client(com.linkedin.r2.transport.common.Client) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 40 with StreamRequestBuilder

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

the class TestDisruptor method testStreamLatencyDisrupt.

@Test
public void testStreamLatencyDisrupt() throws Exception {
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.delay(REQUEST_LATENCY));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    _client.streamRequest(new StreamRequestBuilder(getHttpURI()).build(EntityStreams.emptyStream()), requestContext, new Callback<StreamResponse>() {

        @Override
        public void onSuccess(StreamResponse result) {
            success.set(true);
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            success.set(false);
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
    Assert.assertTrue(success.get(), "Unexpected transport response");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RequestContext(com.linkedin.r2.message.RequestContext) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

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