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;
}
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);
}
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());
}
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");
}
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");
}
Aggregations