use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class AsyncIOServletTest method testIsReadyAtEOF.
@Test
public void testIsReadyAtEOF() throws Exception {
String text = "TEST\n";
byte[] data = text.getBytes(StandardCharsets.UTF_8);
start(new HttpServlet() {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
assertScope();
response.flushBuffer();
AsyncContext async = request.startAsync();
ServletInputStream input = request.getInputStream();
ServletOutputStream output = response.getOutputStream();
input.setReadListener(new ReadListener() {
transient int _i = 0;
transient boolean _minusOne = false;
transient boolean _finished = false;
@Override
public void onDataAvailable() throws IOException {
assertScope();
while (input.isReady() && !input.isFinished()) {
int b = input.read();
if (b == -1)
_minusOne = true;
else if (data[_i++] != b)
throw new IllegalStateException();
}
if (input.isFinished())
_finished = true;
}
@Override
public void onAllDataRead() throws IOException {
assertScope();
output.write(String.format("i=%d eof=%b finished=%b", _i, _minusOne, _finished).getBytes(StandardCharsets.UTF_8));
async.complete();
}
@Override
public void onError(Throwable t) {
assertScope();
t.printStackTrace();
async.complete();
}
});
}
});
ContentResponse response = client.newRequest(newURI()).method(HttpMethod.POST).path(servletPath).header(HttpHeader.CONNECTION, "close").content(new StringContentProvider(text)).timeout(5, TimeUnit.SECONDS).send();
String responseContent = response.getContentAsString();
assertThat(responseContent, containsString("i=" + data.length + " eof=true finished=true"));
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class HttpClientContinueTest method test_Expect100Continue_WithChunkedContent_Respond100Continue.
@Test
public void test_Expect100Continue_WithChunkedContent_Respond100Continue() throws Exception {
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
// Send 100-Continue and copy the content back
ServletInputStream input = request.getInputStream();
// Make sure we chunk the response too
response.flushBuffer();
IO.copy(input, response.getOutputStream());
}
});
byte[] content1 = new byte[10240];
byte[] content2 = new byte[16384];
ContentResponse response = client.newRequest(newURI()).header(HttpHeader.EXPECT, HttpHeaderValue.CONTINUE.asString()).content(new BytesContentProvider(content1, content2) {
@Override
public long getLength() {
return -1;
}
}).timeout(5, TimeUnit.SECONDS).send();
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
int index = 0;
byte[] responseContent = response.getContent();
for (byte b : content1) Assert.assertEquals(b, responseContent[index++]);
for (byte b : content2) Assert.assertEquals(b, responseContent[index++]);
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class Request method getReader.
/* ------------------------------------------------------------ */
/*
* @see javax.servlet.ServletRequest#getReader()
*/
@Override
public BufferedReader getReader() throws IOException {
if (_inputState != __NONE && _inputState != __READER)
throw new IllegalStateException("STREAMED");
if (_inputState == __READER)
return _reader;
String encoding = getCharacterEncoding();
if (encoding == null)
encoding = StringUtil.__ISO_8859_1;
if (_reader == null || !encoding.equalsIgnoreCase(_readerEncoding)) {
final ServletInputStream in = getInputStream();
_readerEncoding = encoding;
_reader = new BufferedReader(new InputStreamReader(in, encoding)) {
@Override
public void close() throws IOException {
in.close();
}
};
}
_inputState = __READER;
return _reader;
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class AsyncIOTest method testLastContentAvailableBeforeService.
@Test
public void testLastContentAvailableBeforeService() throws Exception {
start(new HttpServlet() {
@Override
protected void service(final HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Wait for the data to fully arrive.
sleep(1000);
final AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
request.getInputStream().setReadListener(new EmptyReadListener() {
@Override
public void onDataAvailable() throws IOException {
ServletInputStream input = request.getInputStream();
while (input.isReady()) {
int read = input.read();
if (read < 0)
break;
}
if (input.isFinished())
asyncContext.complete();
}
});
}
});
Session session = newClient(new Session.Listener.Adapter());
HttpFields fields = new HttpFields();
MetaData.Request metaData = newRequest("GET", fields);
HeadersFrame frame = new HeadersFrame(metaData, null, false);
final CountDownLatch latch = new CountDownLatch(1);
FuturePromise<Stream> promise = new FuturePromise<>();
session.newStream(frame, promise, new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
if (frame.isEndStream())
latch.countDown();
}
});
Stream stream = promise.get(5, TimeUnit.SECONDS);
stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(16), true), Callback.NOOP);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
use of javax.servlet.ServletInputStream in project jetty.project by eclipse.
the class AsyncIOTest method testLastContentAvailableAfterServiceReturns.
@Test
public void testLastContentAvailableAfterServiceReturns() throws Exception {
start(new HttpServlet() {
@Override
protected void service(final HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
final AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
request.getInputStream().setReadListener(new EmptyReadListener() {
@Override
public void onDataAvailable() throws IOException {
ServletInputStream input = request.getInputStream();
while (input.isReady()) {
int read = input.read();
if (read < 0)
break;
}
if (input.isFinished())
asyncContext.complete();
}
});
}
});
Session session = newClient(new Session.Listener.Adapter());
HttpFields fields = new HttpFields();
MetaData.Request metaData = newRequest("GET", fields);
HeadersFrame frame = new HeadersFrame(metaData, null, false);
final CountDownLatch latch = new CountDownLatch(1);
FuturePromise<Stream> promise = new FuturePromise<>();
session.newStream(frame, promise, new Stream.Listener.Adapter() {
@Override
public void onHeaders(Stream stream, HeadersFrame frame) {
if (frame.isEndStream())
latch.countDown();
}
});
Stream stream = promise.get(5, TimeUnit.SECONDS);
// Wait until service() returns.
Thread.sleep(1000);
stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(16), true), Callback.NOOP);
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Aggregations