use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncIOServletTest method testWriteFromOnDataAvailable.
@Test
public void testWriteFromOnDataAvailable() throws Exception {
Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
CountDownLatch writeLatch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync();
request.getInputStream().setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
ServletInputStream input = request.getInputStream();
ServletOutputStream output = response.getOutputStream();
while (input.isReady()) {
byte[] buffer = new byte[512];
int read = input.read(buffer);
if (read < 0) {
asyncContext.complete();
break;
}
if (output.isReady())
output.write(buffer, 0, read);
else
Assert.fail();
}
}
@Override
public void onAllDataRead() throws IOException {
asyncContext.complete();
}
@Override
public void onError(Throwable t) {
errors.offer(t);
}
});
response.getOutputStream().setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
writeLatch.countDown();
}
@Override
public void onError(Throwable t) {
errors.offer(t);
}
});
}
});
String content = "0123456789ABCDEF";
DeferredContentProvider contentProvider = new DeferredContentProvider();
contentProvider.offer(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)));
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).send(new BufferingResponseListener() {
@Override
public void onComplete(Result result) {
if (result.isSucceeded()) {
Response response = result.getResponse();
assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
assertThat(getContentAsString(), Matchers.equalTo(content));
assertThat(errors, Matchers.hasSize(0));
clientLatch.countDown();
}
}
});
assertTrue(writeLatch.await(5, TimeUnit.SECONDS));
contentProvider.close();
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncWriteThrows.
private void testAsyncWriteThrows(Throwable throwable) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
AsyncContext asyncContext = request.startAsync(request, response);
response.getOutputStream().setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
assertScope();
if (throwable instanceof RuntimeException)
throw (RuntimeException) throwable;
if (throwable instanceof Error)
throw (Error) throwable;
throw new IOException(throwable);
}
@Override
public void onError(Throwable t) {
assertScope();
latch.countDown();
response.setStatus(500);
asyncContext.complete();
Assert.assertSame(throwable, t);
}
});
}
});
ContentResponse response = client.newRequest(newURI()).path(servletPath).timeout(5, TimeUnit.SECONDS).send();
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncReadEarlyEOF.
@Test
public void testAsyncReadEarlyEOF() throws Exception {
// SSLEngine receives the close alert from the client, and when
// the server passes the response to encrypt and write, SSLEngine
// only generates the close alert back, without encrypting the
// response, so we need to skip the transports over TLS.
Assume.assumeThat(transport, Matchers.not(Matchers.isOneOf(Transport.HTTPS, Transport.H2)));
String content = "jetty";
int responseCode = HttpStatus.NO_CONTENT_204;
CountDownLatch readLatch = new CountDownLatch(content.length());
CountDownLatch errorLatch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AsyncContext asyncContext = request.startAsync();
ServletInputStream input = request.getInputStream();
input.setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
while (input.isReady() && !input.isFinished()) {
int read = input.read();
// System.err.printf("%x%n", read);
readLatch.countDown();
}
}
@Override
public void onAllDataRead() throws IOException {
}
@Override
public void onError(Throwable x) {
response.setStatus(responseCode);
asyncContext.complete();
errorLatch.countDown();
}
});
}
});
CountDownLatch responseLatch = new CountDownLatch(1);
DeferredContentProvider contentProvider = new DeferredContentProvider();
contentProvider.offer(ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)));
org.eclipse.jetty.client.api.Request request = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).content(contentProvider).onResponseSuccess(response -> responseLatch.countDown());
Destination destination = client.getDestination(getScheme(), "localhost", connector.getLocalPort());
FuturePromise<org.eclipse.jetty.client.api.Connection> promise = new FuturePromise<>();
destination.newConnection(promise);
org.eclipse.jetty.client.api.Connection connection = promise.get(5, TimeUnit.SECONDS);
CountDownLatch clientLatch = new CountDownLatch(1);
connection.send(request, result -> {
assertThat(result.getResponse().getStatus(), Matchers.equalTo(responseCode));
clientLatch.countDown();
});
assertTrue(readLatch.await(5, TimeUnit.SECONDS));
switch(transport) {
case HTTP:
case HTTPS:
((HttpConnectionOverHTTP) connection).getEndPoint().shutdownOutput();
break;
case H2C:
case H2:
// In case of HTTP/2, we not only send the request, but also the preface and
// SETTINGS frames. SETTINGS frame need to be replied, so we want to wait to
// write the reply before shutting output down, so that the test does not fail.
Thread.sleep(1000);
Session session = ((HttpConnectionOverHTTP2) connection).getSession();
((HTTP2Session) session).getEndPoint().shutdownOutput();
break;
default:
Assert.fail();
}
// Wait for the response to arrive before finishing the request.
assertTrue(responseLatch.await(5, TimeUnit.SECONDS));
contentProvider.close();
assertTrue(errorLatch.await(5, TimeUnit.SECONDS));
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncIOServletTest method testAsyncWriteClosed.
@Test
public void testAsyncWriteClosed() throws Exception {
String text = "Now is the winter of our discontent. How Now Brown Cow. The quick brown fox jumped over the lazy dog.\n";
for (int i = 0; i < 10; i++) text = text + text;
byte[] data = text.getBytes(StandardCharsets.UTF_8);
CountDownLatch errorLatch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
response.flushBuffer();
AsyncContext async = request.startAsync();
ServletOutputStream out = response.getOutputStream();
out.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
assertScope();
// Wait for the failure to arrive to
// the server while we are about to write.
sleep(1000);
out.write(data);
}
@Override
public void onError(Throwable t) {
assertScope();
async.complete();
errorLatch.countDown();
}
});
}
});
CountDownLatch clientLatch = new CountDownLatch(1);
client.newRequest(newURI()).path(servletPath).onResponseHeaders(response -> {
if (response.getStatus() == HttpStatus.OK_200)
response.abort(new IOException("explicitly_closed_by_test"));
}).send(result -> {
if (result.isFailed())
clientLatch.countDown();
});
assertTrue(errorLatch.await(5, TimeUnit.SECONDS));
assertTrue(clientLatch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.http.HttpServlet in project jetty.project by eclipse.
the class AsyncIOServletTest method testEmptyAsyncRead.
@Test
public void testEmptyAsyncRead() throws Exception {
AtomicBoolean oda = new AtomicBoolean();
CountDownLatch latch = new CountDownLatch(1);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
AsyncContext asyncContext = request.startAsync(request, response);
response.setStatus(200);
response.getOutputStream().close();
request.getInputStream().setReadListener(new ReadListener() {
@Override
public void onDataAvailable() throws IOException {
assertScope();
oda.set(true);
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
asyncContext.complete();
latch.countDown();
}
@Override
public void onError(Throwable t) {
assertScope();
t.printStackTrace();
asyncContext.complete();
}
});
}
});
ContentResponse response = client.newRequest(newURI()).path(servletPath).header(HttpHeader.CONNECTION, "close").timeout(5, TimeUnit.SECONDS).send();
assertThat(response.getStatus(), Matchers.equalTo(HttpStatus.OK_200));
assertTrue(latch.await(5, TimeUnit.SECONDS));
// onDataAvailable must not be called.
Assert.assertFalse(oda.get());
}
Aggregations