@Override protected T postProcess(T filter) { for (Map.Entry<String, String> pathConfig : this.pathConfigs.entrySet()) { filter.processPathConfig(pathConfig.getKey(), pathConfig.getValue()); } return filter; } }
/** * Simple method to abstract out logic from the preHandle implementation - it was getting a bit unruly. * * @since 1.2 */ @SuppressWarnings({"JavaDoc"}) private boolean isFilterChainContinued(ServletRequest request, ServletResponse response, String path, Object pathConfig) throws Exception { if (isEnabled(request, response, path, pathConfig)) { //isEnabled check added in 1.2 if (log.isTraceEnabled()) { log.trace("Filter '{}' is enabled for the current request under path '{}' with config [{}]. " + "Delegating to subclass implementation for 'onPreHandle' check.", new Object[]{getName(), path, pathConfig}); } //The filter is enabled for this specific request, so delegate to subclass implementations //so they can decide if the request should continue through the chain or not: return onPreHandle(request, response, pathConfig); } if (log.isTraceEnabled()) { log.trace("Filter '{}' is disabled for the current request under path '{}' with config [{}]. " + "The next element in the FilterChain will be called immediately.", new Object[]{getName(), path, pathConfig}); } //This filter is disabled for this specific request, //return 'true' immediately to indicate that the filter will not process the request //and let the request/response to continue through the filter chain: return true; }
/** * Returns <code>true</code> if the incoming <code>request</code> matches the specified <code>path</code> pattern, * <code>false</code> otherwise. * <p/> * The default implementation acquires the <code>request</code>'s path within the application and determines * if that matches: * <p/> * <code>String requestURI = {@link #getPathWithinApplication(javax.servlet.ServletRequest) getPathWithinApplication(request)};<br/> * return {@link #pathsMatch(String, String) pathsMatch(path,requestURI)}</code> * * @param path the configured url pattern to check the incoming request against. * @param request the incoming ServletRequest * @return <code>true</code> if the incoming <code>request</code> matches the specified <code>path</code> pattern, * <code>false</code> otherwise. */ protected boolean pathsMatch(String path, ServletRequest request) { String requestURI = getPathWithinApplication(request); log.trace("Attempting to match pattern '{}' with current requestURI '{}'...", path, requestURI); return pathsMatch(path, requestURI); }
if (pathsMatch(path, request)) { log.trace("Current requestURI matches pattern '{}'. Determining filter chain execution...", path); Object config = this.appliedPaths.get(path); return isFilterChainContinued(request, response, path, config);
/** * Path-matching version of the parent class's * {@link #isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)} method, but additionally allows * for inspection of any path-specific configuration values corresponding to the specified request. Subclasses * may wish to inspect this additional mapped configuration to determine if the filter is enabled or not. * <p/> * This method's default implementation ignores the {@code path} and {@code mappedValue} arguments and merely * returns the value from a call to {@link #isEnabled(javax.servlet.ServletRequest, javax.servlet.ServletResponse)}. * It is expected that subclasses override this method if they need to perform enable/disable logic for a specific * request based on any path-specific config for the filter instance. * * @param request the incoming servlet request * @param response the outbound servlet response * @param path the path matched for the incoming servlet request that has been configured with the given {@code mappedValue}. * @param mappedValue the filter-specific config value mapped to this filter in the URL rules mappings for the given {@code path}. * @return {@code true} if this filter should filter the specified request, {@code false} if it should let the * request/response pass through immediately to the next element in the {@code FilterChain}. * @throws Exception in the case of any error * @since 1.2 */ @SuppressWarnings({"UnusedParameters"}) protected boolean isEnabled(ServletRequest request, ServletResponse response, String path, Object mappedValue) throws Exception { return isEnabled(request, response); } }
super.cleanup(request, response, existing);
@Test public void testPostProcess() { PathMatchingFilter filter = createMock(PathMatchingFilter.class); expect(filter.processPathConfig("/1", "first")).andReturn(filter); expect(filter.processPathConfig("/2", "second")).andReturn(filter); replay(filter); Map<String, String> pathConfigMap = new HashMap<String, String>(); pathConfigMap.put("/1", "first"); pathConfigMap.put("/2", "second"); PathMatchingFilterProvider underTest = new PathMatchingFilterProvider(Key.get(PathMatchingFilter.class), pathConfigMap); underTest.postProcess(filter); verify(filter); }
@Override protected T postProcess(T filter) { for (Map.Entry<String, String> pathConfig : this.pathConfigs.entrySet()) { filter.processPathConfig(pathConfig.getKey(), pathConfig.getValue()); } return filter; } }
@Override public Filter processPathConfig(String path, String config) { Filter result = super.processPathConfig(path, config); String[] configValues = (String[]) appliedPaths.get(path); if (configValues.length != 1) { throw new OctopusConfigurationException(String.format("Configuration of Rate limit filter on path %s is wrong (%s)", path, config)); } rateLimiters.put(path, rateLimitConfig.createRateLimiter(configValues[0])); return result; }