1 /*
2 * Copyright (c) 1997, 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
71
72 public AttachmentPartImpl() {
73 headers = new MimeHeaders();
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();
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
213 if (log.isLoggable(Level.FINE))
214 log.log(Level.FINE, "SAAJ0580.soap.set.Content-Type",
215 new String[] { dataHandler.getContentType() });
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) {
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;
341 ByteOutputStream bos = null;
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
349 bos = new ByteOutputStream();
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 {
357 if (bos != null)
358 bos.close();
359 try {
360 if (decoded != null)
361 decoded.close();
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");
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;
435 ByteOutputStream bos = null;
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
442 bos = new ByteOutputStream();
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);
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
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
569 public MimeHeaders getMimeHeaders() {
570 return headers;
571 }
572
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");
|
1 /*
2 * Copyright (c) 1997, 2017, 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
71
72 public AttachmentPartImpl() {
73 headers = new MimeHeaders();
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();
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 @Override
92 public int getSize() throws SOAPException {
93 if (mimePart != null) {
94 try {
95 return mimePart.read().available();
96 } catch (IOException e) {
97 return -1;
98 }
99 }
100 if ((rawContent == null) && (dataHandler == null))
101 return 0;
102
103 if (rawContent != null) {
104 try {
105 return rawContent.getSize();
106 } catch (Exception ex) {
107 log.log(
108 Level.SEVERE,
109 "SAAJ0573.soap.attachment.getrawbytes.ioexception",
110 new String[] { ex.getLocalizedMessage()});
111 throw new SOAPExceptionImpl("Raw InputStream Error: " + ex);
112 }
113 } else {
114 ByteOutputStream bout = new ByteOutputStream();
115 try {
116 dataHandler.writeTo(bout);
117 } catch (IOException ex) {
118 log.log(
119 Level.SEVERE,
120 "SAAJ0501.soap.data.handler.err",
121 new String[] { ex.getLocalizedMessage()});
122 throw new SOAPExceptionImpl("Data handler error: " + ex);
123 }
124 return bout.size();
125 }
126 }
127
128 @Override
129 public void clearContent() {
130 if (mimePart != null) {
131 mimePart.close();
132 mimePart = null;
133 }
134 dataHandler = null;
135 rawContent = null;
136 }
137
138 @Override
139 public Object getContent() throws SOAPException {
140 try {
141 if (mimePart != null) {
142 //return an inputstream
143 return mimePart.read();
144 }
145 if (dataHandler != null) {
146 return getDataHandler().getContent();
147 } else if (rawContent != null) {
148 return rawContent.getContent();
149 } else {
150 log.severe("SAAJ0572.soap.no.content.for.attachment");
151 throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
152 }
153 } catch (Exception ex) {
154 log.log(Level.SEVERE, "SAAJ0575.soap.attachment.getcontent.exception", ex);
155 throw new SOAPExceptionImpl(ex.getLocalizedMessage());
156 }
157 }
158
159 @Override
160 public void setContent(Object object, String contentType)
161 throws IllegalArgumentException {
162 if (mimePart != null) {
163 mimePart.close();
164 mimePart = null;
165 }
166 DataHandler dh = new DataHandler(object, contentType);
167
168 setDataHandler(dh);
169 }
170
171
172 @Override
173 public DataHandler getDataHandler() throws SOAPException {
174 if (mimePart != null) {
175 //return an inputstream
176 return new DataHandler(new DataSource() {
177
178 @Override
179 public InputStream getInputStream() throws IOException {
180 return mimePart.read();
181 }
182
183 @Override
184 public OutputStream getOutputStream() throws IOException {
185 throw new UnsupportedOperationException("getOutputStream cannot be supported : You have enabled LazyAttachments Option");
186 }
187
188 @Override
189 public String getContentType() {
190 return mimePart.getContentType();
191 }
192
193 @Override
194 public String getName() {
195 return "MIMEPart Wrapper DataSource";
196 }
197 });
198 }
199 if (dataHandler == null) {
200 if (rawContent != null) {
201 return new DataHandler(new MimePartDataSource(rawContent));
202 }
203 log.severe("SAAJ0502.soap.no.handler.for.attachment");
204 throw new SOAPExceptionImpl("No data handler associated with this attachment");
205 }
206 return dataHandler;
207 }
208
209 @Override
210 public void setDataHandler(DataHandler dataHandler)
211 throws IllegalArgumentException {
212 if (mimePart != null) {
213 mimePart.close();
214 mimePart = null;
215 }
216 if (dataHandler == null) {
217 log.severe("SAAJ0503.soap.no.null.to.dataHandler");
218 throw new IllegalArgumentException("Null dataHandler argument to setDataHandler");
219 }
220 this.dataHandler = dataHandler;
221 rawContent = null;
222
223 if (log.isLoggable(Level.FINE))
224 log.log(Level.FINE, "SAAJ0580.soap.set.Content-Type",
225 new String[] { dataHandler.getContentType() });
226 setMimeHeader("Content-Type", dataHandler.getContentType());
227 }
228
229 @Override
230 public void removeAllMimeHeaders() {
231 headers.removeAllHeaders();
232 }
233
234 @Override
235 public void removeMimeHeader(String header) {
236 headers.removeHeader(header);
237 }
238
239 @Override
240 public String[] getMimeHeader(String name) {
241 return headers.getHeader(name);
242 }
243
244 @Override
245 public void setMimeHeader(String name, String value) {
246 headers.setHeader(name, value);
247 }
248
249 @Override
250 public void addMimeHeader(String name, String value) {
251 headers.addHeader(name, value);
252 }
253
254 @Override
255 public Iterator<MimeHeader> getAllMimeHeaders() {
256 return headers.getAllHeaders();
257 }
258
259 @Override
260 public Iterator<MimeHeader> getMatchingMimeHeaders(String[] names) {
261 return headers.getMatchingHeaders(names);
262 }
263
264 @Override
265 public Iterator<MimeHeader> getNonMatchingMimeHeaders(String[] names) {
266 return headers.getNonMatchingHeaders(names);
267 }
268
269 boolean hasAllHeaders(MimeHeaders hdrs) {
270 if (hdrs != null) {
271 Iterator i = hdrs.getAllHeaders();
272 while (i.hasNext()) {
273 MimeHeader hdr = (MimeHeader) i.next();
274 String[] values = headers.getHeader(hdr.getName());
275 boolean found = false;
276
277 if (values != null) {
278 for (int j = 0; j < values.length; j++)
279 if (hdr.getValue().equalsIgnoreCase(values[j])) {
280 found = true;
281 break;
282 }
283 }
284
285 if (!found) {
330
331 public static void copyMimeHeaders(MimeBodyPart mbp, AttachmentPartImpl ap)
332 throws SOAPException {
333 try {
334 List hdr = mbp.getAllHeaders();
335 int sz = hdr.size();
336 for( int i=0; i<sz; i++ ) {
337 Header h = (Header)hdr.get(i);
338 if(h.getName().equalsIgnoreCase("Content-Type"))
339 continue; // skip
340 ap.addMimeHeader(h.getName(), h.getValue());
341 }
342 } catch (Exception ex) {
343 log.severe("SAAJ0506.soap.cannot.copy.mime.hdrs.into.attachment");
344 throw new SOAPExceptionImpl(
345 "Unable to copy MIME headers into attachment",
346 ex);
347 }
348 }
349
350 @Override
351 public void setBase64Content(InputStream content, String contentType)
352 throws SOAPException {
353
354 if (mimePart != null) {
355 mimePart.close();
356 mimePart = null;
357 }
358 dataHandler = null;
359 InputStream decoded = null;
360 ByteOutputStream bos = null;
361 try {
362 decoded = MimeUtility.decode(content, "base64");
363 InternetHeaders hdrs = new InternetHeaders();
364 hdrs.setHeader("Content-Type", contentType);
365 //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart
366 // Ctor with inputStream causes problems based on the InputStream
367 // has markSupported()==true
368 bos = new ByteOutputStream();
369 bos.write(decoded);
370 rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount());
371 setMimeHeader("Content-Type", contentType);
372 } catch (Exception e) {
373 log.log(Level.SEVERE, "SAAJ0578.soap.attachment.setbase64content.exception", e);
374 throw new SOAPExceptionImpl(e.getLocalizedMessage());
375 } finally {
376 if (bos != null)
377 bos.close();
378 try {
379 if (decoded != null)
380 decoded.close();
381 } catch (IOException ex) {
382 throw new SOAPException(ex);
383 }
384 }
385 }
386
387 @Override
388 public InputStream getBase64Content() throws SOAPException {
389 InputStream stream;
390 if (mimePart != null) {
391 stream = mimePart.read();
392 } else if (rawContent != null) {
393 try {
394 stream = rawContent.getInputStream();
395 } catch (Exception e) {
396 log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e);
397 throw new SOAPExceptionImpl(e.getLocalizedMessage());
398 }
399 } else if (dataHandler != null) {
400 try {
401 stream = dataHandler.getInputStream();
402 } catch (IOException e) {
403 log.severe("SAAJ0574.soap.attachment.datahandler.ioexception");
404 throw new SOAPExceptionImpl("DataHandler error" + e);
405 }
406 } else {
407 log.severe("SAAJ0572.soap.no.content.for.attachment");
428 buf = bos.toByteArray();
429 return new ByteArrayInputStream(buf);
430 } catch (Exception e) {
431 // throw new SOAPException
432 log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e);
433 throw new SOAPExceptionImpl(e.getLocalizedMessage());
434 } finally {
435 try {
436 stream.close();
437 } catch (IOException ex) {
438 //close the stream
439 }
440 }
441 } else {
442 //throw new SOAPException
443 log.log(Level.SEVERE,"SAAJ0572.soap.no.content.for.attachment");
444 throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
445 }
446 }
447
448 @Override
449 public void setRawContent(InputStream content, String contentType)
450 throws SOAPException {
451 if (mimePart != null) {
452 mimePart.close();
453 mimePart = null;
454 }
455 dataHandler = null;
456 ByteOutputStream bos = null;
457 try {
458 InternetHeaders hdrs = new InternetHeaders();
459 hdrs.setHeader("Content-Type", contentType);
460 //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart
461 // Ctor with inputStream causes problems based on whether the InputStream has
462 // markSupported()==true or false
463 bos = new ByteOutputStream();
464 bos.write(content);
465 rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount());
466 setMimeHeader("Content-Type", contentType);
467 } catch (Exception e) {
468 log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e);
479 }
480
481 /*
482 public void setRawContentBytes(byte[] content, String contentType)
483 throws SOAPException {
484 if (content == null) {
485 throw new SOAPExceptionImpl("Null content passed to setRawContentBytes");
486 }
487 dataHandler = null;
488 try {
489 InternetHeaders hdrs = new InternetHeaders();
490 hdrs.setHeader("Content-Type", contentType);
491 rawContent = new MimeBodyPart(hdrs, content, content.length);
492 setMimeHeader("Content-Type", contentType);
493 } catch (Exception e) {
494 log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e);
495 throw new SOAPExceptionImpl(e.getLocalizedMessage());
496 }
497 } */
498
499 @Override
500 public void setRawContentBytes(
501 byte[] content, int off, int len, String contentType)
502 throws SOAPException {
503 if (mimePart != null) {
504 mimePart.close();
505 mimePart = null;
506 }
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, off, len);
515 setMimeHeader("Content-Type", contentType);
516 } catch (Exception e) {
517 log.log(Level.SEVERE,
518 "SAAJ0576.soap.attachment.setrawcontent.exception", e);
519 throw new SOAPExceptionImpl(e.getLocalizedMessage());
520 }
521 }
522
523 @Override
524 public InputStream getRawContent() throws SOAPException {
525 if (mimePart != null) {
526 return mimePart.read();
527 }
528 if (rawContent != null) {
529 try {
530 return rawContent.getInputStream();
531 } catch (Exception e) {
532 log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e);
533 throw new SOAPExceptionImpl(e.getLocalizedMessage());
534 }
535 } else if (dataHandler != null) {
536 try {
537 return dataHandler.getInputStream();
538 } catch (IOException e) {
539 log.severe("SAAJ0574.soap.attachment.datahandler.ioexception");
540 throw new SOAPExceptionImpl("DataHandler error" + e);
541 }
542 } else {
543 log.severe("SAAJ0572.soap.no.content.for.attachment");
544 throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
545 }
546 }
547
548 @Override
549 public byte[] getRawContentBytes() throws SOAPException {
550 InputStream ret;
551 if (mimePart != null) {
552 try {
553 ret = mimePart.read();
554 return ASCIIUtility.getBytes(ret);
555 } catch (IOException ex) {
556 log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", ex);
557 throw new SOAPExceptionImpl(ex);
558 }
559 }
560 if (rawContent != null) {
561 try {
562 ret = rawContent.getInputStream();
563 return ASCIIUtility.getBytes(ret);
564 } catch (Exception e) {
565 log.log(Level.SEVERE,"SAAJ0577.soap.attachment.getrawcontent.exception", e);
566 throw new SOAPExceptionImpl(e);
567 }
568 } else if (dataHandler != null) {
569 try {
570 ret = dataHandler.getInputStream();
571 return ASCIIUtility.getBytes(ret);
572 } catch (IOException e) {
573 log.severe("SAAJ0574.soap.attachment.datahandler.ioexception");
574 throw new SOAPExceptionImpl("DataHandler error" + e);
575 }
576 } else {
577 log.severe("SAAJ0572.soap.no.content.for.attachment");
578 throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
579 }
580 }
581
582 // attachments are equal if they are the same reference
583 @Override
584 public boolean equals(Object o) {
585 return (this == o);
586 }
587
588 // In JDK 8 we get a warning if we implement equals() but not hashCode().
589 // There is no intuitive value for this, the default one in Object is fine.
590 @Override
591 public int hashCode() {
592 return super.hashCode();
593 }
594
595 public MimeHeaders getMimeHeaders() {
596 return headers;
597 }
598
599 public static void initializeJavaActivationHandlers() {
600 // DataHandler.writeTo() may search for DCH. So adding some default ones.
601 try {
602 CommandMap map = CommandMap.getDefaultCommandMap();
603 if (map instanceof MailcapCommandMap) {
604 MailcapCommandMap mailMap = (MailcapCommandMap) map;
605
606 // registering our DCH since javamail's DCH doesn't handle
607 if (!cmdMapInitialized(mailMap)) {
608 mailMap.addMailcap("text/xml;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler");
609 mailMap.addMailcap("application/xml;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler");
610 mailMap.addMailcap("application/fastinfoset;;x-java-content-handler=com.sun.xml.internal.messaging.saaj.soap.FastInfosetDataContentHandler");
|