1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
/* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. */ /* * $Id: ApacheTransform.java,v 1.23 2005/09/15 14:29:03 mullan Exp $ */ package org.jcp.xml.dsig.internal.dom; import java.io.OutputStream; import java.security.InvalidAlgorithmParameterException; import java.security.spec.AlgorithmParameterSpec; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput; import com.sun.org.apache.xml.internal.security.transforms.Transform; import javax.xml.crypto.*; import javax.xml.crypto.dom.DOMCryptoContext; import javax.xml.crypto.dsig.*; import javax.xml.crypto.dsig.spec.TransformParameterSpec; /** * This is a wrapper/glue class which invokes the Apache XML-Security * Transform. * * @author Sean Mullan * @author Erwin van der Koogh */ public abstract class ApacheTransform extends TransformService { private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom"); private Transform apacheTransform; protected Document ownerDoc; protected Element transformElem; protected TransformParameterSpec params; public final AlgorithmParameterSpec getParameterSpec() { return params; } public void init(XMLStructure parent, XMLCryptoContext context) throws InvalidAlgorithmParameterException { if (context != null && !(context instanceof DOMCryptoContext)) { throw new ClassCastException ("context must be of type DOMCryptoContext"); } transformElem = (Element) ((javax.xml.crypto.dom.DOMStructure) parent).getNode(); ownerDoc = DOMUtils.getOwnerDocument(transformElem); } public void marshalParams(XMLStructure parent, XMLCryptoContext context) throws MarshalException { if (context != null && !(context instanceof DOMCryptoContext)) { throw new ClassCastException ("context must be of type DOMCryptoContext"); } transformElem = (Element) ((javax.xml.crypto.dom.DOMStructure) parent).getNode(); ownerDoc = DOMUtils.getOwnerDocument(transformElem); } public Data transform(Data data, XMLCryptoContext xc) throws TransformException { if (data == null) { throw new NullPointerException("data must not be null"); } return transformIt(data, xc, (OutputStream) null); } public Data transform(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException { if (data == null) { throw new NullPointerException("data must not be null"); } if (os == null) { throw new NullPointerException("output stream must not be null"); } return transformIt(data, xc, os); } private Data transformIt(Data data, XMLCryptoContext xc, OutputStream os) throws TransformException { if (ownerDoc == null) { throw new TransformException("transform must be marshalled"); } if (apacheTransform == null) { try { apacheTransform = Transform.getInstance (ownerDoc, getAlgorithm(), transformElem.getChildNodes()); apacheTransform.setElement(transformElem, xc.getBaseURI()); if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "Created transform for algorithm: " + getAlgorithm()); } } catch (Exception ex) { throw new TransformException ("Couldn't find Transform for: " + getAlgorithm(), ex); } } XMLSignatureInput in; if (data instanceof ApacheData) { if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "ApacheData = true"); } in = ((ApacheData) data).getXMLSignatureInput(); } else if (data instanceof NodeSetData) { if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "isNodeSet() = true"); } if (data instanceof DOMSubTreeData) { if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "DOMSubTreeData = true"); } DOMSubTreeData subTree = (DOMSubTreeData) data; in = new XMLSignatureInput(subTree.getRoot()); in.setExcludeComments(subTree.excludeComments()); } else { Set nodeSet = Utils.toNodeSet(((NodeSetData) data).iterator()); in = new XMLSignatureInput(nodeSet); } } else { if (log.isLoggable(Level.FINE)) { log.log(Level.FINE, "isNodeSet() = false"); } try { in = new XMLSignatureInput (((OctetStreamData)data).getOctetStream()); } catch (Exception ex) { throw new TransformException(ex); } } try { if (os != null) { in = apacheTransform.performTransform(in, os); if (!in.isNodeSet() && !in.isElement()) { return null; } } else { in = apacheTransform.performTransform(in); } if (in.isOctetStream()) { return new ApacheOctetStreamData(in); } else { return new ApacheNodeSetData(in); } } catch (Exception ex) { throw new TransformException(ex); } } public final boolean isFeatureSupported(String feature) { if (feature == null) { throw new NullPointerException(); } else { return false; } } }