Java Code Examples for org.apache.catalina.deploy.FilterMap#addServletName()

The following examples show how to use org.apache.catalina.deploy.FilterMap#addServletName() . 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: 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 2
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 3
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 4
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 5
Source File: ApplicationFilterRegistration.java    From Tomcat7.0.67 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 6
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 7
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 8
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);
}