Search in sources :

Example 96 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project ozone by apache.

the class HttpServer2 method addGlobalFilter.

@Override
public void addGlobalFilter(String name, String classname, Map<String, String> parameters) {
    FilterHolder filterHolder = getFilterHolder(name, classname, parameters);
    FilterMapping fmap = getFilterMapping(name, new String[] { "/*" });
    defineFilter(webAppContext, filterHolder, fmap);
    for (ServletContextHandler ctx : defaultContexts.keySet()) {
        defineFilter(ctx, filterHolder, fmap);
    }
    LOG.info("Added global filter '{}' (class={})", name, classname);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 97 with FilterHolder

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

the class NBWebServer method run.

public void run() {
    // new InetSocketAddress("")
    Server server = new Server(bindPort);
    handlers = new HandlerList();
    if (this.basePaths.size() == 0 && this.servletClasses.size() == 0) {
        logger.warn("No service endpoints or doc paths have been added. Loading dynamically.");
    }
    RewriteHandler rh = new RewriteHandler();
    // rh.addRule(new RedirectRegexRule("/","/docs/"));
    // rh.addRule(new RedirectPatternRule("/","/docs/"));
    handlers.addHandler(rh);
    // Favicon
    for (Path basePath : basePaths) {
        Path icon = basePath.resolve("/favicon.ico");
        if (Files.exists(icon)) {
            FavIconHandler favIconHandler = new FavIconHandler(basePaths.get(0) + "/favicon.ico", false);
            handlers.addHandler(favIconHandler);
            break;
        }
    }
    if (basePaths.size() == 0) {
        Docs docs = new Docs();
        // Load static path contexts which are published within the runtime.
        docs.merge(DocsysPathLoader.loadStaticPaths());
        // default static copy of the docs app
        if (!docs.getPathMap().containsKey("docsys-app")) {
            docs.merge(new DocsysDefaultAppPath().getDocs());
        }
        basePaths.addAll(docs.getPaths());
    }
    for (Path basePath : basePaths) {
        logger.info("Adding path to server: " + basePath.toString());
        ResourceHandler resourceHandler = new ResourceHandler();
        resourceHandler.setDirAllowed(true);
        resourceHandler.setAcceptRanges(true);
        resourceHandler.setWelcomeFiles(new String[] { "index.html" });
        resourceHandler.setRedirectWelcome(false);
        Resource baseResource = new PathResource(basePath);
        if (basePath.toUri().toString().startsWith("jar:")) {
            try {
                baseResource = JarResource.newResource(basePath.toUri());
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }
        resourceHandler.setBaseResource(baseResource);
        resourceHandler.setCacheControl("no-cache");
        handlers.addHandler(resourceHandler);
    }
    // ResourceConfig statusResourceCfg = new ResourceConfig(DocServerStatusEndpoint.class);
    // statusResourceCfg.property("server", this);
    // ServletContainer statusResourceContainer = new ServletContainer(statusResourceCfg);
    // ServletHolder statusResourceServletHolder = new ServletHolder(statusResourceContainer);
    // getContextHandler().addServlet(statusResourceServletHolder, "/_");
    logger.info("adding " + servletClasses.size() + " context handlers...");
    loadDynamicEndpoints();
    ResourceConfig rc = new ResourceConfig();
    rc.addProperties(contextParams);
    rc.property("server", this);
    ServletContainer container = new ServletContainer(rc);
    ServletHolder servlets = new ServletHolder(container);
    String classnames = this.servletClasses.stream().map(Class::getCanonicalName).collect(Collectors.joining(","));
    rc.property(ServerProperties.PROVIDER_CLASSNAMES, classnames);
    // servlets.setInitParameter(ServerProperties.PROVIDER_CLASSNAMES,
    // classnames
    // );
    ServletContextHandler sch = new ServletContextHandler();
    sch.setContextPath("/*");
    sch.addServlet(servlets, "/*");
    FilterHolder filter = new FilterHolder();
    filter.setInitParameter("allowedOrigins", "*");
    filter.setInitParameter("allowedMethods", "POST,GET,OPTIONS,PUT,DELETE,HEAD");
    filter.setInitParameter("allowedHeaders", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
    filter.setInitParameter("preflightMaxAge", "1800");
    filter.setInitParameter("allowCredentials", "true");
    CrossOriginFilter corsFilter = new CrossOriginFilter();
    filter.setFilter(corsFilter);
    FilterMapping filterMapping = new FilterMapping();
    filterMapping.setDispatcherTypes(EnumSet.of(DispatcherType.REQUEST));
    filterMapping.setPathSpec("/*");
    filterMapping.setServletName("cross-origin");
    sch.addFilter(filter, "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    handlers.addHandler(sch);
    // Show contexts
    DefaultHandler defaultHandler = new DefaultHandler();
    defaultHandler.setShowContexts(true);
    defaultHandler.setServeIcon(false);
    handlers.addHandler(defaultHandler);
    // FilterMapping corsMapping = new FilterMapping();
    // corsMapping.set
    // 
    // ServletHandler CorsHandler = new ServletHandler();
    // CorsHandler.setH
    server.setHandler(handlers);
    for (Connector connector : server.getConnectors()) {
        if (connector instanceof AbstractConnector) {
            logger.debug("Setting idle timeout for " + connector + " to 300,000ms");
            ((AbstractConnector) connector).setIdleTimeout(300000);
        }
    }
    try {
        List<Connector> connectors = new ArrayList<>();
        if (bindScheme.equals("http")) {
            ServerConnector sc = new ServerConnector(server);
            sc.setPort(bindPort);
            sc.setHost(bindHost);
            // sc.setDefaultProtocol(bindScheme);
            connectors.add(sc);
        } else if (bindScheme.equals("https")) {
            SslContextFactory.Server server1 = new SslContextFactory.Server();
            ServerConnector sc = new ServerConnector(server, server1);
            sc.setPort(bindPort);
            sc.setHost(bindHost);
            // sc.setDefaultProtocol(bindScheme);
            connectors.add(sc);
        }
        server.setConnectors(connectors.toArray(new Connector[0]));
        server.start();
        System.out.println("Started documentation server at " + bindScheme + "://" + bindHost + ":" + bindPort + "/");
        if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
            System.out.println("Browsing to documentation server at " + bindScheme + "://" + bindHost + ":" + bindPort + "/");
            Desktop.getDesktop().browse(new URI(bindScheme + "://" + bindHost + ":" + bindPort + "/"));
            System.out.println("If the docs app did not open automatically in your browser, open to the the url above.");
        }
        server.join();
    } catch (Exception e) {
        logger.error("error while starting doc server", e);
        e.printStackTrace(System.out);
        System.exit(2);
    }
}
Also used : HandlerList(org.eclipse.jetty.server.handler.HandlerList) MalformedURLException(java.net.MalformedURLException) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) DocsysDefaultAppPath(io.nosqlbench.docsys.DocsysDefaultAppPath) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) URI(java.net.URI) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) RewriteHandler(org.eclipse.jetty.rewrite.handler.RewriteHandler) Path(java.nio.file.Path) DocsysDefaultAppPath(io.nosqlbench.docsys.DocsysDefaultAppPath) PathResource(org.eclipse.jetty.util.resource.PathResource) JarResource(org.eclipse.jetty.util.resource.JarResource) Resource(org.eclipse.jetty.util.resource.Resource) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) FavIconHandler(io.nosqlbench.docsys.handlers.FavIconHandler) MalformedURLException(java.net.MalformedURLException) DefaultHandler(org.eclipse.jetty.server.handler.DefaultHandler) PathResource(org.eclipse.jetty.util.resource.PathResource) ServletContainer(org.glassfish.jersey.servlet.ServletContainer) Docs(io.nosqlbench.docapi.Docs) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

Example 98 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project hive by apache.

the class HttpServer method setupXframeFilter.

private void setupXframeFilter(Builder b, Map<String, String> params) {
    FilterHolder holder = new FilterHolder();
    holder.setClassName(QuotingInputFilter.class.getName());
    holder.setInitParameters(params);
    ServletHandler handler = webAppContext.getServletHandler();
    handler.addFilterWithMapping(holder, "/*", FilterMapping.ALL);
}
Also used : ServletHandler(org.eclipse.jetty.servlet.ServletHandler) FilterHolder(org.eclipse.jetty.servlet.FilterHolder)

Example 99 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jena by apache.

the class ExFusekiMain_2_AddShiro method addShiroFilter.

// From Barry Nouwt : https://lists.apache.org/thread.html/r1e3fa952ff9f4a9108e16f07f1edf78c67e08c9b081497c627e3b833%40%3Cusers.jena.apache.org%3E
public static void addShiroFilter(FusekiServer fusekiServer) {
    Server jettyServer = fusekiServer.getJettyServer();
    ServletContextHandler servletContextHandler = (ServletContextHandler) jettyServer.getHandler();
    ServletHandler servletHandler = servletContextHandler.getServletHandler();
    // for shiro
    EnvironmentLoaderListener ell = new EnvironmentLoaderListener();
    servletContextHandler.addEventListener(ell);
    // Copies
    List<FilterMapping> mappings = new ArrayList<FilterMapping>(Arrays.asList(servletHandler.getFilterMappings()));
    List<FilterHolder> holders = new ArrayList<FilterHolder>(Arrays.asList(servletHandler.getFilters()));
    {
        // add Shiro Filter and mapping
        FilterHolder holder1 = new FilterHolder();
        holder1.setFilter(new ShiroFilter());
        FilterMapping mapping1 = new FilterMapping();
        mapping1.setFilterName(holder1.getName());
        mapping1.setPathSpec("/*");
        mapping1.setDispatcherTypes(EnumSet.allOf(DispatcherType.class));
        mappings.add(0, mapping1);
        holders.add(0, holder1);
    }
    FilterMapping[] mappings3 = new FilterMapping[mappings.size()];
    mappings3 = mappings.toArray(mappings3);
    FilterHolder[] holders3 = new FilterHolder[holders.size()];
    holders3 = holders.toArray(holders3);
    servletHandler.setFilters(holders3);
    servletHandler.setFilterMappings(mappings3);
    // Specify the Session ID Manager
    SessionIdManager idmanager = new DefaultSessionIdManager(jettyServer);
    jettyServer.setSessionIdManager(idmanager);
    // Specify the session handler
    SessionHandler sessionsHandler = new SessionHandler();
    sessionsHandler.setUsingCookies(false);
    servletHandler.setHandler(sessionsHandler);
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) ServletHandler(org.eclipse.jetty.servlet.ServletHandler) EnvironmentLoaderListener(org.apache.shiro.web.env.EnvironmentLoaderListener) DefaultSessionIdManager(org.eclipse.jetty.server.session.DefaultSessionIdManager) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) FusekiServer(org.apache.jena.fuseki.main.FusekiServer) Server(org.eclipse.jetty.server.Server) ArrayList(java.util.ArrayList) FilterMapping(org.eclipse.jetty.servlet.FilterMapping) SessionIdManager(org.eclipse.jetty.server.SessionIdManager) DefaultSessionIdManager(org.eclipse.jetty.server.session.DefaultSessionIdManager) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) ShiroFilter(org.apache.shiro.web.servlet.ShiroFilter)

Example 100 with FilterHolder

use of org.eclipse.jetty.servlet.FilterHolder in project jena by apache.

the class ExFusekiMain_1_Servlet_AddFilter method replacement.

/**
 * Build a replacement Filter
 */
private static FilterHolder replacement(FilterHolder fh) {
    final Filter fx = (fh.getFilter() == null) ? new FusekiFilter() : fh.getFilter();
    Filter filter2 = new Filter() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            // Example: print the Principal
            Principal user = ((HttpServletRequest) request).getUserPrincipal();
            System.out.println("Request User: " + user);
            fx.doFilter(request, response, chain);
        }

        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            fx.init(filterConfig);
        }

        @Override
        public void destroy() {
            fx.destroy();
        }
    };
    FilterHolder fh2 = new FilterHolder(filter2);
    fh2.setClassName(filter2.getClass().getName());
    fh2.setName(fh.getName());
    // fh2.initialize();
    return fh2;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) FusekiFilter(org.apache.jena.fuseki.servlets.FusekiFilter) FusekiFilter(org.apache.jena.fuseki.servlets.FusekiFilter) Principal(java.security.Principal)

Aggregations

FilterHolder (org.eclipse.jetty.servlet.FilterHolder)181 ServletHolder (org.eclipse.jetty.servlet.ServletHolder)78 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)57 DispatcherType (javax.servlet.DispatcherType)31 Test (org.junit.Test)29 Server (org.eclipse.jetty.server.Server)25 CountDownLatch (java.util.concurrent.CountDownLatch)23 HashMap (java.util.HashMap)21 Filter (javax.servlet.Filter)21 Map (java.util.Map)17 File (java.io.File)15 FilterMapping (org.eclipse.jetty.servlet.FilterMapping)15 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)13 CrossOriginFilter (org.eclipse.jetty.servlets.CrossOriginFilter)12 IOException (java.io.IOException)11 ServletTester (org.eclipse.jetty.servlet.ServletTester)11 ServerConnector (org.eclipse.jetty.server.ServerConnector)10 GzipHandler (org.eclipse.jetty.server.handler.gzip.GzipHandler)10 ServletHandler (org.eclipse.jetty.servlet.ServletHandler)10 FilterConfig (javax.servlet.FilterConfig)9