Search in sources :

Example 6 with Filter

use of javax.servlet.Filter in project oxCore by GluuFederation.

the class AbstractCorsFilter method getServerCorsFilter.

public Filter getServerCorsFilter() {
    Filter resultFilter = null;
    for (String filterName : CORS_FILTERS) {
        try {
            Class<?> clazz = Class.forName(filterName);
            Constructor<?> cons = clazz.getDeclaredConstructor();
            resultFilter = (Filter) cons.newInstance();
            break;
        } catch (Exception ex) {
            log.trace(ex.getMessage(), ex);
        }
    }
    if (resultFilter == null) {
        log.error("Failed to prepare CORS filter");
    } else {
        log.debug("Prepared CORS filter: " + resultFilter);
    }
    return resultFilter;
}
Also used : Filter(javax.servlet.Filter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 7 with Filter

use of javax.servlet.Filter in project gerrit by GerritCodeReview.

the class JettyServer method makeContext.

private ContextHandler makeContext(final String contextPath, final JettyEnv env, final Config cfg) {
    final ServletContextHandler app = new ServletContextHandler();
    // This enables the use of sessions in Jetty, feature available
    // for Gerrit plug-ins to enable user-level sessions.
    //
    app.setSessionHandler(new SessionHandler());
    app.setErrorHandler(new HiddenErrorHandler());
    // This is the path we are accessed by clients within our domain.
    //
    app.setContextPath(contextPath);
    // HTTP front-end filters to be used as surrogate of Apache HTTP
    // reverse-proxy filtering.
    // It is meant to be used as simpler tiny deployment of custom-made
    // security enforcement (Security tokens, IP-based security filtering, others)
    String[] filterClassNames = cfg.getStringList("httpd", null, "filterClass");
    for (String filterClassName : filterClassNames) {
        try {
            @SuppressWarnings("unchecked") Class<? extends Filter> filterClass = (Class<? extends Filter>) Class.forName(filterClassName);
            Filter filter = env.webInjector.getInstance(filterClass);
            app.addFilter(new FilterHolder(filter), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
        } catch (Throwable e) {
            String errorMessage = "Unable to instantiate front-end HTTP Filter " + filterClassName;
            log.error(errorMessage, e);
            throw new IllegalArgumentException(errorMessage, e);
        }
    }
    // Perform the same binding as our web.xml would do, but instead
    // of using the listener to create the injector pass the one we
    // already have built.
    //
    GuiceFilter filter = env.webInjector.getInstance(GuiceFilter.class);
    app.addFilter(new FilterHolder(filter), "/*", EnumSet.of(DispatcherType.REQUEST, DispatcherType.ASYNC));
    app.addEventListener(new GuiceServletContextListener() {

        @Override
        protected Injector getInjector() {
            return env.webInjector;
        }
    });
    // Jetty requires at least one servlet be bound before it will
    // bother running the filter above. Since the filter has all
    // of our URLs except the static resources, the only servlet
    // we need to bind is the default static resource servlet from
    // the Jetty container.
    //
    final ServletHolder ds = app.addServlet(DefaultServlet.class, "/");
    ds.setInitParameter("dirAllowed", "false");
    ds.setInitParameter("redirectWelcome", "false");
    ds.setInitParameter("useFileMappedBuffer", "false");
    ds.setInitParameter("gzip", "true");
    app.setWelcomeFiles(new String[0]);
    return app;
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) GuiceFilter(com.google.inject.servlet.GuiceFilter) Filter(javax.servlet.Filter) GuiceFilter(com.google.inject.servlet.GuiceFilter) Injector(com.google.inject.Injector) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) GuiceServletContextListener(com.google.inject.servlet.GuiceServletContextListener)

Example 8 with Filter

use of javax.servlet.Filter in project lucene-solr by apache.

the class JettySolrRunner method stop.

/**
   * Stop the Jetty server
   *
   * @throws Exception if an error occurs on shutdown
   */
public void stop() throws Exception {
    // Do not let Jetty/Solr pollute the MDC for this thread
    Map<String, String> prevContext = MDC.getCopyOfContextMap();
    MDC.clear();
    try {
        Filter filter = dispatchFilter.getFilter();
        server.stop();
        if (server.getState().equals(Server.FAILED)) {
            filter.destroy();
            if (extraFilters != null) {
                for (FilterHolder f : extraFilters) {
                    f.getFilter().destroy();
                }
            }
        }
        server.join();
    } finally {
        if (prevContext != null) {
            MDC.setContextMap(prevContext);
        } else {
            MDC.clear();
        }
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) SolrDispatchFilter(org.apache.solr.servlet.SolrDispatchFilter) Filter(javax.servlet.Filter)

Example 9 with Filter

use of javax.servlet.Filter in project spring-framework by spring-projects.

the class DelegatingFilterProxy method doFilter.

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    // Lazily initialize the delegate if necessary.
    Filter delegateToUse = this.delegate;
    if (delegateToUse == null) {
        synchronized (this.delegateMonitor) {
            if (this.delegate == null) {
                WebApplicationContext wac = findWebApplicationContext();
                if (wac == null) {
                    throw new IllegalStateException("No WebApplicationContext found: " + "no ContextLoaderListener or DispatcherServlet registered?");
                }
                this.delegate = initDelegate(wac);
            }
            delegateToUse = this.delegate;
        }
    }
    // Let the delegate perform the actual doFilter operation.
    invokeDelegate(delegateToUse, request, response, filterChain);
}
Also used : Filter(javax.servlet.Filter) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 10 with Filter

use of javax.servlet.Filter in project spring-framework by spring-projects.

the class MockFilterChain method doFilter.

/**
	 * Invoke registered {@link Filter}s and/or {@link Servlet} also saving the
	 * request and response.
	 */
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
    Assert.notNull(request, "Request must not be null");
    Assert.notNull(response, "Response must not be null");
    Assert.state(this.request == null, "This FilterChain has already been called!");
    if (this.iterator == null) {
        this.iterator = this.filters.iterator();
    }
    if (this.iterator.hasNext()) {
        Filter nextFilter = this.iterator.next();
        nextFilter.doFilter(request, response, this);
    }
    this.request = request;
    this.response = response;
}
Also used : Filter(javax.servlet.Filter)

Aggregations

Filter (javax.servlet.Filter)185 FilterChain (javax.servlet.FilterChain)67 Test (org.junit.Test)57 HttpServletRequest (javax.servlet.http.HttpServletRequest)53 HttpServletResponse (javax.servlet.http.HttpServletResponse)44 ServletRequest (javax.servlet.ServletRequest)43 ServletResponse (javax.servlet.ServletResponse)43 ServletException (javax.servlet.ServletException)28 FilterConfig (javax.servlet.FilterConfig)25 IOException (java.io.IOException)23 ServletContext (javax.servlet.ServletContext)20 Injector (com.google.inject.Injector)17 FilterHolder (org.eclipse.jetty.servlet.FilterHolder)16 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 Map (java.util.Map)9 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)9 ArrayList (java.util.ArrayList)8 Hashtable (java.util.Hashtable)8 DispatcherType (javax.servlet.DispatcherType)8