本文整理汇总了Java中org.apache.catalina.Context.addFilterDef方法的典型用法代码示例。如果您正苦于以下问题:Java Context.addFilterDef方法的具体用法?Java Context.addFilterDef怎么用?Java Context.addFilterDef使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.catalina.Context
的用法示例。
在下文中一共展示了Context.addFilterDef方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRemoteIpFilter
import org.apache.catalina.Context; //导入方法依赖的package包/类
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;
}
示例2: configureTest46243Context
import org.apache.catalina.Context; //导入方法依赖的package包/类
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");
}
示例3: init
import org.apache.catalina.Context; //导入方法依赖的package包/类
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;
}
示例4: doTest
import org.apache.catalina.Context; //导入方法依赖的package包/类
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);
}
示例5: testBug54170
import org.apache.catalina.Context; //导入方法依赖的package包/类
@Test
public void testBug54170() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
Tomcat.addServlet(ctx, "HelloWorld", new HelloWorldServlet());
ctx.addServletMapping("/", "HelloWorld");
// Add a filter with a name that should be escaped if used in a JMX
// object name
FilterDef filterDef = new FilterDef();
filterDef.setFilterClass(AddDefaultCharsetFilter.class.getName());
filterDef.setFilterName("bug54170*");
ctx.addFilterDef(filterDef);
tomcat.start();
final MBeanServer mbeanServer =
Registry.getRegistry(null, null).getMBeanServer();
// There should be one Servlet MBean registered
Set<ObjectName> servlets = mbeanServer.queryNames(
new ObjectName("Tomcat:j2eeType=Servlet,*"), null);
Assert.assertEquals(1, servlets.size());
// There should be one Filter MBean registered
Set<ObjectName> filters = mbeanServer.queryNames(
new ObjectName("Tomcat:j2eeType=Filter,*"), null);
Assert.assertEquals(1, filters.size());
}
示例6: setUpTomcat
import org.apache.catalina.Context; //导入方法依赖的package包/类
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();
}
示例7: testWithTomcatServer
import org.apache.catalina.Context; //导入方法依赖的package包/类
/**
* Test {@link RemoteIpFilter} in Tomcat standalone server
*/
@Test
public void testWithTomcatServer() throws Exception {
// mostly default configuration : enable "x-forwarded-proto"
Map<String, String> remoteIpFilterParameter = new HashMap<String, String>();
remoteIpFilterParameter.put("protocolHeader", "x-forwarded-proto");
// SETUP
Tomcat tomcat = getTomcatInstance();
Context root = tomcat.addContext("", TEMP_DIR);
FilterDef filterDef = new FilterDef();
filterDef.getParameterMap().putAll(remoteIpFilterParameter);
filterDef.setFilterClass(RemoteIpFilter.class.getName());
filterDef.setFilterName(RemoteIpFilter.class.getName());
root.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName(RemoteIpFilter.class.getName());
filterMap.addURLPattern("*");
root.addFilterMap(filterMap);
MockHttpServlet mockServlet = new MockHttpServlet();
Tomcat.addServlet(root, mockServlet.getClass().getName(), mockServlet);
root.addServletMapping("/test", mockServlet.getClass().getName());
getTomcatInstance().start();
// TEST
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(
"http://localhost:" + tomcat.getConnector().getLocalPort() +
"/test").openConnection();
String expectedRemoteAddr = "my-remote-addr";
httpURLConnection.addRequestProperty("x-forwarded-for", expectedRemoteAddr);
httpURLConnection.addRequestProperty("x-forwarded-proto", "https");
// VALIDATE
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpURLConnection.getResponseCode());
HttpServletRequest request = mockServlet.getRequest();
Assert.assertNotNull(request);
// VALIDATE X-FOWARDED-FOR
Assert.assertEquals(expectedRemoteAddr, request.getRemoteAddr());
Assert.assertEquals(expectedRemoteAddr, request.getRemoteHost());
// VALIDATE X-FORWARDED-PROTO
Assert.assertTrue(request.isSecure());
Assert.assertEquals("https", request.getScheme());
Assert.assertEquals(443, request.getServerPort());
}
示例8: addFilterToContext
import org.apache.catalina.Context; //导入方法依赖的package包/类
/**
* 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);
}