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