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