author | mkos |
Sun, 15 Dec 2013 23:35:45 +0100 | |
changeset 23959 | f37ffa18553c |
parent 23782 | 953bfc3fbe31 |
child 24598 | 6203ccb62532 |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
23782
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
12009 | 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.messaging.saaj.soap; |
|
27 |
||
28 |
import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
|
29 |
||
30 |
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.ASCIIUtility; |
|
31 |
||
32 |
import com.sun.xml.internal.messaging.saaj.packaging.mime.Header; |
|
33 |
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimePartDataSource; |
|
34 |
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.InternetHeaders; |
|
35 |
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart; |
|
36 |
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility; |
|
37 |
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; |
|
38 |
import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; |
|
39 |
||
40 |
import java.io.IOException; |
|
41 |
import java.io.InputStream; |
|
42 |
import java.io.ByteArrayOutputStream; |
|
43 |
import java.io.ByteArrayInputStream; |
|
44 |
import java.io.OutputStream; |
|
45 |
import java.util.Iterator; |
|
46 |
import java.util.List; |
|
47 |
import java.util.logging.Level; |
|
48 |
import java.util.logging.Logger; |
|
49 |
||
50 |
import javax.activation.*; |
|
51 |
import javax.xml.soap.*; |
|
52 |
import com.sun.xml.internal.org.jvnet.mimepull.MIMEPart; |
|
53 |
||
54 |
/** |
|
55 |
* Implementation of attachments. |
|
56 |
* |
|
57 |
* @author Anil Vijendran (akv@eng.sun.com) |
|
58 |
*/ |
|
59 |
public class AttachmentPartImpl extends AttachmentPart { |
|
60 |
||
61 |
protected static final Logger log = |
|
62 |
Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, |
|
63 |
"com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); |
|
64 |
||
65 |
private final MimeHeaders headers; |
|
66 |
private MimeBodyPart rawContent = null; |
|
67 |
private DataHandler dataHandler = null; |
|
68 |
||
69 |
//alternate impl that uses a MIMEPart |
|
70 |
private MIMEPart mimePart = null; |
|
71 |
||
72 |
public AttachmentPartImpl() { |
|
73 |
headers = new MimeHeaders(); |
|
23959 | 74 |
|
75 |
// initialization from here should cover most of cases; |
|
76 |
// if not, it would be necessary to call |
|
77 |
// AttachmentPartImpl.initializeJavaActivationHandlers() |
|
78 |
// explicitly by programmer |
|
79 |
initializeJavaActivationHandlers(); |
|
12009 | 80 |
} |
81 |
||
82 |
public AttachmentPartImpl(MIMEPart part) { |
|
83 |
headers = new MimeHeaders(); |
|
84 |
mimePart = part; |
|
85 |
List<? extends com.sun.xml.internal.org.jvnet.mimepull.Header> hdrs = part.getAllHeaders(); |
|
86 |
for (com.sun.xml.internal.org.jvnet.mimepull.Header hd : hdrs) { |
|
87 |
headers.addHeader(hd.getName(), hd.getValue()); |
|
88 |
} |
|
89 |
} |
|
90 |
||
91 |
public int getSize() throws SOAPException { |
|
92 |
if (mimePart != null) { |
|
93 |
try { |
|
94 |
return mimePart.read().available(); |
|
95 |
} catch (IOException e) { |
|
96 |
return -1; |
|
97 |
} |
|
98 |
} |
|
99 |
if ((rawContent == null) && (dataHandler == null)) |
|
100 |
return 0; |
|
101 |
||
102 |
if (rawContent != null) { |
|
103 |
try { |
|
104 |
return rawContent.getSize(); |
|
105 |
} catch (Exception ex) { |
|
106 |
log.log( |
|
107 |
Level.SEVERE, |
|
108 |
"SAAJ0573.soap.attachment.getrawbytes.ioexception", |
|
109 |
new String[] { ex.getLocalizedMessage()}); |
|
110 |
throw new SOAPExceptionImpl("Raw InputStream Error: " + ex); |
|
111 |
} |
|
112 |
} else { |
|
113 |
ByteOutputStream bout = new ByteOutputStream(); |
|
114 |
try { |
|
115 |
dataHandler.writeTo(bout); |
|
116 |
} catch (IOException ex) { |
|
117 |
log.log( |
|
118 |
Level.SEVERE, |
|
119 |
"SAAJ0501.soap.data.handler.err", |
|
120 |
new String[] { ex.getLocalizedMessage()}); |
|
121 |
throw new SOAPExceptionImpl("Data handler error: " + ex); |
|
122 |
} |
|
123 |
return bout.size(); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
public void clearContent() { |
|
128 |
if (mimePart != null) { |
|
129 |
mimePart.close(); |
|
130 |
mimePart = null; |
|
131 |
} |
|
132 |
dataHandler = null; |
|
133 |
rawContent = null; |
|
134 |
} |
|
135 |
||
136 |
public Object getContent() throws SOAPException { |
|
137 |
try { |
|
138 |
if (mimePart != null) { |
|
139 |
//return an inputstream |
|
140 |
return mimePart.read(); |
|
141 |
} |
|
142 |
if (dataHandler != null) { |
|
143 |
return getDataHandler().getContent(); |
|
144 |
} else if (rawContent != null) { |
|
145 |
return rawContent.getContent(); |
|
146 |
} else { |
|
147 |
log.severe("SAAJ0572.soap.no.content.for.attachment"); |
|
148 |
throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
149 |
} |
|
150 |
} catch (Exception ex) { |
|
151 |
log.log(Level.SEVERE, "SAAJ0575.soap.attachment.getcontent.exception", ex); |
|
152 |
throw new SOAPExceptionImpl(ex.getLocalizedMessage()); |
|
153 |
} |
|
154 |
} |
|
155 |
||
156 |
public void setContent(Object object, String contentType) |
|
157 |
throws IllegalArgumentException { |
|
158 |
if (mimePart != null) { |
|
159 |
mimePart.close(); |
|
160 |
mimePart = null; |
|
161 |
} |
|
162 |
DataHandler dh = new DataHandler(object, contentType); |
|
163 |
||
164 |
setDataHandler(dh); |
|
165 |
} |
|
166 |
||
167 |
||
168 |
public DataHandler getDataHandler() throws SOAPException { |
|
169 |
if (mimePart != null) { |
|
170 |
//return an inputstream |
|
171 |
return new DataHandler(new DataSource() { |
|
172 |
||
173 |
public InputStream getInputStream() throws IOException { |
|
174 |
return mimePart.read(); |
|
175 |
} |
|
176 |
||
177 |
public OutputStream getOutputStream() throws IOException { |
|
178 |
throw new UnsupportedOperationException("getOutputStream cannot be supported : You have enabled LazyAttachments Option"); |
|
179 |
} |
|
180 |
||
181 |
public String getContentType() { |
|
182 |
return mimePart.getContentType(); |
|
183 |
} |
|
184 |
||
185 |
public String getName() { |
|
186 |
return "MIMEPart Wrapper DataSource"; |
|
187 |
} |
|
188 |
}); |
|
189 |
} |
|
190 |
if (dataHandler == null) { |
|
191 |
if (rawContent != null) { |
|
192 |
return new DataHandler(new MimePartDataSource(rawContent)); |
|
193 |
} |
|
194 |
log.severe("SAAJ0502.soap.no.handler.for.attachment"); |
|
195 |
throw new SOAPExceptionImpl("No data handler associated with this attachment"); |
|
196 |
} |
|
197 |
return dataHandler; |
|
198 |
} |
|
199 |
||
200 |
public void setDataHandler(DataHandler dataHandler) |
|
201 |
throws IllegalArgumentException { |
|
202 |
if (mimePart != null) { |
|
203 |
mimePart.close(); |
|
204 |
mimePart = null; |
|
205 |
} |
|
206 |
if (dataHandler == null) { |
|
207 |
log.severe("SAAJ0503.soap.no.null.to.dataHandler"); |
|
208 |
throw new IllegalArgumentException("Null dataHandler argument to setDataHandler"); |
|
209 |
} |
|
210 |
this.dataHandler = dataHandler; |
|
211 |
rawContent = null; |
|
212 |
||
16791 | 213 |
if (log.isLoggable(Level.FINE)) |
214 |
log.log(Level.FINE, "SAAJ0580.soap.set.Content-Type", |
|
215 |
new String[] { dataHandler.getContentType() }); |
|
12009 | 216 |
setMimeHeader("Content-Type", dataHandler.getContentType()); |
217 |
} |
|
218 |
||
219 |
public void removeAllMimeHeaders() { |
|
220 |
headers.removeAllHeaders(); |
|
221 |
} |
|
222 |
||
223 |
public void removeMimeHeader(String header) { |
|
224 |
headers.removeHeader(header); |
|
225 |
} |
|
226 |
||
227 |
public String[] getMimeHeader(String name) { |
|
228 |
return headers.getHeader(name); |
|
229 |
} |
|
230 |
||
231 |
public void setMimeHeader(String name, String value) { |
|
232 |
headers.setHeader(name, value); |
|
233 |
} |
|
234 |
||
235 |
public void addMimeHeader(String name, String value) { |
|
236 |
headers.addHeader(name, value); |
|
237 |
} |
|
238 |
||
239 |
public Iterator getAllMimeHeaders() { |
|
240 |
return headers.getAllHeaders(); |
|
241 |
} |
|
242 |
||
243 |
public Iterator getMatchingMimeHeaders(String[] names) { |
|
244 |
return headers.getMatchingHeaders(names); |
|
245 |
} |
|
246 |
||
247 |
public Iterator getNonMatchingMimeHeaders(String[] names) { |
|
248 |
return headers.getNonMatchingHeaders(names); |
|
249 |
} |
|
250 |
||
251 |
boolean hasAllHeaders(MimeHeaders hdrs) { |
|
252 |
if (hdrs != null) { |
|
253 |
Iterator i = hdrs.getAllHeaders(); |
|
254 |
while (i.hasNext()) { |
|
255 |
MimeHeader hdr = (MimeHeader) i.next(); |
|
256 |
String[] values = headers.getHeader(hdr.getName()); |
|
257 |
boolean found = false; |
|
258 |
||
259 |
if (values != null) { |
|
260 |
for (int j = 0; j < values.length; j++) |
|
261 |
if (hdr.getValue().equalsIgnoreCase(values[j])) { |
|
262 |
found = true; |
|
263 |
break; |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
if (!found) { |
|
268 |
return false; |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
return true; |
|
273 |
} |
|
274 |
||
275 |
MimeBodyPart getMimePart() throws SOAPException { |
|
276 |
try { |
|
277 |
if (this.mimePart != null) { |
|
278 |
return new MimeBodyPart(mimePart); |
|
279 |
} |
|
280 |
if (rawContent != null) { |
|
281 |
copyMimeHeaders(headers, rawContent); |
|
282 |
return rawContent; |
|
283 |
} |
|
284 |
||
285 |
MimeBodyPart envelope = new MimeBodyPart(); |
|
286 |
||
287 |
envelope.setDataHandler(dataHandler); |
|
288 |
copyMimeHeaders(headers, envelope); |
|
289 |
||
290 |
return envelope; |
|
291 |
} catch (Exception ex) { |
|
292 |
log.severe("SAAJ0504.soap.cannot.externalize.attachment"); |
|
293 |
throw new SOAPExceptionImpl("Unable to externalize attachment", ex); |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
public static void copyMimeHeaders(MimeHeaders headers, MimeBodyPart mbp) |
|
298 |
throws SOAPException { |
|
299 |
||
300 |
Iterator i = headers.getAllHeaders(); |
|
301 |
||
302 |
while (i.hasNext()) |
|
303 |
try { |
|
304 |
MimeHeader mh = (MimeHeader) i.next(); |
|
305 |
||
306 |
mbp.setHeader(mh.getName(), mh.getValue()); |
|
307 |
} catch (Exception ex) { |
|
308 |
log.severe("SAAJ0505.soap.cannot.copy.mime.hdr"); |
|
309 |
throw new SOAPExceptionImpl("Unable to copy MIME header", ex); |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
public static void copyMimeHeaders(MimeBodyPart mbp, AttachmentPartImpl ap) |
|
314 |
throws SOAPException { |
|
315 |
try { |
|
316 |
List hdr = mbp.getAllHeaders(); |
|
317 |
int sz = hdr.size(); |
|
318 |
for( int i=0; i<sz; i++ ) { |
|
319 |
Header h = (Header)hdr.get(i); |
|
320 |
if(h.getName().equalsIgnoreCase("Content-Type")) |
|
321 |
continue; // skip |
|
322 |
ap.addMimeHeader(h.getName(), h.getValue()); |
|
323 |
} |
|
324 |
} catch (Exception ex) { |
|
325 |
log.severe("SAAJ0506.soap.cannot.copy.mime.hdrs.into.attachment"); |
|
326 |
throw new SOAPExceptionImpl( |
|
327 |
"Unable to copy MIME headers into attachment", |
|
328 |
ex); |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
public void setBase64Content(InputStream content, String contentType) |
|
333 |
throws SOAPException { |
|
334 |
||
335 |
if (mimePart != null) { |
|
336 |
mimePart.close(); |
|
337 |
mimePart = null; |
|
338 |
} |
|
339 |
dataHandler = null; |
|
340 |
InputStream decoded = null; |
|
23782
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
341 |
ByteOutputStream bos = null; |
12009 | 342 |
try { |
343 |
decoded = MimeUtility.decode(content, "base64"); |
|
344 |
InternetHeaders hdrs = new InternetHeaders(); |
|
345 |
hdrs.setHeader("Content-Type", contentType); |
|
346 |
//TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart |
|
347 |
// Ctor with inputStream causes problems based on the InputStream |
|
348 |
// has markSupported()==true |
|
23782
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
349 |
bos = new ByteOutputStream(); |
12009 | 350 |
bos.write(decoded); |
351 |
rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount()); |
|
352 |
setMimeHeader("Content-Type", contentType); |
|
353 |
} catch (Exception e) { |
|
354 |
log.log(Level.SEVERE, "SAAJ0578.soap.attachment.setbase64content.exception", e); |
|
355 |
throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
356 |
} finally { |
|
23782
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
357 |
if (bos != null) |
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
358 |
bos.close(); |
12009 | 359 |
try { |
23782
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
360 |
if (decoded != null) |
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
361 |
decoded.close(); |
12009 | 362 |
} catch (IOException ex) { |
363 |
throw new SOAPException(ex); |
|
364 |
} |
|
365 |
} |
|
366 |
} |
|
367 |
||
368 |
public InputStream getBase64Content() throws SOAPException { |
|
369 |
InputStream stream; |
|
370 |
if (mimePart != null) { |
|
371 |
stream = mimePart.read(); |
|
372 |
} else if (rawContent != null) { |
|
373 |
try { |
|
374 |
stream = rawContent.getInputStream(); |
|
375 |
} catch (Exception e) { |
|
376 |
log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e); |
|
377 |
throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
378 |
} |
|
379 |
} else if (dataHandler != null) { |
|
380 |
try { |
|
381 |
stream = dataHandler.getInputStream(); |
|
382 |
} catch (IOException e) { |
|
383 |
log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
|
384 |
throw new SOAPExceptionImpl("DataHandler error" + e); |
|
385 |
} |
|
386 |
} else { |
|
387 |
log.severe("SAAJ0572.soap.no.content.for.attachment"); |
|
388 |
throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
389 |
} |
|
390 |
||
391 |
//TODO: Write a BASE64EncoderInputStream instead, |
|
392 |
// this code below is inefficient |
|
393 |
// where we are trying to read the whole attachment first |
|
394 |
int len; |
|
395 |
int size = 1024; |
|
396 |
byte [] buf; |
|
397 |
if (stream != null) { |
|
398 |
try { |
|
399 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(size); |
|
400 |
//TODO: try and optimize this on the same lines as |
|
401 |
// ByteOutputStream : to eliminate the temp buffer here |
|
402 |
OutputStream ret = MimeUtility.encode(bos, "base64"); |
|
403 |
buf = new byte[size]; |
|
404 |
while ((len = stream.read(buf, 0, size)) != -1) { |
|
405 |
ret.write(buf, 0, len); |
|
406 |
} |
|
407 |
ret.flush(); |
|
408 |
buf = bos.toByteArray(); |
|
409 |
return new ByteArrayInputStream(buf); |
|
410 |
} catch (Exception e) { |
|
411 |
// throw new SOAPException |
|
412 |
log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e); |
|
413 |
throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
414 |
} finally { |
|
415 |
try { |
|
416 |
stream.close(); |
|
417 |
} catch (IOException ex) { |
|
418 |
//close the stream |
|
419 |
} |
|
420 |
} |
|
421 |
} else { |
|
422 |
//throw new SOAPException |
|
423 |
log.log(Level.SEVERE,"SAAJ0572.soap.no.content.for.attachment"); |
|
424 |
throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
425 |
} |
|
426 |
} |
|
427 |
||
428 |
public void setRawContent(InputStream content, String contentType) |
|
429 |
throws SOAPException { |
|
430 |
if (mimePart != null) { |
|
431 |
mimePart.close(); |
|
432 |
mimePart = null; |
|
433 |
} |
|
434 |
dataHandler = null; |
|
23782
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
435 |
ByteOutputStream bos = null; |
12009 | 436 |
try { |
437 |
InternetHeaders hdrs = new InternetHeaders(); |
|
438 |
hdrs.setHeader("Content-Type", contentType); |
|
439 |
//TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart |
|
440 |
// Ctor with inputStream causes problems based on whether the InputStream has |
|
441 |
// markSupported()==true or false |
|
23782
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
442 |
bos = new ByteOutputStream(); |
12009 | 443 |
bos.write(content); |
444 |
rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount()); |
|
445 |
setMimeHeader("Content-Type", contentType); |
|
446 |
} catch (Exception e) { |
|
447 |
log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e); |
|
448 |
throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
449 |
} finally { |
|
23782
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
450 |
if (bos != null) |
953bfc3fbe31
8036030: Update JAX-WS RI integration to latest version
mkos
parents:
16791
diff
changeset
|
451 |
bos.close(); |
12009 | 452 |
try { |
453 |
content.close(); |
|
454 |
} catch (IOException ex) { |
|
455 |
throw new SOAPException(ex); |
|
456 |
} |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
/* |
|
461 |
public void setRawContentBytes(byte[] content, String contentType) |
|
462 |
throws SOAPException { |
|
463 |
if (content == null) { |
|
464 |
throw new SOAPExceptionImpl("Null content passed to setRawContentBytes"); |
|
465 |
} |
|
466 |
dataHandler = null; |
|
467 |
try { |
|
468 |
InternetHeaders hdrs = new InternetHeaders(); |
|
469 |
hdrs.setHeader("Content-Type", contentType); |
|
470 |
rawContent = new MimeBodyPart(hdrs, content, content.length); |
|
471 |
setMimeHeader("Content-Type", contentType); |
|
472 |
} catch (Exception e) { |
|
473 |
log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e); |
|
474 |
throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
475 |
} |
|
476 |
} */ |
|
477 |
||
478 |
public void setRawContentBytes( |
|
479 |
byte[] content, int off, int len, String contentType) |
|
480 |
throws SOAPException { |
|
481 |
if (mimePart != null) { |
|
482 |
mimePart.close(); |
|
483 |
mimePart = null; |
|
484 |
} |
|
485 |
if (content == null) { |
|
486 |
throw new SOAPExceptionImpl("Null content passed to setRawContentBytes"); |
|
487 |
} |
|
488 |
dataHandler = null; |
|
489 |
try { |
|
490 |
InternetHeaders hdrs = new InternetHeaders(); |
|
491 |
hdrs.setHeader("Content-Type", contentType); |
|
492 |
rawContent = new MimeBodyPart(hdrs, content, off, len); |
|
493 |
setMimeHeader("Content-Type", contentType); |
|
494 |
} catch (Exception e) { |
|
495 |
log.log(Level.SEVERE, |
|
496 |
"SAAJ0576.soap.attachment.setrawcontent.exception", e); |
|
497 |
throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
498 |
} |
|
499 |
} |
|
500 |
||
501 |
public InputStream getRawContent() throws SOAPException { |
|
502 |
if (mimePart != null) { |
|
503 |
return mimePart.read(); |
|
504 |
} |
|
505 |
if (rawContent != null) { |
|
506 |
try { |
|
507 |
return rawContent.getInputStream(); |
|
508 |
} catch (Exception e) { |
|
509 |
log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e); |
|
510 |
throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
511 |
} |
|
512 |
} else if (dataHandler != null) { |
|
513 |
try { |
|
514 |
return dataHandler.getInputStream(); |
|
515 |
} catch (IOException e) { |
|
516 |
log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
|
517 |
throw new SOAPExceptionImpl("DataHandler error" + e); |
|
518 |
} |
|
519 |
} else { |
|
520 |
log.severe("SAAJ0572.soap.no.content.for.attachment"); |
|
521 |
throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
522 |
} |
|
523 |
} |
|
524 |
||
525 |
public byte[] getRawContentBytes() throws SOAPException { |
|
526 |
InputStream ret; |
|
527 |
if (mimePart != null) { |
|
528 |
try { |
|
529 |
ret = mimePart.read(); |
|
530 |
return ASCIIUtility.getBytes(ret); |
|
531 |
} catch (IOException ex) { |
|
532 |
log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", ex); |
|
533 |
throw new SOAPExceptionImpl(ex); |
|
534 |
} |
|
535 |
} |
|
536 |
if (rawContent != null) { |
|
537 |
try { |
|
538 |
ret = rawContent.getInputStream(); |
|
539 |
return ASCIIUtility.getBytes(ret); |
|
540 |
} catch (Exception e) { |
|
541 |
log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e); |
|
542 |
throw new SOAPExceptionImpl(e); |
|
543 |
} |
|
544 |
} else if (dataHandler != null) { |
|
545 |
try { |
|
546 |
ret = dataHandler.getInputStream(); |
|
547 |
return ASCIIUtility.getBytes(ret); |
|
548 |
} catch (IOException e) { |
|
549 |
log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
|
550 |
throw new SOAPExceptionImpl("DataHandler error" + e); |
|
551 |
} |
|
552 |
} else { |
|
553 |
log.severe("SAAJ0572.soap.no.content.for.attachment"); |
|
554 |
throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
// attachments are equal if they are the same reference |
|
559 |
public boolean equals(Object o) { |
|
560 |
return (this == o); |
|
561 |
} |
|
562 |
||
16791 | 563 |
// In JDK 8 we get a warning if we implement equals() but not hashCode(). |
564 |
// There is no intuitive value for this, the default one in Object is fine. |
|
565 |
public int hashCode() { |
|
566 |
return super.hashCode(); |
|
567 |
} |
|
568 |
||
12009 | 569 |
public MimeHeaders getMimeHeaders() { |
570 |
return headers; |
|
571 |
} |
|
572 |
||
23959 | 573 |
public static void initializeJavaActivationHandlers() { |
574 |
// DataHandler.writeTo() may search for DCH. So adding some default ones. |
|
575 |
try { |
|
576 |
CommandMap map = CommandMap.getDefaultCommandMap(); |
|
577 |
if (map instanceof MailcapCommandMap) { |
|
578 |
MailcapCommandMap mailMap = (MailcapCommandMap) map; |
|
579 |
||
580 |
// registering our DCH since javamail's DCH doesn't handle |
|
581 |
if (!cmdMapInitialized(mailMap)) { |
|
582 |
mailMap.addMailcap("text/xml;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler"); |
|
583 |
mailMap.addMailcap("application/xml;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler"); |
|
584 |
mailMap.addMailcap("application/fastinfoset;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.FastInfosetDataContentHandler"); |
|
585 |
mailMap.addMailcap("multipart/*;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.MultipartDataContentHandler"); |
|
586 |
mailMap.addMailcap("image/*;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.ImageDataContentHandler"); |
|
587 |
mailMap.addMailcap("text/plain;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.StringDataContentHandler"); |
|
588 |
} |
|
589 |
} |
|
590 |
} catch (Throwable t) { |
|
591 |
// ignore the exception. |
|
592 |
} |
|
593 |
} |
|
594 |
||
595 |
private static boolean cmdMapInitialized(MailcapCommandMap mailMap) { |
|
596 |
||
597 |
// checking fastinfoset handler, since this one is specific to SAAJ |
|
598 |
CommandInfo[] commands = mailMap.getAllCommands("application/fastinfoset"); |
|
599 |
if (commands == null || commands.length == 0) { |
|
600 |
return false; |
|
601 |
} |
|
602 |
||
603 |
String saajClassName = "com.sun.xml.internal.ws.binding.FastInfosetDataContentHandler"; |
|
604 |
for (CommandInfo command : commands) { |
|
605 |
String commandClass = command.getCommandClass(); |
|
606 |
if (saajClassName.equals(commandClass)) { |
|
607 |
return true; |
|
608 |
} |
|
609 |
} |
|
610 |
return false; |
|
611 |
} |
|
12009 | 612 |
} |