001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.wss.impl.transform;
024:
025: import com.sun.xml.wss.impl.MessageConstants;
026: import com.sun.xml.wss.logging.LogDomainConstants;
027: import java.io.ByteArrayInputStream;
028: import java.io.ByteArrayOutputStream;
029: import java.util.HashSet;
030: import java.util.Iterator;
031: import java.util.Set;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034: import javax.xml.crypto.Data;
035: import javax.xml.crypto.NodeSetData;
036: import javax.xml.crypto.OctetStreamData;
037: import javax.xml.crypto.XMLCryptoContext;
038: import javax.xml.parsers.DocumentBuilderFactory;
039:
040: import org.w3c.dom.Document;
041: import org.w3c.dom.Element;
042: import org.w3c.dom.Node;
043: import org.w3c.dom.NamedNodeMap;
044:
045: /*
046: * author K.Venugopal@sun.com
047: */
048:
049: public class STRTransformImpl {
050: private static Logger logger = Logger.getLogger(
051: LogDomainConstants.IMPL_SIGNATURE_DOMAIN,
052: LogDomainConstants.IMPL_SIGNATURE_DOMAIN_BUNDLE);
053:
054: protected static Data transform(Data data,
055: XMLCryptoContext context, java.io.OutputStream outputStream)
056: throws javax.xml.crypto.dsig.TransformException {
057: try {
058: Set nodeSet = getNodeSet(data);
059: if (outputStream == null) {
060: ByteArrayOutputStream bs = new ByteArrayOutputStream();
061: new Canonicalizer20010315ExclOmitComments()
062: .engineCanonicalizeXPathNodeSet(nodeSet, "",
063: bs, context);
064: OctetStreamData osd = new OctetStreamData(
065: new ByteArrayInputStream(bs.toByteArray()));
066: bs.close();
067: return osd;
068: } else {
069: new Canonicalizer20010315ExclOmitComments()
070: .engineCanonicalizeXPathNodeSet(nodeSet, "",
071: outputStream, context);
072: }
073: return null;
074: } catch (Exception ex) {
075: logger.log(Level.SEVERE, "WSS1322.str_transform", ex);
076: }
077: return null;
078: }
079:
080: private static Set getNodeSet(Data data)
081: throws javax.xml.crypto.dsig.TransformException {
082: HashSet nodeSet = new HashSet();
083: if (data instanceof NodeSetData) {
084: Iterator it = ((NodeSetData) data).iterator();
085: while (it.hasNext()) {
086: Object node = it.next();
087: if (MessageConstants.debug) {
088: logger.log(Level.FINEST, "Data is " + node);
089: }
090: nodeSet.add(node);
091: }
092: } else if (data instanceof OctetStreamData) {
093: try {
094: DocumentBuilderFactory factory = new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
095: factory.setNamespaceAware(true);
096: Document doc = factory.newDocumentBuilder().parse(
097: ((OctetStreamData) data).getOctetStream());
098: toNodeSet(doc, nodeSet);
099: } catch (Exception ex) {
100: logger.log(Level.SEVERE, "WSS1322.str_transform", ex);
101: throw new javax.xml.crypto.dsig.TransformException(ex
102: .getMessage());
103:
104: }
105: }
106: return nodeSet;
107: }
108:
109: static final void toNodeSet(final Node rootNode, final Set result) {
110: //handle EKSHA1 under DKT
111: if (rootNode == null)
112: return;
113: switch (rootNode.getNodeType()) {
114: case Node.ELEMENT_NODE:
115: result.add(rootNode);
116: Element el = (Element) rootNode;
117: if (el.hasAttributes()) {
118: NamedNodeMap nl = ((Element) rootNode).getAttributes();
119: for (int i = 0; i < nl.getLength(); i++) {
120: result.add(nl.item(i));
121: }
122: }
123: //no return keep working
124: case Node.DOCUMENT_NODE:
125: for (Node r = rootNode.getFirstChild(); r != null; r = r
126: .getNextSibling()) {
127: if (r.getNodeType() == Node.TEXT_NODE) {
128: result.add(r);
129: while ((r != null)
130: && (r.getNodeType() == Node.TEXT_NODE)) {
131: r = r.getNextSibling();
132: }
133: if (r == null)
134: return;
135: }
136: toNodeSet(r, result);
137: }
138: return;
139: case Node.COMMENT_NODE:
140: return;
141: case Node.DOCUMENT_TYPE_NODE:
142: return;
143: default:
144: result.add(rootNode);
145: }
146: return;
147: }
148: }
|