use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class TestServletStreamLengthTypeWrite method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileName = request.getServletPath();
byte[] dataBytes = loadContentFileBytes(fileName);
ServletOutputStream out = response.getOutputStream();
response.setContentLength(dataBytes.length);
if (fileName.endsWith("txt"))
response.setContentType("text/plain");
else if (fileName.endsWith("mp3"))
response.setContentType("audio/mpeg");
response.setHeader("ETag", "W/etag-" + fileName);
out.write(dataBytes);
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class TestServletStreamTypeLengthWrite method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String fileName = request.getServletPath();
byte[] dataBytes = loadContentFileBytes(fileName);
ServletOutputStream out = response.getOutputStream();
if (fileName.endsWith("txt"))
response.setContentType("text/plain");
else if (fileName.endsWith("mp3"))
response.setContentType("audio/mpeg");
response.setHeader("ETag", "W/etag-" + fileName);
response.setContentLength(dataBytes.length);
out.write(dataBytes);
}
use of javax.servlet.ServletOutputStream 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.ServletOutputStream 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.ServletOutputStream in project jetty.project by eclipse.
the class HttpClientTest method test_GET_WithParameters_ResponseWithContent.
@Test
public void test_GET_WithParameters_ResponseWithContent() throws Exception {
final String paramName1 = "a";
final String paramName2 = "b";
start(new AbstractHandler() {
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setCharacterEncoding("UTF-8");
ServletOutputStream output = response.getOutputStream();
String paramValue1 = request.getParameter(paramName1);
output.write(paramValue1.getBytes(StandardCharsets.UTF_8));
String paramValue2 = request.getParameter(paramName2);
Assert.assertEquals("", paramValue2);
output.write("empty".getBytes(StandardCharsets.UTF_8));
baseRequest.setHandled(true);
}
});
String value1 = "€";
String paramValue1 = URLEncoder.encode(value1, "UTF-8");
String query = paramName1 + "=" + paramValue1 + "&" + paramName2;
ContentResponse response = client.GET(scheme + "://localhost:" + connector.getLocalPort() + "/?" + query);
Assert.assertNotNull(response);
Assert.assertEquals(200, response.getStatus());
String content = new String(response.getContent(), StandardCharsets.UTF_8);
Assert.assertEquals(value1 + "empty", content);
}
Aggregations