Search in sources :

Example 46 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 47 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 48 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 49 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)

Example 50 with Handler

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

the class XmlConfiguredJetty method getWebAppContexts.

public List<WebAppContext> getWebAppContexts() {
    List<WebAppContext> contexts = new ArrayList<>();
    HandlerCollection handlers = (HandlerCollection) _server.getHandler();
    Handler[] children = handlers.getChildHandlers();
    for (Handler handler : children) {
        if (handler instanceof WebAppContext) {
            WebAppContext context = (WebAppContext) handler;
            contexts.add(context);
        }
    }
    return contexts;
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) ArrayList(java.util.ArrayList) Handler(org.eclipse.jetty.server.Handler) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection)

Aggregations

Handler (org.eclipse.jetty.server.Handler)63 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)18 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)18 Server (org.eclipse.jetty.server.Server)14 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)13 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)12 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)10 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)10 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)10 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)7 ServerConnector (org.eclipse.jetty.server.ServerConnector)7 ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)7 HandlerList (org.eclipse.jetty.server.handler.HandlerList)7 ArrayList (java.util.ArrayList)6 Connector (org.eclipse.jetty.server.Connector)6 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)6 Test (org.junit.Test)6 HandlerWrapper (org.eclipse.jetty.server.handler.HandlerWrapper)5 GzipHandler (org.eclipse.jetty.server.handler.gzip.GzipHandler)5 URI (java.net.URI)4