Search in sources :

Example 41 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project jetty.project by eclipse.

the class ServletRequestLogTest method testLogHandlerCollection.

/**
     * Test a RequestLogHandler at the end of a HandlerCollection.
     * This handler chain is setup to look like Jetty versions up to 9.2. 
     * Default configuration.
     * @throws Exception on test failure
     */
@Test(timeout = 4000)
public void testLogHandlerCollection() 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
    // 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 behavior as defined by etc/jetty-requestlog.xml
    // the id="RequestLog"
    RequestLogHandler requestLog = new RequestLogHandler();
    CaptureLog captureLog = new CaptureLog();
    requestLog.setRequestLog(captureLog);
    handlers.addHandler(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");
    try {
        server.start();
        String host = connector.getHost();
        if (host == null) {
            host = "localhost";
        }
        int port = connector.getLocalPort();
        URI serverUri = new URI("http", null, host, port, requestPath, 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.debug("Response Status Code: {}", statusCode);
            if (statusCode == 200) {
                // collect response message and log it
                String content = getResponseContent(connection);
                LOG.debug("Response Content: {}", content);
            }
        } finally {
            connection.disconnect();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) 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 42 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project jetty.project by eclipse.

the class ServletRequestLogTest method testLogHandlerCollection_ErrorHandler_ServerBean.

/**
     * Test a RequestLogHandler at the end of a HandlerCollection.
     * and also with the default ErrorHandler as server bean in place.
     * @throws Exception on test failure
     */
@Test(timeout = 4000)
public void testLogHandlerCollection_ErrorHandler_ServerBean() throws Exception {
    Server server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(0);
    server.setConnectors(new Connector[] { connector });
    ErrorHandler errorHandler = new ErrorHandler();
    server.addBean(errorHandler);
    // First the behavior as defined in etc/jetty.xml
    // 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 behavior as defined by etc/jetty-requestlog.xml
    // the id="RequestLog"
    RequestLogHandler requestLog = new RequestLogHandler();
    CaptureLog captureLog = new CaptureLog();
    requestLog.setRequestLog(captureLog);
    handlers.addHandler(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");
    try {
        server.start();
        String host = connector.getHost();
        if (host == null) {
            host = "localhost";
        }
        int port = connector.getLocalPort();
        URI serverUri = new URI("http", null, host, port, requestPath, 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.debug("Response Status Code: {}", statusCode);
            if (statusCode == 200) {
                // collect response message and log it
                String content = getResponseContent(connection);
                LOG.debug("Response Content: {}", content);
            }
        } finally {
            connection.disconnect();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server) 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 43 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project jetty.project by eclipse.

the class ServletRequestLogTest method testLogHandlerCollection_SimpleErrorPageMapping.

/**
     * Test a RequestLogHandler at the end of a HandlerCollection
     * using servlet specific error page mapping.
     * @throws Exception on test failure
     */
@Test(timeout = 4000)
public void testLogHandlerCollection_SimpleErrorPageMapping() 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
    // 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 behavior as defined by etc/jetty-requestlog.xml
    // the id="RequestLog"
    RequestLogHandler requestLog = new RequestLogHandler();
    CaptureLog captureLog = new CaptureLog();
    requestLog.setRequestLog(captureLog);
    handlers.addHandler(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, requestPath, 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.debug("Response Status Code: {}", statusCode);
            if (statusCode == 200) {
                // collect response message and log it
                String content = getResponseContent(connection);
                LOG.debug("Response Content: {}", content);
            }
        } finally {
            connection.disconnect();
        }
        assertRequestLog(captureLog);
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) 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 44 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection 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)

Example 45 with HandlerCollection

use of org.eclipse.jetty.server.handler.HandlerCollection in project h2o-3 by h2oai.

the class JettyHTTPD method registerHandlers.

/**
   * Hook up Jetty handlers.  Do this before start() is called.
   */
public void registerHandlers(HandlerWrapper handlerWrapper) {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SECURITY | ServletContextHandler.SESSIONS);
    if (null != H2O.ARGS.context_path && !H2O.ARGS.context_path.isEmpty()) {
        context.setContextPath(H2O.ARGS.context_path);
    } else {
        context.setContextPath("/");
    }
    context.addServlet(NpsBinServlet.class, "/3/NodePersistentStorage.bin/*");
    context.addServlet(PostFileServlet.class, "/3/PostFile.bin");
    context.addServlet(PostFileServlet.class, "/3/PostFile");
    context.addServlet(DatasetServlet.class, "/3/DownloadDataset");
    context.addServlet(DatasetServlet.class, "/3/DownloadDataset.bin");
    context.addServlet(RequestServer.class, "/");
    HandlerCollection hc = new HandlerCollection();
    hc.setHandlers(new Handler[] { new GateHandler(), new AuthenticationHandler(), new ExtensionHandler1(), context });
    handlerWrapper.setHandler(hc);
}
Also used : HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)76 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)44 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)30 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)27 Server (org.eclipse.jetty.server.Server)26 URISyntaxException (java.net.URISyntaxException)18 ServerConnector (org.eclipse.jetty.server.ServerConnector)18 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)18 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)16 Handler (org.eclipse.jetty.server.Handler)15 Test (org.junit.Test)13 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)9 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)9 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)8 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)7 GzipHandler (org.eclipse.jetty.servlets.gzip.GzipHandler)6 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)6 File (java.io.File)5 URI (java.net.URI)5 HttpConnectionFactory (org.eclipse.jetty.server.HttpConnectionFactory)5