Search in sources :

Example 26 with Filter

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

the class CustomFiltersTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry jndi = super.createRegistry();
    List<Filter> filters = new ArrayList<Filter>();
    filters.add(new MyTestFilter());
    jndi.bind("myFilters", filters);
    return jndi;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) Filter(javax.servlet.Filter) ArrayList(java.util.ArrayList)

Example 27 with Filter

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

the class CometdCrossOriginConsumerTest method testCrossOriginFilterAddedWhenOn.

@Test
public void testCrossOriginFilterAddedWhenOn() throws Exception {
    // setup
    CometdComponent component = context.getComponent("cometd", CometdComponent.class);
    Server server = new Server();
    when(endpoint.isCrossOriginFilterOn()).thenReturn(true);
    when(endpoint.getFilterPath()).thenReturn(FILTER_PATH);
    when(endpoint.getAllowedOrigins()).thenReturn(ALLOWED_ORIGINS);
    // act
    component.createServletForConnector(server, connector, endpoint);
    // assert
    ServletContextHandler handler = (ServletContextHandler) server.getHandler();
    assertEquals(1, handler.getServletHandler().getFilters().length);
    FilterHolder filterHolder = handler.getServletHandler().getFilters()[0];
    Filter filter = filterHolder.getFilter();
    assertTrue(filter instanceof CrossOriginFilter);
}
Also used : FilterHolder(org.eclipse.jetty.servlet.FilterHolder) Server(org.eclipse.jetty.server.Server) Filter(javax.servlet.Filter) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) CrossOriginFilter(org.eclipse.jetty.servlets.CrossOriginFilter) Test(org.junit.Test)

Example 28 with Filter

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

the class EventListenerRegistryTest method createFilterHandler.

private static FilterHandler createFilterHandler(final long id, final int ranking, final String... paths) throws InvalidSyntaxException {
    final FilterInfo si = createFilterInfo(id, ranking, paths);
    final ExtServletContext ctx = mock(ExtServletContext.class);
    final Filter filter = mock(Filter.class);
    return new HttpServiceFilterHandler(ctx, si, filter);
}
Also used : ExtServletContext(org.apache.felix.http.base.internal.context.ExtServletContext) Filter(javax.servlet.Filter) HttpServiceFilterHandler(org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler) FilterInfo(org.apache.felix.http.base.internal.runtime.FilterInfo)

Example 29 with Filter

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

the class EventListenerRegistryTest method testSingleFilter.

@Test
public void testSingleFilter() throws InvalidSyntaxException, ServletException {
    final FailedDTOHolder holder = new FailedDTOHolder();
    final ServletContextDTO dto = new ServletContextDTO();
    // check DTO
    reg.getRuntimeInfo(dto, holder.failedFilterDTOs);
    assertEmpty(dto, holder);
    // register filter
    final FilterHandler h1 = createFilterHandler(1L, 0, "/foo");
    reg.addFilter(h1);
    verify(h1.getFilter()).init(Matchers.any(FilterConfig.class));
    // one entry in DTO
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedFilterDTOs);
    assertTrue(holder.failedFilterDTOs.isEmpty());
    assertNotNull(dto.filterDTOs);
    assertEquals(1, dto.filterDTOs.length);
    assertEquals(1, dto.filterDTOs[0].patterns.length);
    assertEquals("/foo", dto.filterDTOs[0].patterns[0]);
    // remove filter
    final Filter f = h1.getFilter();
    reg.removeFilter(h1.getFilterInfo(), true);
    verify(f).destroy();
    // empty again
    clear(dto, holder);
    reg.getRuntimeInfo(dto, holder.failedFilterDTOs);
    assertEmpty(dto, holder);
}
Also used : FailedDTOHolder(org.apache.felix.http.base.internal.runtime.dto.FailedDTOHolder) ServletContextDTO(org.osgi.service.http.runtime.dto.ServletContextDTO) Filter(javax.servlet.Filter) FilterConfig(javax.servlet.FilterConfig) FilterHandler(org.apache.felix.http.base.internal.handler.FilterHandler) HttpServiceFilterHandler(org.apache.felix.http.base.internal.handler.HttpServiceFilterHandler) Test(org.junit.Test)

Example 30 with Filter

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

the class HttpServiceRuntimeTest method exceptionInFilterInitAppearsAsFailure.

@Test
public void exceptionInFilterInitAppearsAsFailure() throws ServletException, InterruptedException {
    Dictionary<String, ?> properties = createDictionary(HTTP_WHITEBOARD_FILTER_PATTERN, "/filter", HTTP_WHITEBOARD_FILTER_NAME, "filter");
    CountDownLatch initLatch = new CountDownLatch(1);
    Filter failingFilter = new TestFilter(initLatch, null) {

        @Override
        public void init(FilterConfig config) throws ServletException {
            super.init(config);
            throw new ServletException();
        }
    };
    registrations.add(m_context.registerService(Filter.class.getName(), failingFilter, properties));
    awaitServiceRegistration(initLatch);
    HttpServiceRuntime serviceRuntime = (HttpServiceRuntime) getService(HttpServiceRuntime.class.getName());
    assertNotNull("HttpServiceRuntime unavailable", serviceRuntime);
    RuntimeDTO runtimeDTO = serviceRuntime.getRuntimeDTO();
    assertEquals(1, runtimeDTO.failedFilterDTOs.length);
    assertEquals("filter", runtimeDTO.failedFilterDTOs[0].name);
    assertEquals(FAILURE_REASON_EXCEPTION_ON_INIT, runtimeDTO.failedFilterDTOs[0].failureReason);
}
Also used : ServletException(javax.servlet.ServletException) Filter(javax.servlet.Filter) HttpServiceRuntime(org.osgi.service.http.runtime.HttpServiceRuntime) FilterConfig(javax.servlet.FilterConfig) RuntimeDTO(org.osgi.service.http.runtime.dto.RuntimeDTO) CountDownLatch(java.util.concurrent.CountDownLatch) 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