|
1 /* |
|
2 * $Id: AttachmentPartImpl.java,v 1.45 2006/01/27 12:49:26 vj135062 Exp $ |
|
3 * $Revision: 1.45 $ |
|
4 * $Date: 2006/01/27 12:49:26 $ |
|
5 */ |
|
6 |
|
7 /* |
|
8 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. |
|
9 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
10 * |
|
11 * This code is free software; you can redistribute it and/or modify it |
|
12 * under the terms of the GNU General Public License version 2 only, as |
|
13 * published by the Free Software Foundation. Sun designates this |
|
14 * particular file as subject to the "Classpath" exception as provided |
|
15 * by Sun in the LICENSE file that accompanied this code. |
|
16 * |
|
17 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
20 * version 2 for more details (a copy is included in the LICENSE file that |
|
21 * accompanied this code). |
|
22 * |
|
23 * You should have received a copy of the GNU General Public License version |
|
24 * 2 along with this work; if not, write to the Free Software Foundation, |
|
25 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
26 * |
|
27 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
28 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
29 * have any questions. |
|
30 */ |
|
31 package com.sun.xml.internal.messaging.saaj.soap; |
|
32 |
|
33 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl; |
|
34 |
|
35 import com.sun.xml.internal.messaging.saaj.packaging.mime.util.ASCIIUtility; |
|
36 |
|
37 import com.sun.xml.internal.messaging.saaj.packaging.mime.Header; |
|
38 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimePartDataSource; |
|
39 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.InternetHeaders; |
|
40 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeBodyPart; |
|
41 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility; |
|
42 import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream; |
|
43 import com.sun.xml.internal.messaging.saaj.util.LogDomainConstants; |
|
44 |
|
45 import javax.activation.CommandMap; |
|
46 import javax.activation.DataHandler; |
|
47 import javax.activation.MailcapCommandMap; |
|
48 import javax.xml.soap.AttachmentPart; |
|
49 import javax.xml.soap.MimeHeader; |
|
50 import javax.xml.soap.MimeHeaders; |
|
51 import javax.xml.soap.SOAPException; |
|
52 import java.io.IOException; |
|
53 import java.io.InputStream; |
|
54 import java.io.ByteArrayOutputStream; |
|
55 import java.io.ByteArrayInputStream; |
|
56 import java.io.OutputStream; |
|
57 import java.util.Iterator; |
|
58 import java.util.List; |
|
59 import java.util.logging.Level; |
|
60 import java.util.logging.Logger; |
|
61 |
|
62 import javax.activation.*; |
|
63 import javax.xml.soap.*; |
|
64 |
|
65 /** |
|
66 * Implementation of attachments. |
|
67 * |
|
68 * @author Anil Vijendran (akv@eng.sun.com) |
|
69 */ |
|
70 public class AttachmentPartImpl extends AttachmentPart { |
|
71 |
|
72 protected static Logger log = |
|
73 Logger.getLogger(LogDomainConstants.SOAP_DOMAIN, |
|
74 "com.sun.xml.internal.messaging.saaj.soap.LocalStrings"); |
|
75 |
|
76 static { |
|
77 try { |
|
78 CommandMap map = CommandMap.getDefaultCommandMap(); |
|
79 if (map instanceof MailcapCommandMap) { |
|
80 MailcapCommandMap mailMap = (MailcapCommandMap) map; |
|
81 String hndlrStr = ";;x-java-content-handler="; |
|
82 mailMap.addMailcap( |
|
83 "text/xml" |
|
84 + hndlrStr |
|
85 + "com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler"); |
|
86 mailMap.addMailcap( |
|
87 "application/xml" |
|
88 + hndlrStr |
|
89 + "com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler"); |
|
90 mailMap.addMailcap( |
|
91 "application/fastinfoset" |
|
92 + hndlrStr |
|
93 + "com.sun.xml.internal.messaging.saaj.soap.FastInfosetDataContentHandler"); |
|
94 mailMap.addMailcap( |
|
95 "image/jpeg" |
|
96 + hndlrStr |
|
97 + "com.sun.xml.internal.messaging.saaj.soap.JpegDataContentHandler"); |
|
98 mailMap.addMailcap( |
|
99 "image/gif" |
|
100 + hndlrStr |
|
101 + "com.sun.xml.internal.messaging.saaj.soap.GifDataContentHandler"); |
|
102 /*mailMap.addMailcap( |
|
103 "multipart/*" |
|
104 + hndlrStr |
|
105 + "com.sun.xml.internal.messaging.saaj.soap.MultipartDataContentHandler");*/ |
|
106 mailMap.addMailcap( |
|
107 "image/*" |
|
108 + hndlrStr |
|
109 + "com.sun.xml.internal.messaging.saaj.soap.ImageDataContentHandler"); |
|
110 mailMap.addMailcap( |
|
111 "text/plain" |
|
112 + hndlrStr |
|
113 + "com.sun.xml.internal.messaging.saaj.soap.StringDataContentHandler"); |
|
114 } else { |
|
115 throw new SOAPExceptionImpl("Default CommandMap is not a MailcapCommandMap"); |
|
116 } |
|
117 } catch (Throwable t) { |
|
118 log.log( |
|
119 Level.SEVERE, |
|
120 "SAAJ0508.soap.cannot.register.handlers", |
|
121 t); |
|
122 if (t instanceof RuntimeException) { |
|
123 throw (RuntimeException) t; |
|
124 } else { |
|
125 throw new RuntimeException(t.getLocalizedMessage()); |
|
126 } |
|
127 } |
|
128 }; |
|
129 |
|
130 private final MimeHeaders headers; |
|
131 private MimeBodyPart rawContent = null; |
|
132 private DataHandler dataHandler = null; |
|
133 |
|
134 public AttachmentPartImpl() { |
|
135 headers = new MimeHeaders(); |
|
136 } |
|
137 |
|
138 public int getSize() throws SOAPException { |
|
139 byte[] bytes; |
|
140 |
|
141 if ((rawContent == null) && (dataHandler == null)) |
|
142 return 0; |
|
143 |
|
144 if (rawContent != null) { |
|
145 try { |
|
146 return rawContent.getSize(); |
|
147 } catch (Exception ex) { |
|
148 log.log( |
|
149 Level.SEVERE, |
|
150 "SAAJ0573.soap.attachment.getrawbytes.ioexception", |
|
151 new String[] { ex.getLocalizedMessage()}); |
|
152 throw new SOAPExceptionImpl("Raw InputStream Error: " + ex); |
|
153 } |
|
154 } else { |
|
155 ByteOutputStream bout = new ByteOutputStream(); |
|
156 try { |
|
157 dataHandler.writeTo(bout); |
|
158 } catch (IOException ex) { |
|
159 log.log( |
|
160 Level.SEVERE, |
|
161 "SAAJ0501.soap.data.handler.err", |
|
162 new String[] { ex.getLocalizedMessage()}); |
|
163 throw new SOAPExceptionImpl("Data handler error: " + ex); |
|
164 } |
|
165 bytes = bout.getBytes(); |
|
166 if (bytes != null) |
|
167 return bytes.length; |
|
168 } |
|
169 return -1; |
|
170 } |
|
171 |
|
172 public void clearContent() { |
|
173 dataHandler = null; |
|
174 rawContent = null; |
|
175 } |
|
176 |
|
177 public Object getContent() throws SOAPException { |
|
178 try { |
|
179 if (dataHandler != null) { |
|
180 return getDataHandler().getContent(); |
|
181 } else if (rawContent != null) { |
|
182 return rawContent.getContent(); |
|
183 } else { |
|
184 log.severe("SAAJ0572.soap.no.content.for.attachment"); |
|
185 throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
186 } |
|
187 } catch (Exception ex) { |
|
188 log.log(Level.SEVERE, "SAAJ0575.soap.attachment.getcontent.exception", ex); |
|
189 throw new SOAPExceptionImpl(ex.getLocalizedMessage()); |
|
190 } |
|
191 } |
|
192 |
|
193 public void setContent(Object object, String contentType) |
|
194 throws IllegalArgumentException { |
|
195 DataHandler dh = new DataHandler(object, contentType); |
|
196 |
|
197 setDataHandler(dh); |
|
198 } |
|
199 |
|
200 |
|
201 public DataHandler getDataHandler() throws SOAPException { |
|
202 if (dataHandler == null) { |
|
203 if (rawContent != null) { |
|
204 return new DataHandler(new MimePartDataSource(rawContent)); |
|
205 } |
|
206 log.severe("SAAJ0502.soap.no.handler.for.attachment"); |
|
207 throw new SOAPExceptionImpl("No data handler associated with this attachment"); |
|
208 } |
|
209 return dataHandler; |
|
210 } |
|
211 |
|
212 public void setDataHandler(DataHandler dataHandler) |
|
213 throws IllegalArgumentException { |
|
214 if (dataHandler == null) { |
|
215 log.severe("SAAJ0503.soap.no.null.to.dataHandler"); |
|
216 throw new IllegalArgumentException("Null dataHandler argument to setDataHandler"); |
|
217 } |
|
218 this.dataHandler = dataHandler; |
|
219 rawContent = null; |
|
220 |
|
221 log.log( |
|
222 Level.FINE, |
|
223 "SAAJ0580.soap.set.Content-Type", |
|
224 new String[] { dataHandler.getContentType()}); |
|
225 setMimeHeader("Content-Type", dataHandler.getContentType()); |
|
226 } |
|
227 |
|
228 public void removeAllMimeHeaders() { |
|
229 headers.removeAllHeaders(); |
|
230 } |
|
231 |
|
232 public void removeMimeHeader(String header) { |
|
233 headers.removeHeader(header); |
|
234 } |
|
235 |
|
236 public String[] getMimeHeader(String name) { |
|
237 return headers.getHeader(name); |
|
238 } |
|
239 |
|
240 public void setMimeHeader(String name, String value) { |
|
241 headers.setHeader(name, value); |
|
242 } |
|
243 |
|
244 public void addMimeHeader(String name, String value) { |
|
245 headers.addHeader(name, value); |
|
246 } |
|
247 |
|
248 public Iterator getAllMimeHeaders() { |
|
249 return headers.getAllHeaders(); |
|
250 } |
|
251 |
|
252 public Iterator getMatchingMimeHeaders(String[] names) { |
|
253 return headers.getMatchingHeaders(names); |
|
254 } |
|
255 |
|
256 public Iterator getNonMatchingMimeHeaders(String[] names) { |
|
257 return headers.getNonMatchingHeaders(names); |
|
258 } |
|
259 |
|
260 boolean hasAllHeaders(MimeHeaders hdrs) { |
|
261 if (hdrs != null) { |
|
262 Iterator i = hdrs.getAllHeaders(); |
|
263 while (i.hasNext()) { |
|
264 MimeHeader hdr = (MimeHeader) i.next(); |
|
265 String[] values = headers.getHeader(hdr.getName()); |
|
266 boolean found = false; |
|
267 |
|
268 if (values != null) { |
|
269 for (int j = 0; j < values.length; j++) |
|
270 if (hdr.getValue().equalsIgnoreCase(values[j])) { |
|
271 found = true; |
|
272 break; |
|
273 } |
|
274 } |
|
275 |
|
276 if (!found) { |
|
277 return false; |
|
278 } |
|
279 } |
|
280 } |
|
281 return true; |
|
282 } |
|
283 |
|
284 MimeBodyPart getMimePart() throws SOAPException { |
|
285 try { |
|
286 if (rawContent != null) { |
|
287 copyMimeHeaders(headers, rawContent); |
|
288 return rawContent; |
|
289 } |
|
290 |
|
291 MimeBodyPart envelope = new MimeBodyPart(); |
|
292 |
|
293 envelope.setDataHandler(dataHandler); |
|
294 copyMimeHeaders(headers, envelope); |
|
295 |
|
296 return envelope; |
|
297 } catch (Exception ex) { |
|
298 log.severe("SAAJ0504.soap.cannot.externalize.attachment"); |
|
299 throw new SOAPExceptionImpl("Unable to externalize attachment", ex); |
|
300 } |
|
301 } |
|
302 |
|
303 public static void copyMimeHeaders(MimeHeaders headers, MimeBodyPart mbp) |
|
304 throws SOAPException { |
|
305 |
|
306 Iterator i = headers.getAllHeaders(); |
|
307 |
|
308 while (i.hasNext()) |
|
309 try { |
|
310 MimeHeader mh = (MimeHeader) i.next(); |
|
311 |
|
312 mbp.setHeader(mh.getName(), mh.getValue()); |
|
313 } catch (Exception ex) { |
|
314 log.severe("SAAJ0505.soap.cannot.copy.mime.hdr"); |
|
315 throw new SOAPExceptionImpl("Unable to copy MIME header", ex); |
|
316 } |
|
317 } |
|
318 |
|
319 public static void copyMimeHeaders(MimeBodyPart mbp, AttachmentPartImpl ap) |
|
320 throws SOAPException { |
|
321 try { |
|
322 List hdr = mbp.getAllHeaders(); |
|
323 int sz = hdr.size(); |
|
324 for( int i=0; i<sz; i++ ) { |
|
325 Header h = (Header)hdr.get(i); |
|
326 if(h.getName().equalsIgnoreCase("Content-Type")) |
|
327 continue; // skip |
|
328 ap.addMimeHeader(h.getName(), h.getValue()); |
|
329 } |
|
330 } catch (Exception ex) { |
|
331 log.severe("SAAJ0506.soap.cannot.copy.mime.hdrs.into.attachment"); |
|
332 throw new SOAPExceptionImpl( |
|
333 "Unable to copy MIME headers into attachment", |
|
334 ex); |
|
335 } |
|
336 } |
|
337 |
|
338 public void setBase64Content(InputStream content, String contentType) |
|
339 throws SOAPException { |
|
340 dataHandler = null; |
|
341 try { |
|
342 InputStream decoded = MimeUtility.decode(content, "base64"); |
|
343 InternetHeaders hdrs = new InternetHeaders(); |
|
344 hdrs.setHeader("Content-Type", contentType); |
|
345 //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart |
|
346 // Ctor with inputStream causes problems based on the InputStream |
|
347 // has markSupported()==true |
|
348 ByteOutputStream bos = new ByteOutputStream(); |
|
349 bos.write(decoded); |
|
350 rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount()); |
|
351 setMimeHeader("Content-Type", contentType); |
|
352 } catch (Exception e) { |
|
353 log.log(Level.SEVERE, "SAAJ0578.soap.attachment.setbase64content.exception", e); |
|
354 throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
355 } |
|
356 } |
|
357 |
|
358 public InputStream getBase64Content() throws SOAPException { |
|
359 InputStream stream; |
|
360 if (rawContent != null) { |
|
361 try { |
|
362 stream = rawContent.getInputStream(); |
|
363 } catch (Exception e) { |
|
364 log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e); |
|
365 throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
366 } |
|
367 } else if (dataHandler != null) { |
|
368 try { |
|
369 stream = dataHandler.getInputStream(); |
|
370 } catch (IOException e) { |
|
371 log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
|
372 throw new SOAPExceptionImpl("DataHandler error" + e); |
|
373 } |
|
374 } else { |
|
375 log.severe("SAAJ0572.soap.no.content.for.attachment"); |
|
376 throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
377 } |
|
378 |
|
379 //TODO: Write a BASE64EncoderInputStream instead, |
|
380 // this code below is inefficient |
|
381 // where we are trying to read the whole attachment first |
|
382 int len; |
|
383 int size = 1024; |
|
384 byte [] buf; |
|
385 if (stream != null) { |
|
386 try { |
|
387 ByteArrayOutputStream bos = new ByteArrayOutputStream(size); |
|
388 //TODO: try and optimize this on the same lines as |
|
389 // ByteOutputStream : to eliminate the temp buffer here |
|
390 OutputStream ret = MimeUtility.encode(bos, "base64"); |
|
391 buf = new byte[size]; |
|
392 while ((len = stream.read(buf, 0, size)) != -1) { |
|
393 ret.write(buf, 0, len); |
|
394 } |
|
395 ret.flush(); |
|
396 buf = bos.toByteArray(); |
|
397 return new ByteArrayInputStream(buf); |
|
398 } catch (Exception e) { |
|
399 // throw new SOAPException |
|
400 log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e); |
|
401 throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
402 } |
|
403 } else { |
|
404 //throw new SOAPException |
|
405 log.log(Level.SEVERE,"SAAJ0572.soap.no.content.for.attachment"); |
|
406 throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
407 } |
|
408 } |
|
409 |
|
410 public void setRawContent(InputStream content, String contentType) |
|
411 throws SOAPException { |
|
412 dataHandler = null; |
|
413 try { |
|
414 InternetHeaders hdrs = new InternetHeaders(); |
|
415 hdrs.setHeader("Content-Type", contentType); |
|
416 //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart |
|
417 // Ctor with inputStream causes problems based on whether the InputStream has |
|
418 // markSupported()==true or false |
|
419 ByteOutputStream bos = new ByteOutputStream(); |
|
420 bos.write(content); |
|
421 rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount()); |
|
422 setMimeHeader("Content-Type", contentType); |
|
423 } catch (Exception e) { |
|
424 log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e); |
|
425 throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
426 } |
|
427 } |
|
428 |
|
429 /* |
|
430 public void setRawContentBytes(byte[] content, String contentType) |
|
431 throws SOAPException { |
|
432 if (content == null) { |
|
433 throw new SOAPExceptionImpl("Null content passed to setRawContentBytes"); |
|
434 } |
|
435 dataHandler = null; |
|
436 try { |
|
437 InternetHeaders hdrs = new InternetHeaders(); |
|
438 hdrs.setHeader("Content-Type", contentType); |
|
439 rawContent = new MimeBodyPart(hdrs, content, content.length); |
|
440 setMimeHeader("Content-Type", contentType); |
|
441 } catch (Exception e) { |
|
442 log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e); |
|
443 throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
444 } |
|
445 } */ |
|
446 |
|
447 public void setRawContentBytes( |
|
448 byte[] content, int off, int len, String contentType) |
|
449 throws SOAPException { |
|
450 if (content == null) { |
|
451 throw new SOAPExceptionImpl("Null content passed to setRawContentBytes"); |
|
452 } |
|
453 dataHandler = null; |
|
454 try { |
|
455 InternetHeaders hdrs = new InternetHeaders(); |
|
456 hdrs.setHeader("Content-Type", contentType); |
|
457 rawContent = new MimeBodyPart(hdrs, content, off, len); |
|
458 setMimeHeader("Content-Type", contentType); |
|
459 } catch (Exception e) { |
|
460 log.log(Level.SEVERE, |
|
461 "SAAJ0576.soap.attachment.setrawcontent.exception", e); |
|
462 throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
463 } |
|
464 } |
|
465 |
|
466 public InputStream getRawContent() throws SOAPException { |
|
467 if (rawContent != null) { |
|
468 try { |
|
469 return rawContent.getInputStream(); |
|
470 } catch (Exception e) { |
|
471 log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e); |
|
472 throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
473 } |
|
474 } else if (dataHandler != null) { |
|
475 try { |
|
476 return dataHandler.getInputStream(); |
|
477 } catch (IOException e) { |
|
478 log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
|
479 throw new SOAPExceptionImpl("DataHandler error" + e); |
|
480 } |
|
481 } else { |
|
482 log.severe("SAAJ0572.soap.no.content.for.attachment"); |
|
483 throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
484 } |
|
485 } |
|
486 |
|
487 //TODO: investigate size differences in mime.AttachImageTest |
|
488 public byte[] getRawContentBytes() throws SOAPException { |
|
489 InputStream ret; |
|
490 if (rawContent != null) { |
|
491 try { |
|
492 ret = rawContent.getInputStream(); |
|
493 return ASCIIUtility.getBytes(ret); |
|
494 } catch (Exception e) { |
|
495 log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e); |
|
496 throw new SOAPExceptionImpl(e.getLocalizedMessage()); |
|
497 } |
|
498 } else if (dataHandler != null) { |
|
499 try { |
|
500 ret = dataHandler.getInputStream(); |
|
501 return ASCIIUtility.getBytes(ret); |
|
502 } catch (IOException e) { |
|
503 log.severe("SAAJ0574.soap.attachment.datahandler.ioexception"); |
|
504 throw new SOAPExceptionImpl("DataHandler error" + e); |
|
505 } |
|
506 } else { |
|
507 log.severe("SAAJ0572.soap.no.content.for.attachment"); |
|
508 throw new SOAPExceptionImpl("No data handler/content associated with this attachment"); |
|
509 } |
|
510 } |
|
511 |
|
512 // attachments are equal if they are the same reference |
|
513 public boolean equals(Object o) { |
|
514 return (this == o); |
|
515 } |
|
516 |
|
517 public MimeHeaders getMimeHeaders() { |
|
518 return headers; |
|
519 } |
|
520 |
|
521 } |