Search in sources :

Example 1 with HttpResourceBinding

use of io.airlift.http.server.HttpServerBinder.HttpResourceBinding in project airlift by airlift.

the class HttpServer method createServletContext.

private static ServletContextHandler createServletContext(Servlet theServlet, Set<HttpResourceBinding> resources, Map<String, String> parameters, Set<Filter> filters, TraceTokenManager tokenManager, LoginService loginService, String... connectorNames) {
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    context.addFilter(new FilterHolder(new TimingFilter()), "/*", null);
    if (tokenManager != null) {
        context.addFilter(new FilterHolder(new TraceTokenFilter(tokenManager)), "/*", null);
    }
    // -- security handler
    if (loginService != null) {
        SecurityHandler securityHandler = createSecurityHandler(loginService);
        context.setSecurityHandler(securityHandler);
    }
    // -- user provided filters
    for (Filter filter : filters) {
        context.addFilter(new FilterHolder(filter), "/*", null);
    }
    // -- static resources
    for (HttpResourceBinding resource : resources) {
        ClassPathResourceFilter servlet = new ClassPathResourceFilter(resource.getBaseUri(), resource.getClassPathResourceBase(), resource.getWelcomeFiles());
        context.addFilter(new FilterHolder(servlet), servlet.getBaseUri() + "/*", null);
    }
    // -- gzip handler
    context.setGzipHandler(new GzipHandler());
    // -- the servlet
    ServletHolder servletHolder = new ServletHolder(theServlet);
    servletHolder.setInitParameters(ImmutableMap.copyOf(parameters));
    context.addServlet(servletHolder, "/*");
    // Starting with Jetty 9 there is no way to specify connectors directly, but
    // there is this wonky @ConnectorName virtual hosts automatically added
    String[] virtualHosts = new String[connectorNames.length];
    for (int i = 0; i < connectorNames.length; i++) {
        virtualHosts[i] = "@" + connectorNames[i];
    }
    context.setVirtualHosts(virtualHosts);
    return context;
}
Also used : ConstraintSecurityHandler(org.eclipse.jetty.security.ConstraintSecurityHandler) SecurityHandler(org.eclipse.jetty.security.SecurityHandler) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpResourceBinding(io.airlift.http.server.HttpServerBinder.HttpResourceBinding) Constraint(org.eclipse.jetty.util.security.Constraint) Filter(javax.servlet.Filter) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Aggregations

HttpResourceBinding (io.airlift.http.server.HttpServerBinder.HttpResourceBinding)1 Filter (javax.servlet.Filter)1 ConstraintSecurityHandler (org.eclipse.jetty.security.ConstraintSecurityHandler)1 SecurityHandler (org.eclipse.jetty.security.SecurityHandler)1 GzipHandler (org.eclipse.jetty.server.handler.gzip.GzipHandler)1 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)1 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)1 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)1 Constraint (org.eclipse.jetty.util.security.Constraint)1