@Test public void testGetChain() throws Exception { IMocksControl ctrl = createStrictControl(); Injector injector = ctrl.createMock(Injector.class); Map<String, Key<? extends Filter>[]> chainMap = new LinkedHashMap<String, Key<? extends Filter>[]>(); final String chainOne = "one"; final String chainTwo = "two"; final String chainThree = "three"; final Key<? extends Filter> key1a = Key.get(Filter.class, Names.named("key1a")); final Key<? extends Filter> key1b = Key.get(Filter.class, Names.named("key1b")); final Key<? extends Filter> key2a = Key.get(Filter.class, Names.named("key2a")); final Key<? extends Filter> key2b = Key.get(Filter.class, Names.named("key2b")); final Key<? extends Filter> key3a = Key.get(Filter.class, Names.named("key3a")); final Key<? extends Filter> key3b = Key.get(Filter.class, Names.named("key3b")); chainMap.put(chainOne, new Key[]{key1a, key1b}); chainMap.put(chainTwo, new Key[]{key2a, key2b}); chainMap.put(chainThree, new Key[]{key3a, key3b}); PatternMatcher patternMatcher = ctrl.createMock(PatternMatcher.class); ServletRequest request = ctrl.createMock(HttpServletRequest.class); ServletResponse response = ctrl.createMock(HttpServletResponse.class); FilterChain originalChain = ctrl.createMock(FilterChain.class); expect(request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE)).andReturn("/mychain"); expect(request.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE)).andReturn(""); expect(request.getCharacterEncoding()).andStubReturn(null); expect(patternMatcher.matches(chainOne, "/mychain")).andReturn(false); expect(patternMatcher.matches(chainTwo, "/mychain")).andReturn(true); Filter filter2a = ctrl.createMock(Filter.class); Filter filter2b = ctrl.createMock(Filter.class); expect((Filter)injector.getInstance(key2a)).andReturn(filter2a); filter2a.doFilter(same(request), same(response), anyObject(FilterChain.class)); expect((Filter)injector.getInstance(key2b)).andReturn(filter2b); filter2b.doFilter(same(request), same(response), anyObject(FilterChain.class)); originalChain.doFilter(request, response); ctrl.replay(); SimpleFilterChainResolver underTest = new SimpleFilterChainResolver(chainMap, injector, patternMatcher); FilterChain got = underTest.getChain(request, response, originalChain); got.doFilter(request, response); got.doFilter(request, response); got.doFilter(request, response); ctrl.verify(); ctrl.reset(); expect(request.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE)).andReturn("/nochain"); expect(request.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE)).andReturn(""); expect(request.getCharacterEncoding()).andStubReturn(null); expect(patternMatcher.matches(chainOne, "/nochain")).andReturn(false); expect(patternMatcher.matches(chainTwo, "/nochain")).andReturn(false); expect(patternMatcher.matches(chainThree, "/nochain")).andReturn(false); ctrl.replay(); assertNull("Expected no chain to match, did not get a null value in return.", underTest.getChain(request, response, originalChain)); ctrl.verify(); }
public FilterChain getChain(ServletRequest request, ServletResponse response, final FilterChain originalChain) { String path = WebUtils.getPathWithinApplication(WebUtils.toHttp(request)); for (final String pathPattern : chains.keySet()) { if (patternMatcher.matches(pathPattern, path)) { final Iterator<Key<? extends Filter>> chain = Arrays.asList(chains.get(pathPattern)).iterator(); return new SimpleFilterChain(originalChain, new Iterator<Filter>() { public boolean hasNext() { return chain.hasNext(); } public Filter next() { return injector.getInstance(chain.next()); } public void remove() { throw new UnsupportedOperationException(); } }); } } return null; }
SimpleFilterChainResolver implements FilterChainResolver { public FilterChain getChain(ServletRequest request, ServletResponse response, final FilterChain originalChain) { String path = WebUtils.getPathWithinApplication(WebUtils.toHttp(request)); for (final String pathPattern : chains.keySet()) { if (patternMatcher.matches(pathPattern, path)) { final Iterator<Key<? extends Filter>> chain = Arrays.asList(chains.get(pathPattern)).iterator(); return new SimpleFilterChain(originalChain, new Iterator<Filter>() { public boolean hasNext() { return chain.hasNext(); } public Filter next() { return injector.getInstance(chain.next()); } public void remove() { throw new UnsupportedOperationException(); } }); } } return null; } }
SimpleFilterChainResolver implements FilterChainResolver { public FilterChain getChain(ServletRequest request, ServletResponse response, final FilterChain originalChain) { String path = WebUtils.getPathWithinApplication(WebUtils.toHttp(request)); for (final String pathPattern : chains.keySet()) { if (patternMatcher.matches(pathPattern, path)) { final Iterator<Key<? extends Filter>> chain = Arrays.asList(chains.get(pathPattern)).iterator(); return new SimpleFilterChain(originalChain, new Iterator<Filter>() { public boolean hasNext() { return chain.hasNext(); } public Filter next() { return injector.getInstance(chain.next()); } public void remove() { throw new UnsupportedOperationException(); } }); } } return null; } SimpleFilterChainResolver(Map<String, Key<? extends Filter>[]> chains, Injector injector, PatternMatcher patternMatcher); }
SimpleFilterChainResolver implements FilterChainResolver { public FilterChain getChain(ServletRequest request, ServletResponse response, final FilterChain originalChain) { String path = WebUtils.getPathWithinApplication(WebUtils.toHttp(request)); for (final String pathPattern : chains.keySet()) { if (patternMatcher.matches(pathPattern, path)) { final Iterator<Key<? extends Filter>> chain = Arrays.asList(chains.get(pathPattern)).iterator(); return new SimpleFilterChain(originalChain, new Iterator<Filter>() { public boolean hasNext() { return chain.hasNext(); } public Filter next() { return injector.getInstance(chain.next()); } public void remove() { throw new UnsupportedOperationException(); } }); } } return null; } SimpleFilterChainResolver(Map<String, Key<? extends Filter>[]> chains, Injector injector, PatternMatcher patternMatcher); FilterChain getChain(ServletRequest request, ServletResponse response, final FilterChain originalChain); }
SimpleFilterChainResolver implements FilterChainResolver { public FilterChain getChain(ServletRequest request, ServletResponse response, final FilterChain originalChain) { String path = WebUtils.getPathWithinApplication(WebUtils.toHttp(request)); for (final String pathPattern : chains.keySet()) { if (patternMatcher.matches(pathPattern, path)) { final Iterator<Key<? extends Filter>> chain = Arrays.asList(chains.get(pathPattern)).iterator(); return new SimpleFilterChain(originalChain, new Iterator<Filter>() { public boolean hasNext() { return chain.hasNext(); } public Filter next() { return injector.getInstance(chain.next()); } public void remove() { throw new UnsupportedOperationException(); } }); } } return null; } SimpleFilterChainResolver(Map<String, Key<? extends Filter>[]> chains, Injector injector, PatternMatcher patternMatcher); FilterChain getChain(ServletRequest request, ServletResponse response, final FilterChain originalChain);  }