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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

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

mercurial