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
ByteChunk bc = new ByteChunk();
Map<String, List<String>> responseHeaders = new HashMap<>();
int rc = getUrl("http://localhost:" + getPort() + "/test", bc, responseHeaders);
// VALIDATE
Assert.assertEquals(expectedResponseStatusCode, rc);
StringBuilder msg = new StringBuilder();
for (Entry<String, List<String>> field : responseHeaders.entrySet()) {
for (String value : field.getValue()) {
msg.append((field.getKey() == null ? "" : field.getKey() + ": ") + value + "\n");
}
}
System.out.println(msg);
Integer actualMaxAgeInSeconds;
String cacheControlHeader = getSingleHeader("Cache-Control", responseHeaders);
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);
String contentType = getSingleHeader("Content-Type", responseHeaders);
int deltaInSeconds = Math.abs(actualMaxAgeInSeconds.intValue() - expectedMaxAgeInSeconds.intValue());
Assert.assertTrue("actualMaxAgeInSeconds: " + actualMaxAgeInSeconds + ", expectedMaxAgeInSeconds: " + expectedMaxAgeInSeconds + ", request time: " + timeBeforeInMillis + " for content type " + contentType, deltaInSeconds < 3);
} finally {
tomcat.stop();
}
}
use of org.apache.tomcat.util.descriptor.web.FilterMap in project tomcat by apache.
the class TestExpiresFilter method testBug63909.
/*
* Tests Expires filter with:
* - per content type expires
* - no default
* - Default servlet returning 304s (without content-type)
*/
@Test
public void testBug63909() throws Exception {
Tomcat tomcat = getTomcatInstanceTestWebapp(false, false);
Context ctxt = (Context) tomcat.getHost().findChild("/test");
FilterDef filterDef = new FilterDef();
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", "");
filterDef.setFilterClass(ExpiresFilter.class.getName());
filterDef.setFilterName(ExpiresFilter.class.getName());
ctxt.addFilterDef(filterDef);
FilterMap filterMap = new FilterMap();
filterMap.setFilterName(ExpiresFilter.class.getName());
filterMap.addURLPatternDecoded("*");
ctxt.addFilterMap(filterMap);
tomcat.start();
ByteChunk bc = new ByteChunk();
Map<String, List<String>> requestHeaders = new CaseInsensitiveKeyMap<>();
List<String> ifModifiedSinceValues = new ArrayList<>();
ifModifiedSinceValues.add(FastHttpDateFormat.getCurrentDate());
requestHeaders.put("If-Modified-Since", ifModifiedSinceValues);
Map<String, List<String>> responseHeaders = new CaseInsensitiveKeyMap<>();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug6nnnn/bug69303.txt", bc, requestHeaders, responseHeaders);
Assert.assertEquals(HttpServletResponse.SC_NOT_MODIFIED, rc);
StringBuilder msg = new StringBuilder();
for (Entry<String, List<String>> field : responseHeaders.entrySet()) {
for (String value : field.getValue()) {
msg.append((field.getKey() == null ? "" : field.getKey() + ": ") + value + "\n");
}
}
System.out.println(msg);
Integer actualMaxAgeInSeconds;
String cacheControlHeader = getSingleHeader("Cache-Control", responseHeaders);
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);
}
}
}
}
Assert.assertNotNull(actualMaxAgeInSeconds);
Assert.assertTrue(Math.abs(actualMaxAgeInSeconds.intValue() - 420) < 3);
}
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 TestRegExpCapture method testBug53387.
@Test
public void testBug53387() throws Exception {
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
// SSI requires a privileged Context
ctx.setPrivileged(true);
FilterDef ssiFilter = new FilterDef();
ssiFilter.setFilterName("ssiFilter");
ssiFilter.setFilterClass(SSIFilter.class.getName());
FilterMap ssiFilterMap = new FilterMap();
ssiFilterMap.setFilterName("ssiFilter");
ssiFilterMap.addURLPatternDecoded("*.shtml");
ctx.addFilterDef(ssiFilter);
ctx.addFilterMap(ssiFilterMap);
ctx.addMimeMapping("shtml", "text/x-server-parsed-html");
tomcat.start();
ByteChunk body = new ByteChunk();
int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug53387.shtml" + queryString, body, null);
Assert.assertEquals(200, rc);
String text = body.toString();
Assert.assertTrue(text, text.contains(expectedInBody));
}
Aggregations