Search in sources :

Example 81 with Filter

use of javax.servlet.Filter in project druid by druid-io.

the class TrustedDomainAuthenticatorTest method testForwardedForTrustedHost.

@Test
public void testForwardedForTrustedHost() throws IOException, ServletException {
    Authenticator authenticator = new TrustedDomainAuthenticator("test-authenticator", "test.com", true, "my-auth", "myUser");
    HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(req.getRemoteAddr()).andReturn("serverA.test2.com");
    EasyMock.expect(req.getHeader("X-Forwarded-For")).andReturn("serverA.test.com");
    req.setAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT, new AuthenticationResult("myUser", "my-auth", "test-authenticator", null));
    EasyMock.expectLastCall().times(1);
    EasyMock.replay(req);
    HttpServletResponse resp = EasyMock.createMock(HttpServletResponse.class);
    EasyMock.replay(resp);
    FilterChain filterChain = EasyMock.createMock(FilterChain.class);
    filterChain.doFilter(req, resp);
    EasyMock.expectLastCall().times(1);
    EasyMock.replay(filterChain);
    Filter authenticatorFilter = authenticator.getFilter();
    authenticatorFilter.doFilter(req, resp, filterChain);
    EasyMock.verify(req, resp, filterChain);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 82 with Filter

use of javax.servlet.Filter in project roboguice by roboguice.

the class FilterDefinitionTest method testFilterInitAndConfig.

public final void testFilterInitAndConfig() throws ServletException {
    Injector injector = createMock(Injector.class);
    Binding binding = createMock(Binding.class);
    final MockFilter mockFilter = new MockFilter();
    expect(binding.acceptScopingVisitor((BindingScopingVisitor) anyObject())).andReturn(true);
    expect(injector.getBinding(Key.get(Filter.class))).andReturn(binding);
    expect(injector.getInstance(Key.get(Filter.class))).andReturn(mockFilter).anyTimes();
    replay(binding, injector);
    // some init params
    // noinspection SSBasedInspection
    final Map<String, String> initParams = new ImmutableMap.Builder<String, String>().put("ahsd", "asdas24dok").put("ahssd", "asdasd124ok").build();
    ServletContext servletContext = createMock(ServletContext.class);
    final String contextName = "thing__!@@44";
    expect(servletContext.getServletContextName()).andReturn(contextName);
    replay(servletContext);
    String pattern = "/*";
    final FilterDefinition filterDef = new FilterDefinition(pattern, Key.get(Filter.class), UriPatternType.get(UriPatternType.SERVLET, pattern), initParams, null);
    filterDef.init(servletContext, injector, Sets.<Filter>newIdentityHashSet());
    assertTrue(filterDef.getFilter() instanceof MockFilter);
    final FilterConfig filterConfig = mockFilter.getConfig();
    assertTrue(null != filterConfig);
    assertEquals(filterConfig.getServletContext().getServletContextName(), contextName);
    assertEquals(filterConfig.getFilterName(), Key.get(Filter.class).toString());
    final Enumeration names = filterConfig.getInitParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        assertTrue(initParams.containsKey(name));
        assertEquals(filterConfig.getInitParameter(name), initParams.get(name));
    }
    verify(binding, injector, servletContext);
}
Also used : Binding(com.google.inject.Binding) Enumeration(java.util.Enumeration) Filter(javax.servlet.Filter) Injector(com.google.inject.Injector) ServletContext(javax.servlet.ServletContext) FilterConfig(javax.servlet.FilterConfig) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 83 with Filter

use of javax.servlet.Filter in project roboguice by roboguice.

the class FilterChainInvocation method doFilter.

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
    GuiceFilter.Context previous = GuiceFilter.localContext.get();
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    HttpServletRequest originalRequest = (previous != null) ? previous.getOriginalRequest() : request;
    GuiceFilter.localContext.set(new GuiceFilter.Context(originalRequest, request, response));
    try {
        Filter filter = findNextFilter(request);
        if (filter != null) {
            // call to the filter, which can either consume the request or
            // recurse back into this method. (in which case we will go to find the next filter,
            // or dispatch to the servlet if no more filters are left)
            filter.doFilter(servletRequest, servletResponse, this);
        } else {
            // we've reached the end of the filterchain, let's try to dispatch to a servlet
            final boolean serviced = servletPipeline.service(servletRequest, servletResponse);
            // dispatch to the normal filter chain only if one of our servlets did not match
            if (!serviced) {
                proceedingChain.doFilter(servletRequest, servletResponse);
            }
        }
    } catch (Throwable t) {
        // want to iterate through the stack elements for every filter.
        if (!cleanedStacks) {
            cleanedStacks = true;
            pruneStacktrace(t);
        }
        Throwables.propagateIfInstanceOf(t, ServletException.class);
        Throwables.propagateIfInstanceOf(t, IOException.class);
        throw Throwables.propagate(t);
    } finally {
        GuiceFilter.localContext.set(previous);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Filter(javax.servlet.Filter) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 84 with Filter

use of javax.servlet.Filter in project undertow by undertow-io.

the class ManagedFilter method createFilter.

public void createFilter() throws ServletException {
    synchronized (this) {
        if (filter == null) {
            try {
                handle = filterInfo.getInstanceFactory().createInstance();
            } catch (Exception e) {
                throw UndertowServletMessages.MESSAGES.couldNotInstantiateComponent(filterInfo.getName(), e);
            }
            Filter filter = handle.getInstance();
            new LifecyleInterceptorInvocation(servletContext.getDeployment().getDeploymentInfo().getLifecycleInterceptors(), filterInfo, filter, new FilterConfigImpl(filterInfo, servletContext)).proceed();
            this.filter = filter;
        }
    }
}
Also used : FilterConfigImpl(io.undertow.servlet.spec.FilterConfigImpl) Filter(javax.servlet.Filter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 85 with Filter

use of javax.servlet.Filter in project stanbol by apache.

the class SolrServerPublishingComponent method updateFilter.

/**
 * A change was made to the tracked CoreContainer (adding ,removal, ranking change).
 * This removes and re-add the Servlet filter to apply such changes.
 * @param name The name of the filter to be updated
 * @param ref The serviceReference for the new CoreContainer to be added for
 * the parsed name. <code>null</code> if the filter for that name needs only
 * to be removed
 * @param server The {@link CoreContainer} may be parsed in cases a reference
 * is already available. If not the {@link #tracker} is used to look it up
 * based on the parsed reference. This is basically a workaround for the
 * fact that if the call originates form
 * {@link ServiceTrackerCustomizer#addingService(ServiceReference)} the
 * {@link CoreContainer} is not yet available via the tracker.
 */
protected void updateFilter(String name, ServiceReference ref, CoreContainer server) {
    String serverPrefix = gloablPrefix + name;
    Filter filter = published.remove(name);
    if (filter != null) {
        extHttpService.unregisterFilter(filter);
        filter = null;
        log.info("removed ServletFilter for SolrServer {} and prefix {}", name, serverPrefix);
    }
    // else no current filter for that name
    if (ref != null || server != null) {
        if (server == null) {
            server = (CoreContainer) tracker.getService(ref);
        }
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(CoreContainer.class.getClassLoader());
        try {
            filter = new SolrFilter(server);
        } finally {
            Thread.currentThread().setContextClassLoader(classLoader);
        }
        Dictionary<String, Object> filterPrpoerties = new Hashtable<String, Object>();
        filterPrpoerties.put("path-prefix", serverPrefix);
        String filterPrefix = serverPrefix + "/.*";
        try {
            extHttpService.registerFilter(filter, filterPrefix, filterPrpoerties, 0, null);
        } catch (ServletException e) {
            throw new IllegalStateException("Unable to register SolrDispatchFilter for" + "CoreContainer with name" + name + " (prefix: " + filterPrefix + "| properties: " + filterPrpoerties + ").", e);
        }
        log.info("added ServletFilter for SolrServer {} and prefix {}", name, serverPrefix);
    }
// else no new filter to add
}
Also used : ServletException(javax.servlet.ServletException) CoreContainer(org.apache.solr.core.CoreContainer) SolrDispatchFilter(org.apache.solr.servlet.SolrDispatchFilter) DelegatingSolrDispatchFilter(org.apache.stanbol.commons.solr.web.dispatch.DelegatingSolrDispatchFilter) Filter(javax.servlet.Filter) Hashtable(java.util.Hashtable)

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