blob: 6d1b01ec8529348bd6d79e1037b3f0c4b65a6e90 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25/*
26 * $Id: ApacheTransform.java,v 1.23 2005/09/15 14:29:03 mullan Exp $
27 */
28package org.jcp.xml.dsig.internal.dom;
29
30import java.io.OutputStream;
31import java.security.InvalidAlgorithmParameterException;
32import java.security.spec.AlgorithmParameterSpec;
33import java.util.Set;
34import java.util.logging.Level;
35import java.util.logging.Logger;
36import org.w3c.dom.Document;
37import org.w3c.dom.Element;
38import org.w3c.dom.NodeList;
39
40import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
41import com.sun.org.apache.xml.internal.security.transforms.Transform;
42
43import javax.xml.crypto.*;
44import javax.xml.crypto.dom.DOMCryptoContext;
45import javax.xml.crypto.dsig.*;
46import javax.xml.crypto.dsig.spec.TransformParameterSpec;
47
48/**
49 * This is a wrapper/glue class which invokes the Apache XML-Security
50 * Transform.
51 *
52 * @author Sean Mullan
53 * @author Erwin van der Koogh
54 */
55public abstract class ApacheTransform extends TransformService {
56
57 private static Logger log = Logger.getLogger("org.jcp.xml.dsig.internal.dom");
58 private Transform apacheTransform;
59 protected Document ownerDoc;
60 protected Element transformElem;
61 protected TransformParameterSpec params;
62
63 public final AlgorithmParameterSpec getParameterSpec() {
64 return params;
65 }
66
67 public void init(XMLStructure parent, XMLCryptoContext context)
68 throws InvalidAlgorithmParameterException {
69 if (context != null && !(context instanceof DOMCryptoContext)) {
70 throw new ClassCastException
71 ("context must be of type DOMCryptoContext");
72 }
73 transformElem = (Element)
74 ((javax.xml.crypto.dom.DOMStructure) parent).getNode();
75 ownerDoc = DOMUtils.getOwnerDocument(transformElem);
76 }
77
78 public void marshalParams(XMLStructure parent, XMLCryptoContext context)
79 throws MarshalException {
80 if (context != null && !(context instanceof DOMCryptoContext)) {
81 throw new ClassCastException
82 ("context must be of type DOMCryptoContext");
83 }
84 transformElem = (Element)
85 ((javax.xml.crypto.dom.DOMStructure) parent).getNode();
86 ownerDoc = DOMUtils.getOwnerDocument(transformElem);
87 }
88
89 public Data transform(Data data, XMLCryptoContext xc)
90 throws TransformException {
91 if (data == null) {
92 throw new NullPointerException("data must not be null");
93 }
94 return transformIt(data, xc, (OutputStream) null);
95 }
96
97 public Data transform(Data data, XMLCryptoContext xc, OutputStream os)
98 throws TransformException {
99 if (data == null) {
100 throw new NullPointerException("data must not be null");
101 }
102 if (os == null) {
103 throw new NullPointerException("output stream must not be null");
104 }
105 return transformIt(data, xc, os);
106 }
107
108 private Data transformIt(Data data, XMLCryptoContext xc, OutputStream os)
109 throws TransformException {
110
111 if (ownerDoc == null) {
112 throw new TransformException("transform must be marshalled");
113 }
114
115 if (apacheTransform == null) {
116 try {
117 apacheTransform = Transform.getInstance
118 (ownerDoc, getAlgorithm(), transformElem.getChildNodes());
119 apacheTransform.setElement(transformElem, xc.getBaseURI());
120 if (log.isLoggable(Level.FINE)) {
121 log.log(Level.FINE, "Created transform for algorithm: "
122 + getAlgorithm());
123 }
124 } catch (Exception ex) {
125 throw new TransformException
126 ("Couldn't find Transform for: " + getAlgorithm(), ex);
127 }
128 }
129
130 XMLSignatureInput in;
131 if (data instanceof ApacheData) {
132 if (log.isLoggable(Level.FINE)) {
133 log.log(Level.FINE, "ApacheData = true");
134 }
135 in = ((ApacheData) data).getXMLSignatureInput();
136 } else if (data instanceof NodeSetData) {
137 if (log.isLoggable(Level.FINE)) {
138 log.log(Level.FINE, "isNodeSet() = true");
139 }
140 if (data instanceof DOMSubTreeData) {
141 if (log.isLoggable(Level.FINE)) {
142 log.log(Level.FINE, "DOMSubTreeData = true");
143 }
144 DOMSubTreeData subTree = (DOMSubTreeData) data;
145 in = new XMLSignatureInput(subTree.getRoot());
146 in.setExcludeComments(subTree.excludeComments());
147 } else {
148 Set nodeSet =
149 Utils.toNodeSet(((NodeSetData) data).iterator());
150 in = new XMLSignatureInput(nodeSet);
151 }
152 } else {
153 if (log.isLoggable(Level.FINE)) {
154 log.log(Level.FINE, "isNodeSet() = false");
155 }
156 try {
157 in = new XMLSignatureInput
158 (((OctetStreamData)data).getOctetStream());
159 } catch (Exception ex) {
160 throw new TransformException(ex);
161 }
162 }
163
164 try {
165 if (os != null) {
166 in = apacheTransform.performTransform(in, os);
167 if (!in.isNodeSet() && !in.isElement()) {
168 return null;
169 }
170 } else {
171 in = apacheTransform.performTransform(in);
172 }
173 if (in.isOctetStream()) {
174 return new ApacheOctetStreamData(in);
175 } else {
176 return new ApacheNodeSetData(in);
177 }
178 } catch (Exception ex) {
179 throw new TransformException(ex);
180 }
181 }
182
183 public final boolean isFeatureSupported(String feature) {
184 if (feature == null) {
185 throw new NullPointerException();
186 } else {
187 return false;
188 }
189 }
190}