Search in sources :

Example 56 with ContextHandlerCollection

use of org.eclipse.jetty.server.handler.ContextHandlerCollection in project JMRI by JMRI.

the class WebServer method registerRedirection.

/**
     * Register a URL pattern to be redirected to another resource.
     *
     * @param urlPattern  the pattern to be redirected
     * @param redirection the path to which the pattern is redirected
     * @throws IllegalArgumentException if urlPattern is already registered for
     *                                  any other purpose
     */
public void registerRedirection(String urlPattern, String redirection) throws IllegalArgumentException {
    Registration registered = this.registeredUrls.get(urlPattern);
    if (registered != null && registered != Registration.REDIRECTION) {
        throw new IllegalArgumentException("\"" + urlPattern + "\" registered to " + registered);
    }
    this.registeredUrls.put(urlPattern, Registration.REDIRECTION);
    ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
    servletContext.setContextPath(urlPattern);
    RedirectionServlet servlet = new RedirectionServlet(urlPattern, redirection);
    // NOI18N
    servletContext.addServlet(new ServletHolder(servlet), "");
    ((ContextHandlerCollection) this.server.getHandler()).addHandler(servletContext);
}
Also used : RedirectionServlet(jmri.web.servlet.RedirectionServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 57 with ContextHandlerCollection

use of org.eclipse.jetty.server.handler.ContextHandlerCollection in project hive by apache.

the class HttpServer method initializeWebServer.

private void initializeWebServer(final Builder b, int queueSize) throws IOException {
    // Set handling for low resource conditions.
    final LowResourceMonitor low = new LowResourceMonitor(webServer);
    low.setLowResourcesIdleTimeout(10000);
    webServer.addBean(low);
    Connector connector = createChannelConnector(queueSize, b);
    webServer.addConnector(connector);
    RewriteHandler rwHandler = new RewriteHandler();
    rwHandler.setRewriteRequestURI(true);
    rwHandler.setRewritePathInfo(false);
    RewriteRegexRule rootRule = new RewriteRegexRule();
    rootRule.setRegex("^/$");
    rootRule.setReplacement(b.contextRootRewriteTarget);
    rootRule.setTerminating(true);
    rwHandler.addRule(rootRule);
    rwHandler.setHandler(webAppContext);
    // Configure web application contexts for the web server
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.addHandler(rwHandler);
    webServer.setHandler(contexts);
    if (b.usePAM) {
        setupPam(b, contexts);
    }
    addServlet("jmx", "/jmx", JMXJsonServlet.class);
    addServlet("conf", "/conf", ConfServlet.class);
    addServlet("stacks", "/stacks", StackServlet.class);
    addServlet("conflog", "/conflog", Log4j2ConfiguratorServlet.class);
    for (Pair<String, Class<? extends HttpServlet>> p : b.servlets) {
        addServlet(p.getFirst(), "/" + p.getFirst(), p.getSecond());
    }
    ServletContextHandler staticCtx = new ServletContextHandler(contexts, "/static");
    staticCtx.setResourceBase(appDir + "/static");
    staticCtx.addServlet(DefaultServlet.class, "/*");
    staticCtx.setDisplayName("static");
    String logDir = getLogDir(b.conf);
    if (logDir != null) {
        ServletContextHandler logCtx = new ServletContextHandler(contexts, "/logs");
        setContextAttributes(logCtx.getServletContext(), b.contextAttrs);
        logCtx.addServlet(AdminAuthorizedServlet.class, "/*");
        logCtx.setResourceBase(logDir);
        logCtx.setDisplayName("logs");
    }
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) Connector(org.eclipse.jetty.server.Connector) RewriteRegexRule(org.eclipse.jetty.rewrite.handler.RewriteRegexRule) HttpServlet(javax.servlet.http.HttpServlet) LowResourceMonitor(org.eclipse.jetty.server.LowResourceMonitor) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler)

Aggregations

ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)57 Server (org.eclipse.jetty.server.Server)27 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)21 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)17 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)16 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)16 ServerConnector (org.eclipse.jetty.server.ServerConnector)15 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)15 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)10 Test (org.junit.Test)10 Handler (org.eclipse.jetty.server.Handler)9 URI (java.net.URI)7 HttpConfiguration (org.eclipse.jetty.server.HttpConfiguration)7 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)7 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)6 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 MBeanContainer (org.eclipse.jetty.jmx.MBeanContainer)5 HttpURLConnection (java.net.HttpURLConnection)4