Example usage for javax.servlet FilterChain FilterChain

List of usage examples for javax.servlet FilterChain FilterChain

Introduction

In this page you can find the example usage for javax.servlet FilterChain FilterChain.

Prototype

FilterChain

Source Link

Usage

From source file:org.opennms.core.test.rest.AbstractSpringJerseyRestTestCase.java

protected void dispatch(final MockHttpServletRequest request, final MockHttpServletResponse response)
        throws Exception {
    final FilterChain filterChain = new FilterChain() {
        @Override//  w ww  .jav  a  2  s.  c o m
        public void doFilter(final ServletRequest filterRequest, final ServletResponse filterResponse)
                throws IOException, ServletException {
            getDispatcher().service(filterRequest, filterResponse);
        }
    };
    if (getFilter() != null) {
        getFilter().doFilter(request, response, filterChain);
    } else {
        filterChain.doFilter(request, response);
    }
}

From source file:org.opennms.web.rest.AbstractSpringJerseyRestTestCase.java

protected void dispatch(final MockHttpServletRequest request, final MockHttpServletResponse response)
        throws Exception {
    final FilterChain filterChain = new FilterChain() {
        @Override//from w  w  w.ja v a 2  s.co  m
        public void doFilter(final ServletRequest filterRequest, final ServletResponse filterResponse)
                throws IOException, ServletException {
            getDispatcher().service(filterRequest, filterResponse);
        }
    };
    if (getFilter() != null)
        getFilter().doFilter(request, response, filterChain);
}

From source file:org.seasar.cubby.plugins.guice.CubbyModuleTest.java

@Test
public void configure() throws Exception {
    final HttpServletRequest request = createNiceMock(HttpServletRequest.class);
    final HttpServletResponse response = createNiceMock(HttpServletResponse.class);
    final ServletContext servletContext = createNiceMock(ServletContext.class);
    expect(servletContext.getInitParameter("cubby.guice.module")).andStubReturn(TestModule.class.getName());
    final Hashtable<String, Object> attributes = new Hashtable<String, Object>();
    expect(servletContext.getAttribute((String) anyObject())).andStubAnswer(new IAnswer<Object>() {

        public Object answer() throws Throwable {
            return attributes.get(getCurrentArguments()[0]);
        }//from  ww w  . j  a v a  2s  .com

    });
    servletContext.setAttribute((String) anyObject(), anyObject());
    expectLastCall().andAnswer(new IAnswer<Void>() {

        public Void answer() throws Throwable {
            attributes.put((String) getCurrentArguments()[0], getCurrentArguments()[1]);
            return null;
        }

    });
    replay(servletContext);

    final CubbyGuiceServletContextListener cubbyGuiceServletContextListener = new CubbyGuiceServletContextListener();
    cubbyGuiceServletContextListener.contextInitialized(new ServletContextEvent(servletContext));

    final GuiceFilter guiceFilter = new GuiceFilter();
    guiceFilter.init(new FilterConfig() {

        public String getFilterName() {
            return "guice";
        }

        public String getInitParameter(final String name) {
            return null;
        }

        @SuppressWarnings("unchecked")
        public Enumeration getInitParameterNames() {
            return new Vector<String>().elements();
        }

        public ServletContext getServletContext() {
            return servletContext;
        }

    });

    final FilterChain chain = new FilterChain() {

        public void doFilter(final ServletRequest request, final ServletResponse response)
                throws IOException, ServletException {
            final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
            final GuicePlugin guicePlugin = new GuicePlugin();
            try {
                guicePlugin.initialize(servletContext);
                guicePlugin.ready();
                pluginRegistry.register(guicePlugin);
            } catch (final Exception e) {
                throw new ServletException(e);
            }

            final Injector injector = guicePlugin.getInjector();
            System.out.println(injector);

            final PathResolverProvider pathResolverProvider = ProviderFactory.get(PathResolverProvider.class);
            final PathResolver pathResolver = pathResolverProvider.getPathResolver();
            System.out.println(pathResolver);

            final PathTemplateParser pathTemplateParser = injector.getInstance(PathTemplateParser.class);
            System.out.println(pathTemplateParser);
            assertTrue(pathTemplateParser instanceof MyPathTemplateParser);

            final ContainerProvider containerProvider = ProviderFactory.get(ContainerProvider.class);
            final Container container = containerProvider.getContainer();
            final Foo foo = container.lookup(Foo.class);
            System.out.println(foo);
            System.out.println(foo.pathResolver);

            assertSame(pathResolver, foo.pathResolver);

            try {
                final Baz baz = injector.getInstance(Baz.class);
                System.out.println(baz);
                fail();
            } catch (final ConfigurationException e) {
                // ok
            }

            final ConverterProvider converterProvider = ProviderFactory.get(ConverterProvider.class);
            System.out.println(converterProvider);
            System.out.println(converterProvider.getConverter(BigDecimalConverter.class));

            final FileUpload fileUpload1 = injector.getInstance(FileUpload.class);
            System.out.println(fileUpload1);
            System.out.println(fileUpload1.getFileItemFactory());

            final FileUpload fileUpload2 = injector.getInstance(FileUpload.class);
            System.out.println(fileUpload2);
            System.out.println(fileUpload2.getFileItemFactory());

            assertNotSame(fileUpload1, fileUpload2);
            assertNotSame(fileUpload1.getFileItemFactory(), fileUpload2.getFileItemFactory());
        }

    };
    guiceFilter.doFilter(request, response, chain);
    cubbyGuiceServletContextListener.contextDestroyed(new ServletContextEvent(servletContext));
    ThreadContext.remove();
}

From source file:org.springframework.web.multipart.commons.CommonsMultipartResolverTests.java

@Test
public void withServletContextAndFilter() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.registerSingleton("filterMultipartResolver", MockCommonsMultipartResolver.class,
            new MutablePropertyValues());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.refresh();/* www  .  ja va2  s  .  c  o m*/
    wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));

    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    filterConfig.addInitParameter("class", "notWritable");
    filterConfig.addInitParameter("unknownParam", "someValue");
    final MultipartFilter filter = new MultipartFilter();
    filter.init(filterConfig);

    final List<MultipartFile> files = new ArrayList<>();
    final FilterChain filterChain = new FilterChain() {
        @Override
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
            MultipartHttpServletRequest request = (MultipartHttpServletRequest) servletRequest;
            files.addAll(request.getFileMap().values());
        }
    };

    FilterChain filterChain2 = new PassThroughFilterChain(filter, filterChain);

    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    filter.doFilter(originalRequest, response, filterChain2);

    CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
    CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
    assertTrue(((MockFileItem) file1.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}

From source file:org.springframework.web.multipart.commons.CommonsMultipartResolverTests.java

@Test
public void withServletContextAndFilterWithCustomBeanName() throws Exception {
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.refresh();//w  w w .  java2 s  .com
    wac.registerSingleton("myMultipartResolver", MockCommonsMultipartResolver.class,
            new MutablePropertyValues());
    wac.getServletContext().setAttribute(WebUtils.TEMP_DIR_CONTEXT_ATTRIBUTE, new File("mytemp"));
    wac.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    CommonsMultipartResolver resolver = new CommonsMultipartResolver(wac.getServletContext());
    assertTrue(resolver.getFileItemFactory().getRepository().getAbsolutePath().endsWith("mytemp"));

    MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
    filterConfig.addInitParameter("multipartResolverBeanName", "myMultipartResolver");

    final List<MultipartFile> files = new ArrayList<>();
    FilterChain filterChain = new FilterChain() {
        @Override
        public void doFilter(ServletRequest originalRequest, ServletResponse response) {
            if (originalRequest instanceof MultipartHttpServletRequest) {
                MultipartHttpServletRequest request = (MultipartHttpServletRequest) originalRequest;
                files.addAll(request.getFileMap().values());
            }
        }
    };

    MultipartFilter filter = new MultipartFilter() {
        private boolean invoked = false;

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                FilterChain filterChain) throws ServletException, IOException {
            super.doFilterInternal(request, response, filterChain);
            super.doFilterInternal(request, response, filterChain);
            if (invoked) {
                throw new ServletException("Should not have been invoked twice");
            }
            invoked = true;
        }
    };
    filter.init(filterConfig);

    MockHttpServletRequest originalRequest = new MockHttpServletRequest();
    originalRequest.setMethod("POST");
    originalRequest.setContentType("multipart/form-data");
    originalRequest.addHeader("Content-type", "multipart/form-data");
    HttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(originalRequest, response, filterChain);
    CommonsMultipartFile file1 = (CommonsMultipartFile) files.get(0);
    CommonsMultipartFile file2 = (CommonsMultipartFile) files.get(1);
    assertTrue(((MockFileItem) file1.getFileItem()).deleted);
    assertTrue(((MockFileItem) file2.getFileItem()).deleted);
}