src/share/jaxws_classes/com/sun/xml/internal/ws/api/message/Messages.java

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

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.api.message;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.istack.internal.Nullable;
ohair@286 30 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
ohair@286 31 import com.sun.xml.internal.ws.api.SOAPVersion;
ohair@286 32 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 33 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
ohair@286 34 import com.sun.xml.internal.ws.api.message.saaj.SAAJFactory;
ohair@286 35 import com.sun.xml.internal.ws.api.pipe.Tube;
ohair@286 36 import com.sun.xml.internal.ws.api.pipe.Codecs;
ohair@286 37 import com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
ohair@286 38 import com.sun.xml.internal.ws.message.AttachmentSetImpl;
ohair@286 39 import com.sun.xml.internal.ws.message.DOMMessage;
ohair@286 40 import com.sun.xml.internal.ws.message.EmptyMessageImpl;
ohair@286 41 import com.sun.xml.internal.ws.message.ProblemActionHeader;
ohair@286 42 import com.sun.xml.internal.ws.message.stream.PayloadStreamReaderMessage;
ohair@286 43 import com.sun.xml.internal.ws.message.jaxb.JAXBMessage;
ohair@286 44 import com.sun.xml.internal.ws.message.source.PayloadSourceMessage;
ohair@286 45 import com.sun.xml.internal.ws.message.source.ProtocolSourceMessage;
ohair@286 46 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
ohair@286 47 import com.sun.xml.internal.ws.streaming.XMLStreamReaderException;
ohair@286 48 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
ohair@286 49 import com.sun.xml.internal.ws.util.DOMUtil;
ohair@286 50 import com.sun.xml.internal.ws.addressing.WsaTubeHelper;
ohair@286 51 import com.sun.xml.internal.ws.addressing.model.MissingAddressingHeaderException;
ohair@286 52 import com.sun.xml.internal.ws.resources.AddressingMessages;
ohair@286 53 import org.w3c.dom.Element;
ohair@286 54 import org.w3c.dom.Node;
ohair@286 55
ohair@286 56 import javax.xml.bind.JAXBContext;
ohair@286 57 import javax.xml.bind.JAXBElement;
ohair@286 58 import javax.xml.bind.Marshaller;
ohair@286 59 import javax.xml.bind.annotation.XmlRootElement;
ohair@286 60 import javax.xml.namespace.QName;
ohair@286 61 import javax.xml.soap.*;
ohair@286 62 import javax.xml.stream.XMLStreamConstants;
ohair@286 63 import javax.xml.stream.XMLStreamException;
ohair@286 64 import javax.xml.stream.XMLStreamReader;
ohair@286 65 import javax.xml.transform.Source;
ohair@286 66 import javax.xml.transform.sax.SAXSource;
ohair@286 67 import javax.xml.transform.stream.StreamSource;
ohair@286 68 import javax.xml.transform.dom.DOMSource;
ohair@286 69 import javax.xml.ws.ProtocolException;
ohair@286 70 import javax.xml.ws.WebServiceException;
ohair@286 71
ohair@286 72 /**
ohair@286 73 * Factory methods for various {@link Message} implementations.
ohair@286 74 *
ohair@286 75 * <p>
ohair@286 76 * This class provides various methods to create different
ohair@286 77 * flavors of {@link Message} classes that store data
ohair@286 78 * in different formats.
ohair@286 79 *
ohair@286 80 * <p>
ohair@286 81 * This is a part of the JAX-WS RI internal API so that
ohair@286 82 * {@link Tube} implementations can reuse the implementations
ohair@286 83 * done inside the JAX-WS.
ohair@286 84 *
ohair@286 85 * <p>
ohair@286 86 * If you find some of the useful convenience methods missing
ohair@286 87 * from this class, please talk to us.
ohair@286 88 *
ohair@286 89 *
ohair@286 90 * @author Kohsuke Kawaguchi
ohair@286 91 */
ohair@286 92 public abstract class Messages {
ohair@286 93 private Messages() {}
ohair@286 94
ohair@286 95 /**
ohair@286 96 * Creates a {@link Message} backed by a JAXB bean.
ohair@286 97 * @deprecated
ohair@286 98 * @param context
ohair@286 99 * The context to be used to produce infoset from the object. Must not be null.
ohair@286 100 * @param jaxbObject
ohair@286 101 * The JAXB object that represents the payload. must not be null. This object
ohair@286 102 * must be bound to an element (which means it either is a {@link JAXBElement} or
ohair@286 103 * an instanceof a class with {@link XmlRootElement}).
ohair@286 104 * @param soapVersion
ohair@286 105 * The SOAP version of the message. Must not be null.
ohair@286 106 */
ohair@286 107 public static Message create(JAXBContext context, Object jaxbObject, SOAPVersion soapVersion) {
ohair@286 108 return JAXBMessage.create(context,jaxbObject,soapVersion);
ohair@286 109 }
ohair@286 110
ohair@286 111 /**
ohair@286 112 * @deprecated
ohair@286 113 * For use when creating a Dispatch object with an unknown JAXB implementation
ohair@286 114 * for he JAXBContext parameter.
ohair@286 115 *
ohair@286 116 */
ohair@286 117 public static Message createRaw(JAXBContext context, Object jaxbObject, SOAPVersion soapVersion) {
ohair@286 118 return JAXBMessage.createRaw(context,jaxbObject,soapVersion);
ohair@286 119 }
ohair@286 120
ohair@286 121 /**
ohair@286 122 * @deprecated
ohair@286 123 * Use {@link #create(JAXBRIContext, Object, SOAPVersion)}
ohair@286 124 */
ohair@286 125 public static Message create(Marshaller marshaller, Object jaxbObject, SOAPVersion soapVersion) {
ohair@286 126 return create(BindingContextFactory.getBindingContext(marshaller).getJAXBContext(),jaxbObject,soapVersion);
ohair@286 127 }
ohair@286 128
ohair@286 129 /**
ohair@286 130 * Creates a {@link Message} backed by a SAAJ {@link SOAPMessage} object.
ohair@286 131 *
ohair@286 132 * <p>
ohair@286 133 * If the {@link SOAPMessage} contains headers and attachments, this method
ohair@286 134 * does the right thing.
ohair@286 135 *
ohair@286 136 * @param saaj
ohair@286 137 * The SOAP message to be represented as a {@link Message}.
ohair@286 138 * Must not be null. Once this method is invoked, the created
ohair@286 139 * {@link Message} will own the {@link SOAPMessage}, so it shall
ohair@286 140 * never be touched directly.
ohair@286 141 */
ohair@286 142 public static Message create(SOAPMessage saaj) {
ohair@286 143 return SAAJFactory.create(saaj);
ohair@286 144 }
ohair@286 145
ohair@286 146 /**
ohair@286 147 * Creates a {@link Message} using {@link Source} as payload.
ohair@286 148 *
ohair@286 149 * @param payload
ohair@286 150 * Source payload is {@link Message}'s payload
ohair@286 151 * Must not be null. Once this method is invoked, the created
ohair@286 152 * {@link Message} will own the {@link Source}, so it shall
ohair@286 153 * never be touched directly.
ohair@286 154 *
ohair@286 155 * @param ver
ohair@286 156 * The SOAP version of the message. Must not be null.
ohair@286 157 */
ohair@286 158 public static Message createUsingPayload(Source payload, SOAPVersion ver) {
ohair@286 159 if (payload instanceof DOMSource) {
ohair@286 160 if (((DOMSource)payload).getNode() == null) {
ohair@286 161 return new EmptyMessageImpl(ver);
ohair@286 162 }
ohair@286 163 } else if (payload instanceof StreamSource) {
ohair@286 164 StreamSource ss = (StreamSource)payload;
ohair@286 165 if (ss.getInputStream() == null && ss.getReader() == null && ss.getSystemId() == null) {
ohair@286 166 return new EmptyMessageImpl(ver);
ohair@286 167 }
ohair@286 168 } else if (payload instanceof SAXSource) {
ohair@286 169 SAXSource ss = (SAXSource)payload;
ohair@286 170 if (ss.getInputSource() == null && ss.getXMLReader() == null) {
ohair@286 171 return new EmptyMessageImpl(ver);
ohair@286 172 }
ohair@286 173 }
ohair@286 174 return new PayloadSourceMessage(payload, ver);
ohair@286 175 }
ohair@286 176
ohair@286 177 /**
ohair@286 178 * Creates a {@link Message} using {@link XMLStreamReader} as payload.
ohair@286 179 *
ohair@286 180 * @param payload
ohair@286 181 * XMLStreamReader payload is {@link Message}'s payload
ohair@286 182 * Must not be null. Once this method is invoked, the created
ohair@286 183 * {@link Message} will own the {@link XMLStreamReader}, so it shall
ohair@286 184 * never be touched directly.
ohair@286 185 *
ohair@286 186 * @param ver
ohair@286 187 * The SOAP version of the message. Must not be null.
ohair@286 188 */
ohair@286 189 public static Message createUsingPayload(XMLStreamReader payload, SOAPVersion ver) {
ohair@286 190 return new PayloadStreamReaderMessage(payload, ver);
ohair@286 191 }
ohair@286 192
ohair@286 193 /**
ohair@286 194 * Creates a {@link Message} from an {@link Element} that represents
ohair@286 195 * a payload.
ohair@286 196 *
ohair@286 197 * @param payload
ohair@286 198 * The element that becomes the child element of the SOAP body.
ohair@286 199 * Must not be null.
ohair@286 200 *
ohair@286 201 * @param ver
ohair@286 202 * The SOAP version of the message. Must not be null.
ohair@286 203 */
ohair@286 204 public static Message createUsingPayload(Element payload, SOAPVersion ver) {
ohair@286 205 return new DOMMessage(ver,payload);
ohair@286 206 }
ohair@286 207
ohair@286 208 /**
ohair@286 209 * Creates a {@link Message} from an {@link Element} that represents
ohair@286 210 * the whole SOAP message.
ohair@286 211 *
ohair@286 212 * @param soapEnvelope
ohair@286 213 * The SOAP envelope element.
ohair@286 214 */
ohair@286 215 public static Message create(Element soapEnvelope) {
ohair@286 216 SOAPVersion ver = SOAPVersion.fromNsUri(soapEnvelope.getNamespaceURI());
ohair@286 217 // find the headers
ohair@286 218 Element header = DOMUtil.getFirstChild(soapEnvelope, ver.nsUri, "Header");
ohair@286 219 HeaderList headers = null;
ohair@286 220 if(header!=null) {
ohair@286 221 for( Node n=header.getFirstChild(); n!=null; n=n.getNextSibling() ) {
ohair@286 222 if(n.getNodeType()==Node.ELEMENT_NODE) {
ohair@286 223 if(headers==null)
alanb@368 224 headers = new HeaderList(ver);
ohair@286 225 headers.add(Headers.create((Element)n));
ohair@286 226 }
ohair@286 227 }
ohair@286 228 }
ohair@286 229
ohair@286 230 // find the payload
ohair@286 231 Element body = DOMUtil.getFirstChild(soapEnvelope, ver.nsUri, "Body");
ohair@286 232 if(body==null)
ohair@286 233 throw new WebServiceException("Message doesn't have <S:Body> "+soapEnvelope);
ohair@286 234 Element payload = DOMUtil.getFirstChild(soapEnvelope, ver.nsUri, "Body");
ohair@286 235
ohair@286 236 if(payload==null) {
ohair@286 237 return new EmptyMessageImpl(headers, new AttachmentSetImpl(), ver);
ohair@286 238 } else {
ohair@286 239 return new DOMMessage(ver,headers,payload);
ohair@286 240 }
ohair@286 241 }
ohair@286 242
ohair@286 243 /**
ohair@286 244 * Creates a {@link Message} using Source as entire envelope.
ohair@286 245 *
ohair@286 246 * @param envelope
ohair@286 247 * Source envelope is used to create {@link Message}
ohair@286 248 * Must not be null. Once this method is invoked, the created
ohair@286 249 * {@link Message} will own the {@link Source}, so it shall
ohair@286 250 * never be touched directly.
ohair@286 251 *
ohair@286 252 */
ohair@286 253 public static Message create(Source envelope, SOAPVersion soapVersion) {
ohair@286 254 return new ProtocolSourceMessage(envelope, soapVersion);
ohair@286 255 }
ohair@286 256
ohair@286 257
ohair@286 258 /**
ohair@286 259 * Creates a {@link Message} that doesn't have any payload.
ohair@286 260 */
ohair@286 261 public static Message createEmpty(SOAPVersion soapVersion) {
ohair@286 262 return new EmptyMessageImpl(soapVersion);
ohair@286 263 }
ohair@286 264
ohair@286 265 /**
ohair@286 266 * Creates a {@link Message} from {@link XMLStreamReader} that points to
ohair@286 267 * the start of the envelope.
ohair@286 268 *
ohair@286 269 * @param reader
ohair@286 270 * can point to the start document or the start element (of &lt;s:Envelope>)
ohair@286 271 */
ohair@286 272 public static @NotNull Message create(@NotNull XMLStreamReader reader) {
ohair@286 273 // skip until the root element
ohair@286 274 if(reader.getEventType()!=XMLStreamConstants.START_ELEMENT)
ohair@286 275 XMLStreamReaderUtil.nextElementContent(reader);
ohair@286 276 assert reader.getEventType()== XMLStreamConstants.START_ELEMENT :reader.getEventType();
ohair@286 277
ohair@286 278 SOAPVersion ver = SOAPVersion.fromNsUri(reader.getNamespaceURI());
ohair@286 279
ohair@286 280 return Codecs.createSOAPEnvelopeXmlCodec(ver).decode(reader);
ohair@286 281 }
ohair@286 282
ohair@286 283 /**
ohair@286 284 * Creates a {@link Message} from {@link XMLStreamBuffer} that retains the
ohair@286 285 * whole envelope infoset.
ohair@286 286 *
ohair@286 287 * @param xsb
ohair@286 288 * This buffer must contain the infoset of the whole envelope.
ohair@286 289 */
ohair@286 290 public static @NotNull Message create(@NotNull XMLStreamBuffer xsb) {
ohair@286 291 // TODO: we should be able to let Messae know that it's working off from a buffer,
ohair@286 292 // to make some of the operations more efficient.
ohair@286 293 // meanwhile, adding this as an API so that our users can take advantage of it
ohair@286 294 // when we get around to such an implementation later.
ohair@286 295 try {
ohair@286 296 return create(xsb.readAsXMLStreamReader());
ohair@286 297 } catch (XMLStreamException e) {
ohair@286 298 throw new XMLStreamReaderException(e);
ohair@286 299 }
ohair@286 300 }
ohair@286 301
ohair@286 302 /**
ohair@286 303 * Creates a {@link Message} that represents an exception as a fault. The
ohair@286 304 * created message reflects if t or t.getCause() is SOAPFaultException.
ohair@286 305 *
ohair@286 306 * creates a fault message with default faultCode env:Server if t or t.getCause()
ohair@286 307 * is not SOAPFaultException. Otherwise, it use SOAPFaultException's faultCode
ohair@286 308 *
ohair@286 309 * @return
ohair@286 310 * Always non-null. A message that wraps this {@link Throwable}.
ohair@286 311 *
ohair@286 312 */
ohair@286 313 public static Message create(Throwable t, SOAPVersion soapVersion) {
ohair@286 314 return SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, null, t);
ohair@286 315 }
ohair@286 316
ohair@286 317 /**
ohair@286 318 * Creates a fault {@link Message}.
ohair@286 319 *
ohair@286 320 * <p>
ohair@286 321 * This method is not designed for efficiency, and we don't expect
ohair@286 322 * to be used for the performance critical codepath.
ohair@286 323 *
ohair@286 324 * @param fault
ohair@286 325 * The populated SAAJ data structure that represents a fault
ohair@286 326 * in detail.
ohair@286 327 *
ohair@286 328 * @return
ohair@286 329 * Always non-null. A message that wraps this {@link SOAPFault}.
ohair@286 330 */
ohair@286 331 public static Message create(SOAPFault fault) {
ohair@286 332 SOAPVersion ver = SOAPVersion.fromNsUri(fault.getNamespaceURI());
ohair@286 333 return new DOMMessage(ver,fault);
ohair@286 334 }
ohair@286 335
ohair@286 336 /**
ohair@286 337 * @deprecated
ohair@286 338 * Use {@link #createAddressingFaultMessage(WSBinding, Packet, QName)}
ohair@286 339 */
ohair@286 340 public static Message createAddressingFaultMessage(WSBinding binding, QName missingHeader) {
ohair@286 341 return createAddressingFaultMessage(binding,null,missingHeader);
ohair@286 342 }
ohair@286 343
ohair@286 344 /**
ohair@286 345 * Creates a fault {@link Message} that captures the code/subcode/subsubcode
ohair@286 346 * defined by WS-Addressing if one of the expected WS-Addressing headers is
ohair@286 347 * missing in the message
ohair@286 348 *
ohair@286 349 * @param binding WSBinding
ohair@286 350 * @param p
ohair@286 351 * {@link Packet} that was missing a WS-Addressing header.
ohair@286 352 * @param missingHeader The missing WS-Addressing Header
ohair@286 353 * @return
ohair@286 354 * A message representing SOAPFault that contains the WS-Addressing code/subcode/subsubcode.
ohair@286 355 */
ohair@286 356 public static Message createAddressingFaultMessage(WSBinding binding, Packet p, QName missingHeader) {
ohair@286 357 AddressingVersion av = binding.getAddressingVersion();
ohair@286 358 if(av == null) {
ohair@286 359 // Addressing is not enabled.
ohair@286 360 throw new WebServiceException(AddressingMessages.ADDRESSING_SHOULD_BE_ENABLED());
ohair@286 361 }
ohair@286 362 WsaTubeHelper helper = av.getWsaHelper(null,null,binding);
ohair@286 363 return create(helper.newMapRequiredFault(new MissingAddressingHeaderException(missingHeader,p)));
ohair@286 364 }
ohair@286 365 /**
ohair@286 366 * Creates a fault {@link Message} that captures the code/subcode/subsubcode
ohair@286 367 * defined by WS-Addressing if wsa:Action is not supported.
ohair@286 368 *
ohair@286 369 * @param unsupportedAction The unsupported Action. Must not be null.
ohair@286 370 * @param av The WS-Addressing version of the message. Must not be null.
ohair@286 371 * @param sv The SOAP Version of the message. Must not be null.
ohair@286 372 *
ohair@286 373 * @return
ohair@286 374 * A message representing SOAPFault that contains the WS-Addressing code/subcode/subsubcode.
ohair@286 375 */
ohair@286 376 public static Message create(@NotNull String unsupportedAction, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
ohair@286 377 QName subcode = av.actionNotSupportedTag;
ohair@286 378 String faultstring = String.format(av.actionNotSupportedText, unsupportedAction);
ohair@286 379
ohair@286 380 Message faultMessage;
ohair@286 381 SOAPFault fault;
ohair@286 382 try {
ohair@286 383 if (sv == SOAPVersion.SOAP_12) {
ohair@286 384 fault = SOAPVersion.SOAP_12.getSOAPFactory().createFault();
ohair@286 385 fault.setFaultCode(SOAPConstants.SOAP_SENDER_FAULT);
ohair@286 386 fault.appendFaultSubcode(subcode);
ohair@286 387 Detail detail = fault.addDetail();
ohair@286 388 SOAPElement se = detail.addChildElement(av.problemActionTag);
ohair@286 389 se = se.addChildElement(av.actionTag);
ohair@286 390 se.addTextNode(unsupportedAction);
ohair@286 391 } else {
ohair@286 392 fault = SOAPVersion.SOAP_11.getSOAPFactory().createFault();
ohair@286 393 fault.setFaultCode(subcode);
ohair@286 394 }
ohair@286 395 fault.setFaultString(faultstring);
ohair@286 396
ohair@286 397 faultMessage = SOAPFaultBuilder.createSOAPFaultMessage(sv, fault);
ohair@286 398 if (sv == SOAPVersion.SOAP_11) {
ohair@286 399 faultMessage.getHeaders().add(new ProblemActionHeader(unsupportedAction, av));
ohair@286 400 }
ohair@286 401 } catch (SOAPException e) {
ohair@286 402 throw new WebServiceException(e);
ohair@286 403 }
ohair@286 404
ohair@286 405 return faultMessage;
ohair@286 406 }
ohair@286 407
ohair@286 408 /**
ohair@286 409 * To be called to convert a {@link ProtocolException} and faultcode for a given {@link SOAPVersion} in to a {@link Message}.
ohair@286 410 *
ohair@286 411 * @param soapVersion {@link SOAPVersion#SOAP_11} or {@link SOAPVersion#SOAP_12}
ohair@286 412 * @param pex a ProtocolException
ohair@286 413 * @param faultcode soap faultcode. Its ignored if the {@link ProtocolException} instance is {@link javax.xml.ws.soap.SOAPFaultException} and it has a
ohair@286 414 * faultcode present in the underlying {@link SOAPFault}.
ohair@286 415 * @return {@link Message} representing SOAP fault
ohair@286 416 */
ohair@286 417 public static @NotNull Message create(@NotNull SOAPVersion soapVersion, @NotNull ProtocolException pex, @Nullable QName faultcode){
ohair@286 418 return SOAPFaultBuilder.createSOAPFaultMessage(soapVersion, pex, faultcode);
ohair@286 419 }
ohair@286 420 }

mercurial