use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.
the class TestContextConfigAnnotation method testDuplicateFilterMapping.
@Test
public void testDuplicateFilterMapping() throws Exception {
WebXml webxml = new WebXml();
Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
ContextConfig config = new ContextConfig();
File pFile = paramClassResource("org/apache/catalina/startup/DuplicateMappingParamFilter");
Assert.assertTrue(pFile.exists());
try {
config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
Assert.fail();
} catch (IllegalArgumentException ex) {
// ignore
}
FilterDef filterDef = webxml.getFilters().get("paramD");
Assert.assertNull(filterDef);
}
use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.
the class TestContextConfigAnnotation method testOverwriteAnnotation.
@Test
public void testOverwriteAnnotation() throws Exception {
WebXml webxml = new WebXml();
Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
ServletDef servletDef = new ServletDef();
servletDef.setServletName("param");
servletDef.setServletClass("org.apache.catalina.startup.ParamServlet");
servletDef.addInitParameter("foo", "tomcat");
servletDef.setDescription("Description");
servletDef.setDisplayName("DisplayName");
servletDef.setLargeIcon("LargeIcon");
servletDef.setSmallIcon("SmallIcon");
servletDef.setAsyncSupported("true");
servletDef.setLoadOnStartup("1");
webxml.addServlet(servletDef);
webxml.addServletMapping("/param", "param");
ContextConfig config = new ContextConfig();
File pFile = paramClassResource("org/apache/catalina/startup/ParamServlet");
Assert.assertTrue(pFile.exists());
config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
Assert.assertEquals(servletDef, webxml.getServlets().get("param"));
Assert.assertEquals("tomcat", servletDef.getParameterMap().get("foo"));
Assert.assertEquals("param", webxml.getServletMappings().get("/param"));
// annotation mapping not added s. Servlet Spec 3.0 (Nov 2009)
// 8.2.3.3.iv page 81
Assert.assertNull(webxml.getServletMappings().get("/annotation/overwrite"));
Assert.assertEquals("Description", servletDef.getDescription());
Assert.assertEquals("DisplayName", servletDef.getDisplayName());
Assert.assertEquals("LargeIcon", servletDef.getLargeIcon());
Assert.assertEquals("SmallIcon", servletDef.getSmallIcon());
Assert.assertEquals(Boolean.TRUE, servletDef.getAsyncSupported());
Assert.assertEquals(Integer.valueOf(1), servletDef.getLoadOnStartup());
Assert.assertNull(servletDef.getEnabled());
Assert.assertNull(servletDef.getJspFile());
}
use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.
the class TestContextConfigAnnotation method testNoMapping.
@Test
public void testNoMapping() throws Exception {
WebXml webxml = new WebXml();
Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
ContextConfig config = new ContextConfig();
File pFile = paramClassResource("org/apache/catalina/startup/NoMappingParamServlet");
Assert.assertTrue(pFile.exists());
config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
ServletDef servletDef = webxml.getServlets().get("param1");
Assert.assertNull(servletDef);
webxml.addServletMapping("/param", "param1");
config.processAnnotationsFile(pFile, webxml, false, javaClassCache);
servletDef = webxml.getServlets().get("param1");
Assert.assertNull(servletDef);
}
use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.
the class TestContextConfigAnnotation method testFilterMapping.
@Test
public void testFilterMapping() throws Exception {
WebXml webxml = new WebXml();
Map<String, JavaClassCacheEntry> javaClassCache = new HashMap<>();
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("Servlet says: ", fdef.getParameterMap().get("message"));
}
use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.
the class ContextConfig method processResourceJARs.
/**
* Scan JARs that contain web-fragment.xml files that will be used to
* configure this application to see if they also contain static resources.
* If static resources are found, add them to the context. Resources are
* added in web-fragment.xml priority order.
* @param fragments The set of fragments that will be scanned for
* static resources
*/
protected void processResourceJARs(Set<WebXml> fragments) {
for (WebXml fragment : fragments) {
URL url = fragment.getURL();
try {
if ("jar".equals(url.getProtocol()) || url.toString().endsWith(".jar")) {
try (Jar jar = JarFactory.newInstance(url)) {
jar.nextEntry();
String entryName = jar.getEntryName();
while (entryName != null) {
if (entryName.startsWith("META-INF/resources/")) {
context.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", url, "/META-INF/resources");
break;
}
jar.nextEntry();
entryName = jar.getEntryName();
}
}
} else if ("file".equals(url.getProtocol())) {
File file = new File(url.toURI());
File resources = new File(file, "META-INF/resources/");
if (resources.isDirectory()) {
context.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.RESOURCE_JAR, "/", resources.getAbsolutePath(), null, "/");
}
}
} catch (IOException | URISyntaxException e) {
log.error(sm.getString("contextConfig.resourceJarFail", url, context.getName()));
}
}
}
Aggregations