Search in sources :

Example 21 with ContextHandlerCollection

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

the class WebsocketComponent method createServer.

protected Server createServer() throws Exception {
    Server server = null;
    if (minThreads == null && maxThreads == null && getThreadPool() == null) {
        minThreads = 1;
        // 1+selectors+acceptors
        maxThreads = 1 + Runtime.getRuntime().availableProcessors() * 2;
    }
    // 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);
        }
        QueuedThreadPool qtp = new QueuedThreadPool();
        if (minThreads != null) {
            qtp.setMinThreads(minThreads.intValue());
        }
        if (maxThreads != null) {
            qtp.setMaxThreads(maxThreads.intValue());
        }
        // let the thread names indicate they are from the server
        qtp.setName("CamelJettyWebSocketServer");
        try {
            qtp.start();
        } catch (Exception e) {
            throw new RuntimeCamelException("Error starting JettyWebSocketServer thread pool: " + qtp, e);
        }
        server = new Server(qtp);
        ContextHandlerCollection collection = new ContextHandlerCollection();
        server.setHandler(collection);
    }
    if (getThreadPool() != null) {
        server = new Server(getThreadPool());
        ContextHandlerCollection collection = new ContextHandlerCollection();
        server.setHandler(collection);
    }
    return server;
}
Also used : Server(org.eclipse.jetty.server.Server) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) RuntimeCamelException(org.apache.camel.RuntimeCamelException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 22 with ContextHandlerCollection

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

the class ProxyServer method start.

public void start() throws PulsarServerException {
    log.info("Starting web socket proxy at port {}", conf.getWebServicePort());
    try {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(true);
        requestLog.setLogTimeZone("GMT");
        requestLog.setLogLatency(true);
        requestLogHandler.setRequestLog(requestLog);
        handlers.add(0, new ContextHandlerCollection());
        handlers.add(requestLogHandler);
        ContextHandlerCollection contexts = new ContextHandlerCollection();
        contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));
        HandlerCollection handlerCollection = new HandlerCollection();
        handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
        server.setHandler(handlerCollection);
        server.start();
    } catch (Exception e) {
        throw new PulsarServerException(e);
    }
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ServletException(javax.servlet.ServletException) DeploymentException(javax.websocket.DeploymentException) GeneralSecurityException(java.security.GeneralSecurityException) MalformedURLException(java.net.MalformedURLException) PulsarClientException(com.yahoo.pulsar.client.api.PulsarClientException) PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 23 with ContextHandlerCollection

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

the class ServerManager method start.

public void start() throws Exception {
    RequestLogHandler requestLogHandler = new RequestLogHandler();
    Slf4jRequestLog requestLog = new Slf4jRequestLog();
    requestLog.setExtended(true);
    requestLog.setLogTimeZone("GMT");
    requestLog.setLogLatency(true);
    requestLogHandler.setRequestLog(requestLog);
    handlers.add(0, new ContextHandlerCollection());
    handlers.add(requestLogHandler);
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    contexts.setHandlers(handlers.toArray(new Handler[handlers.size()]));
    HandlerCollection handlerCollection = new HandlerCollection();
    handlerCollection.setHandlers(new Handler[] { contexts, new DefaultHandler(), requestLogHandler });
    server.setHandler(handlerCollection);
    server.start();
    log.info("Server started at end point {}", getServiceUri());
}
Also used : Slf4jRequestLog(org.eclipse.jetty.server.Slf4jRequestLog) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Example 24 with ContextHandlerCollection

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

the class JettyServer method makeContext.

private Handler makeContext(final JettyEnv env, final Config cfg) {
    final Set<String> paths = new HashSet<>();
    for (URI u : listenURLs(cfg)) {
        String p = u.getPath();
        if (p == null || p.isEmpty()) {
            p = "/";
        }
        while (1 < p.length() && p.endsWith("/")) {
            p = p.substring(0, p.length() - 1);
        }
        paths.add(p);
    }
    final List<ContextHandler> all = new ArrayList<>();
    for (String path : paths) {
        all.add(makeContext(path, env, cfg));
    }
    if (all.size() == 1) {
        //
        return all.get(0);
    }
    // We have more than one path served out of this container so
    // combine them in a handler which supports dispatching to the
    // individual contexts.
    //
    final ContextHandlerCollection r = new ContextHandlerCollection();
    r.setHandlers(all.toArray(new Handler[0]));
    return r;
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ArrayList(java.util.ArrayList) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) SessionHandler(org.eclipse.jetty.server.session.SessionHandler) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) URI(java.net.URI) HashSet(java.util.HashSet)

Example 25 with ContextHandlerCollection

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

the class WebServer method registerResource.

/**
     * Register a URL pattern to return resources from the file system. The
     * filePath may start with any of the following:
     * <ol>
     * <li>{@link jmri.util.FileUtil#PREFERENCES}
     * <li>{@link jmri.util.FileUtil#PROFILE}
     * <li>{@link jmri.util.FileUtil#SETTINGS}
     * <li>{@link jmri.util.FileUtil#PROGRAM}
     * </ol>
     * Note that the filePath can be overridden by an otherwise identical
     * filePath starting with any of the portable paths above it in the
     * preceding list.
     *
     * @param urlPattern the pattern to get resources for
     * @param filePath   the portable path for the resources
     * @throws IllegalArgumentException if urlPattern is already registered to
     *                                  deny access or for a servlet or if
     *                                  filePath is not allowed
     */
public void registerResource(String urlPattern, String filePath) throws IllegalArgumentException {
    if (this.registeredUrls.get(urlPattern) != null) {
        throw new IllegalArgumentException("urlPattern \"" + urlPattern + "\" is already registered.");
    }
    this.registeredUrls.put(urlPattern, Registration.RESOURCE);
    ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.NO_SECURITY);
    servletContext.setContextPath(urlPattern);
    HandlerList handlers = new HandlerList();
    if (filePath.startsWith(FileUtil.PROGRAM) && !filePath.equals(FileUtil.PROGRAM)) {
        // make it possible to override anything under program: with an identical path under preference:, profile:, or settings:
        log.debug("Setting up handler chain for {}", urlPattern);
        ResourceHandler preferenceHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath.replace(FileUtil.PROGRAM, FileUtil.PREFERENCES)));
        ResourceHandler projectHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath.replace(FileUtil.PROGRAM, FileUtil.PROFILE)));
        ResourceHandler settingsHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath.replace(FileUtil.PROGRAM, FileUtil.SETTINGS)));
        ResourceHandler programHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath));
        handlers.setHandlers(new Handler[] { preferenceHandler, projectHandler, settingsHandler, programHandler, new DefaultHandler() });
    } else if (filePath.startsWith(FileUtil.SETTINGS) && !filePath.equals(FileUtil.SETTINGS)) {
        // make it possible to override anything under settings: with an identical path under preference: or profile:
        log.debug("Setting up handler chain for {}", urlPattern);
        ResourceHandler preferenceHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath.replace(FileUtil.SETTINGS, FileUtil.PREFERENCES)));
        ResourceHandler projectHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath.replace(FileUtil.PROGRAM, FileUtil.PROFILE)));
        ResourceHandler settingsHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath));
        handlers.setHandlers(new Handler[] { preferenceHandler, projectHandler, settingsHandler, new DefaultHandler() });
    } else if (filePath.startsWith(FileUtil.PROFILE) && !filePath.equals(FileUtil.PROFILE)) {
        // make it possible to override anything under profile: with an identical path under preference:
        log.debug("Setting up handler chain for {}", urlPattern);
        ResourceHandler preferenceHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath.replace(FileUtil.SETTINGS, FileUtil.PREFERENCES)));
        ResourceHandler projectHandler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath.replace(FileUtil.PROGRAM, FileUtil.PROFILE)));
        handlers.setHandlers(new Handler[] { preferenceHandler, projectHandler, new DefaultHandler() });
    } else if (FileUtil.isPortableFilename(filePath)) {
        log.debug("Setting up handler chain for {}", urlPattern);
        ResourceHandler handler = new DirectoryHandler(FileUtil.getAbsoluteFilename(filePath));
        handlers.setHandlers(new Handler[] { handler, new DefaultHandler() });
    } else if (URIforPortablePath(filePath) == null) {
        throw new IllegalArgumentException("\"" + filePath + "\" is not allowed.");
    }
    ContextHandler handlerContext = new ContextHandler();
    handlerContext.setContextPath(urlPattern);
    handlerContext.setHandler(handlers);
    ((ContextHandlerCollection) this.server.getHandler()).addHandler(handlerContext);
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) DirectoryHandler(jmri.web.servlet.directory.DirectoryHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) DirectoryHandler(jmri.web.servlet.directory.DirectoryHandler) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler)

Aggregations

ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)54 Server (org.eclipse.jetty.server.Server)25 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)21 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)17 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)16 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)14 ServerConnector (org.eclipse.jetty.server.ServerConnector)13 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)13 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)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 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)5 HttpURLConnection (java.net.HttpURLConnection)4