Java Code Examples for org.apache.catalina.deploy.FilterDef#addInitParameter()

The following examples show how to use org.apache.catalina.deploy.FilterDef#addInitParameter() . 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: 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 2
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 3
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeAllowedRemoteAddrWithNullRemoteIpHeader() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "192\\.168\\.0\\.10|192\\.168\\.0\\.11");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = request.getHeader("x-forwarded-for");
    assertNull("x-forwarded-for must be null", actualXForwardedFor);

    String actualXForwardedBy = request.getHeader("x-forwarded-by");
    assertNull("x-forwarded-by must be null", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "192.168.0.10", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "remote-host-original-value", actualRemoteHost);
}
 
Example 4
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncomingRequestIsSecuredButProtocolHeaderSaysItIsNotWithDefaultValues() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("protocolHeader", "x-forwarded-proto");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setSecure(true);
    request.setScheme("https");
    request.setHeader("x-forwarded-for", "140.211.11.130");
    request.setHeader("x-forwarded-proto", "http");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    boolean actualSecure = actualRequest.isSecure();
    assertFalse("request must be unsecured as header x-forwarded-proto said it is http", actualSecure);

    String actualScheme = actualRequest.getScheme();
    assertEquals("scheme must be http as header x-forwarded-proto said it is http", "http", actualScheme);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);

}
 
Example 5
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncomingRequestIsSecuredButProtocolHeaderSaysItIsNotWithCustomValues() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("protocolHeader", "x-forwarded-proto");
    filterDef.addInitParameter("remoteIpHeader", "x-my-forwarded-for");
    filterDef.addInitParameter("httpServerPort", "8080");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setSecure(true);
    request.setScheme("https");
    request.setHeader("x-my-forwarded-for", "140.211.11.130");
    request.setHeader("x-forwarded-proto", "http");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    boolean actualSecure = actualRequest.isSecure();
    assertFalse("request must be unsecured as header x-forwarded-proto said it is http", actualSecure);

    String actualScheme = actualRequest.getScheme();
    assertEquals("scheme must be http as header x-forwarded-proto said it is http", "http", actualScheme);

    int actualServerPort = actualRequest.getServerPort();
    assertEquals("wrong http server port", 8080, actualServerPort);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
}
 
Example 6
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeAllProxiesAreTrustedAndRemoteAddrMatchRegexp() throws Exception {

    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "127\\.0\\.0\\.1|192\\.168\\..*|another-internal-proxy");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");
    request.addHeader("x-forwarded-for", "140.211.11.130");
    request.addHeader("x-forwarded-for", "proxy1");
    request.addHeader("x-forwarded-for", "proxy2");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for");
    assertNull("all proxies are trusted, x-forwarded-for must be null", actualXForwardedFor);

    String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by");
    assertEquals("all proxies are trusted, they must appear in x-forwarded-by", "proxy1, proxy2", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
}
 
Example 7
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequestAttributesForAccessLog() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("protocolHeader", "x-forwarded-proto");
    filterDef.addInitParameter("remoteIpHeader", "x-my-forwarded-for");
    filterDef.addInitParameter("httpServerPort", "8080");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setHeader("x-my-forwarded-for", "140.211.11.130");
    request.setHeader("x-forwarded-proto", "http");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    Assert.assertEquals("org.apache.catalina.AccessLog.ServerPort",
            Integer.valueOf(8080),
            actualRequest.getAttribute(AccessLog.SERVER_PORT_ATTRIBUTE));

    Assert.assertEquals("org.apache.catalina.AccessLog.RemoteAddr",
            "140.211.11.130",
            actualRequest.getAttribute(AccessLog.REMOTE_ADDR_ATTRIBUTE));

    Assert.assertEquals("org.apache.catalina.AccessLog.RemoteHost",
            "140.211.11.130",
            actualRequest.getAttribute(AccessLog.REMOTE_HOST_ATTRIBUTE));
}
 
Example 8
Source File: TestRemoteIpFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testRequestAttributesForAccessLog() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("protocolHeader", "x-forwarded-proto");
    filterDef.addInitParameter("remoteIpHeader", "x-my-forwarded-for");
    filterDef.addInitParameter("httpServerPort", "8080");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setHeader("x-my-forwarded-for", "140.211.11.130");
    request.setHeader("x-forwarded-proto", "http");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    Assert.assertEquals("org.apache.catalina.AccessLog.ServerPort",
            Integer.valueOf(8080),
            actualRequest.getAttribute(AccessLog.SERVER_PORT_ATTRIBUTE));

    Assert.assertEquals("org.apache.catalina.AccessLog.RemoteAddr",
            "140.211.11.130",
            actualRequest.getAttribute(AccessLog.REMOTE_ADDR_ATTRIBUTE));

    Assert.assertEquals("org.apache.catalina.AccessLog.RemoteHost",
            "140.211.11.130",
            actualRequest.getAttribute(AccessLog.REMOTE_HOST_ATTRIBUTE));
}
 
Example 9
Source File: TestRemoteIpFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeUntrustedProxyInTheChain() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "192\\.168\\.0\\.10|192\\.168\\.0\\.11");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();

    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");
    request.setHeader("x-forwarded-for", "140.211.11.130, proxy1, untrusted-proxy, proxy2");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for");
    assertEquals("ip/host before untrusted-proxy must appear in x-forwarded-for", "140.211.11.130, proxy1", actualXForwardedFor);

    String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by");
    assertEquals("ip/host after untrusted-proxy must appear in  x-forwarded-by", "proxy2", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "untrusted-proxy", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "untrusted-proxy", actualRemoteHost);
}
 
Example 10
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeUntrustedProxyInTheChain() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "192\\.168\\.0\\.10|192\\.168\\.0\\.11");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();

    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");
    request.setHeader("x-forwarded-for", "140.211.11.130, proxy1, untrusted-proxy, proxy2");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for");
    assertEquals("ip/host before untrusted-proxy must appear in x-forwarded-for", "140.211.11.130, proxy1", actualXForwardedFor);

    String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by");
    assertEquals("ip/host after untrusted-proxy must appear in  x-forwarded-by", "proxy2", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "untrusted-proxy", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "untrusted-proxy", actualRemoteHost);
}
 
Example 11
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeAllProxiesAreTrustedOrInternal() throws Exception {

    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "192\\.168\\.0\\.10|192\\.168\\.0\\.11");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();

    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");
    request.setHeader("x-forwarded-for", "140.211.11.130, proxy1, proxy2, 192.168.0.10, 192.168.0.11");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for");
    assertNull("all proxies are trusted, x-forwarded-for must be null", actualXForwardedFor);

    String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by");
    assertEquals("all proxies are trusted, they must appear in x-forwarded-by", "proxy1, proxy2", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
}
 
Example 12
Source File: TestRemoteIpFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeAllProxiesAreTrustedAndRemoteAddrMatchRegexp() throws Exception {

    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "127\\.0\\.0\\.1|192\\.168\\..*|another-internal-proxy");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");
    request.addHeader("x-forwarded-for", "140.211.11.130");
    request.addHeader("x-forwarded-for", "proxy1");
    request.addHeader("x-forwarded-for", "proxy2");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for");
    assertNull("all proxies are trusted, x-forwarded-for must be null", actualXForwardedFor);

    String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by");
    assertEquals("all proxies are trusted, they must appear in x-forwarded-by", "proxy1, proxy2", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
}
 
Example 13
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeNotAllowedRemoteAddr() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "192\\.168\\.0\\.10|192\\.168\\.0\\.11");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();

    request.setRemoteAddr("not-allowed-internal-proxy");
    request.setRemoteHost("not-allowed-internal-proxy-host");
    request.setHeader("x-forwarded-for", "140.211.11.130, proxy1, proxy2");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for");
    assertEquals("x-forwarded-for must be unchanged", "140.211.11.130, proxy1, proxy2", actualXForwardedFor);

    String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by");
    assertNull("x-forwarded-by must be null", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "not-allowed-internal-proxy", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "not-allowed-internal-proxy-host", actualRemoteHost);
}
 
Example 14
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 15
Source File: TestRemoteIpFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeAllowedRemoteAddrWithNullRemoteIpHeader() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "192\\.168\\.0\\.10|192\\.168\\.0\\.11");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = request.getHeader("x-forwarded-for");
    assertNull("x-forwarded-for must be null", actualXForwardedFor);

    String actualXForwardedBy = request.getHeader("x-forwarded-by");
    assertNull("x-forwarded-by must be null", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "192.168.0.10", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "remote-host-original-value", actualRemoteHost);
}
 
Example 16
Source File: TestRemoteIpFilter.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
@Test
public void testInvokeAllProxiesAreInternal() throws Exception {

    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("internalProxies", "192\\.168\\.0\\.10|192\\.168\\.0\\.11");
    filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3");
    filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for");
    filterDef.addInitParameter("proxiesHeader", "x-forwarded-by");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setRemoteHost("remote-host-original-value");
    request.addHeader("x-forwarded-for", "140.211.11.130, 192.168.0.10, 192.168.0.11");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for");
    assertNull("all proxies are internal, x-forwarded-for must be null", actualXForwardedFor);

    String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by");
    assertNull("all proxies are internal, x-forwarded-by must be null", actualXForwardedBy);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
}
 
Example 17
Source File: TestRemoteIpFilter.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncomingRequestIsSecuredButProtocolHeaderSaysItIsNotWithCustomValues() throws Exception {
    // PREPARE
    FilterDef filterDef = new FilterDef();
    filterDef.addInitParameter("protocolHeader", "x-forwarded-proto");
    filterDef.addInitParameter("remoteIpHeader", "x-my-forwarded-for");
    filterDef.addInitParameter("httpServerPort", "8080");

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRemoteAddr("192.168.0.10");
    request.setSecure(true);
    request.setScheme("https");
    request.setHeader("x-my-forwarded-for", "140.211.11.130");
    request.setHeader("x-forwarded-proto", "http");

    // TEST
    HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest();

    // VERIFY
    boolean actualSecure = actualRequest.isSecure();
    assertFalse("request must be unsecured as header x-forwarded-proto said it is http", actualSecure);

    String actualScheme = actualRequest.getScheme();
    assertEquals("scheme must be http as header x-forwarded-proto said it is http", "http", actualScheme);

    int actualServerPort = actualRequest.getServerPort();
    assertEquals("wrong http server port", 8080, actualServerPort);

    String actualRemoteAddr = actualRequest.getRemoteAddr();
    assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr);

    String actualRemoteHost = actualRequest.getRemoteHost();
    assertEquals("remoteHost", "140.211.11.130", actualRemoteHost);
}
 
Example 18
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 19
Source File: TestContextConfigAnnotation.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
@Test
public void testOverwriteFilterMapping() throws Exception {
    WebXml webxml = new WebXml();
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterName("paramFilter");
    filterDef.setFilterClass("org.apache.catalina.startup.ParamFilter");
    filterDef.addInitParameter("message", "tomcat");
    filterDef.setDescription("Description");
    filterDef.setDisplayName("DisplayName");
    filterDef.setLargeIcon("LargeIcon");
    filterDef.setSmallIcon("SmallIcon");
    filterDef.setAsyncSupported("true");


    webxml.addFilter(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.addURLPattern("/param1");
    filterMap.setFilterName("paramFilter");
    webxml.addFilterMapping(filterMap);

    ContextConfig config = new ContextConfig();
    File sFile = paramClassResource(
            "org/apache/catalina/startup/ParamServlet");
    config.processAnnotationsFile(sFile, webxml, false);
    File fFile = paramClassResource(
            "org/apache/catalina/startup/ParamFilter");
    config.processAnnotationsFile(fFile, webxml, false);
    FilterDef fdef = webxml.getFilters().get("paramFilter");
    assertNotNull(fdef);
    assertEquals(filterDef,fdef);
    assertEquals("tomcat",fdef.getParameterMap().get("message"));
    Set<FilterMap> filterMappings = webxml.getFilterMappings();
    assertTrue(filterMappings.contains(filterMap));
    // annotation mapping not added s. Servlet Spec 3.0 (Nov 2009)
    // 8.2.3.3.vi page 81
    String[] urlPatterns = filterMap.getURLPatterns();
    assertNotNull(urlPatterns);
    assertEquals(1,urlPatterns.length);
    assertEquals("/param1",urlPatterns[0]);

    // check simple Parameter
    assertEquals("Description", fdef.getDescription());
    assertEquals("DisplayName", fdef.getDisplayName());
    assertEquals("LargeIcon", fdef.getLargeIcon());
    assertEquals("SmallIcon", fdef.getSmallIcon());
    // FIXME: Strange why servletDef is Boolean and FilterDef is String?
    assertEquals("true", fdef.getAsyncSupported());

    String[] dis = filterMap.getDispatcherNames();
    assertEquals(2, dis.length);
    assertEquals(DispatcherType.ERROR.toString(),dis[0]);
    assertEquals(DispatcherType.ASYNC.toString(),dis[1]);

}
 
Example 20
Source File: TestContextConfigAnnotation.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Test
public void testOverwriteFilterMapping() throws Exception {
    WebXml webxml = new WebXml();
    FilterDef filterDef = new FilterDef();
    filterDef.setFilterName("paramFilter");
    filterDef.setFilterClass("org.apache.catalina.startup.ParamFilter");
    filterDef.addInitParameter("message", "tomcat");
    filterDef.setDescription("Description");
    filterDef.setDisplayName("DisplayName");
    filterDef.setLargeIcon("LargeIcon");
    filterDef.setSmallIcon("SmallIcon");
    filterDef.setAsyncSupported("true");


    webxml.addFilter(filterDef);
    FilterMap filterMap = new FilterMap();
    filterMap.addURLPattern("/param1");
    filterMap.setFilterName("paramFilter");
    webxml.addFilterMapping(filterMap);

    ContextConfig config = new ContextConfig();
    File sFile = paramClassResource(
            "org/apache/catalina/startup/ParamServlet");
    config.processAnnotationsFile(sFile, webxml, false);
    File fFile = paramClassResource(
            "org/apache/catalina/startup/ParamFilter");
    config.processAnnotationsFile(fFile, webxml, false);
    FilterDef fdef = webxml.getFilters().get("paramFilter");
    assertNotNull(fdef);
    assertEquals(filterDef,fdef);
    assertEquals("tomcat",fdef.getParameterMap().get("message"));
    Set<FilterMap> filterMappings = webxml.getFilterMappings();
    assertTrue(filterMappings.contains(filterMap));
    // annotation mapping not added s. Servlet Spec 3.0 (Nov 2009)
    // 8.2.3.3.vi page 81
    String[] urlPatterns = filterMap.getURLPatterns();
    assertNotNull(urlPatterns);
    assertEquals(1,urlPatterns.length);
    assertEquals("/param1",urlPatterns[0]);

    // check simple Parameter
    assertEquals("Description", fdef.getDescription());
    assertEquals("DisplayName", fdef.getDisplayName());
    assertEquals("LargeIcon", fdef.getLargeIcon());
    assertEquals("SmallIcon", fdef.getSmallIcon());
    // FIXME: Strange why servletDef is Boolean and FilterDef is String?
    assertEquals("true", fdef.getAsyncSupported());

    String[] dis = filterMap.getDispatcherNames();
    assertEquals(2, dis.length);
    assertEquals(DispatcherType.ERROR.toString(),dis[0]);
    assertEquals(DispatcherType.ASYNC.toString(),dis[1]);

}