001: /*
002: * Transform.java
003: *
004: * Created on January 24, 2006, 3:42 PM
005: */
006:
007: /*
008: * The contents of this file are subject to the terms
009: * of the Common Development and Distribution License
010: * (the License). You may not use this file except in
011: * compliance with the License.
012: *
013: * You can obtain a copy of the license at
014: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * See the License for the specific language governing
016: * permissions and limitations under the License.
017: *
018: * When distributing Covered Code, include this CDDL
019: * Header Notice in each file and include the License file
020: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
021: * If applicable, add the following below the CDDL Header,
022: * with the fields enclosed by brackets [] replaced by
023: * you own identifying information:
024: * "Portions Copyrighted [year] [name of copyright owner]"
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.ws.security.opt.crypto.dsig;
030:
031: import com.sun.xml.ws.security.opt.impl.dsig.StAXEnvelopedTransformWriter;
032: import com.sun.xml.ws.security.opt.impl.dsig.StAXSTRTransformWriter;
033: import com.sun.xml.ws.security.secext10.TransformationParametersType;
034: import com.sun.xml.wss.impl.MessageConstants;
035: import com.sun.xml.wss.impl.c14n.StAXEXC14nCanonicalizerImpl;
036: import com.sun.xml.wss.logging.LogDomainConstants;
037: import java.io.ByteArrayOutputStream;
038: import java.io.IOException;
039: import java.io.OutputStream;
040: import java.security.InvalidAlgorithmParameterException;
041: import java.security.spec.AlgorithmParameterSpec;
042: import java.util.List;
043: import java.util.logging.Level;
044: import java.util.logging.Logger;
045: import javax.xml.bind.JAXBElement;
046: import javax.xml.bind.annotation.XmlRootElement;
047: import javax.xml.bind.annotation.XmlTransient;
048: import javax.xml.crypto.Data;
049: import javax.xml.crypto.XMLCryptoContext;
050: import javax.xml.crypto.dsig.TransformException;
051: import javax.xml.stream.XMLStreamException;
052: import com.sun.xml.wss.logging.impl.opt.signature.LogStringsMessages;
053:
054: /**
055: *
056: * @author Abhijit Das
057: * @author K.Venugopal@sun.com
058: */
059: @XmlRootElement(name="Transform",namespace="http://www.w3.org/2000/09/xmldsig#")
060: public class Transform extends
061: com.sun.xml.security.core.dsig.TransformType implements
062: javax.xml.crypto.dsig.Transform {
063: @XmlTransient
064: private static final Logger logger = Logger.getLogger(
065: LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN,
066: LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN_BUNDLE);
067:
068: @XmlTransient
069: private AlgorithmParameterSpec algSpec = null;
070: @XmlTransient
071: private Exc14nCanonicalizer _exc14nTransform;
072: @XmlTransient
073: private String refId = "";
074:
075: /** Creates a new instance of Transform */
076: public Transform() {
077: }
078:
079: public AlgorithmParameterSpec getParameterSpec() {
080: return algSpec;
081: }
082:
083: public void setParameterSpec(AlgorithmParameterSpec algSpec) {
084: this .algSpec = algSpec;
085: }
086:
087: public void setContent(List content) {
088: this .content = content;
089: }
090:
091: public Data transform(Data data, XMLCryptoContext xMLCryptoContext)
092: throws TransformException {
093: if (getAlgorithm() == javax.xml.crypto.dsig.CanonicalizationMethod.EXCLUSIVE) {
094: if (_exc14nTransform == null) {
095: _exc14nTransform = new Exc14nCanonicalizer();
096: }
097: return _exc14nTransform.transform(data, xMLCryptoContext);
098: } else if (getAlgorithm().equals(Transform.ENVELOPED)) {
099: return new StAXEnvelopedTransformWriter(data);
100: } else if (getAlgorithm().equals(
101: MessageConstants.STR_TRANSFORM_URI)) {
102: return new StAXSTRTransformWriter(data, xMLCryptoContext,
103: refId);
104: }
105: throw new UnsupportedOperationException("Algorithm Transform "
106: + getAlgorithm() + " not supported yet");
107: }
108:
109: public Data transform(Data data, XMLCryptoContext xMLCryptoContext,
110: OutputStream outputStream) throws TransformException {
111:
112: if (getAlgorithm().equals(MessageConstants.STR_TRANSFORM_URI)) {
113: ByteArrayOutputStream bos = new ByteArrayOutputStream();
114: OutputStream fis = outputStream;
115: if (logger.isLoggable(Level.FINEST)) {
116: fis = bos;
117: }
118: StAXEXC14nCanonicalizerImpl _canonicalizer = null;
119: if (algSpec != null || content.size() > 0) {
120: Object ob = content.get(0);
121: if (ob instanceof JAXBElement) {
122: JAXBElement el = (JAXBElement) ob;
123: TransformationParametersType tp = (TransformationParametersType) el
124: .getValue();
125: CanonicalizationMethod cm = (CanonicalizationMethod) tp
126: .getAny().get(0);
127: String algo = cm.getAlgorithm();
128: if (javax.xml.crypto.dsig.CanonicalizationMethod.EXCLUSIVE
129: .equals(algo)) {
130: _canonicalizer = new StAXEXC14nCanonicalizerImpl();
131: if (!logger.isLoggable(Level.FINEST)) {
132: _canonicalizer.setStream(outputStream);
133: } else {
134: _canonicalizer.setStream(fis);
135: }
136: }
137: }
138: }
139:
140: StAXSTRTransformWriter strWriter = new StAXSTRTransformWriter(
141: data, xMLCryptoContext, refId);
142: try {
143: strWriter.write(_canonicalizer);
144: } catch (XMLStreamException ex) {
145: throw new TransformException(ex);
146: }
147:
148: if (logger.isLoggable(Level.FINEST)) {
149: logger.log(Level.FINEST, LogStringsMessages
150: .WSS_1757_CANONICALIZED_TARGET_VALUE(bos
151: .toString()));
152: try {
153: outputStream.write(bos.toByteArray());
154: return null;
155: } catch (IOException ex) {
156: throw new TransformException(ex);
157: }
158: }
159: return null;
160: }
161:
162: ByteArrayOutputStream bos = new ByteArrayOutputStream();
163: OutputStream fis = outputStream;
164: if (logger.isLoggable(Level.FINEST)) {
165: fis = bos;
166: }
167: if (getAlgorithm().intern() == javax.xml.crypto.dsig.CanonicalizationMethod.EXCLUSIVE
168: .intern()) {
169: if (_exc14nTransform == null) {
170: _exc14nTransform = new Exc14nCanonicalizer();
171: try {
172: _exc14nTransform
173: .init((javax.xml.crypto.dsig.spec.TransformParameterSpec) algSpec);
174: } catch (InvalidAlgorithmParameterException e) {
175: throw new TransformException(e);
176: }
177: }
178: if (!logger.isLoggable(Level.FINEST)) {
179: return _exc14nTransform.transform(data,
180: xMLCryptoContext, fis);
181: } else {
182: _exc14nTransform.transform(data, xMLCryptoContext, fis);
183: logger.log(Level.FINEST, LogStringsMessages
184: .WSS_1757_CANONICALIZED_TARGET_VALUE(bos
185: .toString()));
186: try {
187: outputStream.write(bos.toByteArray());
188: return null;
189: } catch (IOException ex) {
190: throw new TransformException(ex);
191: }
192: }
193: }
194: throw new UnsupportedOperationException("Algorithm Transform "
195: + getAlgorithm() + " not supported yet");
196: }
197:
198: public boolean isFeatureSupported(String string) {
199: return false;
200: }
201:
202: public void setReferenceId(String id) {
203: this.refId = id;
204: }
205:
206: }
|