Author: veithen Date: Mon Dec 15 17:04:16 2008 New Revision: 726903 URL: http://svn.apache.org/viewvc?rev=726903&view=rev Log: WSCOMMONS-394: Improved the network detached XMLStreamReader capability by using a custom XMLResolver that returns empty documents. This avoids the network access entirely and gives more predictable results.
Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMDTDTest.java webservices/commons/trunk/modules/axiom/modules/axiom-tests/test-resources/xml/web_w_dtd2.xml Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=726903&r1=726902&r2=726903&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java Mon Dec 15 17:04:16 2008 @@ -33,7 +33,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; @@ -444,42 +443,9 @@ if (!parser.hasText()) { return null; } - String dtdText = getDTDText(); - lastNode = omfactory.createOMDocType(document, dtdText); + lastNode = omfactory.createOMDocType(document, parser.getText()); return lastNode; } - - /** - * The getText() method for a DOCTYPE returns the - * subset of the DOCTYPE (not the direct infoset). - * This may force the parser to get information from - * the network. - * @return doctype subset - * @throws OMException - */ - private String getDTDText() throws OMException { - String text = null; - try { - text = parser.getText(); - } catch (RuntimeException e) { - // Woodstox (and perhaps other parsers) - // attempts to load the external subset even if - // external enties is false. So ignore this error - // if external entity support is explicitly disabled. - Boolean b = (Boolean) parser.getProperty( - XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES); - if (b == null || b == Boolean.TRUE) { - throw e; - } - if (log.isDebugEnabled()) { - log.debug("An exception occurred while calling getText() for a DOCTYPE. " + - "The exception is ignored because external " + - "entites support is disabled. " + - "The ignored exception is " + e); - } - } - return text; - } /** * Method createPI. Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java?rev=726903&r1=726902&r2=726903&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/StAXUtils.java Mon Dec 15 17:04:16 2008 @@ -25,9 +25,12 @@ import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLResolver; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; + +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; @@ -292,6 +295,16 @@ if (isNetworkDetached) { factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); + // Some StAX parser such as Woodstox still try to load the external DTD subset, + // even if IS_SUPPORTING_EXTERNAL_ENTITIES is set to false. To work around this, + // we add a custom XMLResolver that returns empty documents. See WSTX-117 for + // an interesting discussion about this. + factory.setXMLResolver(new XMLResolver() { + public Object resolveEntity(String publicID, String systemID, String baseURI, + String namespace) throws XMLStreamException { + return new ByteArrayInputStream(new byte[0]); + } + }); } return factory; } Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMDTDTest.java URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMDTDTest.java?rev=726903&r1=726902&r2=726903&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMDTDTest.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/OMDTDTest.java Mon Dec 15 17:04:16 2008 @@ -79,15 +79,11 @@ // can be obtained from StAXUtils and used to process configuration files // (like a web.xml) that may contain DTD information. // - // The following test first reads a normal web.xml that has a proper - // DTD. - // The second test has a web.xml where the external link is intentionally - // changed to use the "urn" protocol. This second test is designed to simulate - // reading the XML in a "network disconnected environment". Both - // of these examples should pass without error. - // Read a web.xml file that contains a DTD. + // The following test reads an XML file that has a DTD with a system ID + // that intentionally points to a non existing URL. With a network + // detached reader this should not produce errors. - InputStream is = new FileInputStream("test-resources/xml/web_w_dtd.xml"); + InputStream is = new FileInputStream("test-resources/xml/web_w_dtd2.xml"); XMLStreamReader reader = StAXUtils.createNetworkDetachedXMLStreamReader(is); StAXOMBuilder builder = new StAXOMBuilder(reader); OMElement root = builder.getDocumentElement(); @@ -102,24 +98,5 @@ } } assertTrue(docType != null); - - // Make sure that a web.xml with a dtd can be loaded even if disconnected - // from the network. In this case, the dtd has an invalid protocol (urn) - // to simulate a disconnect from the network. - is = new FileInputStream("test-resources/xml/web_w_dtd2.xml"); - reader = StAXUtils.createNetworkDetachedXMLStreamReader(is); - builder = new StAXOMBuilder(reader); - root = builder.getDocumentElement(); - assertTrue(root.getLocalName().equals("web-app")); - document = builder.getDocument(); - i = document.getChildren(); - docType = null; - while (docType == null && i.hasNext()) { - Object obj = i.next(); - if (obj instanceof OMDocType) { - docType = (OMDocType) obj; - } - } - assertTrue(docType != null); } } Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/test-resources/xml/web_w_dtd2.xml URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/test-resources/xml/web_w_dtd2.xml?rev=726903&r1=726902&r2=726903&view=diff ============================================================================== --- webservices/commons/trunk/modules/axiom/modules/axiom-tests/test-resources/xml/web_w_dtd2.xml (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/test-resources/xml/web_w_dtd2.xml Mon Dec 15 17:04:16 2008 @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="ISO-8859-1"?> -<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "urn://java.sun.com/dtd/web-app_2_3.dtd"> +<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://localhost:4321/this/should/not/exist.dtd"> <!-- The location of the DOCTYPE is intentionally invalid to ensure that the document is built even if disconnected from the network --> <web-app>