Search in sources :

Example 36 with WebXml

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);
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 37 with WebXml

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());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) HashMap(java.util.HashMap) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 38 with WebXml

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);
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) HashMap(java.util.HashMap) ServletDef(org.apache.tomcat.util.descriptor.web.ServletDef) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 39 with WebXml

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"));
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) HashMap(java.util.HashMap) JavaClassCacheEntry(org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry) File(java.io.File) Test(org.junit.Test)

Example 40 with WebXml

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()));
        }
    }
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Jar(org.apache.tomcat.Jar) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) File(java.io.File) URL(java.net.URL)

Aggregations

WebXml (org.apache.tomcat.util.descriptor.web.WebXml)43 File (java.io.File)31 Test (org.junit.Test)28 WebRuleSet (org.apache.tomcat.util.descriptor.web.WebRuleSet)21 Digester (org.apache.tomcat.util.digester.Digester)21 XmlErrorHandler (org.apache.tomcat.util.descriptor.XmlErrorHandler)20 HashMap (java.util.HashMap)10 JavaClassCacheEntry (org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry)9 URL (java.net.URL)7 ServletDef (org.apache.tomcat.util.descriptor.web.ServletDef)7 IOException (java.io.IOException)6 InputSource (org.xml.sax.InputSource)6 HashSet (java.util.HashSet)4 LinkedHashSet (java.util.LinkedHashSet)4 URISyntaxException (java.net.URISyntaxException)3 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)3 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 StandardContext (org.apache.catalina.core.StandardContext)2