coheigea commented on code in PR #192:
URL:
https://github.com/apache/santuario-xml-security-java/pull/192#discussion_r1284357809
##########
src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java:
##########
@@ -228,357 +194,278 @@ public Set<Node> getNodeSet(boolean circumvent) throws
XMLParserException, IOExc
throw new RuntimeException("getNodeSet() called but no input data
present");
}
+
/**
- * Returns the Octet stream(byte Stream) from input which was specified as
- * the parameter of {@link XMLSignatureInput} constructor
+ * Gets the node of this XMLSignatureInput
*
- * @return the Octet stream(byte Stream) from input which was specified as
- * the parameter of {@link XMLSignatureInput} constructor
+ * @return The excludeNode set.
+ */
+ public Node getSubNode() {
+ return subNode;
+ }
+
+
+ /**
+ * @param filter
+ * @throws XMLParserException
* @throws IOException
*/
- public InputStream getOctetStream() throws IOException {
- if (inputOctetStreamProxy != null) {
- return inputOctetStreamProxy;
+ public void addNodeFilter(NodeFilter filter) throws XMLParserException,
IOException {
+ if (hasUnprocessedInput()) {
+ this.subNode = convertToNode();
}
+ nodeFilters.add(filter);
+ }
- if (bytes != null) {
- inputOctetStreamProxy = new ByteArrayInputStream(bytes);
- return inputOctetStreamProxy;
- }
- return null;
+ /**
+ * @return the node filters
+ */
+ public final List<NodeFilter> getNodeFilters() {
+ return nodeFilters;
}
+
/**
- * @return real octet stream
+ * @param nodeSet
*/
- public InputStream getOctetStreamReal() {
- return inputOctetStreamProxy;
+ public final void setNodeSet(boolean nodeSet) {
+ isNodeSet = nodeSet;
}
+
/**
* Returns the byte array from input which was specified as the parameter
of
- * {@link XMLSignatureInput} constructor
- *
- * @return the byte[] from input which was specified as the parameter of
- * {@link XMLSignatureInput} constructor
+ * {@link XMLSignatureInput} constructor OR tries to reconstruct that if
+ * the element or node was already processed.
*
+ * @return the byte array
* @throws CanonicalizationException
* @throws IOException
*/
public byte[] getBytes() throws IOException, CanonicalizationException {
- byte[] inputBytes = getBytesFromInputStream();
- if (inputBytes != null) {
- return inputBytes;
+ if (hasUnprocessedInput()) {
+ return JavaUtils.getBytesFromStream(getUnprocessedInput());
}
- if (isOctetStream() || isElement() || isNodeSet()) {
- Canonicalizer20010315OmitComments c14nizer = new
Canonicalizer20010315OmitComments();
- try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
- c14nizer.engineCanonicalize(this, baos, secureValidation);
- bytes = baos.toByteArray();
- }
+ if (isElement() || isNodeSet()) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ canonize(baos, false);
Review Comment:
Rename to canonicalize
##########
src/main/java/org/apache/xml/security/parser/XMLParser.java:
##########
@@ -27,15 +27,6 @@
*/
public interface XMLParser {
- /**
- * Parses a document from the input stream.
- * Caller is responsible for closing the stream.
- *
- * @param inputStream
- * @param disallowDocTypeDeclarations
- * @return {@link Document}
- * @throws XMLParserException
- */
Review Comment:
Why removing it?
##########
src/main/java/org/apache/xml/security/signature/XMLSignatureInput.java:
##########
@@ -228,357 +194,278 @@ public Set<Node> getNodeSet(boolean circumvent) throws
XMLParserException, IOExc
throw new RuntimeException("getNodeSet() called but no input data
present");
}
+
/**
- * Returns the Octet stream(byte Stream) from input which was specified as
- * the parameter of {@link XMLSignatureInput} constructor
+ * Gets the node of this XMLSignatureInput
*
- * @return the Octet stream(byte Stream) from input which was specified as
- * the parameter of {@link XMLSignatureInput} constructor
+ * @return The excludeNode set.
+ */
+ public Node getSubNode() {
+ return subNode;
+ }
+
+
+ /**
+ * @param filter
+ * @throws XMLParserException
* @throws IOException
*/
- public InputStream getOctetStream() throws IOException {
- if (inputOctetStreamProxy != null) {
- return inputOctetStreamProxy;
+ public void addNodeFilter(NodeFilter filter) throws XMLParserException,
IOException {
+ if (hasUnprocessedInput()) {
+ this.subNode = convertToNode();
}
+ nodeFilters.add(filter);
+ }
- if (bytes != null) {
- inputOctetStreamProxy = new ByteArrayInputStream(bytes);
- return inputOctetStreamProxy;
- }
- return null;
+ /**
+ * @return the node filters
+ */
+ public final List<NodeFilter> getNodeFilters() {
+ return nodeFilters;
}
+
/**
- * @return real octet stream
+ * @param nodeSet
*/
- public InputStream getOctetStreamReal() {
- return inputOctetStreamProxy;
+ public final void setNodeSet(boolean nodeSet) {
+ isNodeSet = nodeSet;
}
+
/**
* Returns the byte array from input which was specified as the parameter
of
- * {@link XMLSignatureInput} constructor
- *
- * @return the byte[] from input which was specified as the parameter of
- * {@link XMLSignatureInput} constructor
+ * {@link XMLSignatureInput} constructor OR tries to reconstruct that if
+ * the element or node was already processed.
*
+ * @return the byte array
* @throws CanonicalizationException
* @throws IOException
*/
public byte[] getBytes() throws IOException, CanonicalizationException {
- byte[] inputBytes = getBytesFromInputStream();
- if (inputBytes != null) {
- return inputBytes;
+ if (hasUnprocessedInput()) {
+ return JavaUtils.getBytesFromStream(getUnprocessedInput());
}
- if (isOctetStream() || isElement() || isNodeSet()) {
- Canonicalizer20010315OmitComments c14nizer = new
Canonicalizer20010315OmitComments();
- try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
- c14nizer.engineCanonicalize(this, baos, secureValidation);
- bytes = baos.toByteArray();
- }
+ if (isElement() || isNodeSet()) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ canonize(baos, false);
+ return baos.toByteArray();
}
- return bytes;
+ return null;
}
+
/**
- * Determines if the object has been set up with a Node set
- *
- * @return true if the object has been set up with a Node set
+ * @return true if the {@link #XMLSignatureInput(Set)} was used or the
node set was parsed from
+ * an input coming from another constructor.
*/
public boolean isNodeSet() {
- return inputOctetStreamProxy == null && inputNodeSet != null ||
isNodeSet;
+ return isNodeSet || inputNodeSet != null;
}
+
/**
* Determines if the object has been set up with an Element
*
* @return true if the object has been set up with an Element
*/
public boolean isElement() {
- return inputOctetStreamProxy == null && subNode != null
- && inputNodeSet == null && !isNodeSet;
+ return subNode != null && inputNodeSet == null && !isNodeSet;
}
- /**
- * Determines if the object has been set up with an octet stream
- *
- * @return true if the object has been set up with an octet stream
- */
- public boolean isOctetStream() {
- return (inputOctetStreamProxy != null || bytes != null)
- && inputNodeSet == null && subNode == null;
- }
/**
- * Determines if {@link #setOutputStream} has been called with a
- * non-null OutputStream.
- *
- * @return true if {@link #setOutputStream} has been called with a
- * non-null OutputStream
+ * @return String given through constructor. Null by default, see
extensions of this class.
*/
- public boolean isOutputStreamSet() {
- return outputStream != null;
+ public String getPreCalculatedDigest() {
+ return null;
}
- /**
- * Determines if the object has been set up with a ByteArray
- *
- * @return true if the object has been set up with an octet stream
- */
- public boolean isByteArray() {
- return bytes != null && this.inputNodeSet == null && subNode == null;
- }
/**
- * Determines if the object has been set up with a pre-calculated digest.
- * @return true if the object has been set up with a pre-calculated digest.
+ * @return the exclude node of this XMLSignatureInput
*/
- public boolean isPreCalculatedDigest() {
- return preCalculatedDigest != null;
+ public Node getExcludeNode() {
+ return excludeNode;
}
+
/**
- * Is the object correctly set up?
+ * Sets the exclude node of this XMLSignatureInput
*
- * @return true if the object has been set up correctly
+ * @param excludeNode The excludeNode to set.
*/
- public boolean isInitialized() {
- return isOctetStream() || isNodeSet();
+ public void setExcludeNode(Node excludeNode) {
+ this.excludeNode = excludeNode;
}
+
/**
- * Returns mimeType
- *
- * @return mimeType
+ * @return Returns the excludeComments.
*/
- public String getMIMEType() {
- return mimeType;
+ public boolean isExcludeComments() {
+ return excludeComments;
}
+
/**
- * Sets mimeType
- *
- * @param mimeType
+ * @param excludeComments The excludeComments to set.
*/
- public void setMIMEType(String mimeType) {
- this.mimeType = mimeType;
+ public void setExcludeComments(boolean excludeComments) {
+ this.excludeComments = excludeComments;
}
+
/**
- * Return SourceURI
- *
- * @return SourceURI
+ * @return Source URI
*/
public String getSourceURI() {
return sourceURI;
}
+
/**
- * Sets SourceURI
- *
* @param sourceURI
*/
public void setSourceURI(String sourceURI) {
this.sourceURI = sourceURI;
}
- /**
- * Method toString
- * {@inheritDoc}
- */
- @Override
- public String toString() {
- if (isNodeSet()) {
- return "XMLSignatureInput/NodeSet/" + inputNodeSet.size()
- + " nodes/" + getSourceURI();
- }
- if (isElement()) {
- return "XMLSignatureInput/Element/" + subNode
- + " exclude "+ excludeNode + " comments:"
- + excludeComments +"/" + getSourceURI();
- }
- try {
- byte[] bytes = getBytes();
- return "XMLSignatureInput/OctetStream/"
- + (bytes != null ? bytes.length : 0)
- + " octets/" + getSourceURI();
- } catch (IOException | CanonicalizationException ex) {
- return "XMLSignatureInput/OctetStream//" + getSourceURI();
- }
- }
/**
- * Method getHTMLRepresentation
+ * Some Transforms may require explicit MIME type, charset (IANA
registered "character set"),
+ * or other such information concerning the data they are receiving from
an earlier Transform
+ * or the source data, although no Transform algorithm specified in this
document needs such
+ * explicit information.
+ * <p>
+ * Such data characteristics are provided as parameters to the Transform
algorithm and should be
+ * described in the specification for the algorithm.
*
- * @throws XMLSignatureException
- * @return The HTML representation for this XMLSignature
+ * @return mimeType
*/
- public String getHTMLRepresentation() throws XMLSignatureException {
- XMLSignatureInputDebugger db = new XMLSignatureInputDebugger(this);
- return db.getHTMLRepresentation();
+ public String getMIMEType() {
+ return mimeType;
}
+
/**
- * Method getHTMLRepresentation
+ * Some Transforms may require explicit MIME type, charset (IANA
registered "character set"),
+ * or other such information concerning the data they are receiving from
an earlier Transform
+ * or the source data, although no Transform algorithm specified in this
document needs such
+ * explicit information.
+ * <p>
+ * Such data characteristics are provided as parameters to the Transform
algorithm and should be
+ * described in the specification for the algorithm.
*
- * @param inclusiveNamespaces
- * @throws XMLSignatureException
- * @return The HTML representation for this XMLSignature
+ * @param mimeType
*/
- public String getHTMLRepresentation(Set<String> inclusiveNamespaces)
- throws XMLSignatureException {
- XMLSignatureInputDebugger db =
- new XMLSignatureInputDebugger(this, inclusiveNamespaces);
- return db.getHTMLRepresentation();
+ public void setMIMEType(String mimeType) {
+ this.mimeType = mimeType;
}
- /**
- * Gets the exclude node of this XMLSignatureInput
- * @return Returns the excludeNode.
- */
- public Node getExcludeNode() {
- return excludeNode;
- }
/**
- * Sets the exclude node of this XMLSignatureInput
- * @param excludeNode The excludeNode to set.
+ * @return true if the structure needs to be expanded.
*/
- public void setExcludeNode(Node excludeNode) {
- this.excludeNode = excludeNode;
+ public boolean isNeedsToBeExpanded() {
+ return needsToBeExpanded;
}
- /**
- * Gets the node of this XMLSignatureInput
- * @return The excludeNode set.
- */
- public Node getSubNode() {
- return subNode;
- }
/**
- * @return Returns the excludeComments.
+ * Set if the structure needs to be expanded.
+ *
+ * @param needsToBeExpanded true if so.
*/
- public boolean isExcludeComments() {
- return excludeComments;
+ public void setNeedsToBeExpanded(boolean needsToBeExpanded) {
+ this.needsToBeExpanded = needsToBeExpanded;
}
+
/**
- * @param excludeComments The excludeComments to set.
+ * @return true by default, enabled validation in r/w operations
*/
- public void setExcludeComments(boolean excludeComments) {
- this.excludeComments = excludeComments;
+ public boolean isSecureValidation() {
+ return secureValidation;
}
+
/**
- * @param diOs
- * @throws IOException
- * @throws CanonicalizationException
+ * Set to false to disable validation in r/w operations.
+ *
+ * @param secureValidation default is true.
*/
- public void updateOutputStream(OutputStream diOs)
- throws CanonicalizationException, IOException {
- updateOutputStream(diOs, false);
+ public void setSecureValidation(boolean secureValidation) {
+ this.secureValidation = secureValidation;
}
- public void updateOutputStream(OutputStream diOs, boolean c14n11)
- throws CanonicalizationException, IOException {
- if (diOs == outputStream) {
- return;
- }
- if (bytes != null) {
- diOs.write(bytes);
- } else if (inputOctetStreamProxy == null) {
- CanonicalizerBase c14nizer = null;
- if (c14n11) {
- c14nizer = new Canonicalizer11_OmitComments();
- } else {
- c14nizer = new Canonicalizer20010315OmitComments();
- }
- c14nizer.engineCanonicalize(this, diOs, secureValidation);
- } else {
- byte[] buffer = new byte[4 * 1024];
- int bytesread = 0;
- try {
- while ((bytesread = inputOctetStreamProxy.read(buffer)) != -1)
{
- diOs.write(buffer, 0, bytesread);
- }
- } catch (IOException ex) {
- inputOctetStreamProxy.close();
- throw ex;
- }
- }
- }
/**
- * @param os
+ * @return true if {@link #setOutputStream} has been called with a
non-null OutputStream
*/
- public void setOutputStream(OutputStream os) {
- outputStream = os;
+ public boolean isOutputStreamSet() {
+ return outputStream != null;
}
- private byte[] getBytesFromInputStream() throws IOException {
- if (bytes != null) {
- return bytes;
- }
- if (inputOctetStreamProxy == null) {
- return null;
- }
- try { //NOPMD
- bytes = JavaUtils.getBytesFromStream(inputOctetStreamProxy);
- } finally {
- inputOctetStreamProxy.close();
- }
- return bytes;
- }
/**
- * @param filter
+ * @param outputStream this stream will be ignored in {@link
#write(OutputStream)} method
*/
- public void addNodeFilter(NodeFilter filter) throws XMLParserException,
IOException {
- if (isOctetStream()) {
- convertToNodes();
- }
- nodeFilters.add(filter);
+ public void setOutputStream(OutputStream outputStream) {
+ this.outputStream = outputStream;
}
+
/**
- * @return the node filters
+ * @return {@link OutputStream} set in {@link
#setOutputStream(OutputStream)}
*/
- public List<NodeFilter> getNodeFilters() {
- return nodeFilters;
+ public OutputStream getOutputStream() {
+ return this.outputStream;
}
+
/**
- * @param b
+ * Creates a short description of this instance.
*/
- public void setNodeSet(boolean b) {
- isNodeSet = b;
- }
-
- private void convertToNodes() throws XMLParserException, IOException {
- // select all nodes, also the comments.
- try {
- Document doc = XMLUtils.read(this.getOctetStream(),
secureValidation);
- this.subNode = doc;
- } finally {
- if (this.inputOctetStreamProxy != null) {
- this.inputOctetStreamProxy.close();
+ @Override
+ public String toString() {
+ String className = getClass().getSimpleName();
+ if (isNodeSet()) {
+ return className + "/NodeSet/" + inputNodeSet.size() + " nodes/" +
getSourceURI();
+ }
+ if (isElement()) {
+ return className + "/Element/" + subNode + " exclude " +
excludeNode + " comments:" + excludeComments
+ + "/" + getSourceURI();
+ }
+ if (hasUnprocessedInput()) {
+ try {
+ int available = getUnprocessedInput().available();
+ return className + "/OctetStream/" + available + " bytes/" +
getSourceURI();
+ } catch (IOException ex) {
+ // Stream is unavailable and toString should not touch any IO,
so ignore it.
}
- this.inputOctetStreamProxy = null;
- this.bytes = null;
}
+ return className + "/OctetStream//" + getSourceURI();
}
- public boolean isSecureValidation() {
- return secureValidation;
- }
-
- public void setSecureValidation(boolean secureValidation) {
- this.secureValidation = secureValidation;
- }
- public String getPreCalculatedDigest() {
- return preCalculatedDigest;
+ /**
+ * Canonizes this object to the output stream.
Review Comment:
Canonicalize
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]