本文整理匯總了Java中org.apache.catalina.Context.addFilterMap方法的典型用法代碼示例。如果您正苦於以下問題:Java Context.addFilterMap方法的具體用法?Java Context.addFilterMap怎麽用?Java Context.addFilterMap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.catalina.Context
的用法示例。
在下文中一共展示了Context.addFilterMap方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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();
}
示例6: 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());
}
示例7: 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);
}