1 /* |
|
2 * Copyright (c) 2014, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.org.jvnet.staxex.util; |
|
27 |
|
28 import java.io.OutputStream; |
|
29 import java.util.Arrays; |
|
30 import java.util.Iterator; |
|
31 import java.util.UUID; |
|
32 |
|
33 import javax.activation.DataHandler; |
|
34 import javax.xml.bind.attachment.AttachmentMarshaller; |
|
35 import javax.xml.soap.SOAPException; |
|
36 import javax.xml.soap.SOAPMessage; |
|
37 import javax.xml.stream.XMLStreamException; |
|
38 |
|
39 import com.sun.xml.internal.org.jvnet.staxex.Base64Data; |
|
40 import com.sun.xml.internal.org.jvnet.staxex.BinaryText; |
|
41 import com.sun.xml.internal.org.jvnet.staxex.MtomEnabled; |
|
42 import com.sun.xml.internal.org.jvnet.staxex.NamespaceContextEx; |
|
43 import com.sun.xml.internal.org.jvnet.staxex.StreamingDataHandler; |
|
44 import com.sun.xml.internal.org.jvnet.staxex.XMLStreamWriterEx; |
|
45 // |
|
46 //import com.sun.xml.internal.ws.api.message.saaj.SaajStaxWriter; |
|
47 //import com.sun.xml.internal.ws.developer.StreamingDataHandler; |
|
48 //import com.sun.xml.internal.ws.streaming.MtomStreamWriter; |
|
49 |
|
50 /** |
|
51 * SaajStaxWriterEx converts XMLStreamWriterEx calls to build an orasaaj SOAPMessage with BinaryTextImpl. |
|
52 * |
|
53 * @author shih-chang.chen@oracle.com |
|
54 */ |
|
55 public class SaajStaxWriterEx extends SaajStaxWriter implements XMLStreamWriterEx, MtomStreamWriter { |
|
56 |
|
57 static final protected String xopNS = "http://www.w3.org/2004/08/xop/include"; |
|
58 static final protected String Include = "Include"; |
|
59 static final protected String href = "href"; |
|
60 |
|
61 private enum State {xopInclude, others}; |
|
62 private State state = State.others; |
|
63 private BinaryText binaryText; |
|
64 |
|
65 public SaajStaxWriterEx(SOAPMessage msg, String uri) throws SOAPException { |
|
66 super(msg, uri); |
|
67 } |
|
68 |
|
69 public void writeStartElement(String prefix, String ln, String ns) throws XMLStreamException { |
|
70 if (xopNS.equals(ns) && Include.equals(ln)) { |
|
71 state = State.xopInclude; |
|
72 return; |
|
73 } else { |
|
74 super.writeStartElement(prefix, ln, ns); |
|
75 } |
|
76 } |
|
77 |
|
78 @Override |
|
79 public void writeEndElement() throws XMLStreamException { |
|
80 if (state.equals(State.xopInclude)) { |
|
81 state = State.others; |
|
82 } else { |
|
83 super.writeEndElement(); |
|
84 } |
|
85 } |
|
86 |
|
87 @Override |
|
88 public void writeAttribute(String prefix, String ns, String ln, String value) throws XMLStreamException { |
|
89 if (binaryText != null && href.equals(ln)) { |
|
90 return; |
|
91 } else { |
|
92 super.writeAttribute(prefix, ns, ln, value); |
|
93 } |
|
94 } |
|
95 |
|
96 // @Override |
|
97 // public void writeComment(String data) throws XMLStreamException { |
|
98 // ((ElementImpl)currentElement).addCommentNode(data); |
|
99 // } |
|
100 // |
|
101 // @Override |
|
102 // public void writeCData(String data) throws XMLStreamException { |
|
103 // CDataTextImpl cdt = new CDataTextImpl(soap.getSOAPPart(), data); |
|
104 // currentElement.appendChild(cdt); |
|
105 // } |
|
106 |
|
107 @Override |
|
108 public NamespaceContextEx getNamespaceContext() { |
|
109 return new NamespaceContextEx() { |
|
110 public String getNamespaceURI(String prefix) { |
|
111 return currentElement.getNamespaceURI(prefix); |
|
112 } |
|
113 public String getPrefix(String namespaceURI) { |
|
114 return currentElement.lookupPrefix(namespaceURI); |
|
115 } |
|
116 public Iterator getPrefixes(final String namespaceURI) { |
|
117 return new Iterator() { |
|
118 String prefix = getPrefix(namespaceURI); |
|
119 public boolean hasNext() { |
|
120 return (prefix != null); |
|
121 } |
|
122 public Object next() { |
|
123 if (prefix == null) throw new java.util.NoSuchElementException(); |
|
124 String next = prefix; |
|
125 prefix = null; |
|
126 return next; |
|
127 } |
|
128 public void remove() {} |
|
129 }; |
|
130 } |
|
131 public Iterator<Binding> iterator() { |
|
132 return new Iterator<Binding>() { |
|
133 public boolean hasNext() { return false; } |
|
134 public Binding next() { return null; } |
|
135 public void remove() {} |
|
136 }; |
|
137 } |
|
138 }; |
|
139 } |
|
140 |
|
141 @Override |
|
142 public void writeBinary(DataHandler data) throws XMLStreamException { |
|
143 // binaryText = BinaryTextImpl.createBinaryTextFromDataHandler((MessageImpl)soap, null, currentElement.getOwnerDocument(), data); |
|
144 // currentElement.appendChild(binaryText); |
|
145 addBinaryText(data); |
|
146 } |
|
147 |
|
148 @Override |
|
149 public OutputStream writeBinary(String arg0) throws XMLStreamException { |
|
150 return null; |
|
151 } |
|
152 |
|
153 @Override |
|
154 public void writeBinary(byte[] data, int offset, int length, String contentType) throws XMLStreamException { |
|
155 // if (mtomThreshold == -1 || mtomThreshold > length) return null; |
|
156 byte[] bytes = (offset == 0 && length == data.length) ? data : Arrays.copyOfRange(data, offset, offset + length); |
|
157 if (currentElement instanceof MtomEnabled) { |
|
158 binaryText = ((MtomEnabled) currentElement).addBinaryText(bytes); |
|
159 } else { |
|
160 throw new IllegalStateException("The currentElement is not MtomEnabled " + currentElement); |
|
161 } |
|
162 } |
|
163 |
|
164 @Override |
|
165 public void writePCDATA(CharSequence arg0) throws XMLStreamException { |
|
166 if (arg0 instanceof Base64Data) { |
|
167 // The fix of StreamReaderBufferCreator preserves this dataHandler |
|
168 addBinaryText(((Base64Data) arg0).getDataHandler()); |
|
169 } else { |
|
170 // We should not normally get here as we expect a DataHandler, |
|
171 // but this is the most general solution. If we do get |
|
172 // something other than a Data Handler, create a Text node with |
|
173 // the data. Another alternative would be to throw an exception, |
|
174 // but in the most general case, we don't know whether this input |
|
175 // is expected. |
|
176 try { |
|
177 currentElement.addTextNode(arg0.toString()); |
|
178 } catch (SOAPException e) { |
|
179 throw new XMLStreamException("Cannot add Text node", e); |
|
180 } |
|
181 } |
|
182 } |
|
183 |
|
184 static private String encodeCid() { |
|
185 String cid = "example.jaxws.sun.com"; |
|
186 String name = UUID.randomUUID() + "@"; |
|
187 return name + cid; |
|
188 } |
|
189 |
|
190 private String addBinaryText(DataHandler data) { |
|
191 String hrefOrCid = null; |
|
192 if (data instanceof StreamingDataHandler) { |
|
193 hrefOrCid = ((StreamingDataHandler) data).getHrefCid(); |
|
194 } |
|
195 if (hrefOrCid == null) hrefOrCid = encodeCid(); |
|
196 |
|
197 String prefixedCid = (hrefOrCid.startsWith("cid:")) ? hrefOrCid : "cid:" + hrefOrCid; |
|
198 // Should we do the threshold processing on DataHandler ? But that would be |
|
199 // expensive as DataHolder need to read the data again from its source |
|
200 //binaryText = BinaryTextImpl.createBinaryTextFromDataHandler((MessageImpl) soap, prefixedCid, currentElement.getOwnerDocument(), data); |
|
201 //currentElement.appendChild(binaryText); |
|
202 if (currentElement instanceof MtomEnabled) { |
|
203 binaryText = ((MtomEnabled) currentElement).addBinaryText(prefixedCid, data); |
|
204 } else { |
|
205 throw new IllegalStateException("The currentElement is not MtomEnabled " + currentElement); |
|
206 } |
|
207 return hrefOrCid; |
|
208 } |
|
209 |
|
210 public AttachmentMarshaller getAttachmentMarshaller() { |
|
211 return new AttachmentMarshaller() { |
|
212 @Override |
|
213 public String addMtomAttachment(DataHandler data, String ns, String ln) { |
|
214 // if (mtomThreshold == -1) return null; |
|
215 String hrefOrCid = addBinaryText(data); |
|
216 // return binaryText.getHref(); |
|
217 return hrefOrCid; |
|
218 } |
|
219 |
|
220 @Override |
|
221 public String addMtomAttachment(byte[] data, int offset, int length, String mimeType, String ns, String ln) { |
|
222 // if (mtomThreshold == -1 || mtomThreshold > length) return null; |
|
223 byte[] bytes = (offset == 0 && length == data.length) ? data : Arrays.copyOfRange(data, offset, offset + length); |
|
224 // binaryText = (BinaryTextImpl) ((ElementImpl) currentElement).addAsBase64TextNode(bytes); |
|
225 if (currentElement instanceof MtomEnabled) { |
|
226 binaryText = ((MtomEnabled) currentElement).addBinaryText(bytes); |
|
227 } else { |
|
228 throw new IllegalStateException("The currentElement is not MtomEnabled " + currentElement); |
|
229 } |
|
230 return binaryText.getHref(); |
|
231 } |
|
232 |
|
233 @Override |
|
234 public String addSwaRefAttachment(DataHandler data) { |
|
235 return "cid:"+encodeCid(); |
|
236 } |
|
237 |
|
238 @Override |
|
239 public boolean isXOPPackage() { |
|
240 return true; |
|
241 } |
|
242 }; |
|
243 } |
|
244 } |
|