Search in sources :

Example 86 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class Server method handleAsync.

/* ------------------------------------------------------------ */
/* Handle a request from a connection.
     * Called to handle a request on the connection when either the header has been received,
     * or after the entire request has been received (for short requests of known length), or
     * on the dispatch of an async request.
     */
public void handleAsync(HttpChannel channel) throws IOException, ServletException {
    final HttpChannelState state = channel.getRequest().getHttpChannelState();
    final AsyncContextEvent event = state.getAsyncContextEvent();
    final Request baseRequest = channel.getRequest();
    final String path = event.getPath();
    if (path != null) {
        // this is a dispatch with a path
        ServletContext context = event.getServletContext();
        String query = baseRequest.getQueryString();
        baseRequest.setURIPathQuery(URIUtil.addPaths(context == null ? null : URIUtil.encodePath(context.getContextPath()), path));
        HttpURI uri = baseRequest.getHttpURI();
        baseRequest.setPathInfo(uri.getDecodedPath());
        if (uri.getQuery() != null)
            //we have to assume dispatch path and query are UTF8
            baseRequest.mergeQueryParameters(query, uri.getQuery(), true);
    }
    final String target = baseRequest.getPathInfo();
    final HttpServletRequest request = (HttpServletRequest) event.getSuppliedRequest();
    final HttpServletResponse response = (HttpServletResponse) event.getSuppliedResponse();
    if (LOG.isDebugEnabled())
        LOG.debug("{} {} {} on {}", request.getDispatcherType(), request.getMethod(), target, channel);
    handle(target, baseRequest, request, response);
    if (LOG.isDebugEnabled())
        LOG.debug("handledAsync={} async={} committed={} on {}", channel.getRequest().isHandled(), request.isAsyncStarted(), response.isCommitted(), channel);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpURI(org.eclipse.jetty.http.HttpURI)

Example 87 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class DebugHandler method handle.

/* ------------------------------------------------------------ */
/*
     * @see org.eclipse.jetty.server.Handler#handle(java.lang.String, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, int)
     */
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    final Response base_response = baseRequest.getResponse();
    final Thread thread = Thread.currentThread();
    final String old_name = thread.getName();
    boolean suspend = false;
    boolean retry = false;
    String name = (String) request.getAttribute("org.eclipse.jetty.thread.name");
    if (name == null)
        name = old_name + ":" + baseRequest.getHttpURI();
    else
        retry = true;
    String ex = null;
    try {
        if (retry)
            print(name, "RESUME");
        else
            print(name, "REQUEST " + baseRequest.getRemoteAddr() + " " + request.getMethod() + " " + baseRequest.getHeader("Cookie") + "; " + baseRequest.getHeader("User-Agent"));
        thread.setName(name);
        getHandler().handle(target, baseRequest, request, response);
    } catch (IOException ioe) {
        ex = ioe.toString();
        throw ioe;
    } catch (ServletException se) {
        ex = se.toString() + ":" + se.getCause();
        throw se;
    } catch (RuntimeException rte) {
        ex = rte.toString();
        throw rte;
    } catch (Error e) {
        ex = e.toString();
        throw e;
    } finally {
        thread.setName(old_name);
        suspend = baseRequest.getHttpChannelState().isSuspended();
        if (suspend) {
            request.setAttribute("org.eclipse.jetty.thread.name", name);
            print(name, "SUSPEND");
        } else
            print(name, "RESPONSE " + base_response.getStatus() + (ex == null ? "" : ("/" + ex)) + " " + base_response.getContentType());
    }
}
Also used : Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 88 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class ServletContextHandlerTest method testFallThrough.

@Test
public void testFallThrough() throws Exception {
    HandlerList list = new HandlerList();
    _server.setHandler(list);
    ServletContextHandler root = new ServletContextHandler(list, "/", ServletContextHandler.SESSIONS);
    ServletHandler servlet = root.getServletHandler();
    servlet.setEnsureDefaultServlet(false);
    servlet.addServletWithMapping(HelloServlet.class, "/hello/*");
    list.addHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            response.sendError(404, "Fell Through");
        }
    });
    _server.start();
    String response = _connector.getResponses("GET /hello HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("200 OK"));
    response = _connector.getResponses("GET /other HTTP/1.0\r\n\r\n");
    Assert.assertThat(response, Matchers.containsString("404 Fell Through"));
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) Test(org.junit.Test)

Example 89 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class DispatcherForwardTest method testQueryAggregatesWithFormBeforeAndAfterForward.

@Test
public void testQueryAggregatesWithFormBeforeAndAfterForward() throws Exception {
    // 1. request /one?a=1 + content b=2
    // 1. assert params => a=1&b=2
    // 1. forward /two?c=3
    // 2. assert query => a=1&c=3 + params => a=1&b=2&c=3
    // 1. assert query => a=1 + params => a=1&b=2
    CountDownLatch latch = new CountDownLatch(1);
    final String query1 = "a=1%20one";
    final String query2 = "c=3%20three";
    final String query3 = "c=3%20three&a=1%20one";
    final String form = "b=2%20two";
    servlet1 = new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
            checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
            req.getRequestDispatcher("/two?" + query2).forward(req, resp);
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
            checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
            checkThat(req.getParameter("c"), Matchers.nullValue());
            latch.countDown();
        }
    };
    servlet2 = new HttpServlet() {

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query3));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
            checkThat(req.getParameter("b"), Matchers.equalTo("2 two"));
            checkThat(req.getParameter("c"), Matchers.equalTo("3 three"));
        }
    };
    prepare();
    String request = "" + "POST /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: " + form.length() + "\r\n" + "Connection: close\r\n" + "\r\n" + form;
    String response = connector.getResponses(request);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 90 with HttpServletResponse

use of javax.servlet.http.HttpServletResponse in project jetty.project by eclipse.

the class DispatcherForwardTest method testQueryRetainedByForwardWithoutQuery.

@Test
public void testQueryRetainedByForwardWithoutQuery() throws Exception {
    // 1. request /one?a=1%20one
    // 1. forward /two
    // 2. assert query => a=1 one
    // 1. assert query => a=1 one
    CountDownLatch latch = new CountDownLatch(1);
    final String query1 = "a=1%20one";
    servlet1 = new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            req.getRequestDispatcher("/two").forward(req, resp);
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
            latch.countDown();
        }
    };
    servlet2 = new HttpServlet() {

        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            checkThat(req.getQueryString(), Matchers.equalTo(query1));
            checkThat(req.getParameter("a"), Matchers.equalTo("1 one"));
        }
    };
    prepare();
    String request = "" + "GET /one?" + query1 + " HTTP/1.1\r\n" + "Host: localhost\r\n" + "Connection: close\r\n" + "\r\n";
    String response = connector.getResponses(request);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(response, response.startsWith("HTTP/1.1 200"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

HttpServletResponse (javax.servlet.http.HttpServletResponse)1635 HttpServletRequest (javax.servlet.http.HttpServletRequest)1312 Test (org.junit.Test)705 IOException (java.io.IOException)576 ServletException (javax.servlet.ServletException)491 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)223 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)195 Request (org.eclipse.jetty.server.Request)186 HttpServlet (javax.servlet.http.HttpServlet)157 CountDownLatch (java.util.concurrent.CountDownLatch)156 FilterChain (javax.servlet.FilterChain)148 PrintWriter (java.io.PrintWriter)138 Test (org.testng.annotations.Test)127 HashMap (java.util.HashMap)106 ServletOutputStream (javax.servlet.ServletOutputStream)105 InterruptedIOException (java.io.InterruptedIOException)97 InputStream (java.io.InputStream)85 OutputStream (java.io.OutputStream)81 HttpSession (javax.servlet.http.HttpSession)75 ServletResponse (javax.servlet.ServletResponse)74