src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/AttachmentPartImpl.java

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

mercurial