/** * Setup cross-origin requests (CORS) filter. * @param b - builder */ private void setupCORSFilter(Builder b) { FilterHolder holder = new FilterHolder(); holder.setClassName(CrossOriginFilter.class.getName()); Map<String, String> params = new HashMap<>(); params.put(CrossOriginFilter.ALLOWED_ORIGINS, b.allowedOrigins); params.put(CrossOriginFilter.ALLOWED_METHODS, b.allowedMethods); params.put(CrossOriginFilter.ALLOWED_HEADERS, b.allowedHeaders); holder.setInitParameters(params); ServletHandler handler = webAppContext.getServletHandler(); handler.addFilterWithMapping(holder, "/*", FilterMapping.ALL); }
/** * Secure the web server with kerberos (AuthenticationFilter). */ void setupSpnegoFilter(Builder b) throws IOException { Map<String, String> params = new HashMap<String, String>(); params.put("kerberos.principal", SecurityUtil.getServerPrincipal(b.spnegoPrincipal, b.host)); params.put("kerberos.keytab", b.spnegoKeytab); params.put(AuthenticationFilter.AUTH_TYPE, "kerberos"); FilterHolder holder = new FilterHolder(); holder.setClassName(AuthenticationFilter.class.getName()); holder.setInitParameters(params); ServletHandler handler = webAppContext.getServletHandler(); handler.addFilterWithMapping( holder, "/*", FilterMapping.ALL); }
private static Server startHttp1() throws Exception { final Server server = new Server(0); final ServletHandler handler = new ServletHandler(); handler.addServletWithMapping(newServletHolder(thriftServlet), TSERVLET_PATH); handler.addServletWithMapping(newServletHolder(rootServlet), "/"); handler.addFilterWithMapping(new FilterHolder(new ConnectionCloseFilter()), "/*", EnumSet.of(DispatcherType.REQUEST)); server.setHandler(handler); for (Connector c : server.getConnectors()) { for (ConnectionFactory f : c.getConnectionFactories()) { for (String p : f.getProtocols()) { if (p.startsWith("h2c")) { fail("Attempted to create a Jetty server without HTTP/2 support, but failed: " + f.getProtocols()); } } } } server.start(); return server; }
handler.addFilterWithMapping(HelloPrintingFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
/** Convenience method to add a filter with a mapping * @param className * @param pathSpec * @param dispatches * @return the filter holder created * @deprecated use {@link #addFilterWithMapping(Class, String, EnumSet<DispatcherType>)} instead */ public FilterHolder addFilter (String className,String pathSpec,EnumSet<DispatcherType> dispatches) { return addFilterWithMapping(className, pathSpec, dispatches); }
/** Convenience method to add a filter with a mapping * @param className * @param pathSpec * @param dispatches * @return the filter holder created * @deprecated use {@link #addFilterWithMapping(Class, String, EnumSet<DispatcherType>)} instead */ public FilterHolder addFilter (String className,String pathSpec,EnumSet<DispatcherType> dispatches) { return addFilterWithMapping(className, pathSpec, dispatches); }
/** Convenience method to add a filter with a mapping * @param className * @param pathSpec * @param dispatches * @return the filter holder created * @deprecated use {@link #addFilterWithMapping(Class, String, EnumSet<DispatcherType>)} instead */ public FilterHolder addFilter (String className,String pathSpec,EnumSet<DispatcherType> dispatches) { return addFilterWithMapping(className, pathSpec, dispatches); }
/** convenience method to add a filter */ public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,EnumSet<DispatcherType> dispatches) { return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches); }
/** convenience method to add a filter */ public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,EnumSet<DispatcherType> dispatches) { return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches); }
/** convenience method to add a filter */ public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,EnumSet<DispatcherType> dispatches) { return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches); }
/** Convenience method to add a filter * @param holder the filter holder * @param pathSpec the path spec * @param dispatches the dispatcher types for this filter */ public void addFilter(FilterHolder holder,String pathSpec,EnumSet<DispatcherType> dispatches) { getServletHandler().addFilterWithMapping(holder,pathSpec,dispatches); }
/** convenience method to add a filter */ public FilterHolder addFilter(String filterClass,String pathSpec,EnumSet<DispatcherType> dispatches) { return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches); }
/** convenience method to add a filter */ public FilterHolder addFilter(String filterClass,String pathSpec,EnumSet<DispatcherType> dispatches) { return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches); }
/** Convenience method to add a filter * @param filterClass the filter class * @param pathSpec the path spec * @param dispatches the dispatcher types for this filter * @return the FilterHolder that was created */ public FilterHolder addFilter(Class<? extends Filter> filterClass,String pathSpec,EnumSet<DispatcherType> dispatches) { return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches); }
/** convenience method to add a filter */ public FilterHolder addFilter(String filterClass,String pathSpec,EnumSet<DispatcherType> dispatches) { return getServletHandler().addFilterWithMapping(filterClass,pathSpec,dispatches); }
/** conveniance method to add a filter */ public void addFilter(FilterHolder holder,String pathSpec,EnumSet<DispatcherType> dispatches) { getServletHandler().addFilterWithMapping(holder,pathSpec,dispatches); }
/** Convenience method to add a filter. * @param filter class of filter to create * @param pathSpec filter mappings for filter * @param dispatches see {@link FilterMapping#setDispatches(int)} * @return The filter holder. */ public FilterHolder addFilterWithMapping (Class<? extends Filter> filter,String pathSpec,EnumSet<DispatcherType> dispatches) { FilterHolder holder = newFilterHolder(Holder.Source.EMBEDDED); holder.setHeldClass(filter); addFilterWithMapping(holder,pathSpec,dispatches); return holder; }
/** Convenience method to add a filter. * @param filter class of filter to create * @param pathSpec filter mappings for filter * @param dispatches see {@link FilterMapping#setDispatches(int)} * @return The filter holder. */ public FilterHolder addFilterWithMapping (Class<? extends Filter> filter,String pathSpec,int dispatches) { FilterHolder holder = newFilterHolder(Holder.Source.EMBEDDED); holder.setHeldClass(filter); addFilterWithMapping(holder,pathSpec,dispatches); return holder; }
private ServletHandler buildServletHandler(Configuration configuration) { ServletHandler servletHandler = new ServletHandler(); BiConsumer<String, ServletHolder> addServletMapping = (path, servletHolder) -> servletHandler.addServletWithMapping(servletHolder, path); BiConsumer<String, Collection<FilterHolder>> addFilterMappings = (path, filterHolders) -> filterHolders.forEach( filterHolder -> servletHandler.addFilterWithMapping(filterHolder, path, EnumSet.of(DispatcherType.REQUEST))); Maps.transformEntries(configuration.getMappings(), this::toServletHolder).forEach(addServletMapping); Multimaps.transformEntries(configuration.getFilters(), this::toFilterHolder).asMap().forEach(addFilterMappings); return servletHandler; }
private ServletHandler buildServletHandler(Configuration configuration) { ServletHandler servletHandler = new ServletHandler(); BiConsumer<String, ServletHolder> addServletMapping = (path, servletHolder) -> servletHandler.addServletWithMapping(servletHolder, path); BiConsumer<String, Collection<FilterHolder>> addFilterMappings = (path, filterHolders) -> filterHolders.forEach( filterHolder -> servletHandler.addFilterWithMapping(filterHolder, path, EnumSet.of(DispatcherType.REQUEST))); Maps.transformEntries(configuration.getMappings(), this::toServletHolder).forEach(addServletMapping); Multimaps.transformEntries(configuration.getFilters(), this::toFilterHolder).asMap().forEach(addFilterMappings); return servletHandler; }