Search in sources :

Example 86 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project gravitee-management-rest-api by gravitee-io.

the class JettyEmbeddedContainer method doStart.

@Override
protected void doStart() throws Exception {
    AbstractHandler noContentHandler = new NoContentOutputErrorHandler();
    // This part is needed to avoid WARN while starting container.
    noContentHandler.setServer(server);
    server.addBean(noContentHandler);
    // Create the servlet context
    final ServletContextHandler context = new ServletContextHandler(server, "/management/*", ServletContextHandler.SESSIONS);
    // REST configuration
    final ServletHolder servletHolder = new ServletHolder(ServletContainer.class);
    servletHolder.setInitParameter("javax.ws.rs.Application", GraviteeApplication.class.getName());
    servletHolder.setInitOrder(0);
    context.addServlet(servletHolder, "/*");
    // Spring configuration
    System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "basic");
    AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext();
    webApplicationContext.register(SecurityConfiguration.class);
    webApplicationContext.setEnvironment((ConfigurableEnvironment) applicationContext.getEnvironment());
    webApplicationContext.setParent(applicationContext);
    context.addEventListener(new ContextLoaderListener(webApplicationContext));
    // Spring Security filter
    context.addFilter(new FilterHolder(new DelegatingFilterProxy("springSecurityFilterChain")), "/*", EnumSet.allOf(DispatcherType.class));
    // start the server
    server.start();
}
Also used : GraviteeApplication(io.gravitee.management.rest.resource.GraviteeApplication) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) NoContentOutputErrorHandler(io.gravitee.management.standalone.jetty.handler.NoContentOutputErrorHandler) ContextLoaderListener(org.springframework.web.context.ContextLoaderListener) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) DispatcherType(javax.servlet.DispatcherType) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy)

Example 87 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project steve by RWTH-i5-IDSG.

the class SteveAppContext method addSecurityFilter.

private void addSecurityFilter(WebAppContext ctx) {
    // The bean name is not arbitrary, but is as expected by Spring
    Filter f = new DelegatingFilterProxy(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME);
    ctx.addFilter(new FilterHolder(f), CONFIG.getSpringManagerMapping(), EnumSet.allOf(DispatcherType.class));
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Filter(javax.servlet.Filter) DispatcherType(javax.servlet.DispatcherType) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy)

Example 88 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project drill by axbaretto.

the class WebServer method createServletContextHandler.

private ServletContextHandler createServletContextHandler(final boolean authEnabled) throws DrillbitStartupException {
    // Add resources
    final ErrorHandler errorHandler = new DrillErrorHandler();
    errorHandler.setShowStacks(true);
    errorHandler.setShowMessageInTitle(true);
    final ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContextHandler.setErrorHandler(errorHandler);
    servletContextHandler.setContextPath("/");
    final ServletHolder servletHolder = new ServletHolder(new ServletContainer(new DrillRestServer(workManager, servletContextHandler.getServletContext(), drillbit)));
    servletHolder.setInitOrder(1);
    servletContextHandler.addServlet(servletHolder, "/*");
    servletContextHandler.addServlet(new ServletHolder(new MetricsServlet(metrics)), "/status/metrics");
    servletContextHandler.addServlet(new ServletHolder(new ThreadDumpServlet()), "/status/threads");
    final ServletHolder staticHolder = new ServletHolder("static", DefaultServlet.class);
    // Get resource URL for Drill static assets, based on where Drill icon is located
    String drillIconResourcePath = Resource.newClassPathResource(BASE_STATIC_PATH + DRILL_ICON_RESOURCE_RELATIVE_PATH).getURL().toString();
    staticHolder.setInitParameter("resourceBase", drillIconResourcePath.substring(0, drillIconResourcePath.length() - DRILL_ICON_RESOURCE_RELATIVE_PATH.length()));
    staticHolder.setInitParameter("dirAllowed", "false");
    staticHolder.setInitParameter("pathInfoOnly", "true");
    servletContextHandler.addServlet(staticHolder, "/static/*");
    if (authEnabled) {
        // DrillSecurityHandler is used to support SPNEGO and FORM authentication together
        servletContextHandler.setSecurityHandler(new DrillHttpSecurityHandlerProvider(config, workManager.getContext()));
        servletContextHandler.setSessionHandler(createSessionHandler(servletContextHandler.getSecurityHandler()));
    }
    if (isImpersonationOnlyEnabled(workManager.getContext().getConfig())) {
        for (String path : new String[] { "/query", "/query.json" }) {
            servletContextHandler.addFilter(UserNameFilter.class, path, EnumSet.of(DispatcherType.REQUEST));
        }
    }
    if (config.getBoolean(ExecConstants.HTTP_CORS_ENABLED)) {
        FilterHolder holder = new FilterHolder(CrossOriginFilter.class);
        holder.setInitParameter(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_ORIGINS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOWED_METHODS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_METHODS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOWED_HEADERS_PARAM, StringUtils.join(config.getStringList(ExecConstants.HTTP_CORS_ALLOWED_HEADERS), ","));
        holder.setInitParameter(CrossOriginFilter.ALLOW_CREDENTIALS_PARAM, String.valueOf(config.getBoolean(ExecConstants.HTTP_CORS_CREDENTIALS)));
        for (String path : new String[] { "*.json", "/storage/*/enable/*", "/status*" }) {
            servletContextHandler.addFilter(holder, path, EnumSet.of(DispatcherType.REQUEST));
        }
    }
    return servletContextHandler;
}
Also used : DrillErrorHandler(org.apache.drill.exec.server.rest.auth.DrillErrorHandler) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) DrillErrorHandler(org.apache.drill.exec.server.rest.auth.DrillErrorHandler) DrillHttpSecurityHandlerProvider(org.apache.drill.exec.server.rest.auth.DrillHttpSecurityHandlerProvider) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MetricsServlet(com.codahale.metrics.servlets.MetricsServlet) ThreadDumpServlet(com.codahale.metrics.servlets.ThreadDumpServlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 89 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project dropwizard-guicey by xvik.

the class WebMappingsRenderer method renderContextFilters.

@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
private Multimap<String, FilterReference> renderContextFilters(final MappingsConfig config, final MutableServletContextHandler handler, final TreeNode root) throws Exception {
    final Multimap<String, FilterReference> servletFilters = LinkedHashMultimap.create();
    for (FilterMapping mapping : handler.getServletHandler().getFilterMappings()) {
        final FilterHolder holder = handler.getServletHandler().getFilter(mapping.getFilterName());
        // single filter instance used for both contexts and so the name is also the same
        final boolean isGuiceFilter = mapping.getFilterName().equals(GuiceWebModule.GUICE_FILTER);
        if ((isGuiceFilter && !config.isGuiceMappings()) || !isAllowed(holder.getClassName(), config)) {
            continue;
        }
        if (mapping.getServletNames() != null && mapping.getServletNames().length > 0) {
            // filters targeting exact servlet are only remembered to be shown below target servlet
            for (String servlet : mapping.getServletNames()) {
                servletFilters.put(servlet, new FilterReference(mapping, holder));
            }
        } else {
            final TreeNode filter = renderFilter(mapping, holder, root);
            if (isGuiceFilter) {
                renderGuiceWeb(filter);
            }
        }
    }
    return servletFilters;
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) TreeNode(ru.vyarus.dropwizard.guice.debug.util.TreeNode) FilterMapping(org.eclipse.jetty.servlet.FilterMapping)

Example 90 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project validator by validator.

the class Main method main.

public static void main(String[] args) throws Exception {
    System.setProperties(new ThreadLocalProperties(System.getProperties()));
    if (!"1".equals(System.getProperty("nu.validator.servlet.read-local-log4j-properties"))) {
        PropertyConfigurator.configure(Main.class.getClassLoader().getResource("nu/validator/localentities/files/log4j.properties"));
    } else {
        PropertyConfigurator.configure(System.getProperty("nu.validator.servlet.log4j-properties", "log4j.properties"));
    }
    Logger log4j = Logger.getLogger(Main.class);
    ConsoleAppender console = new ConsoleAppender();
    console.setLayout(new PatternLayout("%m%n"));
    console.activateOptions();
    log4j.setAdditivity(false);
    log4j.addAppender(console);
    ServletContextHandler contextHandler = new ServletContextHandler();
    contextHandler.setContextPath("/");
    GzipHandler gzipHandler = new GzipHandler();
    gzipHandler.setIncludedMethods("POST", "GET");
    gzipHandler.setInflateBufferSize(2048);
    gzipHandler.setHandler(contextHandler);
    contextHandler.addFilter(new FilterHolder(new InboundSizeLimitFilter(SIZE_LIMIT)), "/*", EnumSet.of(DispatcherType.REQUEST));
    contextHandler.addFilter(new FilterHolder(new InboundGzipFilter()), "/*", EnumSet.of(DispatcherType.REQUEST));
    contextHandler.addFilter(new FilterHolder(new MultipartFormDataFilter()), "/*", EnumSet.of(DispatcherType.REQUEST));
    contextHandler.addServlet(new ServletHolder(new VerifierServlet()), "/*");
    Server server = new Server(new QueuedThreadPool(100));
    server.setHandler(contextHandler);
    server.setHandler(gzipHandler);
    ServerConnector serverConnector = new ServerConnector(server, new HttpConnectionFactory(new HttpConfiguration()));
    int port = args.length > 0 ? Integer.parseInt(args[0]) : 8888;
    serverConnector.setPort(port);
    serverConnector.setHost(BIND_ADDRESS);
    server.setConnectors(new Connector[] { serverConnector });
    int stopPort = -1;
    if (args.length > 1) {
        stopPort = Integer.parseInt(args[1]);
    }
    if (stopPort != -1) {
        try (Socket clientSocket = new Socket(InetAddress.getByName("127.0.0.1"), stopPort);
            InputStream in = clientSocket.getInputStream()) {
            in.read();
        } catch (ConnectException e) {
        }
        server.start();
        emitStartupMessage(log4j, serverConnector.getHost(), port);
        try (ServerSocket serverSocket = new ServerSocket(stopPort, 0, InetAddress.getByName("127.0.0.1"));
            Socket s = serverSocket.accept()) {
            server.stop();
            s.getOutputStream().close();
        }
    } else {
        server.start();
        emitStartupMessage(log4j, serverConnector.getHost(), port);
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PatternLayout(org.apache.log4j.PatternLayout) ServerSocket(java.net.ServerSocket) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) Logger(org.apache.log4j.Logger) ServerConnector(org.eclipse.jetty.server.ServerConnector) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) GzipHandler(org.eclipse.jetty.server.handler.gzip.GzipHandler) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) ConnectException(java.net.ConnectException)

Aggregations

FilterHolder (org.eclipse.jetty.servlet.FilterHolder)233 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)105 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)86 DispatcherType (javax.servlet.DispatcherType)45 Server (org.eclipse.jetty.server.Server)40 Test (org.junit.Test)31 Filter (javax.servlet.Filter)27 CountDownLatch (java.util.concurrent.CountDownLatch)24 HashMap (java.util.HashMap)22 IOException (java.io.IOException)20 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)20 Map (java.util.Map)19 ServerConnector (org.eclipse.jetty.server.ServerConnector)19 File (java.io.File)17 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)17 CrossOriginFilter (org.eclipse.jetty.servlets.CrossOriginFilter)16 ServletContainer (org.glassfish.jersey.servlet.ServletContainer)16 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)15 ServletException (javax.servlet.ServletException)12 ArrayList (java.util.ArrayList)11