Wed, 27 Apr 2016 01:27:09 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17
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
23 * questions.
24 */
26 package com.sun.xml.internal.ws.handler;
28 import com.sun.xml.internal.ws.api.message.MessageHeaders;
29 import com.sun.xml.internal.ws.api.message.Packet;
30 import com.sun.xml.internal.ws.api.message.Message;
31 import com.sun.xml.internal.ws.api.message.AttachmentSet;
32 import com.sun.xml.internal.ws.api.WSBinding;
33 import com.sun.xml.internal.ws.spi.db.BindingContext;
34 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
35 import com.sun.xml.internal.ws.util.xml.XmlUtil;
36 import com.sun.xml.internal.ws.message.EmptyMessageImpl;
37 import com.sun.xml.internal.ws.message.DOMMessage;
38 import com.sun.xml.internal.ws.message.jaxb.JAXBMessage;
39 import com.sun.xml.internal.ws.message.source.PayloadSourceMessage;
41 import javax.xml.bind.JAXBContext;
42 import javax.xml.bind.JAXBException;
43 import javax.xml.bind.Marshaller;
44 import javax.xml.bind.Unmarshaller;
45 import javax.xml.bind.util.JAXBSource;
46 import javax.xml.transform.Source;
47 import javax.xml.transform.Transformer;
48 import javax.xml.transform.TransformerException;
49 import javax.xml.transform.dom.DOMResult;
50 import javax.xml.transform.dom.DOMSource;
51 import javax.xml.ws.LogicalMessage;
52 import javax.xml.ws.WebServiceException;
54 import org.w3c.dom.Element;
55 import org.w3c.dom.Node;
56 import org.w3c.dom.Document;
58 /**
59 * Implementation of {@link LogicalMessage}. This class implements the methods
60 * used by LogicalHandlers to get/set the request or response either
61 * as a JAXB object or as javax.xml.transform.Source.
62 * <p/>
63 * <p>The {@link Message} that is passed into the constructor
64 * is used to retrieve the payload of the request or response.
65 *
66 * @author WS Development Team
67 * @see Message
68 * @see LogicalMessageContextImpl
69 */
70 class LogicalMessageImpl implements LogicalMessage {
71 private Packet packet;
72 // protected JAXBContext defaultJaxbContext;
73 protected BindingContext defaultJaxbContext;
74 private ImmutableLM lm = null;
77 public LogicalMessageImpl(BindingContext defaultJaxbContext, Packet
78 packet) {
79 // don't create extract payload until Users wants it.
80 this.packet = packet;
81 this.defaultJaxbContext = defaultJaxbContext;
82 }
84 public Source getPayload() {
85 if (lm == null) {
86 Source payload = packet.getMessage().copy().readPayloadAsSource();
87 if (payload instanceof DOMSource) {
88 lm = createLogicalMessageImpl(payload);
89 }
90 return payload;
91 } else {
92 return lm.getPayload();
93 }
94 }
96 public void setPayload(Source payload) {
97 lm = createLogicalMessageImpl(payload);
98 }
100 private ImmutableLM createLogicalMessageImpl(Source payload) {
101 if (payload == null) {
102 lm = new EmptyLogicalMessageImpl();
103 } else if (payload instanceof DOMSource) {
104 lm = new DOMLogicalMessageImpl((DOMSource) payload);
105 } else {
106 lm = new SourceLogicalMessageImpl(payload);
107 }
108 return lm;
109 }
111 public Object getPayload(BindingContext context) {
112 if (context == null) {
113 context = defaultJaxbContext;
114 }
115 if (context == null)
116 throw new WebServiceException("JAXBContext parameter cannot be null");
118 Object o;
119 if (lm == null) {
120 try {
121 o = packet.getMessage().copy().readPayloadAsJAXB(context.createUnmarshaller());
122 } catch (JAXBException e) {
123 throw new WebServiceException(e);
124 }
125 } else {
126 o = lm.getPayload(context);
127 lm = new JAXBLogicalMessageImpl(context.getJAXBContext(), o);
128 }
129 return o;
130 }
132 public Object getPayload(JAXBContext context) {
133 if (context == null) {
134 return getPayload(defaultJaxbContext);
135 }
136 if (context == null)
137 throw new WebServiceException("JAXBContext parameter cannot be null");
139 Object o;
140 if (lm == null) {
141 try {
142 o = packet.getMessage().copy().readPayloadAsJAXB(context.createUnmarshaller());
143 } catch (JAXBException e) {
144 throw new WebServiceException(e);
145 }
146 } else {
147 o = lm.getPayload(context);
148 lm = new JAXBLogicalMessageImpl(context, o);
149 }
150 return o;
151 }
153 public void setPayload(Object payload, BindingContext context) {
154 if (context == null) {
155 context = defaultJaxbContext;
156 }
157 if (payload == null) {
158 lm = new EmptyLogicalMessageImpl();
159 } else {
160 lm = new JAXBLogicalMessageImpl(context.getJAXBContext(), payload);
161 }
162 }
164 public void setPayload(Object payload, JAXBContext context) {
165 if (context == null) {
166 setPayload(payload, defaultJaxbContext);
167 }
168 if (payload == null) {
169 lm = new EmptyLogicalMessageImpl();
170 } else {
171 lm = new JAXBLogicalMessageImpl(context, payload);
172 }
173 }
175 public boolean isPayloadModifed() {
176 return (lm != null);
177 }
179 /**
180 * This should be called by first checking if the payload is modified.
181 *
182 * @param headers
183 * @param attachments
184 * @param binding
185 * @return
186 */
187 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
188 assert isPayloadModifed();
189 if(isPayloadModifed()) {
190 return lm.getMessage(headers,attachments,binding);
191 } else {
192 return packet.getMessage();
193 }
195 }
198 private abstract class ImmutableLM {
199 public abstract Source getPayload();
200 public abstract Object getPayload(BindingContext context);
201 public abstract Object getPayload(JAXBContext context);
202 public abstract Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding);
204 }
206 private class DOMLogicalMessageImpl extends SourceLogicalMessageImpl {
207 private DOMSource dom;
209 public DOMLogicalMessageImpl(DOMSource dom) {
210 super(dom);
211 this.dom = dom;
212 }
214 @Override
215 public Source getPayload() {
216 return dom;
217 }
219 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
220 Node n = dom.getNode();
221 if(n.getNodeType()== Node.DOCUMENT_NODE) {
222 n = ((Document)n).getDocumentElement();
223 }
224 return new DOMMessage(binding.getSOAPVersion(), headers, (Element)n, attachments);
225 }
226 }
228 private class EmptyLogicalMessageImpl extends ImmutableLM {
229 public EmptyLogicalMessageImpl() {
231 }
233 @Override
234 public Source getPayload() {
235 return null;
236 }
238 @Override
239 public Object getPayload(JAXBContext context) {
240 return null;
241 }
243 @Override
244 public Object getPayload(BindingContext context) {
245 return null;
246 }
248 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
249 return new EmptyMessageImpl(headers,attachments,binding.getSOAPVersion());
250 }
251 }
253 private class JAXBLogicalMessageImpl extends ImmutableLM {
254 private JAXBContext ctxt;
255 private Object o;
257 public JAXBLogicalMessageImpl(JAXBContext ctxt, Object o) {
258 this.ctxt = ctxt;
259 this.o = o;
261 }
263 @Override
264 public Source getPayload() {
265 JAXBContext context = ctxt;
266 if (context == null) {
267 context = defaultJaxbContext.getJAXBContext();
268 }
269 try {
270 return new JAXBSource(context, o);
271 } catch (JAXBException e) {
272 throw new WebServiceException(e);
273 }
274 }
276 @Override
277 public Object getPayload(JAXBContext context) {
278 // if(context == ctxt) {
279 // return o;
280 // }
281 try {
282 Source payloadSrc = getPayload();
283 if (payloadSrc == null)
284 return null;
285 Unmarshaller unmarshaller = context.createUnmarshaller();
286 return unmarshaller.unmarshal(payloadSrc);
287 } catch (JAXBException e) {
288 throw new WebServiceException(e);
289 }
290 }
291 public Object getPayload(BindingContext context) {
292 // if(context == ctxt) {
293 // return o;
294 // }
295 try {
296 Source payloadSrc = getPayload();
297 if (payloadSrc == null)
298 return null;
299 Unmarshaller unmarshaller = context.createUnmarshaller();
300 return unmarshaller.unmarshal(payloadSrc);
301 } catch (JAXBException e) {
302 throw new WebServiceException(e);
303 }
304 }
306 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
307 return JAXBMessage.create(BindingContextFactory.create(ctxt), o,binding.getSOAPVersion(), headers,attachments);
308 }
309 }
311 private class SourceLogicalMessageImpl extends ImmutableLM {
312 private Source payloadSrc;
314 public SourceLogicalMessageImpl(Source source) {
315 this.payloadSrc = source;
316 }
318 public Source getPayload() {
319 assert (!(payloadSrc instanceof DOMSource));
320 try {
321 Transformer transformer = XmlUtil.newTransformer();
322 DOMResult domResult = new DOMResult();
323 transformer.transform(payloadSrc, domResult);
324 DOMSource dom = new DOMSource(domResult.getNode());
325 lm = new DOMLogicalMessageImpl((DOMSource) dom);
326 payloadSrc = null;
327 return dom;
328 } catch (TransformerException te) {
329 throw new WebServiceException(te);
330 }
331 }
333 public Object getPayload(JAXBContext context) {
334 try {
335 Source payloadSrc = getPayload();
336 if (payloadSrc == null)
337 return null;
338 Unmarshaller unmarshaller = context.createUnmarshaller();
339 return unmarshaller.unmarshal(payloadSrc);
340 } catch (JAXBException e) {
341 throw new WebServiceException(e);
342 }
344 }
346 public Object getPayload(BindingContext context) {
347 try {
348 Source payloadSrc = getPayload();
349 if (payloadSrc == null)
350 return null;
351 Unmarshaller unmarshaller = context.createUnmarshaller();
352 return unmarshaller.unmarshal(payloadSrc);
353 } catch (JAXBException e) {
354 throw new WebServiceException(e);
355 }
357 }
359 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
360 assert (payloadSrc!=null);
361 return new PayloadSourceMessage(headers, payloadSrc, attachments,binding.getSOAPVersion());
362 }
363 }
364 }