Search in sources :

Example 1 with ContextHandlerCollection

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

the class CometdComponent method createServer.

protected Server createServer() throws Exception {
    Server server = new Server();
    ContextHandlerCollection collection = new ContextHandlerCollection();
    server.setHandler(collection);
    return server;
}
Also used : Server(org.eclipse.jetty.server.Server) BayeuxServer(org.cometd.bayeux.server.BayeuxServer) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection)

Example 2 with ContextHandlerCollection

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

the class BaseHttpTest method handlers.

protected HandlerCollection handlers(Handler... handlers) {
    HandlerCollection collection = new ContextHandlerCollection();
    collection.setHandlers(handlers);
    return collection;
}
Also used : ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) HandlerCollection(org.eclipse.jetty.server.handler.HandlerCollection) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection)

Example 3 with ContextHandlerCollection

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

the class JettyHttpComponent method createServer.

protected Server createServer() {
    Server s = null;
    ThreadPool tp = threadPool;
    QueuedThreadPool qtp = null;
    // 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);
        }
        qtp = new QueuedThreadPool();
        if (minThreads != null) {
            qtp.setMinThreads(minThreads.intValue());
        }
        if (maxThreads != null) {
            qtp.setMaxThreads(maxThreads.intValue());
        }
        tp = qtp;
    }
    if (tp != null) {
        try {
            if (!Server.getVersion().startsWith("8")) {
                s = Server.class.getConstructor(ThreadPool.class).newInstance(tp);
            } else {
                s = new Server();
                if (isEnableJmx()) {
                    enableJmx(s);
                }
                Server.class.getMethod("setThreadPool", ThreadPool.class).invoke(s, tp);
            }
        } catch (Exception e) {
        //ignore
        }
    }
    if (s == null) {
        s = new Server();
    }
    if (qtp != null) {
        // let the thread names indicate they are from the server
        qtp.setName("CamelJettyServer(" + ObjectHelper.getIdentityHashCode(s) + ")");
        try {
            qtp.start();
        } catch (Exception e) {
            throw new RuntimeCamelException("Error starting JettyServer thread pool: " + qtp, e);
        }
    }
    ContextHandlerCollection collection = new ContextHandlerCollection();
    s.setHandler(collection);
    // setup the error handler if it set to Jetty component
    if (getErrorHandler() != null) {
        s.addBean(getErrorHandler());
    } else if (!Server.getVersion().startsWith("8")) {
        //need an error handler that won't leak information about the exception 
        //back to the client.
        ErrorHandler eh = new ErrorHandler() {

            public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
                String msg = HttpStatus.getMessage(response.getStatus());
                request.setAttribute(RequestDispatcher.ERROR_MESSAGE, msg);
                if (response instanceof Response) {
                    //need to use the deprecated method to support compiling with Jetty 8
                    ((Response) response).setStatus(response.getStatus(), msg);
                }
                super.handle(target, baseRequest, request, response);
            }

            protected void writeErrorPage(HttpServletRequest request, Writer writer, int code, String message, boolean showStacks) throws IOException {
                super.writeErrorPage(request, writer, code, message, false);
            }
        };
        s.addBean(eh, false);
    }
    return s;
}
Also used : ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) Server(org.eclipse.jetty.server.Server) MBeanServer(javax.management.MBeanServer) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) ThreadPool(org.eclipse.jetty.util.thread.ThreadPool) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) GeneralSecurityException(java.security.GeneralSecurityException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) Endpoint(org.apache.camel.Endpoint) HttpCommonEndpoint(org.apache.camel.http.common.HttpCommonEndpoint) HttpServletRequest(javax.servlet.http.HttpServletRequest) Response(org.eclipse.jetty.server.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Writer(java.io.Writer)

Example 4 with ContextHandlerCollection

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

the class HttpServer2 method initializeWebServer.

private void initializeWebServer(String name, String hostName, Configuration conf, String[] pathSpecs) throws IOException {
    Preconditions.checkNotNull(webAppContext);
    int maxThreads = conf.getInt(HTTP_MAX_THREADS_KEY, -1);
    // If HTTP_MAX_THREADS is not configured, QueueThreadPool() will use the
    // default value (currently 250).
    QueuedThreadPool threadPool = (QueuedThreadPool) webServer.getThreadPool();
    threadPool.setDaemon(true);
    if (maxThreads != -1) {
        threadPool.setMaxThreads(maxThreads);
    }
    SessionManager sm = webAppContext.getSessionHandler().getSessionManager();
    if (sm instanceof AbstractSessionManager) {
        AbstractSessionManager asm = (AbstractSessionManager) sm;
        asm.setHttpOnly(true);
        asm.getSessionCookieConfig().setSecure(true);
    }
    ContextHandlerCollection contexts = new ContextHandlerCollection();
    RequestLog requestLog = HttpRequestLog.getRequestLog(name);
    handlers.addHandler(contexts);
    if (requestLog != null) {
        RequestLogHandler requestLogHandler = new RequestLogHandler();
        requestLogHandler.setRequestLog(requestLog);
        handlers.addHandler(requestLogHandler);
    }
    handlers.addHandler(webAppContext);
    final String appDir = getWebAppsPath(name);
    addDefaultApps(contexts, appDir, conf);
    webServer.setHandler(handlers);
    Map<String, String> xFrameParams = new HashMap<>();
    xFrameParams.put(X_FRAME_ENABLED, String.valueOf(this.xFrameOptionIsEnabled));
    xFrameParams.put(X_FRAME_VALUE, this.xFrameOption.toString());
    addGlobalFilter("safety", QuotingInputFilter.class.getName(), xFrameParams);
    final FilterInitializer[] initializers = getFilterInitializers(conf);
    if (initializers != null) {
        conf = new Configuration(conf);
        conf.set(BIND_ADDRESS, hostName);
        for (FilterInitializer c : initializers) {
            c.initFilter(this, conf);
        }
    }
    addDefaultServlets();
    if (pathSpecs != null) {
        for (String path : pathSpecs) {
            LOG.info("adding path spec: " + path);
            addFilterPathMapping(path, webAppContext);
        }
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HashMap(java.util.HashMap) SessionManager(org.eclipse.jetty.server.SessionManager) AbstractSessionManager(org.eclipse.jetty.server.session.AbstractSessionManager) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) AuthenticationFilterInitializer(org.apache.hadoop.security.AuthenticationFilterInitializer) RequestLog(org.eclipse.jetty.server.RequestLog) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RequestLogHandler(org.eclipse.jetty.server.handler.RequestLogHandler) AbstractSessionManager(org.eclipse.jetty.server.session.AbstractSessionManager)

Example 5 with ContextHandlerCollection

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

the class HttpServer method initializeWebServer.

void initializeWebServer(Builder b) {
    // Create the thread pool for the web server to handle HTTP requests
    QueuedThreadPool threadPool = new QueuedThreadPool();
    if (b.maxThreads > 0) {
        threadPool.setMaxThreads(b.maxThreads);
    }
    threadPool.setDaemon(true);
    threadPool.setName(b.name + "-web");
    webServer.setThreadPool(threadPool);
    // Create the channel connector for the web server
    Connector connector = createChannelConnector(threadPool.getMaxThreads(), b);
    connector.setHost(b.host);
    connector.setPort(b.port);
    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);
    addServlet("jmx", "/jmx", JMXJsonServlet.class);
    addServlet("conf", "/conf", ConfServlet.class);
    addServlet("stacks", "/stacks", StackServlet.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 : SelectChannelConnector(org.eclipse.jetty.server.nio.SelectChannelConnector) SslSelectChannelConnector(org.eclipse.jetty.server.ssl.SslSelectChannelConnector) Connector(org.eclipse.jetty.server.Connector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) RewriteRegexRule(org.eclipse.jetty.rewrite.handler.RewriteRegexRule) HttpServlet(javax.servlet.http.HttpServlet) 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)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