use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerFailedWhileWaiting.
@Test
public void testInputStreamResponseListenerFailedWhileWaiting() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
byte[] data = new byte[1024];
response.setContentLength(data.length);
ServletOutputStream output = response.getOutputStream();
output.write(data);
}
});
CountDownLatch failedLatch = new CountDownLatch(1);
CountDownLatch contentLatch = new CountDownLatch(1);
InputStreamResponseListener listener = new InputStreamResponseListener() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
super.onContent(response, content, new Callback() {
@Override
public void failed(Throwable x) {
failedLatch.countDown();
callback.failed(x);
}
});
contentLatch.countDown();
}
};
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
// Wait until we get some content.
Assert.assertTrue(contentLatch.await(5, TimeUnit.SECONDS));
// Abort the response.
response.abort(new Exception());
// Make sure that the callback has been invoked.
Assert.assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerFailedBeforeResponse.
@Test
public void testInputStreamResponseListenerFailedBeforeResponse() throws Exception {
start(new EmptyServerHandler());
int port = connector.getLocalPort();
server.stop();
InputStreamResponseListener listener = new InputStreamResponseListener();
// Connect to the wrong port
client.newRequest("localhost", port).scheme(getScheme()).send(listener);
Result result = listener.await(5, TimeUnit.SECONDS);
Assert.assertNotNull(result);
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class HttpClientStreamTest method testInputStreamResponseListenerClosedBeforeContent.
@Test(expected = AsynchronousCloseException.class)
public void testInputStreamResponseListenerClosedBeforeContent() throws Exception {
AtomicReference<AsyncContext> contextRef = new AtomicReference<>();
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
contextRef.set(request.startAsync());
response.flushBuffer();
}
});
CountDownLatch latch = new CountDownLatch(1);
InputStreamResponseListener listener = new InputStreamResponseListener() {
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
super.onContent(response, content, new Callback() {
@Override
public void failed(Throwable x) {
latch.countDown();
callback.failed(x);
}
});
}
};
client.newRequest("localhost", connector.getLocalPort()).scheme(getScheme()).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
InputStream input = listener.getInputStream();
input.close();
AsyncContext asyncContext = contextRef.get();
asyncContext.getResponse().getOutputStream().write(new byte[1024]);
asyncContext.complete();
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
// Throws
input.read();
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class Usage method testResponseInputStream.
@Test
public void testResponseInputStream() throws Exception {
HttpClient client = new HttpClient();
client.start();
InputStreamResponseListener listener = new InputStreamResponseListener();
// Send asynchronously with the InputStreamResponseListener
client.newRequest("localhost", 8080).send(listener);
// Call to the listener's get() blocks until the headers arrived
Response response = listener.get(5, TimeUnit.SECONDS);
// Now check the response information that arrived to decide whether to read the content
if (response.getStatus() == 200) {
byte[] buffer = new byte[256];
try (InputStream input = listener.getInputStream()) {
while (true) {
int read = input.read(buffer);
if (read < 0)
break;
// Do something with the bytes just read
}
}
} else {
response.abort(new Exception());
}
}
use of org.eclipse.jetty.client.util.InputStreamResponseListener in project jetty.project by eclipse.
the class ProxyServletTest method testProxyRequestFailureInTheMiddleOfProxyingBigContent.
@Test
public void testProxyRequestFailureInTheMiddleOfProxyingBigContent() throws Exception {
int outputBufferSize = 1024;
final CountDownLatch chunk1Latch = new CountDownLatch(1);
final byte[] chunk1 = new byte[outputBufferSize];
new Random().nextBytes(chunk1);
final int chunk2 = 'w';
startServer(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream output = response.getOutputStream();
output.write(chunk1);
response.flushBuffer();
// Wait for the client to receive this chunk.
await(chunk1Latch, 5000);
// Send second chunk, must not be received by proxy.
output.write(chunk2);
}
private boolean await(CountDownLatch latch, long ms) throws IOException {
try {
return latch.await(ms, TimeUnit.MILLISECONDS);
} catch (InterruptedException x) {
throw new InterruptedIOException();
}
}
});
final long proxyTimeout = 1000;
Map<String, String> proxyParams = new HashMap<>();
proxyParams.put("timeout", String.valueOf(proxyTimeout));
proxyParams.put("outputBufferSize", String.valueOf(outputBufferSize));
startProxy(proxyParams);
startClient();
InputStreamResponseListener listener = new InputStreamResponseListener();
int port = serverConnector.getLocalPort();
client.newRequest("localhost", port).send(listener);
Response response = listener.get(5, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatus());
InputStream input = listener.getInputStream();
for (int i = 0; i < chunk1.length; ++i) Assert.assertEquals(chunk1[i] & 0xFF, input.read());
TimeUnit.MILLISECONDS.sleep(2 * proxyTimeout);
chunk1Latch.countDown();
try {
// Make sure the proxy does not receive chunk2.
input.read();
Assert.fail();
} catch (EOFException x) {
// Expected
}
HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination("http", "localhost", port);
DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
Assert.assertEquals(0, connectionPool.getIdleConnections().size());
}
Aggregations