You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by co...@apache.org on 2020/01/21 11:30:10 UTC

svn commit: r1873006 - in /santuario/xml-security-java/trunk/src: main/java/org/apache/xml/security/c14n/ main/java/org/apache/xml/security/c14n/implementations/ main/java/org/apache/xml/security/encryption/ test/java/org/apache/xml/security/test/dom/e...

Author: coheigea
Date: Tue Jan 21 11:30:10 2020
New Revision: 1873006

URL: http://svn.apache.org/viewvc?rev=1873006&view=rev
Log:
SANTUARIO-521 - Remove reset property of Canonicalizers

Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
    santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java?rev=1873006&r1=1873005&r2=1873006&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/Canonicalizer.java Tue Jan 21 11:30:10 2020
@@ -105,7 +105,6 @@ public final class Canonicalizer {
                 canonicalizerHash.get(algorithmURI);
 
             canonicalizerSpi = implementingClass.newInstance();
-            canonicalizerSpi.reset = true;
         } catch (Exception e) {
             Object[] exArgs = { algorithmURI };
             throw new InvalidCanonicalizerException(
@@ -212,24 +211,6 @@ public final class Canonicalizer {
     }
 
     /**
-     * Method getURI
-     *
-     * @return the URI defined for this c14n instance.
-     */
-    public final String getURI() {
-        return canonicalizerSpi.engineGetURI();
-    }
-
-    /**
-     * Method getIncludeComments
-     *
-     * @return true if the c14n respect the comments.
-     */
-    public boolean getIncludeComments() {
-        return canonicalizerSpi.engineGetIncludeComments();
-    }
-
-    /**
      * This method tries to canonicalize the given bytes. It's possible to even
      * canonicalize non-wellformed sequences if they are well-formed after being
      * wrapped with a <CODE>&gt;a&lt;...&gt;/a&lt;</CODE>.
@@ -320,22 +301,6 @@ public final class Canonicalizer {
         canonicalizerSpi.setWriter(os);
     }
 
-    /**
-     * Returns the name of the implementing {@link CanonicalizerSpi} class
-     *
-     * @return the name of the implementing {@link CanonicalizerSpi} class
-     */
-    public String getImplementingCanonicalizerClass() {
-        return canonicalizerSpi.getClass().getName();
-    }
-
-    /**
-     * Set the canonicalizer behaviour to not reset.
-     */
-    public void notReset() {
-        canonicalizerSpi.reset = false;
-    }
-
     public boolean isSecureValidation() {
         return secureValidation;
     }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java?rev=1873006&r1=1873005&r2=1873006&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/CanonicalizerSpi.java Tue Jan 21 11:30:10 2020
@@ -32,8 +32,6 @@ import org.w3c.dom.Node;
  */
 public abstract class CanonicalizerSpi {
 
-    /** Reset the writer after a c14n */
-    protected boolean reset = false;
     protected boolean secureValidation;
 
     /**

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java?rev=1873006&r1=1873005&r2=1873006&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/c14n/implementations/CanonicalizerBase.java Tue Jan 21 11:30:10 2020
@@ -61,7 +61,10 @@ public abstract class CanonicalizerBase
     public static final String XMLNS_URI = Constants.NamespaceSpecNS;
     public static final String XML_LANG_URI = Constants.XML_LANG_SPACE_SpecNS;
 
-    protected static final AttrCompare COMPARE = new AttrCompare();
+    protected static final AttrCompare COMPARE = new AttrCompare();     // thread-safe
+    protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
+    protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
+    protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
 
     // Make sure you clone the following mutable arrays before passing to
     // potentially untrusted objects such as OutputStreams.
@@ -79,20 +82,11 @@ public abstract class CanonicalizerBase
     private static final byte[] AMP = {'&','a','m','p',';'};
     private static final byte[] EQUALS_STR = {'=','\"'};
 
-    protected static final int NODE_BEFORE_DOCUMENT_ELEMENT = -1;
-    protected static final int NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT = 0;
-    protected static final int NODE_AFTER_DOCUMENT_ELEMENT = 1;
-
     private List<NodeFilter> nodeFilter;
 
     private boolean includeComments;
     private Set<Node> xpathNodeSet;
 
-    /**
-     * The node to be skipped/excluded from the DOM tree
-     * in subtree canonicalizations.
-     */
-    private Node excludeNode;
     private OutputStream writer = new ByteArrayOutputStream();
 
    /**
@@ -192,7 +186,6 @@ public abstract class CanonicalizerBase
      */
     protected byte[] engineCanonicalizeSubTree(Node rootNode, Node excludeNode)
         throws CanonicalizationException {
-        this.excludeNode = excludeNode;
         try {
             NameSpaceSymbTable ns = new NameSpaceSymbTable();
             int nodeLevel = NODE_BEFORE_DOCUMENT_ELEMENT;
@@ -201,23 +194,15 @@ public abstract class CanonicalizerBase
                 getParentNameSpaces((Element)rootNode, ns);
                 nodeLevel = NODE_NOT_BEFORE_OR_AFTER_DOCUMENT_ELEMENT;
             }
-            this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel);
+            this.canonicalizeSubTree(rootNode, ns, rootNode, nodeLevel, excludeNode);
             this.writer.flush();
             if (this.writer instanceof ByteArrayOutputStream) {
                 byte[] result = ((ByteArrayOutputStream)this.writer).toByteArray();
-                if (reset) {
-                    ((ByteArrayOutputStream)this.writer).reset();
-                } else {
-                    this.writer.close();
-                }
+                this.writer.close();
                 return result;
             } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
                 byte[] result = ((UnsyncByteArrayOutputStream)this.writer).toByteArray();
-                if (reset) {
-                    ((UnsyncByteArrayOutputStream)this.writer).reset();
-                } else {
-                    this.writer.close();
-                }
+                this.writer.close();
                 return result;
             } else {
                 this.writer.close();
@@ -238,11 +223,14 @@ public abstract class CanonicalizerBase
      * @param currentNode
      * @param ns
      * @param endnode
+     * @param documentLevel
+     * @param excludeNode
      * @throws CanonicalizationException
      * @throws IOException
      */
-    protected final void canonicalizeSubTree(
-        Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel
+    private void canonicalizeSubTree(
+        Node currentNode, NameSpaceSymbTable ns, Node endnode, int documentLevel,
+        Node excludeNode
     ) throws CanonicalizationException, IOException {
         if (currentNode == null || isVisibleInt(currentNode) == -1) {
             return;
@@ -250,8 +238,6 @@ public abstract class CanonicalizerBase
         Node sibling = null;
         Node parentNode = null;
         final OutputStream writer = this.writer;
-        final Node excludeNode = this.excludeNode;
-        final boolean includeComments = this.includeComments;
         Map<String, byte[]> cache = new HashMap<>();
         do {
             switch (currentNode.getNodeType()) {
@@ -350,19 +336,11 @@ public abstract class CanonicalizerBase
             this.writer.flush();
             if (this.writer instanceof ByteArrayOutputStream) {
                 byte[] sol = ((ByteArrayOutputStream)this.writer).toByteArray();
-                if (reset) {
-                    ((ByteArrayOutputStream)this.writer).reset();
-                } else {
-                    this.writer.close();
-                }
+                this.writer.close();
                 return sol;
             } else if (this.writer instanceof UnsyncByteArrayOutputStream) {
                 byte[] result = ((UnsyncByteArrayOutputStream)this.writer).toByteArray();
-                if (reset) {
-                    ((UnsyncByteArrayOutputStream)this.writer).reset();
-                } else {
-                    this.writer.close();
-                }
+                this.writer.close();
                 return result;
             } else {
                 this.writer.close();
@@ -384,7 +362,7 @@ public abstract class CanonicalizerBase
      * @throws CanonicalizationException
      * @throws IOException
      */
-    protected final void canonicalizeXPathNodeSet(Node currentNode, Node endnode)
+    private void canonicalizeXPathNodeSet(Node currentNode, Node endnode)
         throws CanonicalizationException, IOException {
         if (isVisibleInt(currentNode) == -1) {
             return;
@@ -418,7 +396,7 @@ public abstract class CanonicalizerBase
                 break;
 
             case Node.COMMENT_NODE :
-                if (this.includeComments && isVisibleDO(currentNode, ns.getLevel()) == 1) {
+                if (includeComments && isVisibleDO(currentNode, ns.getLevel()) == 1) {
                     outputCommentToWriter((Comment) currentNode, writer, documentLevel);
                 }
                 break;
@@ -603,7 +581,7 @@ public abstract class CanonicalizerBase
      * @param el
      * @param ns
      */
-    protected final void getParentNameSpaces(Element el, NameSpaceSymbTable ns)  {
+    private void getParentNameSpaces(Element el, NameSpaceSymbTable ns)  {
         Node n1 = el.getParentNode();
         if (n1 == null || Node.ELEMENT_NODE != n1.getNodeType()) {
             return;
@@ -828,7 +806,7 @@ public abstract class CanonicalizerBase
      * @param writer writer where to write the things
      * @throws IOException
      */
-    protected static final void outputTextToWriter(
+    private static final void outputTextToWriter(
         final String text, final OutputStream writer
     ) throws IOException {
         final int length = text.length();

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java?rev=1873006&r1=1873005&r2=1873006&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/encryption/AbstractSerializer.java Tue Jan 21 11:30:10 2020
@@ -100,7 +100,6 @@ public abstract class AbstractSerializer
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
             canon.setWriter(baos);
-            canon.notReset();
             for (int i = 0; i < content.getLength(); i++) {
                 canon.canonicalizeSubtree(content.item(i));
             }
@@ -123,7 +122,6 @@ public abstract class AbstractSerializer
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
             canon.setWriter(baos);
-            canon.notReset();
             for (int i = 0; i < content.getLength(); i++) {
                 canon.canonicalizeSubtree(content.item(i));
             }
@@ -141,7 +139,6 @@ public abstract class AbstractSerializer
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
             canon.setWriter(baos);
-            canon.notReset();
             canon.canonicalizeSubtree(node);
             String ret = baos.toString(StandardCharsets.UTF_8.name());
             baos.reset();
@@ -159,7 +156,6 @@ public abstract class AbstractSerializer
         try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
             canon.setSecureValidation(secureValidation);
             canon.setWriter(baos);
-            canon.notReset();
             canon.canonicalizeSubtree(node);
             return baos.toByteArray();
         }

Modified: santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java?rev=1873006&r1=1873005&r2=1873006&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java (original)
+++ santuario/xml-security-java/trunk/src/test/java/org/apache/xml/security/test/dom/encryption/XMLCipherTest.java Tue Jan 21 11:30:10 2020
@@ -831,7 +831,6 @@ public class XMLCipherTest {
             Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS);
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         canon.setWriter(baos);
-        canon.notReset();
         canon.canonicalizeSubtree(e);
         baos.close();
         String before = baos.toString(StandardCharsets.UTF_8.name());