/** * Remove a filter mapping. * * @param filterMap * The filter mapping to be removed */ public void remove(FilterMap filterMap) { synchronized (lock) { // Make sure this filter mapping is currently present int n = -1; for (int i = 0; i < array.length; i++) { if (array[i] == filterMap) { n = i; break; } } if (n < 0) return; // Remove the specified filter mapping FilterMap results[] = new FilterMap[array.length - 1]; System.arraycopy(array, 0, results, 0, n); System.arraycopy(array, n + 1, results, n, (array.length - 1) - n); array = results; if (n < insertPoint) { insertPoint--; } } }
/** * Remove a filter mapping. * * @param filterMap The filter mapping to be removed */ public void remove(FilterMap filterMap) { synchronized (lock) { // Make sure this filter mapping is currently present int n = -1; for (int i = 0; i < array.length; i++) { if (array[i] == filterMap) { n = i; break; } } if (n < 0) return; // Remove the specified filter mapping FilterMap results[] = new FilterMap[array.length - 1]; System.arraycopy(array, 0, results, 0, n); System.arraycopy(array, n + 1, results, n, (array.length - 1) - n); array = results; if (n < insertPoint) { insertPoint--; } } }
/** * Return <code>true</code> if the context-relative request path * matches the requirements of the specified filter mapping; * otherwise, return <code>false</code>. * * @param filterMap Filter mapping being checked * @param requestPath Context-relative request path of this request */ private boolean matchFiltersURL(FilterMap filterMap, String requestPath) { // Check the specific "*" special URL pattern, which also matches // named dispatches if (filterMap.getMatchAllUrlPatterns()) return (true); if (requestPath == null) return (false); // Match on context relative request path String[] testPaths = filterMap.getURLPatterns(); for (int i = 0; i < testPaths.length; i++) { if (matchFiltersURL(testPaths[i], requestPath)) { return (true); } } // No match return (false); }
private MockFilterChain testRemoteIpFilter(FilterDef filterDef, Request request) throws LifecycleException, IOException, ServletException { Tomcat tomcat = getTomcatInstance(); Context root = tomcat.addContext("", TEMP_DIR); RemoteIpFilter remoteIpFilter = new RemoteIpFilter(); filterDef.setFilterClass(RemoteIpFilter.class.getName()); filterDef.setFilter(remoteIpFilter); filterDef.setFilterName(RemoteIpFilter.class.getName()); root.addFilterDef(filterDef); FilterMap filterMap = new FilterMap(); filterMap.setFilterName(RemoteIpFilter.class.getName()); filterMap.addURLPattern("*"); root.addFilterMap(filterMap); getTomcatInstance().start(); MockFilterChain filterChain = new MockFilterChain(); // TEST TesterResponse response = new TesterResponse(); response.setRequest(request); remoteIpFilter.doFilter(request, response, filterChain); return filterChain; }
private static void configureTest46243Context(Context context, boolean fail) { // Add a test filter that fails FilterDef filterDef = new FilterDef(); filterDef.setFilterClass(Bug46243Filter.class.getName()); filterDef.setFilterName("Bug46243"); filterDef.addInitParameter("fail", Boolean.toString(fail)); context.addFilterDef(filterDef); FilterMap filterMap = new FilterMap(); filterMap.setFilterName("Bug46243"); filterMap.addURLPattern("*"); context.addFilterMap(filterMap); // Add a test servlet so there is something to generate a response if // it works (although it shouldn't) Tomcat.addServlet(context, "Bug46243", new HelloWorldServlet()); context.addServletMapping("/", "Bug46243"); }
/** * Return <code>true</code> if the specified servlet name matches the * requirements of the specified filter mapping; otherwise return * <code>false</code>. * * @param filterMap * Filter mapping being checked * @param servletName * Servlet name being checked */ private boolean matchFiltersServlet(FilterMap filterMap, String servletName) { if (servletName == null) { return (false); } // Check the specific "*" special servlet name else if (filterMap.getMatchAllServletNames()) { return (true); } else { String[] servletNames = filterMap.getServletNames(); for (int i = 0; i < servletNames.length; i++) { if (servletName.equals(servletNames[i])) { return (true); } } return false; } }
/** * Return <code>true</code> if the specified servlet name matches * the requirements of the specified filter mapping; otherwise * return <code>false</code>. * * @param filterMap Filter mapping being checked * @param servletName Servlet name being checked */ private boolean matchFiltersServlet(FilterMap filterMap, String servletName) { if (servletName == null) { return (false); } // Check the specific "*" special servlet name else if (filterMap.getMatchAllServletNames()) { return (true); } else { String[] servletNames = filterMap.getServletNames(); for (int i = 0; i < servletNames.length; i++) { if (servletName.equals(servletNames[i])) { return (true); } } return false; } }
protected void startTomcat() throws Exception { tomcat = new Tomcat(); File base = new File(System.getProperty("java.io.tmpdir")); org.apache.catalina.Context ctx = tomcat.addContext("/foo",base.getAbsolutePath()); FilterDef fd = new FilterDef(); fd.setFilterClass(TestFilter.class.getName()); fd.setFilterName("TestFilter"); FilterMap fm = new FilterMap(); fm.setFilterName("TestFilter"); fm.addURLPattern("/*"); fm.addServletName("/bar"); ctx.addFilterDef(fd); ctx.addFilterMap(fm); tomcat.addServlet(ctx, "/bar", TestServlet.class.getName()); ctx.addServletMapping("/bar", "/bar"); host = "localhost"; port = getLocalPort(); tomcat.setHostname(host); tomcat.setPort(port); tomcat.start(); }
/** * Validate the supplied FilterMap. */ private void validateFilterMap(FilterMap filterMap) { // Validate the proposed filter mapping String filterName = filterMap.getFilterName(); String[] servletNames = filterMap.getServletNames(); String[] urlPatterns = filterMap.getURLPatterns(); if (findFilterDef(filterName) == null) throw new IllegalArgumentException(sm.getString("standardContext.filterMap.name", filterName)); if (!filterMap.getMatchAllServletNames() && !filterMap.getMatchAllUrlPatterns() && (servletNames.length == 0) && (urlPatterns.length == 0)) throw new IllegalArgumentException(sm.getString("standardContext.filterMap.either")); // FIXME: Older spec revisions may still check this /* * if ((servletNames.length != 0) && (urlPatterns.length != 0)) throw * new IllegalArgumentException * (sm.getString("standardContext.filterMap.either")); */ for (int i = 0; i < urlPatterns.length; i++) { if (!validateURLPattern(urlPatterns[i])) { throw new IllegalArgumentException(sm.getString("standardContext.filterMap.pattern", urlPatterns[i])); } } }
/** * Validate the supplied FilterMap. */ private void validateFilterMap(FilterMap filterMap) { // Validate the proposed filter mapping String filterName = filterMap.getFilterName(); String[] servletNames = filterMap.getServletNames(); String[] urlPatterns = filterMap.getURLPatterns(); if (findFilterDef(filterName) == null) throw new IllegalArgumentException (sm.getString("standardContext.filterMap.name", filterName)); if (!filterMap.getMatchAllServletNames() && !filterMap.getMatchAllUrlPatterns() && (servletNames.length == 0) && (urlPatterns.length == 0)) throw new IllegalArgumentException (sm.getString("standardContext.filterMap.either")); // FIXME: Older spec revisions may still check this /* if ((servletNames.length != 0) && (urlPatterns.length != 0)) throw new IllegalArgumentException (sm.getString("standardContext.filterMap.either")); */ for (int i = 0; i < urlPatterns.length; i++) { if (!validateURLPattern(urlPatterns[i])) { throw new IllegalArgumentException (sm.getString("standardContext.filterMap.pattern", urlPatterns[i])); } } }
/** * Remove a filter mapping from this Context. * * @param filterMap The filter mapping to be removed */ @Override public void removeFilterMap(FilterMap filterMap) { filterMaps.remove(filterMap); // Inform interested listeners fireContainerEvent("removeFilterMap", filterMap); }
/** * Add a filter mapping at the end of the current set of filter * mappings. * * @param filterMap * The filter mapping to be added */ public void add(FilterMap filterMap) { synchronized (lock) { FilterMap results[] = Arrays.copyOf(array, array.length + 1); results[array.length] = filterMap; array = results; } }
/** * Add a filter mapping before the mappings defined in the deployment * descriptor but after any other mappings added via this method. * * @param filterMap * The filter mapping to be added */ public void addBefore(FilterMap filterMap) { synchronized (lock) { FilterMap results[] = new FilterMap[array.length + 1]; System.arraycopy(array, 0, results, 0, insertPoint); System.arraycopy(array, insertPoint, results, insertPoint + 1, array.length - insertPoint); results[insertPoint] = filterMap; array = results; insertPoint++; } }
/** * Convenience method which returns true if the dispatcher type * matches the dispatcher types specified in the FilterMap */ private boolean matchDispatcher(FilterMap filterMap, DispatcherType type) { switch (type) { case FORWARD : { if ((filterMap.getDispatcherMapping() & FilterMap.FORWARD) > 0) { return true; } break; } case INCLUDE : { if ((filterMap.getDispatcherMapping() & FilterMap.INCLUDE) > 0) { return true; } break; } case REQUEST : { if ((filterMap.getDispatcherMapping() & FilterMap.REQUEST) > 0) { return true; } break; } case ERROR : { if ((filterMap.getDispatcherMapping() & FilterMap.ERROR) > 0) { return true; } break; } case ASYNC : { if ((filterMap.getDispatcherMapping() & FilterMap.ASYNC) > 0) { return true; } break; } } return false; }
@Override public void addMappingForServletNames( EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... servletNames) { FilterMap filterMap = new FilterMap(); filterMap.setFilterName(filterDef.getFilterName()); if (dispatcherTypes != null) { for (DispatcherType dispatcherType : dispatcherTypes) { filterMap.setDispatcher(dispatcherType.name()); } } if (servletNames != null) { for (String servletName : servletNames) { filterMap.addServletName(servletName); } if (isMatchAfter) { context.addFilterMap(filterMap); } else { context.addFilterMapBefore(filterMap); } } // else error? }
@Override public void addMappingForUrlPatterns( EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) { FilterMap filterMap = new FilterMap(); filterMap.setFilterName(filterDef.getFilterName()); if (dispatcherTypes != null) { for (DispatcherType dispatcherType : dispatcherTypes) { filterMap.setDispatcher(dispatcherType.name()); } } if (urlPatterns != null) { for (String urlPattern : urlPatterns) { filterMap.addURLPattern(urlPattern); } if (isMatchAfter) { context.addFilterMap(filterMap); } else { context.addFilterMapBefore(filterMap); } } // else error? }
@Override public Collection<String> getServletNameMappings() { Collection<String> result = new HashSet<String>(); FilterMap[] filterMaps = context.findFilterMaps(); for (FilterMap filterMap : filterMaps) { if (filterMap.getFilterName().equals(filterDef.getFilterName())) { for (String servletName : filterMap.getServletNames()) { result.add(servletName); } } } return result; }
@Override public Collection<String> getUrlPatternMappings() { Collection<String> result = new HashSet<String>(); FilterMap[] filterMaps = context.findFilterMaps(); for (FilterMap filterMap : filterMaps) { if (filterMap.getFilterName().equals(filterDef.getFilterName())) { for (String urlPattern : filterMap.getURLPatterns()) { result.add(urlPattern); } } } return result; }
private synchronized void init() throws Exception { if (init) return; Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context root = tomcat.addContext("", null); Tomcat.addServlet(root, "Bug37794", new Bug37794Servlet()); root.addServletMapping("/test", "Bug37794"); if (createFilter) { FilterDef failedRequestFilter = new FilterDef(); failedRequestFilter.setFilterName("failedRequestFilter"); failedRequestFilter.setFilterClass( FailedRequestFilter.class.getName()); FilterMap failedRequestFilterMap = new FilterMap(); failedRequestFilterMap.setFilterName("failedRequestFilter"); failedRequestFilterMap.addURLPattern("/*"); root.addFilterDef(failedRequestFilter); root.addFilterMap(failedRequestFilterMap); } tomcat.start(); setPort(tomcat.getConnector().getLocalPort()); init = true; }
private void setUpApplication() throws Exception { context = tomcat.addContext(CONTEXT_PATH_LOGIN, System.getProperty("java.io.tmpdir")); context.setSessionTimeout(SHORT_SESSION_TIMEOUT_MINS); Tomcat.addServlet(context, SERVLET_NAME, new TesterServlet()); context.addServletMapping(URI_PROTECTED, SERVLET_NAME); FilterDef filterDef = new FilterDef(); filterDef.setFilterName(FILTER_NAME); filterDef.setFilterClass(RestCsrfPreventionFilter.class.getCanonicalName()); filterDef.addInitParameter(FILTER_INIT_PARAM, REMOVE_CUSTOMER + "," + ADD_CUSTOMER); context.addFilterDef(filterDef); FilterMap filterMap = new FilterMap(); filterMap.setFilterName(FILTER_NAME); filterMap.addURLPattern(URI_CSRF_PROTECTED); context.addFilterMap(filterMap); SecurityCollection collection = new SecurityCollection(); collection.addPattern(URI_PROTECTED); SecurityConstraint sc = new SecurityConstraint(); sc.addAuthRole(ROLE); sc.addCollection(collection); context.addConstraint(sc); LoginConfig lc = new LoginConfig(); lc.setAuthMethod(METHOD); context.setLoginConfig(lc); AuthenticatorBase basicAuthenticator = new BasicAuthenticator(); context.getPipeline().addValve(basicAuthenticator); }
private void doTest(String encoding, String expected, int mode) throws Exception { // Setup Tomcat instance Tomcat tomcat = getTomcatInstance(); // No file system docBase required Context ctx = tomcat.addContext("", null); // Add the Servlet CharsetServlet servlet = new CharsetServlet(mode); Tomcat.addServlet(ctx, "servlet", servlet); ctx.addServletMapping("/", "servlet"); // Add the Filter FilterDef filterDef = new FilterDef(); filterDef.setFilterClass(AddDefaultCharsetFilter.class.getName()); filterDef.setFilterName("filter"); if (encoding != null) { filterDef.addInitParameter("encoding", encoding); } ctx.addFilterDef(filterDef); FilterMap filterMap = new FilterMap(); filterMap.setFilterName("filter"); filterMap.addServletName("servlet"); ctx.addFilterMap(filterMap); tomcat.start(); Map<String, List<String>> headers = new HashMap<String, List<String>>(); getUrl("http://localhost:" + getPort() + "/", new ByteChunk(), headers); List<String> ctHeaders = headers.get("Content-Type"); assertEquals(1, ctHeaders.size()); String ct = ctHeaders.get(0); assertEquals("text/plain;charset=" + expected, ct); }
/** * Remove a filter mapping from this Context. * * @param filterMap The filter mapping to be removed */ public void removeFilterMap(FilterMap filterMap) { synchronized (filterMaps) { // Make sure this filter mapping is currently present int n = -1; for (int i = 0; i < filterMaps.length; i++) { if (filterMaps[i] == filterMap) { n = i; break; } } if (n < 0) return; // Remove the specified filter mapping FilterMap results[] = new FilterMap[filterMaps.length - 1]; System.arraycopy(filterMaps, 0, results, 0, n); System.arraycopy(filterMaps, n + 1, results, n, (filterMaps.length - 1) - n); filterMaps = results; } // Inform interested listeners fireContainerEvent("removeFilterMap", filterMap); }
/** * Return <code>true</code> if the specified servlet name matches * the requirements of the specified filter mapping; otherwise * return <code>false</code>. * * @param filterMap Filter mapping being checked * @param servletName Servlet name being checked */ private boolean matchFiltersServlet(FilterMap filterMap, String servletName) { // if (debug >= 3) // log(" Matching servlet name '" + servletName + // "' against mapping " + filterMap); if (servletName == null) return (false); else return (servletName.equals(filterMap.getServletName())); }
/** * Add a filter mapping to this Context. * * @param filterMap The filter mapping to be added * * @exception IllegalArgumentException if the specified filter name * does not match an existing filter definition, or the filter mapping * is malformed */ public void addFilterMap(FilterMap filterMap) { // Validate the proposed filter mapping String filterName = filterMap.getFilterName(); String servletName = filterMap.getServletName(); String urlPattern = filterMap.getURLPattern(); if (findFilterDef(filterName) == null) throw new IllegalArgumentException (sm.getString("standardContext.filterMap.name", filterName)); if ((servletName == null) && (urlPattern == null)) throw new IllegalArgumentException (sm.getString("standardContext.filterMap.either")); if ((servletName != null) && (urlPattern != null)) throw new IllegalArgumentException (sm.getString("standardContext.filterMap.either")); // Because filter-pattern is new in 2.3, no need to adjust // for 2.2 backwards compatibility if ((urlPattern != null) && !validateURLPattern(urlPattern)) throw new IllegalArgumentException (sm.getString("standardContext.filterMap.pattern", urlPattern)); // Add this filter mapping to our registered set synchronized (filterMaps) { FilterMap results[] =new FilterMap[filterMaps.length + 1]; System.arraycopy(filterMaps, 0, results, 0, filterMaps.length); results[filterMaps.length] = filterMap; filterMaps = results; } fireContainerEvent("addFilterMap", filterMap); }
/** * Convenience method which returns true if the dispatcher type matches the * dispatcher types specified in the FilterMap */ private boolean matchDispatcher(FilterMap filterMap, DispatcherType type) { switch (type) { case FORWARD: { if ((filterMap.getDispatcherMapping() & FilterMap.FORWARD) > 0) { return true; } break; } case INCLUDE: { if ((filterMap.getDispatcherMapping() & FilterMap.INCLUDE) > 0) { return true; } break; } case REQUEST: { if ((filterMap.getDispatcherMapping() & FilterMap.REQUEST) > 0) { return true; } break; } case ERROR: { if ((filterMap.getDispatcherMapping() & FilterMap.ERROR) > 0) { return true; } break; } case ASYNC: { if ((filterMap.getDispatcherMapping() & FilterMap.ASYNC) > 0) { return true; } break; } } return false; }
@Override public void addMappingForUrlPatterns(EnumSet<DispatcherType> dispatcherTypes, boolean isMatchAfter, String... urlPatterns) { FilterMap filterMap = new FilterMap(); filterMap.setFilterName(filterDef.getFilterName()); if (dispatcherTypes != null) { for (DispatcherType dispatcherType : dispatcherTypes) { filterMap.setDispatcher(dispatcherType.name()); } } if (urlPatterns != null) { for (String urlPattern : urlPatterns) { filterMap.addURLPattern(urlPattern); } if (isMatchAfter) { context.addFilterMap(filterMap); } else { context.addFilterMapBefore(filterMap); } } // else error? }