View Javadoc
1   /*
2    * Copyright (c) 1997, 2012, 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.ws.message;
27  
28  import com.sun.istack.internal.NotNull;
29  import com.sun.xml.internal.ws.api.message.Attachment;
30  import com.sun.xml.internal.ws.util.ByteArrayBuffer;
31  
32  import javax.activation.DataHandler;
33  import javax.xml.soap.AttachmentPart;
34  import javax.xml.soap.SOAPException;
35  import javax.xml.soap.SOAPMessage;
36  import javax.xml.transform.Source;
37  import javax.xml.transform.stream.StreamSource;
38  import javax.xml.ws.WebServiceException;
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.io.OutputStream;
42  import java.io.ByteArrayOutputStream;
43  
44  /**
45   * @author Jitendra Kotamraju
46   */
47  public final class DataHandlerAttachment implements Attachment {
48  
49      private final DataHandler dh;
50      private final String contentId;
51      String contentIdNoAngleBracket;
52  
53      /**
54       * This will be constructed by {@link com.sun.xml.internal.ws.message.jaxb.AttachmentMarshallerImpl}
55       */
56      public DataHandlerAttachment(@NotNull String contentId, @NotNull DataHandler dh) {
57          this.dh = dh;
58          this.contentId = contentId;
59      }
60  
61      public String getContentId() {
62  //        return contentId;
63          if (contentIdNoAngleBracket == null) {
64              contentIdNoAngleBracket = contentId;
65              if (contentIdNoAngleBracket != null && contentIdNoAngleBracket.charAt(0) == '<')
66                  contentIdNoAngleBracket = contentIdNoAngleBracket.substring(1, contentIdNoAngleBracket.length()-1);
67          }
68          return contentIdNoAngleBracket;
69      }
70  
71      public String getContentType() {
72          return dh.getContentType();
73      }
74  
75      public byte[] asByteArray() {
76          try {
77              ByteArrayOutputStream os = new ByteArrayOutputStream();
78              dh.writeTo(os);
79              return os.toByteArray();
80          } catch (IOException e) {
81              throw new WebServiceException(e);
82          }
83      }
84  
85      public DataHandler asDataHandler() {
86          return dh;
87      }
88  
89      public Source asSource() {
90          try {
91              return new StreamSource(dh.getInputStream());
92          } catch (IOException e) {
93              throw new WebServiceException(e);
94          }
95      }
96  
97      public InputStream asInputStream() {
98          try {
99              return dh.getInputStream();
100         } catch (IOException e) {
101             throw new WebServiceException(e);
102         }
103     }
104 
105     public void writeTo(OutputStream os) throws IOException {
106         dh.writeTo(os);
107     }
108 
109     public void writeTo(SOAPMessage saaj) throws SOAPException {
110         AttachmentPart part = saaj.createAttachmentPart();
111         part.setDataHandler(dh);
112         part.setContentId(contentId);
113         saaj.addAttachmentPart(part);
114     }
115 }