private void start() throws IOException { HttpServer server = HttpServer.create(new InetSocketAddress(portNumber), queueSize); SessionRegistry sessionRegistry = new SessionRegistry(); SessionFilter sessionFilter = new SessionFilter(sessionRegistry, "/login"); AccessLogFilter accessLogFilter = new AccessLogFilter(); List<Filter> defaultFilters = Arrays.asList(new Filter[] { accessLogFilter, sessionFilter }); server.createContext("/", new StaticHandler()).getFilters().addAll(defaultFilters); server.createContext("/login", new LoginHandler(sessionRegistry, "/", "home")).getFilters().add(accessLogFilter); // no session filter or else infinite redirect loop! server.createContext("/search", new SearchHandler()).getFilters().addAll(defaultFilters); server.createContext("/language", new LanguageHandler()).getFilters().addAll(defaultFilters); final ExecutorService threadPool = Executors.newFixedThreadPool(poolSize); server.setExecutor(threadPool); server.start(); LOG.info(() -> "Server is listening on port " + portNumber); }
private void setSecurityFilter(String filterName) { try { Class c = Class.forName(filterName); securityFilter = (Filter) c.newInstance(); } catch (ClassCastException | ClassNotFoundException | InstantiationException | IllegalAccessException e) { e.printStackTrace(); securityFilter = new SecurityFilter(); } }
public Main() throws Exception { filters = new ArrayList<Filter>(); filters.add(new SuperDuperFilter()); try( InputStream in = Main.class.getResourceAsStream("/version.properties") ) //$NON-NLS-1$ { config = new ManagerConfig(new File(findInstall()), in); } }
@Override public Filter[] getFilters() { final Filter[] defaultFilters = super.getFilters(); if (isSslEnabled() && configuration.getBoolean("server.ssl.hsts.enabled", DEFAULT_SSL_HSTS_ENABLED)) { final Filter[] filters = Arrays.copyOf(defaultFilters, defaultFilters.length + 1); filters[filters.length - 1] = new HSTSResponseFilter(new ISupplier<Integer>() { @Override public Integer get() { return configuration.getInt("server.ssl.hsts.maxage", DEFAULT_SSL_HSTS_MAX_AGE); } }, new ISupplier<Boolean>() { @Override public Boolean get() { return configuration.getBoolean("server.ssl.hsts.includesubdomains", DEFAULT_SSL_HSTS_INCLUDESUBDOMAINS); } }, new ISupplier<Boolean>() { @Override public Boolean get() { return configuration.getBoolean("server.ssl.hsts.preload", DEFAULT_SSL_HSTS_PRELOAD); } }); return filters; } else { return defaultFilters; } }
protected Collection<HttpContext> createContexts() { final Filter[] filtersArray = createFilters(); final Collection<Filter> filtersList; if (filtersArray != null && filtersArray.length != 0) { filtersList = Arrays.asList(filtersArray); } else { filtersList = Collections.emptyList(); } final Collection<HttpContext> httpContexts = new ArrayList<HttpContext>(); final Authenticator authenticator = httpServerConfiguration.getAuthenticator(); final HttpPathHandler[] handlersArray = createHandlers(); if (handlersArray != null && handlersArray.length > 0) { for (final HttpPathHandler handler : handlersArray) { final HttpContext httpContext = server.createContext(handler.getPath(), handler); if (filtersArray != null && filtersArray.length != 0) { httpContext.getFilters().addAll(filtersList); } if (authenticator != null) { httpContext.setAuthenticator(authenticator); } httpContexts.add(httpContext); } } else { logger.log(Level.WARNING, JFaceMessages.get("msg.httpserver.configuration.handlers.none")); } return httpContexts; }
@Override protected void service(HttpExchange exchange) throws IOException { for (final Filter filter : exchange.getHttpContext().getFilters()) { logger.fine(filter.description()); } super.service(exchange); }
@Test public void shouldSetFiltersAndAuthenticatorsOnHttpContext() throws Exception { // setup Map<String, HttpHandler> contexts = new HashMap<String, HttpHandler>(); HttpHandler httpHandler = mock(HttpHandler.class); contexts.put("test1", httpHandler); Filter filter = mock(Filter.class); List<Filter> filters = new ArrayList<Filter>(); filters.add(filter); Authenticator authenticator = mock(Authenticator.class); simpleHttpServerFactoryBean.setContexts(contexts); simpleHttpServerFactoryBean.setFilters(filters); simpleHttpServerFactoryBean.setAuthenticator(authenticator); List<Filter> httpContextFilters = new ArrayList<Filter>(); HttpContext httpContext = mock(HttpContext.class); when(httpContext.getFilters()).thenReturn(httpContextFilters); when(httpServer.createContext("test1", httpHandler)).thenReturn(httpContext); // act simpleHttpServerFactoryBean.afterPropertiesSet(); // assert verify(httpContext).setAuthenticator(authenticator); assertThat(httpContextFilters.contains(filter), is(true)); }
@Override public void doFilter(HttpExchange exchange, Filter.Chain chain) throws IOException { parseGetParameters(exchange); parsePostParameters(exchange); chain.doFilter(exchange); }
@Override public List<Filter> getFilters() { return _filters; }
public ODataJerseyServer addHttpServerFilter(Filter filter) { httpServerFilters.add(filter); return this; }
public Filter getSecurityFilter() { return securityFilter; }
@Nullable Filter[] getFilters();
@Override public Filter[] getFilters() { return new Filter[] { new GzipRequestFilter(), new DateResponseFilter() }; }
/** * Register common {@link com.sun.net.httpserver.Filter Filters} to be * applied to all locally registered {@link #setContexts contexts}. */ public void setFilters(List<Filter> filters) { this.filters = filters; }
/** * Register common {@link com.sun.net.httpserver.Filter Filters} to be * applied to all detected {@link javax.jws.WebService} annotated beans. */ public void setFilters(List<Filter> filters) { this.filters = filters; }
/** * Creates {@link com.sun.net.httpserver.Filter Filter} objects. * * @return the array containing the filters. */ protected Filter[] createFilters() { return httpServerConfiguration.getFilters(); }
/** * Register common {@link com.sun.net.httpserver.Filter Filters} to be applied to all locally registered * {@link #setContexts contexts}. */ public void setFilters(List<Filter> aFilters) { this.filters = aFilters; }