Java Code Examples for org.apache.tomcat.util.descriptor.web.FilterDef#addInitParameter()
The following examples show how to use
org.apache.tomcat.util.descriptor.web.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: TestStandardContext.java From Tomcat8-Source-Read with MIT License | 6 votes |
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.addURLPatternDecoded("*"); 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.addServletMappingDecoded("/", "Bug46243"); }
Example 2
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 6 votes |
@Test public void testRequestForwarded() 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.tomcat.request.forwarded", Boolean.TRUE, actualRequest.getAttribute(Globals.REQUEST_FORWARDED_ATTRIBUTE)); }
Example 3
Source File: MdwServletContainerFactory.java From mdw with Apache License 2.0 | 5 votes |
@Override public void customize(Context context) { context.addApplicationListener("org.apache.tomcat.websocket.server.WsContextListener"); context.addErrorPage(new ErrorPage() { @Override public int getErrorCode() { return 404; } @Override public String getLocation() { return "/404"; } }); context.addErrorPage(new ErrorPage() { @Override public int getErrorCode() { return 500; } @Override public String getLocation() { return "/error"; } }); // CORS access is wide open FilterDef corsFilter = new FilterDef(); corsFilter.setFilterName("CorsFilter"); corsFilter.setFilterClass("org.apache.catalina.filters.CorsFilter"); corsFilter.addInitParameter("cors.allowed.methods", "GET,POST,PUT,DELETE,HEAD,OPTIONS"); corsFilter.addInitParameter("cors.allowed.headers", "Authorization,Content-Type,X-Requested-With,Accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Accept-Encoding,Accept-Language,Cache-Control,Connection,Host,Pragma,Referer,User-Agent"); corsFilter.addInitParameter("cors.allowed.origins", "*"); context.addFilterDef(corsFilter); FilterMap filterMap = new FilterMap(); filterMap.setFilterName(corsFilter.getFilterName()); filterMap.addURLPattern("/api/*"); filterMap.addURLPattern("/services/AppSummary"); context.addFilterMap(filterMap); }
Example 4
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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 5
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@Test public void testInvokeXforwardedHostAndPort() throws Exception { // PREPARE FilterDef filterDef = new FilterDef(); filterDef.addInitParameter("hostHeader", "x-forwarded-host"); filterDef.addInitParameter("portHeader", "x-forwarded-port"); filterDef.addInitParameter("protocolHeader", "x-forwarded-proto"); MockHttpServletRequest request = new MockHttpServletRequest(); // client ip request.setRemoteAddr("192.168.0.10"); request.setRemoteHost("192.168.0.10"); // protocol request.setSecure(false); request.setServerPort(8080); request.setScheme("http"); // host and port request.getCoyoteRequest().serverName().setString("10.0.0.1"); request.setHeader("x-forwarded-host", "example.com:8443"); request.setHeader("x-forwarded-proto", "https"); // TEST HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest(); // VERIFY // protocol String actualServerName = actualRequest.getServerName(); Assert.assertEquals("postInvoke serverName", "example.com", actualServerName); String actualScheme = actualRequest.getScheme(); Assert.assertEquals("postInvoke scheme", "https", actualScheme); int actualServerPort = actualRequest.getServerPort(); Assert.assertEquals("postInvoke serverPort", 443, actualServerPort); boolean actualSecure = actualRequest.isSecure(); Assert.assertTrue("postInvoke secure", actualSecure); }
Example 6
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@Test public void testInvokeXforwardedHost() throws Exception { // PREPARE FilterDef filterDef = new FilterDef(); filterDef.addInitParameter("hostHeader", "x-forwarded-host"); filterDef.addInitParameter("portHeader", "x-forwarded-port"); filterDef.addInitParameter("protocolHeader", "x-forwarded-proto"); MockHttpServletRequest request = new MockHttpServletRequest(); // client ip request.setRemoteAddr("192.168.0.10"); request.setRemoteHost("192.168.0.10"); // protocol request.setSecure(false); request.setServerPort(8080); request.setScheme("http"); // host and port request.getCoyoteRequest().serverName().setString("10.0.0.1"); request.setHeader("x-forwarded-host", "example.com"); request.setHeader("x-forwarded-port", "8443"); request.setHeader("x-forwarded-proto", "https"); // TEST HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest(); // VERIFY // protocol String actualServerName = actualRequest.getServerName(); Assert.assertEquals("postInvoke serverName", "example.com", actualServerName); String actualScheme = actualRequest.getScheme(); Assert.assertEquals("postInvoke scheme", "https", actualScheme); int actualServerPort = actualRequest.getServerPort(); Assert.assertEquals("postInvoke serverPort", 8443, actualServerPort); boolean actualSecure = actualRequest.isSecure(); Assert.assertTrue("postInvoke secure", actualSecure); }
Example 7
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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"); Assert.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"); Assert.assertEquals("ip/host after untrusted-proxy must appear in x-forwarded-by", "proxy2", actualXForwardedBy); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "untrusted-proxy", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "untrusted-proxy", actualRemoteHost); }
Example 8
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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"); Assert.assertEquals("x-forwarded-for must be unchanged", "140.211.11.130, proxy1, proxy2", actualXForwardedFor); String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by"); Assert.assertNull("x-forwarded-by must be null", actualXForwardedBy); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "not-allowed-internal-proxy", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "not-allowed-internal-proxy-host", actualRemoteHost); }
Example 9
Source File: AbstractTomcatMetricsTest.java From tomcat_exporter with Apache License 2.0 | 5 votes |
public static void setUpTomcat(String dataSourceFactory) throws LifecycleException, ServletException { // create a tomcat instance tomcat = new Tomcat(); tomcat.setBaseDir("."); tomcat.setPort(0); tomcat.enableNaming(); // create a context with our test servlet Context ctx = tomcat.addContext(CONTEXT_PATH, new File(".").getAbsolutePath()); Tomcat.addServlet(ctx, SERVLET_NAME, new TestServlet()); ctx.addServletMappingDecoded("/*", SERVLET_NAME); // add our metrics filter FilterDef def = new FilterDef(); def.setFilterClass(TomcatServletMetricsFilter.class.getName()); def.setFilterName("metricsFilter"); def.addInitParameter("buckets",".01, .05, .1, .25, .5, 1, 2.5, 5, 10, 30"); ctx.addFilterDef(def); FilterMap map = new FilterMap(); map.setFilterName("metricsFilter"); map.addURLPattern("/*"); ctx.addFilterMap(map); // create a datasource ContextResource resource = new ContextResource(); resource.setName("jdbc/db"); resource.setAuth("Container"); resource.setType("javax.sql.DataSource"); resource.setScope("Sharable"); resource.setProperty("name", "foo"); resource.setProperty("factory", dataSourceFactory); resource.setProperty("driverClassName", "org.h2.Driver"); resource.setProperty("url", "jdbc:h2:mem:dummy"); resource.setProperty("jdbcInterceptors", "nl.nlighten.prometheus.tomcat.TomcatJdbcInterceptor(logFailed=true,logSlow=true,threshold=0,buckets=.01|.05|.1|1|10,slowQueryBuckets=1|10|30)"); ctx.getNamingResources().addResource(resource); // start instance tomcat.init(); tomcat.start(); }
Example 10
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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"); Assert.assertNull("all proxies are trusted, x-forwarded-for must be null", actualXForwardedFor); String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by"); Assert.assertEquals("all proxies are trusted, they must appear in x-forwarded-by", "proxy1, proxy2", actualXForwardedBy); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); }
Example 11
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@Test public void testInvokeAllProxiesAreTrustedUnusedInternal() throws Exception { // PREPARE RemoteIpFilter remoteIpFilter = new RemoteIpFilter(); FilterDef filterDef = new FilterDef(); filterDef.addInitParameter("trustedProxies", "proxy1|proxy2|proxy3"); filterDef.addInitParameter("remoteIpHeader", "x-forwarded-for"); filterDef.addInitParameter("proxiesHeader", "x-forwarded-by"); filterDef.setFilter(remoteIpFilter); MockHttpServletRequest request = new MockHttpServletRequest(); request.setRemoteAddr("proxy3"); request.setRemoteHost("remote-host-original-value"); 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"); Assert.assertNull("all proxies are trusted, x-forwarded-for must be null", actualXForwardedFor); String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by"); Assert.assertEquals("all proxies are trusted, they must appear in x-forwarded-by", "proxy1, proxy2, proxy3", actualXForwardedBy); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); }
Example 12
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@Test public void testInvokeAllProxiesAreTrusted() throws Exception { // PREPARE RemoteIpFilter remoteIpFilter = new RemoteIpFilter(); 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"); filterDef.setFilter(remoteIpFilter); 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"); // TEST HttpServletRequest actualRequest = testRemoteIpFilter(filterDef, request).getRequest(); // VERIFY String actualXForwardedFor = actualRequest.getHeader("x-forwarded-for"); Assert.assertNull("all proxies are trusted, x-forwarded-for must be null", actualXForwardedFor); String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by"); Assert.assertEquals("all proxies are trusted, they must appear in x-forwarded-by", "proxy1, proxy2", actualXForwardedBy); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); }
Example 13
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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"); Assert.assertNull("all proxies are internal, x-forwarded-for must be null", actualXForwardedFor); String actualXForwardedBy = actualRequest.getHeader("x-forwarded-by"); Assert.assertNull("all proxies are internal, x-forwarded-by must be null", actualXForwardedBy); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); }
Example 14
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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"); Assert.assertNull("x-forwarded-for must be null", actualXForwardedFor); String actualXForwardedBy = request.getHeader("x-forwarded-by"); Assert.assertNull("x-forwarded-by must be null", actualXForwardedBy); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "192.168.0.10", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "remote-host-original-value", actualRemoteHost); }
Example 15
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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(); Assert.assertFalse("request must be unsecured as header x-forwarded-proto said it is http", actualSecure); String actualScheme = actualRequest.getScheme(); Assert.assertEquals("scheme must be http as header x-forwarded-proto said it is http", "http", actualScheme); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); }
Example 16
Source File: TestRemoteIpFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
@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(); Assert.assertFalse("request must be unsecured as header x-forwarded-proto said it is http", actualSecure); String actualScheme = actualRequest.getScheme(); Assert.assertEquals("scheme must be http as header x-forwarded-proto said it is http", "http", actualScheme); int actualServerPort = actualRequest.getServerPort(); Assert.assertEquals("wrong http server port", 8080, actualServerPort); String actualRemoteAddr = actualRequest.getRemoteAddr(); Assert.assertEquals("remoteAddr", "140.211.11.130", actualRemoteAddr); String actualRemoteHost = actualRequest.getRemoteHost(); Assert.assertEquals("remoteHost", "140.211.11.130", actualRemoteHost); }
Example 17
Source File: TestAddCharSetFilter.java From Tomcat8-Source-Read with MIT License | 5 votes |
private void doTest(String encoding, String expected, int mode, boolean useSetContentType) 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, useSetContentType); Tomcat.addServlet(ctx, "servlet", servlet); ctx.addServletMappingDecoded("/", "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<>(); getUrl("http://localhost:" + getPort() + "/", new ByteChunk(), headers); String ct = getSingleHeader("Content-Type", headers).toLowerCase(Locale.ENGLISH); Assert.assertEquals("text/plain;charset=" + expected.toLowerCase(Locale.ENGLISH), ct); }
Example 18
Source File: TomcatRsRegistry.java From tomee with Apache License 2.0 | 4 votes |
@Override public AddressInfo createRsHttpListener(final String appId, final String webContext, final HttpListener listener, final ClassLoader classLoader, final String completePath, final String virtualHost, final String auth, final String realm) { String path = webContext; if (path == null) { throw new NullPointerException("contextRoot is null"); } if (listener == null) { throw new NullPointerException("listener is null"); } // find the existing host (we do not auto-create hosts) Container host; Context context = null; if (virtualHost == null) { host = hosts.getDefault(); } else { host = hosts.get(virtualHost); } if (host == null) { for (final Host h : hosts) { context = findContext(h, webContext); if (context != null) { host = h; if (classLoader != null && classLoader.equals(context.getLoader().getClassLoader())) { break; } // else try next to find something better } } if (host == null) { throw new IllegalArgumentException("Invalid virtual host '" + virtualHost + "'. Do you have a matching Host entry in the server.xml?"); } } else { context = findContext(host, webContext); } if (context == null) { throw new IllegalStateException("Invalid context '" + webContext + "'. Cannot find context in host " + host.getName()); } final CxfRsHttpListener cxfRsHttpListener = findCxfRsHttpListener(listener); final String description = "tomee-jaxrs-" + listener; String mapping = completePath; if (!completePath.endsWith("/*")) { // respect servlet spec (!= from our embedded listeners) if (completePath.endsWith("*")) { mapping = completePath.substring(0, completePath.length() - 1); } mapping = mapping + "/*"; } final String urlPattern = removeWebContext(webContext, mapping); cxfRsHttpListener.setUrlPattern(urlPattern.substring(0, urlPattern.length() - 1)); final FilterDef filterDef = new FilterDef(); filterDef.setAsyncSupported("true"); filterDef.setDescription(description); filterDef.setFilterName(description); filterDef.setDisplayName(description); filterDef.setFilter(new CXFJAXRSFilter(cxfRsHttpListener, context.findWelcomeFiles())); filterDef.setFilterClass(CXFJAXRSFilter.class.getName()); filterDef.addInitParameter("mapping", urlPattern.substring(0, urlPattern.length() - "/*".length())); // just keep base path context.addFilterDef(filterDef); final FilterMap filterMap = new FilterMap(); filterMap.addURLPattern(urlPattern); for (final DispatcherType type : DispatcherType.values()) { filterMap.setDispatcher(type.name()); } filterMap.setFilterName(filterDef.getFilterName()); context.addFilterMap(filterMap); Registrations.addFilterConfig(context, filterDef); path = address(connectors, host.getName(), webContext); final String key = address(connectors, host.getName(), completePath); listeners.put(new Key(appId, key), listener); return new AddressInfo(path, key); }
Example 19
Source File: TestContextConfigAnnotation.java From Tomcat8-Source-Read with MIT License | 4 votes |
@Test public void testOverwriteFilterMapping() throws Exception { WebXml webxml = new WebXml(); Map<String,JavaClassCacheEntry> javaClassCache = new HashMap<>(); 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.addURLPatternDecoded("/param1"); filterMap.setFilterName("paramFilter"); webxml.addFilterMapping(filterMap); ContextConfig config = new ContextConfig(); File sFile = paramClassResource( "org/apache/catalina/startup/ParamServlet"); config.processAnnotationsFile(sFile, webxml, false, javaClassCache); File fFile = paramClassResource( "org/apache/catalina/startup/ParamFilter"); config.processAnnotationsFile(fFile, webxml, false, javaClassCache); FilterDef fdef = webxml.getFilters().get("paramFilter"); Assert.assertNotNull(fdef); Assert.assertEquals(filterDef,fdef); Assert.assertEquals("tomcat",fdef.getParameterMap().get("message")); Set<FilterMap> filterMappings = webxml.getFilterMappings(); Assert.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(); Assert.assertNotNull(urlPatterns); Assert.assertEquals(1,urlPatterns.length); Assert.assertEquals("/param1",urlPatterns[0]); // check simple Parameter Assert.assertEquals("Description", fdef.getDescription()); Assert.assertEquals("DisplayName", fdef.getDisplayName()); Assert.assertEquals("LargeIcon", fdef.getLargeIcon()); Assert.assertEquals("SmallIcon", fdef.getSmallIcon()); // FIXME: Strange why servletDef is Boolean and FilterDef is String? Assert.assertEquals("true", fdef.getAsyncSupported()); String[] dis = filterMap.getDispatcherNames(); Assert.assertEquals(2, dis.length); Assert.assertEquals(DispatcherType.ERROR.toString(),dis[0]); Assert.assertEquals(DispatcherType.ASYNC.toString(),dis[1]); }
Example 20
Source File: TomcatFilterUtil.java From aceql-http with GNU Lesser General Public License v2.1 | 4 votes |
/** * Adds a predefined Filter definition and mapping to a Context Method * * @param rootCtx * the Context to add the predefined Filter to */ public static void addFilterToContext(Context rootCtx) { // See https://tomcat.apache.org/tomcat-8.5-doc/config/filter.html for // filters /* * <filter> <filter-name>CorsFilter</filter-name> * <filter-class>org.apache.catalina.filters.CorsFilter</filter-class> * <init-param> <param-name>cors.allowed.origins</param-name> * <param-value>*</param-value> </init-param> <init-param> * <param-name>cors.allowed.methods</param-name> * <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value> </init-param> * <init-param> <param-name>cors.allowed.headers</param-name> * <param-value>Content-Type,X-Requested-With,accept,Origin,Access- * Control-Request-Method,Access-Control-Request-Headers</param-value> * </init-param> * * </filter> <filter-mapping> <filter-name>CorsFilter</filter-name> * <url-pattern>/*</url-pattern> </filter-mapping> */ String filterName = "CorsFilter"; String filterClass = "org.apache.catalina.filters.CorsFilter"; FilterDef filterDef = new FilterDef(); filterDef.setFilterName(filterName); filterDef.setFilterClass(filterClass); filterDef.addInitParameter("cors.allowed.origins", "*"); filterDef.addInitParameter("cors.allowed.methods", "GET,POST,HEAD,OPTIONS,PUT"); filterDef.addInitParameter("cors.allowed.headers", "Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers"); // Don't forget mapping FilterMap filterMap = new FilterMap(); filterMap.setFilterName(filterName); filterMap.addURLPattern("/*"); // Add both filter definition & mapping to context that will be used by // a servlet: rootCtx.addFilterDef(filterDef); rootCtx.addFilterMap(filterMap); }