Search in sources :

Example 31 with ServletOutputStream

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();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream)

Example 32 with ServletOutputStream

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);
    }
}
Also used : PrintStream(java.io.PrintStream) ServletOutputStream(javax.servlet.ServletOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 33 with ServletOutputStream

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);
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletOutputStream(javax.servlet.ServletOutputStream) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 34 with ServletOutputStream

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));
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) AsyncContext(javax.servlet.AsyncContext) HttpOutput(org.eclipse.jetty.server.HttpOutput) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ByteBuffer(java.nio.ByteBuffer)

Example 35 with ServletOutputStream

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));
}
Also used : BadMessageException(org.eclipse.jetty.http.BadMessageException) Request(org.eclipse.jetty.server.Request) ServletException(javax.servlet.ServletException) HttpChannel(org.eclipse.jetty.server.HttpChannel) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) ServletInputStream(javax.servlet.ServletInputStream) AbstractHTTP2ServerConnectionFactory(org.eclipse.jetty.http2.server.AbstractHTTP2ServerConnectionFactory) TimeoutException(java.util.concurrent.TimeoutException) ByteBuffer(java.nio.ByteBuffer) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) WriteListener(javax.servlet.WriteListener) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Callback(org.eclipse.jetty.util.Callback) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ReadListener(javax.servlet.ReadListener) StacklessLogging(org.eclipse.jetty.util.log.StacklessLogging) Assert(org.junit.Assert) ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Callback(org.eclipse.jetty.util.Callback) WriteListener(javax.servlet.WriteListener) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

ServletOutputStream (javax.servlet.ServletOutputStream)515 IOException (java.io.IOException)211 HttpServletResponse (javax.servlet.http.HttpServletResponse)148 Test (org.junit.Test)112 HttpServletRequest (javax.servlet.http.HttpServletRequest)108 ServletException (javax.servlet.ServletException)90 InputStream (java.io.InputStream)63 File (java.io.File)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 FileInputStream (java.io.FileInputStream)40 PrintWriter (java.io.PrintWriter)27 CountDownLatch (java.util.concurrent.CountDownLatch)27 WriteListener (javax.servlet.WriteListener)27 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)27 HttpServlet (javax.servlet.http.HttpServlet)25 AsyncContext (javax.servlet.AsyncContext)23 ServletInputStream (javax.servlet.ServletInputStream)23 ArrayList (java.util.ArrayList)21 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19