Search in sources :

Example 11 with WebXml

use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.

the class ContextConfig method processAnnotations.

protected void processAnnotations(Set<WebXml> fragments, boolean handlesTypesOnly, Map<String, JavaClassCacheEntry> javaClassCache) {
    for (WebXml fragment : fragments) {
        // Only need to scan for @HandlesTypes matches if any of the
        // following are true:
        // - it has already been determined only @HandlesTypes is required
        //   (e.g. main web.xml has metadata-complete="true"
        // - this fragment is for a container JAR (Servlet 3.1 section 8.1)
        // - this fragment has metadata-complete="true"
        boolean htOnly = handlesTypesOnly || !fragment.getWebappJar() || fragment.isMetadataComplete();
        WebXml annotations = new WebXml();
        // no impact on distributable
        annotations.setDistributable(true);
        URL url = fragment.getURL();
        processAnnotationsUrl(url, annotations, htOnly, javaClassCache);
        Set<WebXml> set = new HashSet<>();
        set.add(annotations);
        // Merge annotations into fragment - fragment takes priority
        fragment.merge(set);
    }
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) URL(java.net.URL) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 12 with WebXml

use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.

the class ContextConfig method getDefaultWebXmlFragment.

private WebXml getDefaultWebXmlFragment(WebXmlParser webXmlParser) {
    // Host should never be null
    Host host = (Host) context.getParent();
    DefaultWebXmlCacheEntry entry = hostWebXmlCache.get(host);
    InputSource globalWebXml = getGlobalWebXmlSource();
    InputSource hostWebXml = getHostWebXmlSource();
    long globalTimeStamp = 0;
    long hostTimeStamp = 0;
    if (globalWebXml != null) {
        URLConnection uc = null;
        try {
            URL url = new URL(globalWebXml.getSystemId());
            uc = url.openConnection();
            globalTimeStamp = uc.getLastModified();
        } catch (IOException e) {
            globalTimeStamp = -1;
        } finally {
            if (uc != null) {
                try {
                    uc.getInputStream().close();
                } catch (IOException e) {
                    ExceptionUtils.handleThrowable(e);
                    globalTimeStamp = -1;
                }
            }
        }
    }
    if (hostWebXml != null) {
        URLConnection uc = null;
        try {
            URL url = new URL(hostWebXml.getSystemId());
            uc = url.openConnection();
            hostTimeStamp = uc.getLastModified();
        } catch (IOException e) {
            hostTimeStamp = -1;
        } finally {
            if (uc != null) {
                try {
                    uc.getInputStream().close();
                } catch (IOException e) {
                    ExceptionUtils.handleThrowable(e);
                    hostTimeStamp = -1;
                }
            }
        }
    }
    if (entry != null && entry.getGlobalTimeStamp() == globalTimeStamp && entry.getHostTimeStamp() == hostTimeStamp) {
        InputSourceUtil.close(globalWebXml);
        InputSourceUtil.close(hostWebXml);
        return entry.getWebXml();
    }
    // already be held on the host by another thread
    synchronized (host.getPipeline()) {
        entry = hostWebXmlCache.get(host);
        if (entry != null && entry.getGlobalTimeStamp() == globalTimeStamp && entry.getHostTimeStamp() == hostTimeStamp) {
            return entry.getWebXml();
        }
        WebXml webXmlDefaultFragment = createWebXml();
        webXmlDefaultFragment.setOverridable(true);
        // Set to distributable else every app will be prevented from being
        // distributable when the default fragment is merged with the main
        // web.xml
        webXmlDefaultFragment.setDistributable(true);
        // When merging, the default welcome files are only used if the app has
        // not defined any welcomes files.
        webXmlDefaultFragment.setAlwaysAddWelcomeFiles(false);
        // Parse global web.xml if present
        if (globalWebXml == null) {
            // This is unusual enough to log
            log.info(sm.getString("contextConfig.defaultMissing"));
        } else {
            if (!webXmlParser.parseWebXml(globalWebXml, webXmlDefaultFragment, false)) {
                ok = false;
            }
        }
        // Parse host level web.xml if present
        // Additive apart from welcome pages
        webXmlDefaultFragment.setReplaceWelcomeFiles(true);
        if (!webXmlParser.parseWebXml(hostWebXml, webXmlDefaultFragment, false)) {
            ok = false;
        }
        // Don't update the cache if an error occurs
        if (globalTimeStamp != -1 && hostTimeStamp != -1) {
            entry = new DefaultWebXmlCacheEntry(webXmlDefaultFragment, globalTimeStamp, hostTimeStamp);
            hostWebXmlCache.put(host, entry);
        }
        return webXmlDefaultFragment;
    }
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) InputSource(org.xml.sax.InputSource) Host(org.apache.catalina.Host) StandardHost(org.apache.catalina.core.StandardHost) IOException(java.io.IOException) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 13 with WebXml

use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.

the class TestSchemaValidation method testWebapp_3_1.

@Test
public void testWebapp_3_1() throws Exception {
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
    digester.setErrorHandler(handler);
    digester.push(new WebXml());
    WebXml desc = (WebXml) digester.parse(new File("test/webapp-3.1/WEB-INF/web.xml"));
    Assert.assertEquals("3.1", desc.getVersion());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) WebRuleSet(org.apache.tomcat.util.descriptor.web.WebRuleSet) File(java.io.File) Test(org.junit.Test)

Example 14 with WebXml

use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.

the class TestSchemaValidation method testWebapp_4_0.

@Test
public void testWebapp_4_0() throws Exception {
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
    digester.setErrorHandler(handler);
    digester.push(new WebXml());
    WebXml desc = (WebXml) digester.parse(new File("test/webapp-4.0/WEB-INF/web.xml"));
    Assert.assertEquals("4.0", desc.getVersion());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) WebRuleSet(org.apache.tomcat.util.descriptor.web.WebRuleSet) File(java.io.File) Test(org.junit.Test)

Example 15 with WebXml

use of org.apache.tomcat.util.descriptor.web.WebXml in project tomcat by apache.

the class TestSchemaValidation method testWebapp.

@Test
public void testWebapp() throws Exception {
    XmlErrorHandler handler = new XmlErrorHandler();
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(false), true);
    digester.setErrorHandler(handler);
    digester.push(new WebXml());
    WebXml desc = (WebXml) digester.parse(new File("test/webapp/WEB-INF/web.xml"));
    Assert.assertEquals("4.0", desc.getVersion());
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) Digester(org.apache.tomcat.util.digester.Digester) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) WebRuleSet(org.apache.tomcat.util.descriptor.web.WebRuleSet) File(java.io.File) Test(org.junit.Test)

Aggregations

WebXml (org.apache.tomcat.util.descriptor.web.WebXml)22 File (java.io.File)18 Test (org.junit.Test)17 HashMap (java.util.HashMap)10 JavaClassCacheEntry (org.apache.catalina.startup.ContextConfig.JavaClassCacheEntry)9 XmlErrorHandler (org.apache.tomcat.util.descriptor.XmlErrorHandler)8 WebRuleSet (org.apache.tomcat.util.descriptor.web.WebRuleSet)8 Digester (org.apache.tomcat.util.digester.Digester)8 ServletDef (org.apache.tomcat.util.descriptor.web.ServletDef)5 URL (java.net.URL)4 IOException (java.io.IOException)3 FilterDef (org.apache.tomcat.util.descriptor.web.FilterDef)3 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)2 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)2 WebXmlParser (org.apache.tomcat.util.descriptor.web.WebXmlParser)2 InputSource (org.xml.sax.InputSource)2 URISyntaxException (java.net.URISyntaxException)1 URLConnection (java.net.URLConnection)1