Fri, 14 Feb 2014 11:13:45 +0100
8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu
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 */
26 package com.sun.xml.internal.ws.message;
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;
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;
44 /**
45 * @author Jitendra Kotamraju
46 */
47 public final class DataHandlerAttachment implements Attachment {
49 private final DataHandler dh;
50 private final String contentId;
51 String contentIdNoAngleBracket;
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 }
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 }
71 public String getContentType() {
72 return dh.getContentType();
73 }
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 }
85 public DataHandler asDataHandler() {
86 return dh;
87 }
89 public Source asSource() {
90 try {
91 return new StreamSource(dh.getInputStream());
92 } catch (IOException e) {
93 throw new WebServiceException(e);
94 }
95 }
97 public InputStream asInputStream() {
98 try {
99 return dh.getInputStream();
100 } catch (IOException e) {
101 throw new WebServiceException(e);
102 }
103 }
105 public void writeTo(OutputStream os) throws IOException {
106 dh.writeTo(os);
107 }
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 }