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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2013, 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


 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             }


 371                     continue;   // skip
 372                 ap.addMimeHeader(h.getName(), h.getValue());
 373             }
 374         } catch (Exception ex) {
 375             log.severe("SAAJ0506.soap.cannot.copy.mime.hdrs.into.attachment");
 376             throw new SOAPExceptionImpl(
 377                 "Unable to copy MIME headers into attachment",
 378                 ex);
 379         }
 380     }
 381 
 382     public  void setBase64Content(InputStream content, String contentType)
 383         throws SOAPException {
 384 
 385         if (mimePart != null) {
 386             mimePart.close();
 387             mimePart = null;
 388         }
 389         dataHandler = null;
 390         InputStream decoded = null;

 391         try {
 392             decoded = MimeUtility.decode(content, "base64");
 393             InternetHeaders hdrs = new InternetHeaders();
 394             hdrs.setHeader("Content-Type", contentType);
 395             //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart
 396             // Ctor with inputStream causes problems based on the InputStream
 397             // has markSupported()==true
 398             ByteOutputStream bos = new ByteOutputStream();
 399             bos.write(decoded);
 400             rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount());
 401             setMimeHeader("Content-Type", contentType);
 402         } catch (Exception e) {
 403             log.log(Level.SEVERE, "SAAJ0578.soap.attachment.setbase64content.exception", e);
 404             throw new SOAPExceptionImpl(e.getLocalizedMessage());
 405         } finally {


 406             try {

 407                 decoded.close();
 408             } catch (IOException ex) {
 409                 throw new SOAPException(ex);
 410             }
 411         }
 412     }
 413 
 414     public  InputStream getBase64Content() throws SOAPException {
 415         InputStream stream;
 416         if (mimePart != null) {
 417             stream = mimePart.read();
 418         } else if (rawContent != null) {
 419             try {
 420                  stream = rawContent.getInputStream();
 421             } catch (Exception e) {
 422                 log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e);
 423                 throw new SOAPExceptionImpl(e.getLocalizedMessage());
 424             }
 425         } else if (dataHandler != null) {
 426             try {


 461                 try {
 462                     stream.close();
 463                 } catch (IOException ex) {
 464                   //close the stream
 465                 }
 466             }
 467         } else {
 468           //throw  new SOAPException
 469           log.log(Level.SEVERE,"SAAJ0572.soap.no.content.for.attachment");
 470           throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
 471         }
 472     }
 473 
 474     public void setRawContent(InputStream content, String contentType)
 475         throws SOAPException {
 476         if (mimePart != null) {
 477             mimePart.close();
 478             mimePart = null;
 479         }
 480         dataHandler = null;

 481         try {
 482             InternetHeaders hdrs = new InternetHeaders();
 483             hdrs.setHeader("Content-Type", contentType);
 484             //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart
 485             // Ctor with inputStream causes problems based on whether the InputStream has
 486             // markSupported()==true or false
 487             ByteOutputStream bos = new ByteOutputStream();
 488             bos.write(content);
 489             rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount());
 490             setMimeHeader("Content-Type", contentType);
 491         } catch (Exception e) {
 492             log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e);
 493             throw new SOAPExceptionImpl(e.getLocalizedMessage());
 494         } finally {


 495             try {
 496                 content.close();
 497             } catch (IOException ex) {
 498                 throw new SOAPException(ex);
 499             }
 500         }
 501     }
 502 
 503    /*
 504     public void setRawContentBytes(byte[] content, String contentType)
 505         throws SOAPException {
 506         if (content == null) {
 507             throw new SOAPExceptionImpl("Null content passed to setRawContentBytes");
 508         }
 509         dataHandler = null;
 510         try {
 511             InternetHeaders hdrs = new InternetHeaders();
 512             hdrs.setHeader("Content-Type", contentType);
 513             rawContent = new MimeBodyPart(hdrs, content, content.length);
 514             setMimeHeader("Content-Type", contentType);


   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


 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         if (mimePart != null) {
 142             try {
 143                 return mimePart.read().available();
 144             } catch (IOException e) {
 145                 return -1;
 146             }
 147         }
 148         if ((rawContent == null) && (dataHandler == null))
 149             return 0;
 150 
 151         if (rawContent != null) {
 152             try {
 153                 return rawContent.getSize();
 154             } catch (Exception ex) {
 155                 log.log(
 156                     Level.SEVERE,
 157                     "SAAJ0573.soap.attachment.getrawbytes.ioexception",
 158                     new String[] { ex.getLocalizedMessage()});
 159                 throw new SOAPExceptionImpl("Raw InputStream Error: " + ex);
 160             }


 370                     continue;   // skip
 371                 ap.addMimeHeader(h.getName(), h.getValue());
 372             }
 373         } catch (Exception ex) {
 374             log.severe("SAAJ0506.soap.cannot.copy.mime.hdrs.into.attachment");
 375             throw new SOAPExceptionImpl(
 376                 "Unable to copy MIME headers into attachment",
 377                 ex);
 378         }
 379     }
 380 
 381     public  void setBase64Content(InputStream content, String contentType)
 382         throws SOAPException {
 383 
 384         if (mimePart != null) {
 385             mimePart.close();
 386             mimePart = null;
 387         }
 388         dataHandler = null;
 389         InputStream decoded = null;
 390         ByteOutputStream bos = null;
 391         try {
 392             decoded = MimeUtility.decode(content, "base64");
 393             InternetHeaders hdrs = new InternetHeaders();
 394             hdrs.setHeader("Content-Type", contentType);
 395             //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart
 396             // Ctor with inputStream causes problems based on the InputStream
 397             // has markSupported()==true
 398             bos = new ByteOutputStream();
 399             bos.write(decoded);
 400             rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount());
 401             setMimeHeader("Content-Type", contentType);
 402         } catch (Exception e) {
 403             log.log(Level.SEVERE, "SAAJ0578.soap.attachment.setbase64content.exception", e);
 404             throw new SOAPExceptionImpl(e.getLocalizedMessage());
 405         } finally {
 406             if (bos != null)
 407                 bos.close();
 408             try {
 409                 if (decoded != null)
 410                     decoded.close();
 411             } catch (IOException ex) {
 412                 throw new SOAPException(ex);
 413             }
 414         }
 415     }
 416 
 417     public  InputStream getBase64Content() throws SOAPException {
 418         InputStream stream;
 419         if (mimePart != null) {
 420             stream = mimePart.read();
 421         } else if (rawContent != null) {
 422             try {
 423                  stream = rawContent.getInputStream();
 424             } catch (Exception e) {
 425                 log.log(Level.SEVERE,"SAAJ0579.soap.attachment.getbase64content.exception", e);
 426                 throw new SOAPExceptionImpl(e.getLocalizedMessage());
 427             }
 428         } else if (dataHandler != null) {
 429             try {


 464                 try {
 465                     stream.close();
 466                 } catch (IOException ex) {
 467                   //close the stream
 468                 }
 469             }
 470         } else {
 471           //throw  new SOAPException
 472           log.log(Level.SEVERE,"SAAJ0572.soap.no.content.for.attachment");
 473           throw new SOAPExceptionImpl("No data handler/content associated with this attachment");
 474         }
 475     }
 476 
 477     public void setRawContent(InputStream content, String contentType)
 478         throws SOAPException {
 479         if (mimePart != null) {
 480             mimePart.close();
 481             mimePart = null;
 482         }
 483         dataHandler = null;
 484         ByteOutputStream bos = null;
 485         try {
 486             InternetHeaders hdrs = new InternetHeaders();
 487             hdrs.setHeader("Content-Type", contentType);
 488             //TODO: reading the entire attachment here is ineffcient. Somehow the MimeBodyPart
 489             // Ctor with inputStream causes problems based on whether the InputStream has
 490             // markSupported()==true or false
 491             bos = new ByteOutputStream();
 492             bos.write(content);
 493             rawContent = new MimeBodyPart(hdrs, bos.getBytes(), bos.getCount());
 494             setMimeHeader("Content-Type", contentType);
 495         } catch (Exception e) {
 496             log.log(Level.SEVERE, "SAAJ0576.soap.attachment.setrawcontent.exception", e);
 497             throw new SOAPExceptionImpl(e.getLocalizedMessage());
 498         } finally {
 499             if (bos != null)
 500                 bos.close();
 501             try {
 502                 content.close();
 503             } catch (IOException ex) {
 504                 throw new SOAPException(ex);
 505             }
 506         }
 507     }
 508 
 509    /*
 510     public void setRawContentBytes(byte[] content, String contentType)
 511         throws SOAPException {
 512         if (content == null) {
 513             throw new SOAPExceptionImpl("Null content passed to setRawContentBytes");
 514         }
 515         dataHandler = null;
 516         try {
 517             InternetHeaders hdrs = new InternetHeaders();
 518             hdrs.setHeader("Content-Type", contentType);
 519             rawContent = new MimeBodyPart(hdrs, content, content.length);
 520             setMimeHeader("Content-Type", contentType);