use of org.apache.tomcat.util.descriptor.web.FilterMap in project tomcat by apache.
the class ContextConfig method configureStop.
/**
* Process a "stop" event for this Context.
*/
protected synchronized void configureStop() {
if (log.isDebugEnabled()) {
log.debug(sm.getString("contextConfig.stop"));
}
int i;
// Removing children
Container[] children = context.findChildren();
for (i = 0; i < children.length; i++) {
context.removeChild(children[i]);
}
// Removing application parameters
/*
ApplicationParameter[] applicationParameters =
context.findApplicationParameters();
for (i = 0; i < applicationParameters.length; i++) {
context.removeApplicationParameter
(applicationParameters[i].getName());
}
*/
// Removing security constraints
SecurityConstraint[] securityConstraints = context.findConstraints();
for (i = 0; i < securityConstraints.length; i++) {
context.removeConstraint(securityConstraints[i]);
}
// Removing Ejbs
/*
ContextEjb[] contextEjbs = context.findEjbs();
for (i = 0; i < contextEjbs.length; i++) {
context.removeEjb(contextEjbs[i].getName());
}
*/
// Removing environments
/*
ContextEnvironment[] contextEnvironments = context.findEnvironments();
for (i = 0; i < contextEnvironments.length; i++) {
context.removeEnvironment(contextEnvironments[i].getName());
}
*/
// Removing errors pages
ErrorPage[] errorPages = context.findErrorPages();
for (i = 0; i < errorPages.length; i++) {
context.removeErrorPage(errorPages[i]);
}
// Removing filter defs
FilterDef[] filterDefs = context.findFilterDefs();
for (i = 0; i < filterDefs.length; i++) {
context.removeFilterDef(filterDefs[i]);
}
// Removing filter maps
FilterMap[] filterMaps = context.findFilterMaps();
for (i = 0; i < filterMaps.length; i++) {
context.removeFilterMap(filterMaps[i]);
}
// Removing local ejbs
/*
ContextLocalEjb[] contextLocalEjbs = context.findLocalEjbs();
for (i = 0; i < contextLocalEjbs.length; i++) {
context.removeLocalEjb(contextLocalEjbs[i].getName());
}
*/
// Removing Mime mappings
String[] mimeMappings = context.findMimeMappings();
for (i = 0; i < mimeMappings.length; i++) {
context.removeMimeMapping(mimeMappings[i]);
}
// Removing parameters
String[] parameters = context.findParameters();
for (i = 0; i < parameters.length; i++) {
context.removeParameter(parameters[i]);
}
// Removing resource env refs
/*
String[] resourceEnvRefs = context.findResourceEnvRefs();
for (i = 0; i < resourceEnvRefs.length; i++) {
context.removeResourceEnvRef(resourceEnvRefs[i]);
}
*/
// Removing resource links
/*
ContextResourceLink[] contextResourceLinks =
context.findResourceLinks();
for (i = 0; i < contextResourceLinks.length; i++) {
context.removeResourceLink(contextResourceLinks[i].getName());
}
*/
// Removing resources
/*
ContextResource[] contextResources = context.findResources();
for (i = 0; i < contextResources.length; i++) {
context.removeResource(contextResources[i].getName());
}
*/
// Removing security role
String[] securityRoles = context.findSecurityRoles();
for (i = 0; i < securityRoles.length; i++) {
context.removeSecurityRole(securityRoles[i]);
}
// Removing servlet mappings
String[] servletMappings = context.findServletMappings();
for (i = 0; i < servletMappings.length; i++) {
context.removeServletMapping(servletMappings[i]);
}
// FIXME : Removing status pages
// Removing welcome files
String[] welcomeFiles = context.findWelcomeFiles();
for (i = 0; i < welcomeFiles.length; i++) {
context.removeWelcomeFile(welcomeFiles[i]);
}
// Removing wrapper lifecycles
String[] wrapperLifecycles = context.findWrapperLifecycles();
for (i = 0; i < wrapperLifecycles.length; i++) {
context.removeWrapperLifecycle(wrapperLifecycles[i]);
}
// Removing wrapper listeners
String[] wrapperListeners = context.findWrapperListeners();
for (i = 0; i < wrapperListeners.length; i++) {
context.removeWrapperListener(wrapperListeners[i]);
}
// Remove (partially) folders and files created by antiLocking
if (antiLockingDocBase != null) {
// No need to log failure - it is expected in this case
ExpandWar.delete(antiLockingDocBase, false);
}
// Reset ServletContextInitializer scanning
initializerClassMap.clear();
typeInitializerMap.clear();
ok = true;
}
use of org.apache.tomcat.util.descriptor.web.FilterMap in project tomcat by apache.
the class ContextMBean method findFilterMaps.
/**
* Return the set of filter mappings for this Context.
* @return a string array with a representation of all the filter mappings
* @throws MBeanException propagated from the managed resource access
*/
public String[] findFilterMaps() throws MBeanException {
Context context = doGetManagedResource();
FilterMap[] maps = context.findFilterMaps();
String[] stringMaps = new String[maps.length];
for (int counter = 0; counter < maps.length; counter++) {
stringMaps[counter] = maps[counter].toString();
}
return stringMaps;
}
use of org.apache.tomcat.util.descriptor.web.FilterMap in project tomcat by apache.
the class TestRemoteIpFilter method testWithTomcatServer.
/*
* 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<>();
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.addURLPatternDecoded("*");
root.addFilterMap(filterMap);
MockHttpServlet mockServlet = new MockHttpServlet();
Tomcat.addServlet(root, mockServlet.getClass().getName(), mockServlet);
root.addServletMappingDecoded("/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-FORWARDED-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());
}
use of org.apache.tomcat.util.descriptor.web.FilterMap in project tomcat by apache.
the class TestRestCsrfPreventionFilter2 method setUpApplication.
private void setUpApplication() throws Exception {
context = tomcat.addContext(CONTEXT_PATH_LOGIN, System.getProperty("java.io.tmpdir"));
context.setSessionTimeout(SHORT_SESSION_TIMEOUT_MINS);
Tomcat.addServlet(context, SERVLET_NAME, new TesterServlet());
context.addServletMappingDecoded(URI_PROTECTED, SERVLET_NAME);
FilterDef filterDef = new FilterDef();
filterDef.setFilterName(FILTER_NAME);
filterDef.setFilterClass(RestCsrfPreventionFilter.class.getCanonicalName());
filterDef.addInitParameter(FILTER_INIT_PARAM, REMOVE_CUSTOMER + "," + ADD_CUSTOMER);
context.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName(FILTER_NAME);
filterMap.addURLPatternDecoded(URI_CSRF_PROTECTED);
context.addFilterMap(filterMap);
SecurityCollection collection = new SecurityCollection();
collection.addPatternDecoded(URI_PROTECTED);
SecurityConstraint sc = new SecurityConstraint();
sc.addAuthRole(ROLE);
sc.addCollection(collection);
context.addConstraint(sc);
LoginConfig lc = new LoginConfig();
lc.setAuthMethod(METHOD);
context.setLoginConfig(lc);
AuthenticatorBase basicAuthenticator = new BasicAuthenticator();
context.getPipeline().addValve(basicAuthenticator);
}
use of org.apache.tomcat.util.descriptor.web.FilterMap in project tomcat by apache.
the class TestExpiresFilter method validate.
protected void validate(HttpServlet servlet, Integer expectedMaxAgeInSeconds, int expectedResponseStatusCode) throws Exception {
// SETUP
Tomcat tomcat = getTomcatInstance();
Context root = tomcat.addContext("", TEMP_DIR);
FilterDef filterDef = new FilterDef();
filterDef.addInitParameter("ExpiresDefault", "access plus 1 minute");
filterDef.addInitParameter("ExpiresByType text/xml;charset=utf-8", "access plus 3 minutes");
filterDef.addInitParameter("ExpiresByType text/xml", "access plus 5 minutes");
filterDef.addInitParameter("ExpiresByType text", "access plus 7 minutes");
filterDef.addInitParameter("ExpiresExcludedResponseStatusCodes", "304, 503");
filterDef.setFilterClass(ExpiresFilter.class.getName());
filterDef.setFilterName(ExpiresFilter.class.getName());
root.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName(ExpiresFilter.class.getName());
filterMap.addURLPatternDecoded("*");
root.addFilterMap(filterMap);
Tomcat.addServlet(root, servlet.getClass().getName(), servlet);
root.addServletMappingDecoded("/test", servlet.getClass().getName());
tomcat.start();
try {
Calendar.getInstance(TimeZone.getTimeZone("GMT"));
long timeBeforeInMillis = System.currentTimeMillis();
// TEST
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("http://localhost:" + tomcat.getConnector().getLocalPort() + "/test").openConnection();
// VALIDATE
Assert.assertEquals(expectedResponseStatusCode, httpURLConnection.getResponseCode());
StringBuilder msg = new StringBuilder();
for (Entry<String, List<String>> field : httpURLConnection.getHeaderFields().entrySet()) {
for (String value : field.getValue()) {
msg.append((field.getKey() == null ? "" : field.getKey() + ": ") + value + "\n");
}
}
System.out.println(msg);
Integer actualMaxAgeInSeconds;
String cacheControlHeader = httpURLConnection.getHeaderField("Cache-Control");
if (cacheControlHeader == null) {
actualMaxAgeInSeconds = null;
} else {
actualMaxAgeInSeconds = null;
StringTokenizer cacheControlTokenizer = new StringTokenizer(cacheControlHeader, ",");
while (cacheControlTokenizer.hasMoreTokens() && actualMaxAgeInSeconds == null) {
String cacheDirective = cacheControlTokenizer.nextToken();
StringTokenizer cacheDirectiveTokenizer = new StringTokenizer(cacheDirective, "=");
if (cacheDirectiveTokenizer.countTokens() == 2) {
String key = cacheDirectiveTokenizer.nextToken().trim();
String value = cacheDirectiveTokenizer.nextToken().trim();
if (key.equalsIgnoreCase("max-age")) {
actualMaxAgeInSeconds = Integer.valueOf(value);
}
}
}
}
if (expectedMaxAgeInSeconds == null) {
Assert.assertNull("actualMaxAgeInSeconds '" + actualMaxAgeInSeconds + "' should be null", actualMaxAgeInSeconds);
return;
}
Assert.assertNotNull(actualMaxAgeInSeconds);
int deltaInSeconds = Math.abs(actualMaxAgeInSeconds.intValue() - expectedMaxAgeInSeconds.intValue());
Assert.assertTrue("actualMaxAgeInSeconds: " + actualMaxAgeInSeconds + ", expectedMaxAgeInSeconds: " + expectedMaxAgeInSeconds + ", request time: " + timeBeforeInMillis + " for content type " + httpURLConnection.getContentType(), deltaInSeconds < 3);
} finally {
tomcat.stop();
}
}
Aggregations