Search in sources :

Example 1 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class ValidatorTask method execute.

// --------------------------------------------------------- Public Methods
/**
     * Execute the specified command.  This logic only performs the common
     * attribute validation required by all subclasses; it does not perform
     * any functional logic directly.
     *
     * @exception BuildException if a validation error occurs
     */
@Override
public void execute() throws BuildException {
    if (path == null) {
        throw new BuildException("Must specify 'path'");
    }
    File file = new File(path, Constants.ApplicationWebXml);
    if (!file.canRead()) {
        throw new BuildException("Cannot find web.xml");
    }
    // Commons-logging likes having the context classloader set
    ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(ValidatorTask.class.getClassLoader());
    // Called through trusted manager interface. If running under a
    // SecurityManager assume that untrusted applications may be deployed.
    Digester digester = DigesterFactory.newDigester(true, true, null, Globals.IS_SECURITY_ENABLED);
    try (InputStream stream = new BufferedInputStream(new FileInputStream(file.getCanonicalFile()))) {
        InputSource is = new InputSource(file.toURI().toURL().toExternalForm());
        is.setByteStream(stream);
        digester.parse(is);
        handleOutput("web.xml validated");
    } catch (Exception e) {
        if (isFailOnError()) {
            throw new BuildException("Validation failure", e);
        } else {
            handleErrorOutput("Validation failure: " + e);
        }
    } finally {
        Thread.currentThread().setContextClassLoader(oldCL);
        closeRedirector();
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Digester(org.apache.tomcat.util.digester.Digester) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) FileInputStream(java.io.FileInputStream) BuildException(org.apache.tools.ant.BuildException)

Example 2 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class MemoryRealm method getDigester.

// ------------------------------------------------------ Protected Methods
/**
     * @return a configured <code>Digester</code> to use for processing
     * the XML input file, creating a new one if necessary.
     */
protected synchronized Digester getDigester() {
    if (digester == null) {
        digester = new Digester();
        digester.setValidating(false);
        try {
            digester.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
        } catch (Exception e) {
            log.warn(sm.getString("memoryRealm.xmlFeatureEncoding"), e);
        }
        digester.addRuleSet(new MemoryRuleSet());
    }
    return (digester);
}
Also used : Digester(org.apache.tomcat.util.digester.Digester) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException)

Example 3 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class MemoryRealm method startInternal.

// ------------------------------------------------------ Lifecycle Methods
/**
     * Prepare for the beginning of active use of the public methods of this
     * component and implement the requirements of
     * {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
@Override
protected void startInternal() throws LifecycleException {
    String pathName = getPathname();
    try (InputStream is = ConfigFileLoader.getInputStream(pathName)) {
        // Load the contents of the database file
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("memoryRealm.loadPath", pathName));
        }
        Digester digester = getDigester();
        try {
            synchronized (digester) {
                digester.push(this);
                digester.parse(is);
            }
        } catch (Exception e) {
            throw new LifecycleException(sm.getString("memoryRealm.readXml"), e);
        } finally {
            digester.reset();
        }
    } catch (IOException ioe) {
        throw new LifecycleException(sm.getString("memoryRealm.loadExist", pathName), ioe);
    }
    super.startInternal();
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) InputStream(java.io.InputStream) Digester(org.apache.tomcat.util.digester.Digester) IOException(java.io.IOException) LifecycleException(org.apache.catalina.LifecycleException) IOException(java.io.IOException)

Example 4 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class TestWebRuleSet method parse.

private synchronized void parse(WebXml webXml, String target, boolean fragment, boolean expected) {
    Digester d;
    if (fragment) {
        d = fragmentDigester;
        fragmentRuleSet.recycle();
    } else {
        d = webDigester;
        webRuleSet.recycle();
    }
    d.push(webXml);
    File f = new File("test/org/apache/catalina/startup/" + target);
    boolean result = true;
    try (InputStream is = new FileInputStream(f)) {
        d.parse(is);
    } catch (Exception e) {
        if (expected) {
            // Didn't expect an exception
            e.printStackTrace();
        }
        result = false;
    }
    if (expected) {
        assertTrue(result);
    } else {
        assertFalse(result);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Digester(org.apache.tomcat.util.digester.Digester) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 5 with Digester

use of org.apache.tomcat.util.digester.Digester in project tomcat by apache.

the class TestWebXml method doTestValidateVersion.

private void doTestValidateVersion(String version) throws IOException, SAXException {
    WebXml webxml = new WebXml();
    // Special cases
    if ("2.2".equals(version)) {
        webxml.setPublicId(XmlIdentifiers.WEB_22_PUBLIC);
    } else if ("2.3".equals(version)) {
        webxml.setPublicId(XmlIdentifiers.WEB_23_PUBLIC);
    } else {
        webxml.setVersion(version);
    }
    // Merged web.xml that is published as MERGED_WEB_XML context attribute
    // in the simplest case consists of webapp's web.xml file
    // plus the default conf/web.xml one.
    Set<WebXml> defaults = new HashSet<>();
    defaults.add(getDefaultWebXmlFragment());
    webxml.merge(defaults);
    Digester digester = DigesterFactory.newDigester(true, true, new WebRuleSet(), true);
    XmlErrorHandler handler = new XmlErrorHandler();
    digester.setErrorHandler(handler);
    InputSource is = new InputSource(new StringReader(webxml.toXml()));
    WebXml webxmlResult = new WebXml();
    digester.push(webxmlResult);
    digester.parse(is);
    Assert.assertEquals(0, handler.getErrors().size());
    Assert.assertEquals(0, handler.getWarnings().size());
    Assert.assertEquals(version, webxml.getVersion());
    Assert.assertEquals(version, webxmlResult.getVersion());
}
Also used : InputSource(org.xml.sax.InputSource) Digester(org.apache.tomcat.util.digester.Digester) StringReader(java.io.StringReader) XmlErrorHandler(org.apache.tomcat.util.descriptor.XmlErrorHandler) HashSet(java.util.HashSet)

Aggregations

Digester (org.apache.tomcat.util.digester.Digester)28 File (java.io.File)14 XmlErrorHandler (org.apache.tomcat.util.descriptor.XmlErrorHandler)11 IOException (java.io.IOException)8 WebRuleSet (org.apache.tomcat.util.descriptor.web.WebRuleSet)8 WebXml (org.apache.tomcat.util.descriptor.web.WebXml)8 Test (org.junit.Test)8 InputStream (java.io.InputStream)6 FileInputStream (java.io.FileInputStream)5 InputSource (org.xml.sax.InputSource)5 LifecycleException (org.apache.catalina.LifecycleException)4 SAXParseException (org.xml.sax.SAXParseException)3 ConnectException (java.net.ConnectException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 BufferedInputStream (java.io.BufferedInputStream)1 OutputStream (java.io.OutputStream)1 StringReader (java.io.StringReader)1 Socket (java.net.Socket)1