Search in sources :

Example 41 with WebXml

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

the class ContextConfig method getTomcatWebXmlFragment.

private WebXml getTomcatWebXmlFragment(WebXmlParser webXmlParser) {
    WebXml webXmlTomcatFragment = createWebXml();
    webXmlTomcatFragment.setOverridable(true);
    // Set to distributable else every app will be prevented from being
    // distributable when the Tomcat fragment is merged with the main
    // web.xml
    webXmlTomcatFragment.setDistributable(true);
    // When merging, the default welcome files are only used if the app has
    // not defined any welcomes files.
    webXmlTomcatFragment.setAlwaysAddWelcomeFiles(false);
    WebResource resource = context.getResources().getResource(Constants.TomcatWebXml);
    if (resource.isFile()) {
        try {
            InputSource source = new InputSource(resource.getURL().toURI().toString());
            source.setByteStream(resource.getInputStream());
            if (!webXmlParser.parseWebXml(source, webXmlTomcatFragment, false)) {
                ok = false;
            }
        } catch (URISyntaxException e) {
            log.error(sm.getString("contextConfig.tomcatWebXmlError"), e);
        }
    }
    return webXmlTomcatFragment;
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) InputSource(org.xml.sax.InputSource) WebResource(org.apache.catalina.WebResource) URISyntaxException(java.net.URISyntaxException)

Example 42 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);
            // Add a Lifecycle listener to the Host that will remove it from
            // the hostWebXmlCache once the Host is destroyed
            host.addLifecycleListener(new HostWebXmlCacheCleaner());
        }
        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 43 with WebXml

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

the class JspCServletContext method scanForResourceJARs.

private List<URL> scanForResourceJARs(Set<WebXml> orderedFragments, Collection<WebXml> fragments) throws JasperException {
    List<URL> resourceJars = new ArrayList<>();
    // Build list of potential resource JARs. Use same ordering as ContextConfig
    Set<WebXml> resourceFragments = new LinkedHashSet<>(orderedFragments);
    for (WebXml fragment : fragments) {
        if (!resourceFragments.contains(fragment)) {
            resourceFragments.add(fragment);
        }
    }
    for (WebXml resourceFragment : resourceFragments) {
        try (Jar jar = JarFactory.newInstance(resourceFragment.getURL())) {
            if (jar.exists("META-INF/resources/")) {
                // This is a resource JAR
                resourceJars.add(resourceFragment.getURL());
            }
        } catch (IOException ioe) {
            throw new JasperException(ioe);
        }
    }
    return resourceJars;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WebXml(org.apache.tomcat.util.descriptor.web.WebXml) JasperException(org.apache.jasper.JasperException) ArrayList(java.util.ArrayList) Jar(org.apache.tomcat.Jar) IOException(java.io.IOException) 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