src/share/jaxws_classes/com/sun/xml/internal/ws/handler/LogicalMessageImpl.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

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

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.xml.internal.ws.handler;
ohair@286 27
alanb@368 28 import com.sun.xml.internal.ws.api.message.MessageHeaders;
ohair@286 29 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 30 import com.sun.xml.internal.ws.api.message.Message;
ohair@286 31 import com.sun.xml.internal.ws.api.message.AttachmentSet;
ohair@286 32 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 33 import com.sun.xml.internal.ws.spi.db.BindingContext;
ohair@286 34 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
ohair@286 35 import com.sun.xml.internal.ws.util.xml.XmlUtil;
ohair@286 36 import com.sun.xml.internal.ws.message.EmptyMessageImpl;
ohair@286 37 import com.sun.xml.internal.ws.message.DOMMessage;
ohair@286 38 import com.sun.xml.internal.ws.message.jaxb.JAXBMessage;
ohair@286 39 import com.sun.xml.internal.ws.message.source.PayloadSourceMessage;
ohair@286 40
ohair@286 41 import javax.xml.bind.JAXBContext;
ohair@286 42 import javax.xml.bind.JAXBException;
ohair@286 43 import javax.xml.bind.Marshaller;
ohair@286 44 import javax.xml.bind.Unmarshaller;
ohair@286 45 import javax.xml.bind.util.JAXBSource;
ohair@286 46 import javax.xml.transform.Source;
ohair@286 47 import javax.xml.transform.Transformer;
ohair@286 48 import javax.xml.transform.TransformerException;
ohair@286 49 import javax.xml.transform.dom.DOMResult;
ohair@286 50 import javax.xml.transform.dom.DOMSource;
ohair@286 51 import javax.xml.ws.LogicalMessage;
ohair@286 52 import javax.xml.ws.WebServiceException;
ohair@286 53
ohair@286 54 import org.w3c.dom.Element;
ohair@286 55 import org.w3c.dom.Node;
ohair@286 56 import org.w3c.dom.Document;
ohair@286 57
ohair@286 58 /**
ohair@286 59 * Implementation of {@link LogicalMessage}. This class implements the methods
ohair@286 60 * used by LogicalHandlers to get/set the request or response either
ohair@286 61 * as a JAXB object or as javax.xml.transform.Source.
ohair@286 62 * <p/>
ohair@286 63 * <p>The {@link Message} that is passed into the constructor
ohair@286 64 * is used to retrieve the payload of the request or response.
ohair@286 65 *
ohair@286 66 * @author WS Development Team
ohair@286 67 * @see Message
ohair@286 68 * @see LogicalMessageContextImpl
ohair@286 69 */
ohair@286 70 class LogicalMessageImpl implements LogicalMessage {
ohair@286 71 private Packet packet;
ohair@286 72 // protected JAXBContext defaultJaxbContext;
ohair@286 73 protected BindingContext defaultJaxbContext;
ohair@286 74 private ImmutableLM lm = null;
ohair@286 75
ohair@286 76
ohair@286 77 public LogicalMessageImpl(BindingContext defaultJaxbContext, Packet
ohair@286 78 packet) {
ohair@286 79 // don't create extract payload until Users wants it.
ohair@286 80 this.packet = packet;
ohair@286 81 this.defaultJaxbContext = defaultJaxbContext;
ohair@286 82 }
ohair@286 83
ohair@286 84 public Source getPayload() {
ohair@286 85 if (lm == null) {
ohair@286 86 Source payload = packet.getMessage().copy().readPayloadAsSource();
ohair@286 87 if (payload instanceof DOMSource) {
ohair@286 88 lm = createLogicalMessageImpl(payload);
ohair@286 89 }
ohair@286 90 return payload;
ohair@286 91 } else {
ohair@286 92 return lm.getPayload();
ohair@286 93 }
ohair@286 94 }
ohair@286 95
ohair@286 96 public void setPayload(Source payload) {
ohair@286 97 lm = createLogicalMessageImpl(payload);
ohair@286 98 }
ohair@286 99
ohair@286 100 private ImmutableLM createLogicalMessageImpl(Source payload) {
ohair@286 101 if (payload == null) {
ohair@286 102 lm = new EmptyLogicalMessageImpl();
ohair@286 103 } else if (payload instanceof DOMSource) {
ohair@286 104 lm = new DOMLogicalMessageImpl((DOMSource) payload);
ohair@286 105 } else {
ohair@286 106 lm = new SourceLogicalMessageImpl(payload);
ohair@286 107 }
ohair@286 108 return lm;
ohair@286 109 }
ohair@286 110
ohair@286 111 public Object getPayload(BindingContext context) {
ohair@286 112 if (context == null) {
ohair@286 113 context = defaultJaxbContext;
ohair@286 114 }
ohair@286 115 if (context == null)
ohair@286 116 throw new WebServiceException("JAXBContext parameter cannot be null");
ohair@286 117
ohair@286 118 Object o;
ohair@286 119 if (lm == null) {
ohair@286 120 try {
ohair@286 121 o = packet.getMessage().copy().readPayloadAsJAXB(context.createUnmarshaller());
ohair@286 122 } catch (JAXBException e) {
ohair@286 123 throw new WebServiceException(e);
ohair@286 124 }
ohair@286 125 } else {
ohair@286 126 o = lm.getPayload(context);
ohair@286 127 lm = new JAXBLogicalMessageImpl(context.getJAXBContext(), o);
ohair@286 128 }
ohair@286 129 return o;
ohair@286 130 }
ohair@286 131
ohair@286 132 public Object getPayload(JAXBContext context) {
ohair@286 133 if (context == null) {
ohair@286 134 return getPayload(defaultJaxbContext);
ohair@286 135 }
ohair@286 136 if (context == null)
ohair@286 137 throw new WebServiceException("JAXBContext parameter cannot be null");
ohair@286 138
ohair@286 139 Object o;
ohair@286 140 if (lm == null) {
ohair@286 141 try {
ohair@286 142 o = packet.getMessage().copy().readPayloadAsJAXB(context.createUnmarshaller());
ohair@286 143 } catch (JAXBException e) {
ohair@286 144 throw new WebServiceException(e);
ohair@286 145 }
ohair@286 146 } else {
ohair@286 147 o = lm.getPayload(context);
ohair@286 148 lm = new JAXBLogicalMessageImpl(context, o);
ohair@286 149 }
ohair@286 150 return o;
ohair@286 151 }
ohair@286 152
ohair@286 153 public void setPayload(Object payload, BindingContext context) {
ohair@286 154 if (context == null) {
ohair@286 155 context = defaultJaxbContext;
ohair@286 156 }
ohair@286 157 if (payload == null) {
ohair@286 158 lm = new EmptyLogicalMessageImpl();
ohair@286 159 } else {
ohair@286 160 lm = new JAXBLogicalMessageImpl(context.getJAXBContext(), payload);
ohair@286 161 }
ohair@286 162 }
ohair@286 163
ohair@286 164 public void setPayload(Object payload, JAXBContext context) {
ohair@286 165 if (context == null) {
ohair@286 166 setPayload(payload, defaultJaxbContext);
ohair@286 167 }
ohair@286 168 if (payload == null) {
ohair@286 169 lm = new EmptyLogicalMessageImpl();
ohair@286 170 } else {
ohair@286 171 lm = new JAXBLogicalMessageImpl(context, payload);
ohair@286 172 }
ohair@286 173 }
ohair@286 174
ohair@286 175 public boolean isPayloadModifed() {
ohair@286 176 return (lm != null);
ohair@286 177 }
ohair@286 178
ohair@286 179 /**
ohair@286 180 * This should be called by first checking if the payload is modified.
ohair@286 181 *
ohair@286 182 * @param headers
ohair@286 183 * @param attachments
ohair@286 184 * @param binding
ohair@286 185 * @return
ohair@286 186 */
alanb@368 187 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
ohair@286 188 assert isPayloadModifed();
ohair@286 189 if(isPayloadModifed()) {
ohair@286 190 return lm.getMessage(headers,attachments,binding);
ohair@286 191 } else {
ohair@286 192 return packet.getMessage();
ohair@286 193 }
ohair@286 194
ohair@286 195 }
ohair@286 196
ohair@286 197
ohair@286 198 private abstract class ImmutableLM {
ohair@286 199 public abstract Source getPayload();
ohair@286 200 public abstract Object getPayload(BindingContext context);
ohair@286 201 public abstract Object getPayload(JAXBContext context);
alanb@368 202 public abstract Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding);
ohair@286 203
ohair@286 204 }
ohair@286 205
ohair@286 206 private class DOMLogicalMessageImpl extends SourceLogicalMessageImpl {
ohair@286 207 private DOMSource dom;
ohair@286 208
ohair@286 209 public DOMLogicalMessageImpl(DOMSource dom) {
ohair@286 210 super(dom);
ohair@286 211 this.dom = dom;
ohair@286 212 }
ohair@286 213
ohair@286 214 @Override
ohair@286 215 public Source getPayload() {
ohair@286 216 return dom;
ohair@286 217 }
ohair@286 218
alanb@368 219 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
ohair@286 220 Node n = dom.getNode();
ohair@286 221 if(n.getNodeType()== Node.DOCUMENT_NODE) {
ohair@286 222 n = ((Document)n).getDocumentElement();
ohair@286 223 }
alanb@368 224 return new DOMMessage(binding.getSOAPVersion(), headers, (Element)n, attachments);
ohair@286 225 }
ohair@286 226 }
ohair@286 227
ohair@286 228 private class EmptyLogicalMessageImpl extends ImmutableLM {
ohair@286 229 public EmptyLogicalMessageImpl() {
ohair@286 230
ohair@286 231 }
ohair@286 232
ohair@286 233 @Override
ohair@286 234 public Source getPayload() {
ohair@286 235 return null;
ohair@286 236 }
ohair@286 237
ohair@286 238 @Override
ohair@286 239 public Object getPayload(JAXBContext context) {
ohair@286 240 return null;
ohair@286 241 }
ohair@286 242
ohair@286 243 @Override
ohair@286 244 public Object getPayload(BindingContext context) {
ohair@286 245 return null;
ohair@286 246 }
ohair@286 247
alanb@368 248 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
ohair@286 249 return new EmptyMessageImpl(headers,attachments,binding.getSOAPVersion());
ohair@286 250 }
ohair@286 251 }
ohair@286 252
ohair@286 253 private class JAXBLogicalMessageImpl extends ImmutableLM {
ohair@286 254 private JAXBContext ctxt;
ohair@286 255 private Object o;
ohair@286 256
ohair@286 257 public JAXBLogicalMessageImpl(JAXBContext ctxt, Object o) {
ohair@286 258 this.ctxt = ctxt;
ohair@286 259 this.o = o;
ohair@286 260
ohair@286 261 }
ohair@286 262
ohair@286 263 @Override
ohair@286 264 public Source getPayload() {
ohair@286 265 JAXBContext context = ctxt;
ohair@286 266 if (context == null) {
ohair@286 267 context = defaultJaxbContext.getJAXBContext();
ohair@286 268 }
ohair@286 269 try {
ohair@286 270 return new JAXBSource(context, o);
ohair@286 271 } catch (JAXBException e) {
ohair@286 272 throw new WebServiceException(e);
ohair@286 273 }
ohair@286 274 }
ohair@286 275
ohair@286 276 @Override
ohair@286 277 public Object getPayload(JAXBContext context) {
ohair@286 278 // if(context == ctxt) {
ohair@286 279 // return o;
ohair@286 280 // }
ohair@286 281 try {
ohair@286 282 Source payloadSrc = getPayload();
ohair@286 283 if (payloadSrc == null)
ohair@286 284 return null;
ohair@286 285 Unmarshaller unmarshaller = context.createUnmarshaller();
ohair@286 286 return unmarshaller.unmarshal(payloadSrc);
ohair@286 287 } catch (JAXBException e) {
ohair@286 288 throw new WebServiceException(e);
ohair@286 289 }
ohair@286 290 }
ohair@286 291 public Object getPayload(BindingContext context) {
ohair@286 292 // if(context == ctxt) {
ohair@286 293 // return o;
ohair@286 294 // }
ohair@286 295 try {
ohair@286 296 Source payloadSrc = getPayload();
ohair@286 297 if (payloadSrc == null)
ohair@286 298 return null;
ohair@286 299 Unmarshaller unmarshaller = context.createUnmarshaller();
ohair@286 300 return unmarshaller.unmarshal(payloadSrc);
ohair@286 301 } catch (JAXBException e) {
ohair@286 302 throw new WebServiceException(e);
ohair@286 303 }
ohair@286 304 }
ohair@286 305
alanb@368 306 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
ohair@286 307 return JAXBMessage.create(BindingContextFactory.create(ctxt), o,binding.getSOAPVersion(), headers,attachments);
ohair@286 308 }
ohair@286 309 }
ohair@286 310
ohair@286 311 private class SourceLogicalMessageImpl extends ImmutableLM {
ohair@286 312 private Source payloadSrc;
ohair@286 313
ohair@286 314 public SourceLogicalMessageImpl(Source source) {
ohair@286 315 this.payloadSrc = source;
ohair@286 316 }
ohair@286 317
ohair@286 318 public Source getPayload() {
ohair@286 319 assert (!(payloadSrc instanceof DOMSource));
ohair@286 320 try {
ohair@286 321 Transformer transformer = XmlUtil.newTransformer();
ohair@286 322 DOMResult domResult = new DOMResult();
ohair@286 323 transformer.transform(payloadSrc, domResult);
ohair@286 324 DOMSource dom = new DOMSource(domResult.getNode());
ohair@286 325 lm = new DOMLogicalMessageImpl((DOMSource) dom);
ohair@286 326 payloadSrc = null;
ohair@286 327 return dom;
ohair@286 328 } catch (TransformerException te) {
ohair@286 329 throw new WebServiceException(te);
ohair@286 330 }
ohair@286 331 }
ohair@286 332
ohair@286 333 public Object getPayload(JAXBContext context) {
ohair@286 334 try {
ohair@286 335 Source payloadSrc = getPayload();
ohair@286 336 if (payloadSrc == null)
ohair@286 337 return null;
ohair@286 338 Unmarshaller unmarshaller = context.createUnmarshaller();
ohair@286 339 return unmarshaller.unmarshal(payloadSrc);
ohair@286 340 } catch (JAXBException e) {
ohair@286 341 throw new WebServiceException(e);
ohair@286 342 }
ohair@286 343
ohair@286 344 }
ohair@286 345
ohair@286 346 public Object getPayload(BindingContext context) {
ohair@286 347 try {
ohair@286 348 Source payloadSrc = getPayload();
ohair@286 349 if (payloadSrc == null)
ohair@286 350 return null;
ohair@286 351 Unmarshaller unmarshaller = context.createUnmarshaller();
ohair@286 352 return unmarshaller.unmarshal(payloadSrc);
ohair@286 353 } catch (JAXBException e) {
ohair@286 354 throw new WebServiceException(e);
ohair@286 355 }
ohair@286 356
ohair@286 357 }
ohair@286 358
alanb@368 359 public Message getMessage(MessageHeaders headers, AttachmentSet attachments, WSBinding binding) {
ohair@286 360 assert (payloadSrc!=null);
ohair@286 361 return new PayloadSourceMessage(headers, payloadSrc, attachments,binding.getSOAPVersion());
ohair@286 362 }
ohair@286 363 }
ohair@286 364 }

mercurial