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.ws.security.opt.impl.dsig;
024:
025: import com.sun.xml.ws.security.opt.crypto.JAXBData;
026: import com.sun.xml.ws.security.opt.crypto.StreamWriterData;
027: import com.sun.xml.ws.security.opt.impl.crypto.OctectStreamData;
028: import com.sun.xml.wss.XWSSecurityException;
029: import com.sun.xml.wss.impl.MessageConstants;
030: import com.sun.xml.wss.logging.LogDomainConstants;
031: import com.sun.xml.wss.logging.impl.opt.signature.LogStringsMessages;
032: import java.util.logging.Level;
033: import java.util.logging.Logger;
034: import javax.xml.crypto.Data;
035: import javax.xml.namespace.NamespaceContext;
036: import javax.xml.stream.XMLStreamException;
037: import javax.xml.stream.XMLStreamWriter;
038: import org.jvnet.staxex.NamespaceContextEx;
039:
040: /**
041: *
042: * @author K.Venugopal@sun.com
043: */
044: public class StAXEnvelopedTransformWriter implements XMLStreamWriter,
045: StreamWriterData {
046: private static final Logger logger = Logger.getLogger(
047: LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN,
048: LogDomainConstants.IMPL_OPT_SIGNATURE_DOMAIN_BUNDLE);
049:
050: private XMLStreamWriter nextWriter = null;
051: private boolean ignore = false;
052: private Data data = null;
053: private int index = 0;
054: private NamespaceContextEx ns = null;
055:
056: /** Creates a new instance of StAXEnvelopedTransformWriter */
057: public StAXEnvelopedTransformWriter(XMLStreamWriter writer,
058: Data data) {
059: this .nextWriter = writer;
060: this .data = data;
061: if (data instanceof JAXBData) {
062: ns = ((JAXBData) data).getNamespaceContext();
063: } else if (data instanceof StreamWriterData) {
064: ns = ((StreamWriterData) data).getNamespaceContext();
065: }
066:
067: }
068:
069: public StAXEnvelopedTransformWriter(Data data) {
070: this .data = data;
071: if (data instanceof JAXBData) {
072: ns = ((JAXBData) data).getNamespaceContext();
073: } else if (data instanceof StreamWriterData) {
074: ns = ((StreamWriterData) data).getNamespaceContext();
075: }
076: }
077:
078: public NamespaceContextEx getNamespaceContext() {
079: return ns;
080: }
081:
082: public void close() throws XMLStreamException {
083: nextWriter.close();
084: }
085:
086: public void flush() throws XMLStreamException {
087: nextWriter.flush();
088: }
089:
090: public void writeEndDocument() throws XMLStreamException {
091: if (index > 0) {
092: int size = index;
093: for (int i = 0; i < size; i++) {
094: writeEndElement();
095: }
096: }
097: nextWriter.writeEndDocument();
098: }
099:
100: public void writeEndElement() throws XMLStreamException {
101: if (index > 0) {
102: index--;
103: }
104: if (index == 0) {
105: ignore = false;
106: }
107: if (!ignore) {
108: nextWriter.writeEndElement();
109: }
110: }
111:
112: public void writeStartDocument() throws XMLStreamException {
113: if (!ignore) {
114: nextWriter.writeStartDocument();
115: }
116: }
117:
118: public void writeCharacters(char[] c, int index, int len)
119: throws XMLStreamException {
120: if (!ignore) {
121: nextWriter.writeCharacters(c, index, len);
122: }
123: }
124:
125: public void setDefaultNamespace(String string)
126: throws XMLStreamException {
127: if (!ignore) {
128: nextWriter.setDefaultNamespace(string);
129: }
130: }
131:
132: public void writeCData(String string) throws XMLStreamException {
133: if (!ignore) {
134: nextWriter.writeCData(string);
135: }
136: }
137:
138: public void writeCharacters(String string)
139: throws XMLStreamException {
140: if (!ignore) {
141: nextWriter.writeCharacters(string);
142: }
143: }
144:
145: public void writeComment(String string) throws XMLStreamException {
146: if (!ignore) {
147: nextWriter.writeComment(string);
148: }
149: }
150:
151: public void writeDTD(String string) throws XMLStreamException {
152: if (!ignore) {
153: nextWriter.writeDTD(string);
154: }
155: }
156:
157: public void writeDefaultNamespace(String string)
158: throws XMLStreamException {
159: if (!ignore) {
160: nextWriter.writeDefaultNamespace(string);
161: }
162: }
163:
164: public void writeEmptyElement(String string)
165: throws XMLStreamException {
166: if (!ignore) {
167: nextWriter.writeEmptyElement(string);
168: }
169: }
170:
171: public void writeEntityRef(String string) throws XMLStreamException {
172: if (!ignore) {
173: nextWriter.writeEntityRef(string);
174: }
175: }
176:
177: public void writeProcessingInstruction(String string)
178: throws XMLStreamException {
179: if (!ignore) {
180: nextWriter.writeProcessingInstruction(string);
181: }
182: }
183:
184: public void writeStartDocument(String string)
185: throws XMLStreamException {
186: if (!ignore) {
187: nextWriter.writeStartDocument(string);
188: }
189: }
190:
191: public void writeStartElement(String string)
192: throws XMLStreamException {
193: if (!ignore) {
194: nextWriter.writeStartElement(string);
195: }
196: }
197:
198: public void setNamespaceContext(NamespaceContext namespaceContext)
199: throws XMLStreamException {
200: if (!ignore) {
201: nextWriter.setNamespaceContext(namespaceContext);
202: }
203: }
204:
205: public Object getProperty(String string)
206: throws IllegalArgumentException {
207: return nextWriter.getProperty(string);
208: }
209:
210: public String getPrefix(String string) throws XMLStreamException {
211: return nextWriter.getPrefix(string);
212: }
213:
214: public void setPrefix(String string, String string0)
215: throws XMLStreamException {
216: if (!ignore) {
217: nextWriter.setPrefix(string, string0);
218: }
219: }
220:
221: public void writeAttribute(String localName, String value)
222: throws XMLStreamException {
223: if (!ignore) {
224: nextWriter.writeAttribute(localName, value);
225: }
226: }
227:
228: public void writeEmptyElement(String string, String string0)
229: throws XMLStreamException {
230: if (!ignore) {
231: nextWriter.writeEmptyElement(string, string0);
232: }
233: }
234:
235: public void writeNamespace(String string, String string0)
236: throws XMLStreamException {
237: if (!ignore) {
238: nextWriter.writeNamespace(string, string0);
239: }
240: }
241:
242: public void writeProcessingInstruction(String string, String string0)
243: throws XMLStreamException {
244: if (!ignore) {
245: nextWriter.writeProcessingInstruction(string, string0);
246: }
247: }
248:
249: public void writeStartDocument(String string, String string0)
250: throws XMLStreamException {
251: if (!ignore) {
252: nextWriter.writeStartDocument(string, string0);
253: }
254: }
255:
256: public void writeStartElement(String namespaceURI, String localName)
257: throws XMLStreamException {
258: if (!ignore) {
259: if (localName == MessageConstants.SIGNATURE_LNAME
260: && namespaceURI == MessageConstants.DSIG_NS) {
261: ignore = true;
262: index++;
263: return;
264: }
265: nextWriter.writeStartElement(namespaceURI, localName);
266: } else {
267: index++;
268: }
269: }
270:
271: public void writeAttribute(String prefix, String namespaceURI,
272: String localName, String value) throws XMLStreamException {
273: if (!ignore) {
274: nextWriter.writeAttribute(prefix, namespaceURI, localName,
275: value);
276: }
277: }
278:
279: public void writeEmptyElement(String string, String string0,
280: String string1) throws XMLStreamException {
281: if (!ignore) {
282: nextWriter.writeEmptyElement(string, string0, string1);
283: }
284: }
285:
286: public void writeStartElement(String prefix, String localName,
287: String namespaceURI) throws XMLStreamException {
288: if (!ignore) {
289: if (localName == MessageConstants.SIGNATURE_LNAME
290: && namespaceURI == MessageConstants.DSIG_NS) {
291: ignore = true;
292: index++;
293: return;
294: }
295: nextWriter.writeStartElement(prefix, localName,
296: namespaceURI);
297: } else {
298: index++;
299: }
300: }
301:
302: public void writeAttribute(String namespaceURI, String localName,
303: String value) throws XMLStreamException {
304: if (!ignore) {
305: nextWriter.writeAttribute(namespaceURI, localName, value);
306: }
307: }
308:
309: public void write(XMLStreamWriter writer) throws XMLStreamException {
310: this .nextWriter = writer;
311: if (data instanceof JAXBData) {
312: try {
313: ((JAXBData) data).writeTo(this );
314: } catch (XWSSecurityException ex) {
315: logger.log(Level.SEVERE, LogStringsMessages
316: .WSS_1706_ERROR_ENVELOPED_SIGNATURE());
317: throw new XMLStreamException(
318: "Error occurred while performing Enveloped Signature");
319: }
320: } else if (data instanceof StreamWriterData) {
321: ((StreamWriterData) data).write(this );
322: } else if (data instanceof OctectStreamData) {
323: ((OctectStreamData) data).write(this);
324: }
325: }
326: }
|