@Test @Ignore("PAXWEB-483: Filtering without a Servlet doesn't work with Http-Service but within a war") public void testFilterOnly() throws Exception { ServiceTracker<WebContainer, WebContainer> tracker = new ServiceTracker<WebContainer, WebContainer>(bundleContext, WebContainer.class, null); tracker.open(); WebContainer service = tracker.waitForService(TimeUnit.SECONDS.toMillis(20)); Filter filter = new SimpleOnlyFilter(); service.registerFilter( filter, new String[] { "/testFilter/*", }, null, null, null); testClient.testWebPath("http://127.0.0.1:8181/testFilter/filterMe", "Hello Whiteboard Filter"); service.unregisterFilter(filter); }
@Test @Ignore("Test fails due to a filter doesn't work right now for the root '/'") public void testRootFilterRegistration() throws Exception { ServiceTracker<WebContainer, WebContainer> tracker = new ServiceTracker<WebContainer, WebContainer>(bundleContext, WebContainer.class, null); tracker.open(); WebContainer service = tracker.waitForService(TimeUnit.SECONDS.toMillis(20)); final String fullContent = "This content is Filtered by a javax.servlet.Filter"; Filter filter = new Filter() { @Override public void init(FilterConfig filterConfig) throws ServletException {} @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { PrintWriter writer = response.getWriter(); writer.write(fullContent); writer.flush(); } @Override public void destroy() {} }; final StringWriter writer = new StringWriter(); // CHECKSTYLE:OFF filter.doFilter( null, (ServletResponse) Proxy.newProxyInstance( getClass().getClassLoader(), new Class[] {ServletResponse.class}, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("getWriter")) { return new PrintWriter(writer); } return null; } }), null); // CHECKSTYLE:OFF // Check if our example filter do write the string to the writer... Assert.assertEquals(fullContent, writer.toString()); // Now register the Filter under some alias... service.registerFilter( filter, new String[] {"*", "/*", "/", "/some/random/path"}, null, null, null); // If it works, always the filter should take over and return the same string regardeless of the // URL String expectedContent = "content is Filtered by"; testClient.testWebPath("http://127.0.0.1:8181/test-context/some/random/path", expectedContent); testClient.testWebPath( "http://127.0.0.1:8181/test-context/some/notregistered/random/path", expectedContent); testClient.testWebPath("http://127.0.0.1:8181/test-context/", expectedContent); // Even for existing path! testClient.testWebPath("http://127.0.0.1:8181/helloworld/hs", expectedContent); // And even for images testClient.testWebPath("http://127.0.0.1:8181/images/logo.png", expectedContent); // of course we should be able to deregister :-) service.unregisterFilter(filter); tracker.close(); }