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.spi.db.XMLBridge;
31 import com.sun.xml.internal.ws.util.ByteArrayBuffer;
32 import com.sun.xml.internal.ws.encoding.DataSourceStreamingDataHandler;
34 import javax.activation.DataHandler;
35 import javax.activation.DataSource;
36 import javax.xml.bind.JAXBException;
37 import javax.xml.soap.AttachmentPart;
38 import javax.xml.soap.SOAPException;
39 import javax.xml.soap.SOAPMessage;
40 import javax.xml.transform.Source;
41 import javax.xml.transform.stream.StreamSource;
42 import javax.xml.ws.WebServiceException;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.io.OutputStream;
47 /**
48 * @author Jitendra Kotamraju
49 */
50 public final class JAXBAttachment implements Attachment, DataSource {
52 private final String contentId;
53 private final String mimeType;
54 private final Object jaxbObject;
55 private final XMLBridge bridge;
57 public JAXBAttachment(@NotNull String contentId, Object jaxbObject, XMLBridge bridge, String mimeType) {
58 this.contentId = contentId;
59 this.jaxbObject = jaxbObject;
60 this.bridge = bridge;
61 this.mimeType = mimeType;
62 }
64 @Override
65 public String getContentId() {
66 return contentId;
67 }
69 @Override
70 public String getContentType() {
71 return mimeType;
72 }
74 @Override
75 public byte[] asByteArray() {
76 ByteArrayBuffer bab = new ByteArrayBuffer();
77 try {
78 writeTo(bab);
79 } catch (IOException e) {
80 throw new WebServiceException(e);
81 }
82 return bab.getRawData();
83 }
85 @Override
86 public DataHandler asDataHandler() {
87 return new DataSourceStreamingDataHandler(this);
88 }
90 @Override
91 public Source asSource() {
92 return new StreamSource(asInputStream());
93 }
95 @Override
96 public InputStream asInputStream() {
97 ByteArrayBuffer bab = new ByteArrayBuffer();
98 try {
99 writeTo(bab);
100 } catch (IOException e) {
101 throw new WebServiceException(e);
102 }
103 return bab.newInputStream();
104 }
106 @Override
107 public void writeTo(OutputStream os) throws IOException {
108 try {
109 bridge.marshal(jaxbObject, os, null, null);
110 } catch (JAXBException e) {
111 throw new WebServiceException(e);
112 }
113 }
115 @Override
116 public void writeTo(SOAPMessage saaj) throws SOAPException {
117 AttachmentPart part = saaj.createAttachmentPart();
118 part.setDataHandler(asDataHandler());
119 part.setContentId(contentId);
120 saaj.addAttachmentPart(part);
121 }
123 @Override
124 public InputStream getInputStream() throws IOException {
125 return asInputStream();
126 }
128 @Override
129 public OutputStream getOutputStream() throws IOException {
130 throw new UnsupportedOperationException();
131 }
133 @Override
134 public String getName() {
135 return null;
136 }
138 }