xmlsignatureinput.java
来自「JAVA 所有包」· Java 代码 · 共 613 行 · 第 1/2 页
JAVA
613 行
/* * Copyright 1999-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package com.sun.org.apache.xml.internal.security.signature;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;import com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315OmitComments;import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityRuntimeException;import com.sun.org.apache.xml.internal.security.utils.JavaUtils;import com.sun.org.apache.xml.internal.security.utils.XMLUtils;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.xml.sax.SAXException;/** * Class XMLSignatureInput * * @author Christian Geuer-Pollmann * $todo$ check whether an XMLSignatureInput can be _both_, octet stream _and_ node set? */public class XMLSignatureInput implements Cloneable { static java.util.logging.Logger log = java.util.logging.Logger.getLogger(XMLSignatureInput.class.getName()); /* * The XMLSignature Input can be either: * A byteArray like with/or without InputStream. * Or a nodeSet like defined either: * * as a collection of nodes * * or as subnode excluding or not commets and excluding or * not other nodes. */ /** * Some InputStreams do not support the {@link java.io.InputStream#reset} * method, so we read it in completely and work on our Proxy. */ InputStream _inputOctetStreamProxy = null; /** * The original NodeSet for this XMLSignatureInput */ Set _inputNodeSet = null; /** * The original Element */ Node _subNode=null; /** * Exclude Node *for enveloped transformations* */ Node excludeNode=null; /** * */ boolean excludeComments=false; boolean isNodeSet=false; /** * A cached bytes */ byte []bytes=null; /** * 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. Such data characteristics are provided as parameters to the Transform algorithm and should be described in the specification for the algorithm. */ private String _MIMEType = null; /** * Field _SourceURI */ private String _SourceURI = null; /** * Node Filter list. */ List nodeFilters=new ArrayList(); boolean needsToBeExpanded=false; /** * Check if the structured is needed to be circumbented. * @return true if so. */ public boolean isNeedsToBeExpanded() { return needsToBeExpanded; } /** * Set if the structured is needed to be circumbented. * @param needsToBeExpanded true if so. */ public void setNeedsToBeExpanded(boolean needsToBeExpanded) { this.needsToBeExpanded = needsToBeExpanded; } OutputStream outputStream=null; /** * Construct a XMLSignatureInput from an octet array. * <p> * This is a comfort method, which internally converts the byte[] array into an InputStream * <p>NOTE: no defensive copy</p> * @param inputOctets an octet array which including XML document or node */ public XMLSignatureInput(byte[] inputOctets) { // NO defensive copy //this._inputOctetStreamProxy = new ByteArrayInputStream(inputOctets); this.bytes=inputOctets; } /** * Constructs a <code>XMLSignatureInput</code> from an octet stream. The * stream is directly read. * * @param inputOctetStream */ public XMLSignatureInput(InputStream inputOctetStream) { this._inputOctetStreamProxy=inputOctetStream; //this(JavaUtils.getBytesFromStream(inputOctetStream)); } /** * Construct a XMLSignatureInput from a String. * <p> * This is a comfort method, which internally converts the String into a byte[] array using the {@link java.lang.String#getBytes()} method. * @deprecated * @param inputStr the input String which including XML document or node */ public XMLSignatureInput(String inputStr) { this(inputStr.getBytes()); } /** * Construct a XMLSignatureInput from a String with a given encoding. * <p> * This is a comfort method, which internally converts the String into a byte[] array using the {@link java.lang.String#getBytes()} method. * * @deprecated * @param inputStr the input String with encoding <code>encoding</code> * @param encoding the encoding of <code>inputStr</code> * @throws UnsupportedEncodingException */ public XMLSignatureInput(String inputStr, String encoding) throws UnsupportedEncodingException { this(inputStr.getBytes(encoding)); } /** * Construct a XMLSignatureInput from a subtree rooted by rootNode. This * method included the node and <I>all</I> his descendants in the output. * * @param rootNode */ public XMLSignatureInput(Node rootNode) { this._subNode = rootNode; } /** * Constructor XMLSignatureInput * * @param inputNodeSet * @param usedXPathAPI */ public XMLSignatureInput(Set inputNodeSet) { this._inputNodeSet = inputNodeSet; } /** * Returns the node set from input which was specified as the parameter of {@link XMLSignatureInput} constructor * * @return the node set * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws CanonicalizationException * @throws CanonicalizationException * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public Set getNodeSet() throws CanonicalizationException, ParserConfigurationException, IOException, SAXException { return getNodeSet(false); } /** * Returns the node set from input which was specified as the parameter of {@link XMLSignatureInput} constructor * @param circunvent * * @return the node set * @throws SAXException * @throws IOException * @throws ParserConfigurationException * @throws CanonicalizationException * @throws CanonicalizationException * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ public Set getNodeSet(boolean circunvent) throws ParserConfigurationException, IOException, SAXException, CanonicalizationException { if (this._inputNodeSet!=null) { return this._inputNodeSet; } if (this.isElement()) { if (circunvent) { XMLUtils.circumventBug2650(XMLUtils.getOwnerDocument(_subNode)); } this._inputNodeSet = new HashSet(); XMLUtils.getSet(_subNode,this._inputNodeSet, excludeNode, this.excludeComments); return this._inputNodeSet; } else if (this.isOctetStream()) { convertToNodes(); HashSet result=new HashSet(); XMLUtils.getSet(_subNode, result,null,false); //this._inputNodeSet=result; return result; } throw new RuntimeException( "getNodeSet() called but no input data present"); } /** * Returns the Octect stream(byte Stream) from input which was specified as the parameter of {@link XMLSignatureInput} constructor * * @return the Octect stream(byte Stream) from input which was specified as the parameter of {@link XMLSignatureInput} constructor * @throws IOException */ public InputStream getOctetStream() throws IOException { return getResetableInputStream(); } /** * @return real octect stream */ public InputStream getOctetStreamReal () { return this._inputOctetStreamProxy; } /** * 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 * * @throws CanonicalizationException * @throws IOException */ public byte[] getBytes() throws IOException, CanonicalizationException { if (bytes!=null) { return bytes; } InputStream is = getResetableInputStream(); if (is!=null) { //reseatable can read again bytes. if (bytes==null) { is.reset(); bytes=JavaUtils.getBytesFromStream(is); } return bytes; } Canonicalizer20010315OmitComments c14nizer = new Canonicalizer20010315OmitComments(); bytes=c14nizer.engineCanonicalize(this); return bytes; } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?