Search in sources :

Example 21 with Filter

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

the class AbstractFilterRegistrationBean method onStartup.

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    Filter filter = getFilter();
    Assert.notNull(filter, "Filter must not be null");
    String name = getOrDeduceName(filter);
    if (!isEnabled()) {
        this.logger.info("Filter " + name + " was not registered (disabled)");
        return;
    }
    FilterRegistration.Dynamic added = servletContext.addFilter(name, filter);
    if (added == null) {
        this.logger.info("Filter " + name + " was not registered " + "(possibly already registered?)");
        return;
    }
    configure(added);
}
Also used : Filter(javax.servlet.Filter) FilterRegistration(javax.servlet.FilterRegistration)

Example 22 with Filter

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

the class TestHostnameFilter method testMissingHostname.

@Test
public void testMissingHostname() throws Exception {
    ServletRequest request = Mockito.mock(ServletRequest.class);
    Mockito.when(request.getRemoteAddr()).thenReturn(null);
    ServletResponse response = Mockito.mock(ServletResponse.class);
    final AtomicBoolean invoked = new AtomicBoolean();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertTrue(HostnameFilter.get().contains("???"));
            invoked.set(true);
        }
    };
    Filter filter = new HostnameFilter();
    filter.init(null);
    assertNull(HostnameFilter.get());
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    assertNull(HostnameFilter.get());
    filter.destroy();
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) Test(org.junit.Test)

Example 23 with Filter

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

the class TestMDCFilter method mdc.

@Test
public void mdc() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.when(request.getUserPrincipal()).thenReturn(null);
    Mockito.when(request.getMethod()).thenReturn("METHOD");
    Mockito.when(request.getPathInfo()).thenReturn("/pathinfo");
    ServletResponse response = Mockito.mock(ServletResponse.class);
    final AtomicBoolean invoked = new AtomicBoolean();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), null);
            assertEquals(MDC.get("user"), null);
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    MDC.clear();
    Filter filter = new MDCFilter();
    filter.init(null);
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    assertNull(MDC.get("hostname"));
    assertNull(MDC.get("user"));
    assertNull(MDC.get("method"));
    assertNull(MDC.get("path"));
    Mockito.when(request.getUserPrincipal()).thenReturn(new Principal() {

        @Override
        public String getName() {
            return "name";
        }
    });
    invoked.set(false);
    chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), null);
            assertEquals(MDC.get("user"), "name");
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    HostnameFilter.HOSTNAME_TL.set("HOST");
    invoked.set(false);
    chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, ServletException {
            assertEquals(MDC.get("hostname"), "HOST");
            assertEquals(MDC.get("user"), "name");
            assertEquals(MDC.get("method"), "METHOD");
            assertEquals(MDC.get("path"), "/pathinfo");
            invoked.set(true);
        }
    };
    filter.doFilter(request, response, chain);
    assertTrue(invoked.get());
    HostnameFilter.HOSTNAME_TL.remove();
    filter.destroy();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletResponse(javax.servlet.ServletResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) IOException(java.io.IOException) Principal(java.security.Principal) Test(org.junit.Test)

Example 24 with Filter

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

the class JettyHttpComponent method enableMultipartFilter.

private void enableMultipartFilter(HttpCommonEndpoint endpoint, Server server, String connectorKey) throws Exception {
    ServletContextHandler context = server.getChildHandlerByClass(ServletContextHandler.class);
    CamelContext camelContext = this.getCamelContext();
    FilterHolder filterHolder = new FilterHolder();
    filterHolder.setInitParameter("deleteFiles", "true");
    if (ObjectHelper.isNotEmpty(camelContext.getProperty(TMP_DIR))) {
        File file = new File(camelContext.getProperty(TMP_DIR));
        if (!file.isDirectory()) {
            throw new RuntimeCamelException("The temp file directory of camel-jetty is not exists, please recheck it with directory name :" + camelContext.getProperties().get(TMP_DIR));
        }
        context.setAttribute("javax.servlet.context.tempdir", file);
    }
    // if a filter ref was provided, use it.
    Filter filter = ((JettyHttpEndpoint) endpoint).getMultipartFilter();
    if (filter == null) {
        // if no filter ref was provided, use the default filter
        filter = new MultiPartFilter();
    }
    filterHolder.setFilter(new CamelFilterWrapper(filter));
    String pathSpec = endpoint.getPath();
    if (pathSpec == null || "".equals(pathSpec)) {
        pathSpec = "/";
    }
    if (endpoint.isMatchOnUriPrefix()) {
        pathSpec = pathSpec.endsWith("/") ? pathSpec + "*" : pathSpec + "/*";
    }
    addFilter(context, filterHolder, pathSpec);
    LOG.debug("using multipart filter implementation " + filter.getClass().getName() + " for path " + pathSpec);
}
Also used : CamelContext(org.apache.camel.CamelContext) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) MultiPartFilter(org.eclipse.jetty.servlets.MultiPartFilter) Filter(javax.servlet.Filter) MultiPartFilter(org.eclipse.jetty.servlets.MultiPartFilter) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) File(java.io.File)

Example 25 with Filter

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

the class JettyHttpComponent method setFilters.

private void setFilters(JettyHttpEndpoint endpoint, Server server, String connectorKey) {
    ServletContextHandler context = server.getChildHandlerByClass(ServletContextHandler.class);
    List<Filter> filters = endpoint.getFilters();
    for (Filter filter : filters) {
        FilterHolder filterHolder = new FilterHolder();
        if (endpoint.getFilterInitParameters() != null) {
            filterHolder.setInitParameters(endpoint.getFilterInitParameters());
        }
        filterHolder.setFilter(new CamelFilterWrapper(filter));
        String pathSpec = endpoint.getPath();
        if (pathSpec == null || "".equals(pathSpec)) {
            pathSpec = "/";
        }
        if (endpoint.isMatchOnUriPrefix()) {
            pathSpec = pathSpec.endsWith("/") ? pathSpec + "*" : pathSpec + "/*";
        }
        addFilter(context, filterHolder, pathSpec);
    }
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Filter(javax.servlet.Filter) MultiPartFilter(org.eclipse.jetty.servlets.MultiPartFilter) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler)

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