Search in sources :

Example 66 with Handler

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

the class HandlerWrapper method insertHandler.

/* ------------------------------------------------------------ */
/** 
     * Replace the current handler with another HandlerWrapper
     * linked to the current handler.  
     * <p>
     * This is equivalent to:
     * <pre>
     *   wrapper.setHandler(getHandler());
     *   setHandler(wrapper);
     * </pre>
     * @param wrapper the wrapper to insert
     */
public void insertHandler(HandlerWrapper wrapper) {
    if (wrapper == null)
        throw new IllegalArgumentException();
    HandlerWrapper tail = wrapper;
    while (tail.getHandler() instanceof HandlerWrapper) tail = (HandlerWrapper) tail.getHandler();
    if (tail.getHandler() != null)
        throw new IllegalArgumentException("bad tail of inserted wrapper chain");
    Handler next = getHandler();
    setHandler(wrapper);
    tail.setHandler(next);
}
Also used : Handler(org.eclipse.jetty.server.Handler)

Example 67 with Handler

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

the class HandlerWrapper method setHandler.

/* ------------------------------------------------------------ */
/**
     * @param handler Set the {@link Handler} which should be wrapped.
     */
public void setHandler(Handler handler) {
    if (isStarted())
        throw new IllegalStateException(STARTED);
    // check for loops
    if (handler == this || (handler instanceof HandlerContainer && Arrays.asList(((HandlerContainer) handler).getChildHandlers()).contains(this)))
        throw new IllegalStateException("setHandler loop");
    if (handler != null)
        handler.setServer(getServer());
    Handler old = _handler;
    _handler = handler;
    updateBean(old, _handler, true);
}
Also used : Handler(org.eclipse.jetty.server.Handler) HandlerContainer(org.eclipse.jetty.server.HandlerContainer)

Example 68 with Handler

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

the class AbstractHandlerContainer method findContainerOf.

/* ------------------------------------------------------------ */
public static <T extends HandlerContainer> T findContainerOf(HandlerContainer root, Class<T> type, Handler handler) {
    if (root == null || handler == null)
        return null;
    Handler[] branches = root.getChildHandlersByClass(type);
    if (branches != null) {
        for (Handler h : branches) {
            @SuppressWarnings("unchecked") T container = (T) h;
            Handler[] candidates = container.getChildHandlersByClass(handler.getClass());
            if (candidates != null) {
                for (Handler c : candidates) if (c == handler)
                    return container;
            }
        }
    }
    return null;
}
Also used : Handler(org.eclipse.jetty.server.Handler)

Example 69 with Handler

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

the class ShutdownHandlerTest method start.

public void start(HandlerWrapper wrapper) throws Exception {
    server = new Server();
    connector = new ServerConnector(server);
    server.addConnector(connector);
    Handler shutdown = new ShutdownHandler(shutdownToken);
    Handler handler = shutdown;
    if (wrapper != null) {
        wrapper.setHandler(shutdown);
        handler = wrapper;
    }
    server.setHandler(handler);
    server.start();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Server(org.eclipse.jetty.server.Server) Handler(org.eclipse.jetty.server.Handler)

Example 70 with Handler

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

the class ServletRequestLogTest method testLogHandlerWrapped.

/**
     * Test an alternate (proposed) setup for using RequestLogHandler in a wrapped style
     * @throws Exception on test failure
     */
@Test(timeout = 4000)
public void testLogHandlerWrapped() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    // First the behavior as defined in etc/jetty.xml (as is)
    // id="Handlers"
    HandlerCollection handlers = new HandlerCollection();
    // id="Contexts"
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    // id="DefaultHandler"
    DefaultHandler defaultHandler = new DefaultHandler();
    handlers.setHandlers(new Handler[] { contexts, defaultHandler });
    server.setHandler(handlers);
    // Next the proposed behavioral change to etc/jetty-requestlog.xml
    // the id="RequestLog"
    RequestLogHandler requestLog = new RequestLogHandler();
    CaptureLog captureLog = new CaptureLog();
    requestLog.setRequestLog(captureLog);
    Handler origServerHandler = server.getHandler();
    requestLog.setHandler(origServerHandler);
    server.setHandler(requestLog);
    // Lastly, the behavior as defined by deployment of a webapp
    // Add the Servlet Context
    ServletContextHandler app = new ServletContextHandler(ServletContextHandler.SESSIONS);
    app.setContextPath("/");
    contexts.addHandler(app);
    // Add the test servlet
    ServletHolder testHolder = new ServletHolder(testServlet);
    app.addServlet(testHolder, "/test");
    app.addServlet(CustomErrorServlet.class, "/errorpage");
    // Add error page mapping
    ErrorPageErrorHandler errorMapper = new ErrorPageErrorHandler();
    errorMapper.addErrorPage(500, "/errorpage");
    app.setErrorHandler(errorMapper);
    try {
        server.start();
        String host = connector.getHost();
        if (host == null) {
            host = "localhost";
        }
        int port = connector.getLocalPort();
        URI serverUri = new URI("http", null, host, port, "/test", null, null);
        // Make call to test handler
        HttpURLConnection connection = (HttpURLConnection) serverUri.toURL().openConnection();
        try {
            connection.setAllowUserInteraction(false);
            // log response status code
            int statusCode = connection.getResponseCode();
            LOG.info("Response Status Code: {}", statusCode);
            if (statusCode == 200) {
                // collect response message and log it
                String content = getResponseContent(connection);
                LOG.info("Response Content: {}", content);
            }
        } finally {
            connection.disconnect();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) Handler(org.eclipse.jetty.server.Handler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) URI(java.net.URI) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpURLConnection(java.net.HttpURLConnection) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) Test(org.junit.Test)

Aggregations

Handler (org.eclipse.jetty.server.Handler)119 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)37 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)37 Server (org.eclipse.jetty.server.Server)35 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)26 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)25 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)23 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)22 ArrayList (java.util.ArrayList)21 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)18 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)18 IOException (java.io.IOException)15 Test (org.junit.Test)15 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 ServerConnector (org.eclipse.jetty.server.ServerConnector)13 ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Request (org.eclipse.jetty.server.Request)12 URI (java.net.URI)11 SecurityHandler (org.eclipse.jetty.security.SecurityHandler)11