use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class LoginServlet method doGet.
/* ------------------------------------------------------------ */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<html>");
out.println("<br/>Before getUserPrincipal=" + request.getUserPrincipal());
out.println("<br/>Before getRemoteUser=" + request.getRemoteUser());
String param = request.getParameter("action");
if ("login".equals(param)) {
request.login("jetty", "jetty");
} else if ("logout".equals(param)) {
request.logout();
} else if ("wrong".equals(param)) {
request.login("jetty", "123");
}
out.println("<br/>After getUserPrincipal=" + request.getUserPrincipal());
out.println("<br/>After getRemoteUser=" + request.getRemoteUser());
out.println("</html>");
out.flush();
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class SecureModeServlet method doGet.
/* ------------------------------------------------------------ */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<html>");
out.println(" <title>Secure Jetty Test Webapp</title>");
try {
runPropertyChecks(out);
runFileSystemChecks(out);
runLoggingChecks(out);
runClassloaderChecks(out);
} catch (Exception e) {
e.printStackTrace(new PrintStream(out));
}
out.println("</html>");
out.flush();
try {
Thread.sleep(200);
} catch (InterruptedException e) {
getServletContext().log("exception", e);
}
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class FragmentServlet method doGet.
/* ------------------------------------------------------------ */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
response.setContentType("text/html");
ServletOutputStream out = response.getOutputStream();
out.println("<html>");
out.println("<h1>Jetty Fragment Servlet</h1>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
out.flush();
} catch (Exception e) {
throw new ServletException(e);
}
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class DataRateLimitedServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get the path of the static resource to serve.
String info = request.getPathInfo();
// We don't handle directories
if (info.endsWith("/")) {
response.sendError(503, "directories not supported");
return;
}
// Set the mime type of the response
String content_type = getServletContext().getMimeType(info);
response.setContentType(content_type == null ? "application/x-data" : content_type);
// Look for a matching file path
String path = request.getPathTranslated();
// If we have a file path and this is a jetty response, we can use the JettyStream impl
ServletOutputStream out = response.getOutputStream();
if (path != null && out instanceof HttpOutput) {
// If the file exists
File file = new File(path);
if (file.exists() && file.canRead()) {
// Set the content length
response.setContentLengthLong(file.length());
// Look for a file mapped buffer in the cache
ByteBuffer mapped = cache.get(path);
// Handle cache miss
if (mapped == null) {
// TODO implement LRU cache flush
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
ByteBuffer buf = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
mapped = cache.putIfAbsent(path, buf);
if (mapped == null)
mapped = buf;
}
}
// start async request handling
AsyncContext async = request.startAsync();
// Set a JettyStream as the write listener to write the content asynchronously.
out.setWriteListener(new JettyDataStream(mapped, async, out));
return;
}
}
// Jetty API was not used, so lets try the standards approach
// Can we find the content as an input stream
InputStream content = getServletContext().getResourceAsStream(info);
if (content == null) {
response.sendError(404);
return;
}
// Set a StandardStream as he write listener to write the content asynchronously
out.setWriteListener(new StandardDataStream(content, request.startAsync(), out));
}
use of javax.servlet.ServletOutputStream in project jetty.project by eclipse.
the class ServerTimeoutsTest method testAsyncWriteIdleTimeoutFires.
@Test
public void testAsyncWriteIdleTimeoutFires() throws Exception {
CountDownLatch handlerLatch = new CountDownLatch(1);
start(new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
baseRequest.setHandled(true);
AsyncContext asyncContext = request.startAsync();
asyncContext.setTimeout(0);
ServletOutputStream output = response.getOutputStream();
output.setWriteListener(new WriteListener() {
@Override
public void onWritePossible() throws IOException {
output.write(new byte[64 * 1024 * 1024]);
}
@Override
public void onError(Throwable failure) {
if (failure instanceof TimeoutException) {
asyncContext.complete();
handlerLatch.countDown();
}
}
});
}
});
long idleTimeout = 2500;
setServerIdleTimeout(idleTimeout);
BlockingQueue<Callback> callbacks = new LinkedBlockingQueue<>();
CountDownLatch resultLatch = new CountDownLatch(1);
client.newRequest(newURI()).onResponseContentAsync((response, content, callback) -> {
// Do not succeed the callback so the server will block writing.
callbacks.offer(callback);
}).send(result -> {
if (result.isFailed())
resultLatch.countDown();
});
// Async write should timeout.
Assert.assertTrue(handlerLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
// After the server stopped sending, consume on the client to read the early EOF.
while (true) {
Callback callback = callbacks.poll(1, TimeUnit.SECONDS);
if (callback == null)
break;
callback.succeeded();
}
Assert.assertTrue(resultLatch.await(5, TimeUnit.SECONDS));
}
Aggregations