Search in sources :

Example 1 with Response

use of org.eclipse.jetty.server.Response in project camel by apache.

the class JettyHttpComponent method createServer.

protected Server createServer() {
    Server s = null;
    ThreadPool tp = threadPool;
    QueuedThreadPool qtp = null;
    // configure thread pool if min/max given
    if (minThreads != null || maxThreads != null) {
        if (getThreadPool() != null) {
            throw new IllegalArgumentException("You cannot configure both minThreads/maxThreads and a custom threadPool on JettyHttpComponent: " + this);
        }
        qtp = new QueuedThreadPool();
        if (minThreads != null) {
            qtp.setMinThreads(minThreads.intValue());
        }
        if (maxThreads != null) {
            qtp.setMaxThreads(maxThreads.intValue());
        }
        tp = qtp;
    }
    if (tp != null) {
        try {
            if (!Server.getVersion().startsWith("8")) {
                s = Server.class.getConstructor(ThreadPool.class).newInstance(tp);
            } else {
                s = new Server();
                if (isEnableJmx()) {
                    enableJmx(s);
                }
                Server.class.getMethod("setThreadPool", ThreadPool.class).invoke(s, tp);
            }
        } catch (Exception e) {
        //ignore
        }
    }
    if (s == null) {
        s = new Server();
    }
    if (qtp != null) {
        // let the thread names indicate they are from the server
        qtp.setName("CamelJettyServer(" + ObjectHelper.getIdentityHashCode(s) + ")");
        try {
            qtp.start();
        } catch (Exception e) {
            throw new RuntimeCamelException("Error starting JettyServer thread pool: " + qtp, e);
        }
    }
    ContextHandlerCollection collection = new ContextHandlerCollection();
    s.setHandler(collection);
    // setup the error handler if it set to Jetty component
    if (getErrorHandler() != null) {
        s.addBean(getErrorHandler());
    } else if (!Server.getVersion().startsWith("8")) {
        //need an error handler that won't leak information about the exception 
        //back to the client.
        ErrorHandler eh = new ErrorHandler() {

            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
                String msg = HttpStatus.getMessage(response.getStatus());
                request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
                if (response instanceof Response) {
                    //need to use the deprecated method to support compiling with Jetty 8
                    ((Response) response).setStatus(response.getStatus(), msg);
                }
                super.handle(target, baseRequest, request, response);
            }

            protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
                super.writeErrorPage(request, writer, code, message, false);
            }
        };
        s.addBean(eh, false);
    }
    return s;
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server) MBeanServer(javax.management.MBeanServer) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) GeneralSecurityException(java.security.GeneralSecurityException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) Endpoint(org.apache.camel.Endpoint) HttpCommonEndpoint(org.apache.camel.http.common.HttpCommonEndpoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Writer(java.io.Writer)

Example 2 with Response

use of org.eclipse.jetty.server.Response in project jetty.project by eclipse.

the class StatisticsHandler method updateResponse.

protected void updateResponse(Request request) {
    Response response = request.getResponse();
    if (request.isHandled()) {
        switch(response.getStatus() / 100) {
            case 1:
                _responses1xx.increment();
                break;
            case 2:
                _responses2xx.increment();
                break;
            case 3:
                _responses3xx.increment();
                break;
            case 4:
                _responses4xx.increment();
                break;
            case 5:
                _responses5xx.increment();
                break;
            default:
                break;
        }
    } else
        // will fall through to not found handler
        _responses4xx.increment();
    _responsesTotalBytes.add(response.getContentCount());
}
Also used : Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 3 with Response

use of org.eclipse.jetty.server.Response 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 4 with Response

use of org.eclipse.jetty.server.Response in project jetty.project by eclipse.

the class ScopedHandlerTest method testDouble.

@Test
public void testDouble() throws Exception {
    Request request = new Request(null, null);
    Response response = new Response(null, null);
    TestHandler handler0 = new TestHandler("0");
    OtherHandler handlerA = new OtherHandler("A");
    TestHandler handler1 = new TestHandler("1");
    OtherHandler handlerB = new OtherHandler("B");
    handler0.setServer(new Server());
    handlerA.setServer(handler0.getServer());
    handler1.setServer(handler0.getServer());
    handlerB.setServer(handler0.getServer());
    handler0.setHandler(handlerA);
    handlerA.setHandler(handler1);
    handler1.setHandler(handlerB);
    handler0.start();
    handler0.handle("target", request, request, response);
    handler0.stop();
    String history = _history.toString();
    assertEquals(">S0>S1>W0>HA>W1>HB<HB<W1<HA<W0<S1<S0", history);
}
Also used : Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Server(org.eclipse.jetty.server.Server) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Example 5 with Response

use of org.eclipse.jetty.server.Response in project jetty.project by eclipse.

the class ScopedHandlerTest method testTriple.

@Test
public void testTriple() throws Exception {
    Request request = new Request(null, null);
    Response response = new Response(null, null);
    TestHandler handler0 = new TestHandler("0");
    OtherHandler handlerA = new OtherHandler("A");
    TestHandler handler1 = new TestHandler("1");
    OtherHandler handlerB = new OtherHandler("B");
    TestHandler handler2 = new TestHandler("2");
    OtherHandler handlerC = new OtherHandler("C");
    handler0.setServer(new Server());
    handlerA.setServer(handler0.getServer());
    handler1.setServer(handler0.getServer());
    handlerB.setServer(handler0.getServer());
    handler2.setServer(handler0.getServer());
    handlerC.setServer(handler0.getServer());
    handler0.setHandler(handlerA);
    handlerA.setHandler(handler1);
    handler1.setHandler(handlerB);
    handlerB.setHandler(handler2);
    handler2.setHandler(handlerC);
    handler0.start();
    handler0.handle("target", request, request, response);
    handler0.stop();
    String history = _history.toString();
    assertEquals(">S0>S1>S2>W0>HA>W1>HB>W2>HC<HC<W2<HB<W1<HA<W0<S2<S1<S0", history);
}
Also used : Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) Server(org.eclipse.jetty.server.Server) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Test(org.junit.Test)

Aggregations

Response (org.eclipse.jetty.server.Response)16 HttpServletResponse (javax.servlet.http.HttpServletResponse)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 Request (org.eclipse.jetty.server.Request)6 IOException (java.io.IOException)5 ServletException (javax.servlet.ServletException)4 HttpSession (javax.servlet.http.HttpSession)4 Authentication (org.eclipse.jetty.server.Authentication)4 HttpCookie (org.eclipse.jetty.http.HttpCookie)3 Server (org.eclipse.jetty.server.Server)3 URISyntaxException (java.net.URISyntaxException)2 RequestDispatcher (javax.servlet.RequestDispatcher)2 ServletRequest (javax.servlet.ServletRequest)2 ServletResponse (javax.servlet.ServletResponse)2 ServerAuthException (org.eclipse.jetty.security.ServerAuthException)2 UserAuthentication (org.eclipse.jetty.security.UserAuthentication)2 DeferredAuthentication (org.eclipse.jetty.security.authentication.DeferredAuthentication)2 User (org.eclipse.jetty.server.Authentication.User)2 Handler (org.eclipse.jetty.server.Handler)2 UserIdentity (org.eclipse.jetty.server.UserIdentity)2