Search in sources :

Example 1 with WebXmlParser

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

the class ContextConfig method webConfig.

/**
 * Scan the web.xml files that apply to the web application and merge them
 * using the rules defined in the spec. For the global web.xml files,
 * where there is duplicate configuration, the most specific level wins. ie
 * an application's web.xml takes precedence over the host level or global
 * web.xml file.
 */
protected void webConfig() {
    /*
         * Anything and everything can override the global and host defaults.
         * This is implemented in two parts
         * - Handle as a web fragment that gets added after everything else so
         *   everything else takes priority
         * - Mark Servlets as overridable so SCI configuration can replace
         *   configuration from the defaults
         */
    /*
         * The rules for annotation scanning are not as clear-cut as one might
         * think. Tomcat implements the following process:
         * - As per SRV.1.6.2, Tomcat will scan for annotations regardless of
         *   which Servlet spec version is declared in web.xml. The EG has
         *   confirmed this is the expected behaviour.
         * - As per http://java.net/jira/browse/SERVLET_SPEC-36, if the main
         *   web.xml is marked as metadata-complete, JARs are still processed
         *   for SCIs.
         * - If metadata-complete=true and an absolute ordering is specified,
         *   JARs excluded from the ordering are also excluded from the SCI
         *   processing.
         * - If an SCI has a @HandlesType annotation then all classes (except
         *   those in JARs excluded from an absolute ordering) need to be
         *   scanned to check if they match.
         */
    WebXmlParser webXmlParser = new WebXmlParser(context.getXmlNamespaceAware(), context.getXmlValidation(), context.getXmlBlockExternal());
    Set<WebXml> defaults = new HashSet<>();
    defaults.add(getDefaultWebXmlFragment(webXmlParser));
    Set<WebXml> tomcatWebXml = new HashSet<>();
    tomcatWebXml.add(getTomcatWebXmlFragment(webXmlParser));
    WebXml webXml = createWebXml();
    // Parse context level web.xml
    InputSource contextWebXml = getContextWebXmlSource();
    if (!webXmlParser.parseWebXml(contextWebXml, webXml, false)) {
        ok = false;
    }
    ServletContext sContext = context.getServletContext();
    // Ordering is important here
    // Step 1. Identify all the JARs packaged with the application and those
    // provided by the container. If any of the application JARs have a
    // web-fragment.xml it will be parsed at this point. web-fragment.xml
    // files are ignored for container provided JARs.
    Map<String, WebXml> fragments = processJarsForWebFragments(webXml, webXmlParser);
    // Step 2. Order the fragments.
    Set<WebXml> orderedFragments = null;
    orderedFragments = WebXml.orderWebFragments(webXml, fragments, sContext);
    // Step 3. Look for ServletContainerInitializer implementations
    if (ok) {
        processServletContainerInitializers();
    }
    if (!webXml.isMetadataComplete() || typeInitializerMap.size() > 0) {
        // Steps 4 & 5.
        processClasses(webXml, orderedFragments);
    }
    if (!webXml.isMetadataComplete()) {
        // file.
        if (ok) {
            ok = webXml.merge(orderedFragments);
        }
        // Step 7a
        // merge tomcat-web.xml
        webXml.merge(tomcatWebXml);
        // Step 7b. Apply global defaults
        // Have to merge defaults before JSP conversion since defaults
        // provide JSP servlet definition.
        webXml.merge(defaults);
        // Step 8. Convert explicitly mentioned jsps to servlets
        if (ok) {
            convertJsps(webXml);
        }
        // Step 9. Apply merged web.xml to Context
        if (ok) {
            configureContext(webXml);
        }
    } else {
        webXml.merge(tomcatWebXml);
        webXml.merge(defaults);
        convertJsps(webXml);
        configureContext(webXml);
    }
    if (context.getLogEffectiveWebXml()) {
        log.info(sm.getString("contextConfig.effectiveWebXml", webXml.toXml()));
    }
    // Step 10. Look for static resources packaged in JARs
    if (ok) {
        // Spec does not define an order.
        // Use ordered JARs followed by remaining JARs
        Set<WebXml> resourceJars = new LinkedHashSet<>(orderedFragments);
        for (WebXml fragment : fragments.values()) {
            if (!resourceJars.contains(fragment)) {
                resourceJars.add(fragment);
            }
        }
        processResourceJARs(resourceJars);
    // See also StandardContext.resourcesStart() for
    // WEB-INF/classes/META-INF/resources configuration
    }
    // context
    if (ok) {
        for (Map.Entry<ServletContainerInitializer, Set<Class<?>>> entry : initializerClassMap.entrySet()) {
            if (entry.getValue().isEmpty()) {
                context.addServletContainerInitializer(entry.getKey(), null);
            } else {
                context.addServletContainerInitializer(entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InputSource(org.xml.sax.InputSource) Set(java.util.Set) RuleSet(org.apache.tomcat.util.digester.RuleSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) WebXmlParser(org.apache.tomcat.util.descriptor.web.WebXmlParser) ServletContainerInitializer(jakarta.servlet.ServletContainerInitializer) WebXml(org.apache.tomcat.util.descriptor.web.WebXml) ServletContext(jakarta.servlet.ServletContext) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with WebXmlParser

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

the class JspCServletContext method buildMergedWebXml.

private WebXml buildMergedWebXml(boolean validate, boolean blockExternal) throws JasperException {
    WebXml webXml = new WebXml();
    WebXmlParser webXmlParser = new WebXmlParser(validate, validate, blockExternal);
    // Use this class's classloader as Ant will have set the TCCL to its own
    webXmlParser.setClassLoader(getClass().getClassLoader());
    try {
        URL url = getResource(org.apache.tomcat.util.descriptor.web.Constants.WEB_XML_LOCATION);
        if (!webXmlParser.parseWebXml(url, webXml, false)) {
            throw new JasperException(Localizer.getMessage("jspc.error.invalidWebXml"));
        }
    } catch (IOException e) {
        throw new JasperException(e);
    }
    // if the application is metadata-complete then we can skip fragment processing
    if (webXml.isMetadataComplete()) {
        return webXml;
    }
    // If an empty absolute ordering element is present, fragment processing
    // may be skipped.
    Set<String> absoluteOrdering = webXml.getAbsoluteOrdering();
    if (absoluteOrdering != null && absoluteOrdering.isEmpty()) {
        return webXml;
    }
    Map<String, WebXml> fragments = scanForFragments(webXmlParser);
    Set<WebXml> orderedFragments = WebXml.orderWebFragments(webXml, fragments, this);
    // Find resource JARs
    this.resourceJARs = scanForResourceJARs(orderedFragments, fragments.values());
    // JspC is not affected by annotations so skip that processing, proceed to merge
    webXml.merge(orderedFragments);
    return webXml;
}
Also used : WebXml(org.apache.tomcat.util.descriptor.web.WebXml) JasperException(org.apache.jasper.JasperException) IOException(java.io.IOException) WebXmlParser(org.apache.tomcat.util.descriptor.web.WebXmlParser) URL(java.net.URL)

Aggregations

WebXml (org.apache.tomcat.util.descriptor.web.WebXml)2 WebXmlParser (org.apache.tomcat.util.descriptor.web.WebXmlParser)2 ServletContainerInitializer (jakarta.servlet.ServletContainerInitializer)1 ServletContext (jakarta.servlet.ServletContext)1 IOException (java.io.IOException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JasperException (org.apache.jasper.JasperException)1 FilterMap (org.apache.tomcat.util.descriptor.web.FilterMap)1 RuleSet (org.apache.tomcat.util.digester.RuleSet)1 InputSource (org.xml.sax.InputSource)1