org.apache.catalina.deploy.FilterMap Java Examples

The following examples show how to use org.apache.catalina.deploy.FilterMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestRemoteIpFilter.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #2
Source File: ApplicationFilterFactory.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #3
Source File: ApplicationFilterFactory.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    
}
 
Example #4
Source File: AuthenticatorTestCase.java    From registry with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #5
Source File: AuthenticatorTestCase.java    From big-c with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #6
Source File: AuthenticatorTestCase.java    From hadoop with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #7
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #8
Source File: TestStandardContext.java    From tomcatsrc with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example #9
Source File: TestStandardContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
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");
}
 
Example #10
Source File: ApplicationFilterFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
    }

}
 
Example #11
Source File: ServletRamlMessageTest.java    From raml-tester with Apache License 2.0 6 votes vote down vote up
@Override
protected void init(Context ctx) {
    final FilterDef filterDef = new FilterDef();
    filterDef.setFilter(testFilter);
    filterDef.setFilterName("filter");
    ctx.addFilterDef(filterDef);

    final FilterMap filterMap = new FilterMap();
    filterMap.addURLPattern("/*");
    filterMap.setFilterName("filter");
    ctx.addFilterMap(filterMap);

    Tomcat.addServlet(ctx, "test", testServlet);
    Tomcat.addServlet(ctx, "gzip", gzipTestServlet);
    ctx.addServletMapping("/test/*", "test");
    ctx.addServletMapping("/gzip/*", "gzip");
}
 
Example #12
Source File: ApplicationFilterFactory.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    
}
 
Example #13
Source File: ServletTest.java    From raml-tester with Apache License 2.0 6 votes vote down vote up
@Override
protected void init(Context ctx) {
    final FilterDef filterDef = new FilterDef();
    filterDef.setFilter(testFilter);
    filterDef.setFilterName("filter");
    ctx.addFilterDef(filterDef);

    final FilterMap filterMap = new FilterMap();
    filterMap.addServletName("app");
    filterMap.addURLPattern("/*");
    filterMap.setFilterName("filter");
    ctx.addFilterMap(filterMap);

    Tomcat.addServlet(ctx, "app", new TestServlet());
    ctx.addServletMapping("/*", "app");
}
 
Example #14
Source File: AbstractValveTest.java    From tomcat-mongo-access-log with Apache License 2.0 6 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  Tomcat tomcat = getTomcatInstance();
  
  tomcat.getConnector().setURIEncoding("UTF-8");
  Context ctx = tomcat.addContext("", getTemporaryDirectory().getAbsolutePath());
  
  Tomcat.addServlet(ctx, "servlet", new FakeServlet());
  ctx.addServletMapping("/", "servlet");

  FilterDef filterDef = new FilterDef();
  filterDef.setFilterClass("org.apache.catalina.filters.SetCharacterEncodingFilter");
  filterDef.setFilterName("setCharacterEncodingFilter");
  filterDef.addInitParameter("encoding", "UTF-8");
  filterDef.setAsyncSupported("true");
  ctx.addFilterDef(filterDef);
  
  FilterMap filterMap = new FilterMap();
  filterMap.setFilterName("setCharacterEncodingFilter");
  filterMap.addURLPattern("/*");
  ctx.addFilterMap(filterMap);
  
  this.sb = new StringBuilder();
  setUpValve(tomcat);
}
 
Example #15
Source File: ApplicationFilterFactory.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #16
Source File: Runner.java    From myrrix-recommender with Apache License 2.0 5 votes vote down vote up
private static void addFilter(Context context, Filter filter, String path, Map<String,String> args) {
  String name = filter.getClass().getSimpleName();
  FilterDef dosFilterDef = new FilterDef();
  dosFilterDef.setFilter(filter);
  dosFilterDef.setFilterName(name);
  for (Map.Entry<String,String> entry : args.entrySet()) {
    dosFilterDef.addInitParameter(entry.getKey(), entry.getValue());
  }
  context.addFilterDef(dosFilterDef); 
  
  FilterMap dosFilterMap = new FilterMap();
  dosFilterMap.setFilterName(name);
  dosFilterMap.addURLPattern(path);
  context.addFilterMap(dosFilterMap);
}
 
Example #17
Source File: JCacheFilterTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
private void addJcsFilter(final Context ctx) {
    final FilterDef filterDef = new FilterDef();
    filterDef.setFilterName("jcs");
    filterDef.setFilterClass(JCacheFilter.class.getName());
    ctx.addFilterDef(filterDef);

    final FilterMap filterMap = new FilterMap();
    filterMap.setFilterName(filterDef.getFilterName());
    filterMap.addURLPattern("/*");
    ctx.addFilterMap(filterMap);
}
 
Example #18
Source File: ApplicationFilterFactory.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #19
Source File: ApplicationFilterRegistration.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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?
}
 
Example #20
Source File: ApplicationFilterRegistration.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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?
    
}
 
Example #21
Source File: ApplicationFilterRegistration.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #22
Source File: ApplicationFilterRegistration.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #23
Source File: TestRequest.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #24
Source File: TestRestCsrfPreventionFilter2.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #25
Source File: TestAddCharSetFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #26
Source File: TestAddCharSetFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #27
Source File: TestRestCsrfPreventionFilter2.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #28
Source File: TestRequest.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #29
Source File: ApplicationFilterRegistration.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #30
Source File: ApplicationFilterRegistration.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@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;
}