Search in sources :

Example 36 with Filter

use of javax.servlet.Filter in project ddf by codice.

the class ProxyFilter method destroy.

/**
     * @see javax.servlet.Filter#destroy()
     */
public void destroy() {
    for (Filter filter : filters) {
        LOGGER.debug("Destroying filter {}.", filter.getClass().getName());
        filter.destroy();
    }
}
Also used : Filter(javax.servlet.Filter)

Example 37 with Filter

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

the class LogTracerTest method checkTracing.

@Test
public void checkTracing() throws Exception {
    HttpServletRequest request = mock(SlingHttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter(LogTracer.PARAM_TRACER_CONFIG)).thenReturn("a.b.c;level=trace,a.b;level=debug");
    activateTracer();
    Level oldLevel = rootLogger().getLevel();
    rootLogger().setLevel(Level.INFO);
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            TurboFilter turboFilter = context.getService(TurboFilter.class);
            assertNotNull(turboFilter);
            getLogContext().addTurboFilter(turboFilter);
            getLogger("a").info("a-info");
            getLogger("a").debug("a-debug");
            getLogger("a.b").info("a.b-info");
            getLogger("a.b").debug("a.b-debug");
            getLogger("a.b").trace("a.b-trace");
            getLogger("a.b.c").debug("a.b.c-debug");
            getLogger("a.b.c").trace("a.b.c-trace");
            getLogger("a.b.c.d").trace("a.b.c.d-trace");
            if (getLogger("a.b.c").isTraceEnabled()) {
                getLogger("a.b.c").trace("a.b.c-trace2");
            }
        }
    };
    Filter filter = getFilter(true);
    filter.doFilter(request, response, chain);
    assertNull(context.getService(TurboFilter.class));
    List<String> logs = logCollector.getLogs();
    assertThat(logs, hasItem("a-info"));
    assertThat(logs, not(hasItem("a-debug")));
    assertThat(logs, hasItem("a.b-info"));
    assertThat(logs, hasItem("a.b-debug"));
    assertThat(logs, not(hasItem("a.b-trace")));
    assertThat(logs, hasItem("a.b.c-debug"));
    assertThat(logs, hasItem("a.b.c-trace"));
    assertThat(logs, hasItem("a.b.c-trace2"));
    assertThat(logs, hasItem("a.b.c.d-trace"));
    rootLogger().setLevel(oldLevel);
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Level(ch.qos.logback.classic.Level) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 38 with Filter

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

the class LogTracerTest method prepareChain.

private FilterChain prepareChain(FilterChain end) throws InvalidSyntaxException {
    Filter servletFilter = getFilter(false);
    Filter slingFilter = getFilter(true);
    return new FilterChainImpl(end, servletFilter, slingFilter);
}
Also used : TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter)

Example 39 with Filter

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

the class LogTracerTest method turboFilterRegisteredWhenTracingRequested.

@Test
public void turboFilterRegisteredWhenTracingRequested() throws Exception {
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getHeader(LogTracer.HEADER_TRACER_CONFIG)).thenReturn("foo.bar");
    activateTracer();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertNotNull(context.getService(TurboFilter.class));
        }
    };
    Filter filter = getFilter(false);
    filter.doFilter(request, response, chain);
    assertNull("TurboFilter should get removed once request is done", context.getService(TurboFilter.class));
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

Example 40 with Filter

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

the class LogTracerTest method turboFilterRegisteredWhenTracingRequested_Sling.

@Test
public void turboFilterRegisteredWhenTracingRequested_Sling() throws Exception {
    HttpServletRequest request = mock(SlingHttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(request.getParameter(LogTracer.PARAM_TRACER_CONFIG)).thenReturn("foo.bar");
    activateTracer();
    FilterChain chain = new FilterChain() {

        @Override
        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            assertNotNull(context.getService(TurboFilter.class));
        }
    };
    Filter filter = getFilter(true);
    filter.doFilter(request, response, chain);
    assertNull("TurboFilter should get removed once request is done", context.getService(TurboFilter.class));
}
Also used : SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockSlingHttpServletRequest(org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Filter(javax.servlet.Filter) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) TurboFilter(ch.qos.logback.classic.turbo.TurboFilter) Test(org.junit.Test)

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