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.message.jaxb; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.istack.internal.FragmentContentHandler; |
aoqi@0 | 29 | import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; |
aoqi@0 | 30 | import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; |
aoqi@0 | 31 | import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult; |
aoqi@0 | 32 | import com.sun.xml.internal.ws.api.SOAPVersion; |
aoqi@0 | 33 | import com.sun.xml.internal.ws.api.message.AttachmentSet; |
aoqi@0 | 34 | import com.sun.xml.internal.ws.api.message.Header; |
aoqi@0 | 35 | import com.sun.xml.internal.ws.api.message.HeaderList; |
aoqi@0 | 36 | import com.sun.xml.internal.ws.api.message.Message; |
aoqi@0 | 37 | import com.sun.xml.internal.ws.api.message.MessageHeaders; |
aoqi@0 | 38 | import com.sun.xml.internal.ws.api.message.StreamingSOAP; |
aoqi@0 | 39 | import com.sun.xml.internal.ws.encoding.SOAPBindingCodec; |
aoqi@0 | 40 | import com.sun.xml.internal.ws.message.AbstractMessageImpl; |
aoqi@0 | 41 | import com.sun.xml.internal.ws.message.AttachmentSetImpl; |
aoqi@0 | 42 | import com.sun.xml.internal.ws.message.RootElementSniffer; |
aoqi@0 | 43 | import com.sun.xml.internal.ws.message.stream.StreamMessage; |
aoqi@0 | 44 | import com.sun.xml.internal.ws.spi.db.BindingContext; |
aoqi@0 | 45 | import com.sun.xml.internal.ws.spi.db.BindingContextFactory; |
aoqi@0 | 46 | import com.sun.xml.internal.ws.spi.db.XMLBridge; |
aoqi@0 | 47 | import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil; |
aoqi@0 | 48 | import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil; |
aoqi@0 | 49 | import com.sun.xml.internal.ws.streaming.MtomStreamWriter; |
aoqi@0 | 50 | import com.sun.xml.internal.ws.util.xml.XMLReaderComposite; |
aoqi@0 | 51 | import com.sun.xml.internal.ws.util.xml.XMLReaderComposite.ElemInfo; |
aoqi@0 | 52 | |
aoqi@0 | 53 | import org.xml.sax.ContentHandler; |
aoqi@0 | 54 | import org.xml.sax.ErrorHandler; |
aoqi@0 | 55 | import org.xml.sax.SAXException; |
aoqi@0 | 56 | |
aoqi@0 | 57 | import javax.xml.bind.JAXBContext; |
aoqi@0 | 58 | import javax.xml.bind.JAXBElement; |
aoqi@0 | 59 | import javax.xml.bind.JAXBException; |
aoqi@0 | 60 | import javax.xml.bind.Marshaller; |
aoqi@0 | 61 | import javax.xml.bind.Unmarshaller; |
aoqi@0 | 62 | import javax.xml.bind.attachment.AttachmentMarshaller; |
aoqi@0 | 63 | import javax.xml.bind.annotation.XmlRootElement; |
aoqi@0 | 64 | import javax.xml.bind.util.JAXBResult; |
aoqi@0 | 65 | import javax.xml.namespace.QName; |
aoqi@0 | 66 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 67 | import javax.xml.stream.XMLStreamReader; |
aoqi@0 | 68 | import javax.xml.stream.XMLStreamWriter; |
aoqi@0 | 69 | import static javax.xml.stream.XMLStreamConstants.START_DOCUMENT; |
aoqi@0 | 70 | import javax.xml.transform.Source; |
aoqi@0 | 71 | import javax.xml.ws.WebServiceException; |
aoqi@0 | 72 | import java.io.OutputStream; |
aoqi@0 | 73 | import java.util.List; |
aoqi@0 | 74 | |
aoqi@0 | 75 | /** |
aoqi@0 | 76 | * {@link Message} backed by a JAXB bean. |
aoqi@0 | 77 | * |
aoqi@0 | 78 | * @author Kohsuke Kawaguchi |
aoqi@0 | 79 | */ |
aoqi@0 | 80 | public final class JAXBMessage extends AbstractMessageImpl implements StreamingSOAP { |
aoqi@0 | 81 | private MessageHeaders headers; |
aoqi@0 | 82 | |
aoqi@0 | 83 | /** |
aoqi@0 | 84 | * The JAXB object that represents the payload. |
aoqi@0 | 85 | */ |
aoqi@0 | 86 | private final Object jaxbObject; |
aoqi@0 | 87 | |
aoqi@0 | 88 | private final XMLBridge bridge; |
aoqi@0 | 89 | |
aoqi@0 | 90 | /** |
aoqi@0 | 91 | * For the use case of a user-supplied JAXB context that is not |
aoqi@0 | 92 | * a known JAXB type, as when creating a Disaptch object with a |
aoqi@0 | 93 | * JAXB object parameter, we will marshal and unmarshal directly with |
aoqi@0 | 94 | * the context object, as there is no Bond available. In this case, |
aoqi@0 | 95 | * swaRef is not supported. |
aoqi@0 | 96 | */ |
aoqi@0 | 97 | private final JAXBContext rawContext; |
aoqi@0 | 98 | |
aoqi@0 | 99 | /** |
aoqi@0 | 100 | * Lazily sniffed payload element name |
aoqi@0 | 101 | */ |
aoqi@0 | 102 | private String nsUri,localName; |
aoqi@0 | 103 | |
aoqi@0 | 104 | /** |
aoqi@0 | 105 | * If we have the infoset representation for the payload, this field is non-null. |
aoqi@0 | 106 | */ |
aoqi@0 | 107 | private XMLStreamBuffer infoset; |
aoqi@0 | 108 | |
aoqi@0 | 109 | public static Message create(BindingContext context, Object jaxbObject, SOAPVersion soapVersion, MessageHeaders headers, AttachmentSet attachments) { |
aoqi@0 | 110 | if(!context.hasSwaRef()) { |
aoqi@0 | 111 | return new JAXBMessage(context,jaxbObject,soapVersion,headers,attachments); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | // If we have swaRef, then that means we might have attachments. |
aoqi@0 | 115 | // to comply with the packet API, we need to eagerly turn the JAXB object into infoset |
aoqi@0 | 116 | // to correctly find out about attachments. |
aoqi@0 | 117 | |
aoqi@0 | 118 | try { |
aoqi@0 | 119 | MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer(); |
aoqi@0 | 120 | |
aoqi@0 | 121 | Marshaller m = context.createMarshaller(); |
aoqi@0 | 122 | AttachmentMarshallerImpl am = new AttachmentMarshallerImpl(attachments); |
aoqi@0 | 123 | m.setAttachmentMarshaller(am); |
aoqi@0 | 124 | am.cleanup(); |
aoqi@0 | 125 | m.marshal(jaxbObject,xsb.createFromXMLStreamWriter()); |
aoqi@0 | 126 | |
aoqi@0 | 127 | // any way to reuse this XMLStreamBuffer in StreamMessage? |
aoqi@0 | 128 | return new StreamMessage(headers,attachments,xsb.readAsXMLStreamReader(),soapVersion); |
aoqi@0 | 129 | } catch (JAXBException e) { |
aoqi@0 | 130 | throw new WebServiceException(e); |
aoqi@0 | 131 | } catch (XMLStreamException e) { |
aoqi@0 | 132 | throw new WebServiceException(e); |
aoqi@0 | 133 | } |
aoqi@0 | 134 | } |
aoqi@0 | 135 | /** |
aoqi@0 | 136 | * Creates a {@link Message} backed by a JAXB bean. |
aoqi@0 | 137 | * |
aoqi@0 | 138 | * @param context |
aoqi@0 | 139 | * The JAXBContext to be used for marshalling. |
aoqi@0 | 140 | * @param jaxbObject |
aoqi@0 | 141 | * The JAXB object that represents the payload. must not be null. This object |
aoqi@0 | 142 | * must be bound to an element (which means it either is a {@link JAXBElement} or |
aoqi@0 | 143 | * an instanceof a class with {@link XmlRootElement}). |
aoqi@0 | 144 | * @param soapVersion |
aoqi@0 | 145 | * The SOAP version of the message. Must not be null. |
aoqi@0 | 146 | */ |
aoqi@0 | 147 | public static Message create(BindingContext context, Object jaxbObject, SOAPVersion soapVersion) { |
aoqi@0 | 148 | return create(context,jaxbObject,soapVersion,null,null); |
aoqi@0 | 149 | } |
aoqi@0 | 150 | /** @deprecated */ |
aoqi@0 | 151 | public static Message create(JAXBContext context, Object jaxbObject, SOAPVersion soapVersion) { |
aoqi@0 | 152 | return create(BindingContextFactory.create(context),jaxbObject,soapVersion,null,null); |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | /** |
aoqi@0 | 156 | * @deprecated |
aoqi@0 | 157 | * For use when creating a Dispatch object with an unknown JAXB implementation |
aoqi@0 | 158 | * for he JAXBContext parameter. |
aoqi@0 | 159 | * |
aoqi@0 | 160 | */ |
aoqi@0 | 161 | public static Message createRaw(JAXBContext context, Object jaxbObject, SOAPVersion soapVersion) { |
aoqi@0 | 162 | return new JAXBMessage(context,jaxbObject,soapVersion,null,null); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | |
aoqi@0 | 165 | private JAXBMessage( BindingContext context, Object jaxbObject, SOAPVersion soapVer, MessageHeaders headers, AttachmentSet attachments ) { |
aoqi@0 | 166 | super(soapVer); |
aoqi@0 | 167 | // this.bridge = new MarshallerBridge(context); |
aoqi@0 | 168 | this.bridge = context.createFragmentBridge(); |
aoqi@0 | 169 | this.rawContext = null; |
aoqi@0 | 170 | this.jaxbObject = jaxbObject; |
aoqi@0 | 171 | this.headers = headers; |
aoqi@0 | 172 | this.attachmentSet = attachments; |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | private JAXBMessage( JAXBContext rawContext, Object jaxbObject, SOAPVersion soapVer, MessageHeaders headers, AttachmentSet attachments ) { |
aoqi@0 | 176 | super(soapVer); |
aoqi@0 | 177 | // this.bridge = new MarshallerBridge(context); |
aoqi@0 | 178 | this.rawContext = rawContext; |
aoqi@0 | 179 | this.bridge = null; |
aoqi@0 | 180 | this.jaxbObject = jaxbObject; |
aoqi@0 | 181 | this.headers = headers; |
aoqi@0 | 182 | this.attachmentSet = attachments; |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | /** |
aoqi@0 | 186 | * Creates a {@link Message} backed by a JAXB bean. |
aoqi@0 | 187 | * |
aoqi@0 | 188 | * @param bridge |
aoqi@0 | 189 | * Specify the payload tag name and how <tt>jaxbObject</tt> is bound. |
aoqi@0 | 190 | * @param jaxbObject |
aoqi@0 | 191 | */ |
aoqi@0 | 192 | public static Message create(XMLBridge bridge, Object jaxbObject, SOAPVersion soapVer) { |
aoqi@0 | 193 | if(!bridge.context().hasSwaRef()) { |
aoqi@0 | 194 | return new JAXBMessage(bridge,jaxbObject,soapVer); |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | // If we have swaRef, then that means we might have attachments. |
aoqi@0 | 198 | // to comply with the packet API, we need to eagerly turn the JAXB object into infoset |
aoqi@0 | 199 | // to correctly find out about attachments. |
aoqi@0 | 200 | |
aoqi@0 | 201 | try { |
aoqi@0 | 202 | MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer(); |
aoqi@0 | 203 | |
aoqi@0 | 204 | AttachmentSetImpl attachments = new AttachmentSetImpl(); |
aoqi@0 | 205 | AttachmentMarshallerImpl am = new AttachmentMarshallerImpl(attachments); |
aoqi@0 | 206 | bridge.marshal(jaxbObject,xsb.createFromXMLStreamWriter(), am); |
aoqi@0 | 207 | am.cleanup(); |
aoqi@0 | 208 | |
aoqi@0 | 209 | // any way to reuse this XMLStreamBuffer in StreamMessage? |
aoqi@0 | 210 | return new StreamMessage(null,attachments,xsb.readAsXMLStreamReader(),soapVer); |
aoqi@0 | 211 | } catch (JAXBException e) { |
aoqi@0 | 212 | throw new WebServiceException(e); |
aoqi@0 | 213 | } catch (XMLStreamException e) { |
aoqi@0 | 214 | throw new WebServiceException(e); |
aoqi@0 | 215 | } |
aoqi@0 | 216 | } |
aoqi@0 | 217 | |
aoqi@0 | 218 | private JAXBMessage(XMLBridge bridge, Object jaxbObject, SOAPVersion soapVer) { |
aoqi@0 | 219 | super(soapVer); |
aoqi@0 | 220 | // TODO: think about a better way to handle BridgeContext |
aoqi@0 | 221 | this.bridge = bridge; |
aoqi@0 | 222 | this.rawContext = null; |
aoqi@0 | 223 | this.jaxbObject = jaxbObject; |
aoqi@0 | 224 | QName tagName = bridge.getTypeInfo().tagName; |
aoqi@0 | 225 | this.nsUri = tagName.getNamespaceURI(); |
aoqi@0 | 226 | this.localName = tagName.getLocalPart(); |
aoqi@0 | 227 | this.attachmentSet = new AttachmentSetImpl(); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | |
aoqi@0 | 230 | /** |
aoqi@0 | 231 | * Copy constructor. |
aoqi@0 | 232 | */ |
aoqi@0 | 233 | public JAXBMessage(JAXBMessage that) { |
aoqi@0 | 234 | super(that); |
aoqi@0 | 235 | this.headers = that.headers; |
aoqi@0 | 236 | if(this.headers!=null) |
aoqi@0 | 237 | this.headers = new HeaderList(this.headers); |
aoqi@0 | 238 | this.attachmentSet = that.attachmentSet; |
aoqi@0 | 239 | |
aoqi@0 | 240 | this.jaxbObject = that.jaxbObject; |
aoqi@0 | 241 | this.bridge = that.bridge; |
aoqi@0 | 242 | this.rawContext = that.rawContext; |
aoqi@0 | 243 | } |
aoqi@0 | 244 | |
aoqi@0 | 245 | @Override |
aoqi@0 | 246 | public boolean hasHeaders() { |
aoqi@0 | 247 | return headers!=null && headers.hasHeaders(); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | @Override |
aoqi@0 | 251 | public MessageHeaders getHeaders() { |
aoqi@0 | 252 | if(headers==null) |
aoqi@0 | 253 | headers = new HeaderList(getSOAPVersion()); |
aoqi@0 | 254 | return headers; |
aoqi@0 | 255 | } |
aoqi@0 | 256 | |
aoqi@0 | 257 | @Override |
aoqi@0 | 258 | public String getPayloadLocalPart() { |
aoqi@0 | 259 | if(localName==null) |
aoqi@0 | 260 | sniff(); |
aoqi@0 | 261 | return localName; |
aoqi@0 | 262 | } |
aoqi@0 | 263 | |
aoqi@0 | 264 | @Override |
aoqi@0 | 265 | public String getPayloadNamespaceURI() { |
aoqi@0 | 266 | if(nsUri==null) |
aoqi@0 | 267 | sniff(); |
aoqi@0 | 268 | return nsUri; |
aoqi@0 | 269 | } |
aoqi@0 | 270 | |
aoqi@0 | 271 | @Override |
aoqi@0 | 272 | public boolean hasPayload() { |
aoqi@0 | 273 | return true; |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | /** |
aoqi@0 | 277 | * Obtains the tag name of the root element. |
aoqi@0 | 278 | */ |
aoqi@0 | 279 | private void sniff() { |
aoqi@0 | 280 | RootElementSniffer sniffer = new RootElementSniffer(false); |
aoqi@0 | 281 | try { |
aoqi@0 | 282 | if (rawContext != null) { |
aoqi@0 | 283 | Marshaller m = rawContext.createMarshaller(); |
aoqi@0 | 284 | m.setProperty("jaxb.fragment", Boolean.TRUE); |
aoqi@0 | 285 | m.marshal(jaxbObject,sniffer); |
aoqi@0 | 286 | } else |
aoqi@0 | 287 | bridge.marshal(jaxbObject,sniffer,null); |
aoqi@0 | 288 | } catch (JAXBException e) { |
aoqi@0 | 289 | // if it's due to us aborting the processing after the first element, |
aoqi@0 | 290 | // we can safely ignore this exception. |
aoqi@0 | 291 | // |
aoqi@0 | 292 | // if it's due to error in the object, the same error will be reported |
aoqi@0 | 293 | // when the readHeader() method is used, so we don't have to report |
aoqi@0 | 294 | // an error right now. |
aoqi@0 | 295 | nsUri = sniffer.getNsUri(); |
aoqi@0 | 296 | localName = sniffer.getLocalName(); |
aoqi@0 | 297 | } |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | @Override |
aoqi@0 | 301 | public Source readPayloadAsSource() { |
aoqi@0 | 302 | return new JAXBBridgeSource(bridge,jaxbObject); |
aoqi@0 | 303 | } |
aoqi@0 | 304 | |
aoqi@0 | 305 | @Override |
aoqi@0 | 306 | public <T> T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException { |
aoqi@0 | 307 | JAXBResult out = new JAXBResult(unmarshaller); |
aoqi@0 | 308 | // since the bridge only produces fragments, we need to fire start/end document. |
aoqi@0 | 309 | try { |
aoqi@0 | 310 | out.getHandler().startDocument(); |
aoqi@0 | 311 | if (rawContext != null) { |
aoqi@0 | 312 | Marshaller m = rawContext.createMarshaller(); |
aoqi@0 | 313 | m.setProperty("jaxb.fragment", Boolean.TRUE); |
aoqi@0 | 314 | m.marshal(jaxbObject,out); |
aoqi@0 | 315 | } else |
aoqi@0 | 316 | bridge.marshal(jaxbObject,out); |
aoqi@0 | 317 | out.getHandler().endDocument(); |
aoqi@0 | 318 | } catch (SAXException e) { |
aoqi@0 | 319 | throw new JAXBException(e); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | return (T)out.getResult(); |
aoqi@0 | 322 | } |
aoqi@0 | 323 | |
aoqi@0 | 324 | @Override |
aoqi@0 | 325 | public XMLStreamReader readPayload() throws XMLStreamException { |
aoqi@0 | 326 | try { |
aoqi@0 | 327 | if(infoset==null) { |
aoqi@0 | 328 | if (rawContext != null) { |
aoqi@0 | 329 | XMLStreamBufferResult sbr = new XMLStreamBufferResult(); |
aoqi@0 | 330 | Marshaller m = rawContext.createMarshaller(); |
aoqi@0 | 331 | m.setProperty("jaxb.fragment", Boolean.TRUE); |
aoqi@0 | 332 | m.marshal(jaxbObject, sbr); |
aoqi@0 | 333 | infoset = sbr.getXMLStreamBuffer(); |
aoqi@0 | 334 | } else { |
aoqi@0 | 335 | MutableXMLStreamBuffer buffer = new MutableXMLStreamBuffer(); |
aoqi@0 | 336 | writePayloadTo(buffer.createFromXMLStreamWriter()); |
aoqi@0 | 337 | infoset = buffer; |
aoqi@0 | 338 | } |
aoqi@0 | 339 | } |
aoqi@0 | 340 | XMLStreamReader reader = infoset.readAsXMLStreamReader(); |
aoqi@0 | 341 | if(reader.getEventType()== START_DOCUMENT) |
aoqi@0 | 342 | XMLStreamReaderUtil.nextElementContent(reader); |
aoqi@0 | 343 | return reader; |
aoqi@0 | 344 | } catch (JAXBException e) { |
aoqi@0 | 345 | // bug 6449684, spec 4.3.4 |
aoqi@0 | 346 | throw new WebServiceException(e); |
aoqi@0 | 347 | } |
aoqi@0 | 348 | } |
aoqi@0 | 349 | |
aoqi@0 | 350 | /** |
aoqi@0 | 351 | * Writes the payload as SAX events. |
aoqi@0 | 352 | */ |
aoqi@0 | 353 | @Override |
aoqi@0 | 354 | protected void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException { |
aoqi@0 | 355 | try { |
aoqi@0 | 356 | if(fragment) |
aoqi@0 | 357 | contentHandler = new FragmentContentHandler(contentHandler); |
aoqi@0 | 358 | AttachmentMarshallerImpl am = new AttachmentMarshallerImpl(attachmentSet); |
aoqi@0 | 359 | if (rawContext != null) { |
aoqi@0 | 360 | Marshaller m = rawContext.createMarshaller(); |
aoqi@0 | 361 | m.setProperty("jaxb.fragment", Boolean.TRUE); |
aoqi@0 | 362 | m.setAttachmentMarshaller(am); |
aoqi@0 | 363 | m.marshal(jaxbObject,contentHandler); |
aoqi@0 | 364 | } else |
aoqi@0 | 365 | bridge.marshal(jaxbObject,contentHandler, am); |
aoqi@0 | 366 | am.cleanup(); |
aoqi@0 | 367 | } catch (JAXBException e) { |
aoqi@0 | 368 | // this is really more helpful but spec compliance |
aoqi@0 | 369 | // errorHandler.fatalError(new SAXParseException(e.getMessage(),NULL_LOCATOR,e)); |
aoqi@0 | 370 | // bug 6449684, spec 4.3.4 |
aoqi@0 | 371 | throw new WebServiceException(e.getMessage(),e); |
aoqi@0 | 372 | } |
aoqi@0 | 373 | } |
aoqi@0 | 374 | |
aoqi@0 | 375 | @Override |
aoqi@0 | 376 | public void writePayloadTo(XMLStreamWriter sw) throws XMLStreamException { |
aoqi@0 | 377 | try { |
aoqi@0 | 378 | // MtomCodec sets its own AttachmentMarshaller |
aoqi@0 | 379 | AttachmentMarshaller am = (sw instanceof MtomStreamWriter) |
aoqi@0 | 380 | ? ((MtomStreamWriter)sw).getAttachmentMarshaller() |
aoqi@0 | 381 | : new AttachmentMarshallerImpl(attachmentSet); |
aoqi@0 | 382 | |
aoqi@0 | 383 | // Get the encoding of the writer |
aoqi@0 | 384 | String encoding = XMLStreamWriterUtil.getEncoding(sw); |
aoqi@0 | 385 | |
aoqi@0 | 386 | // Get output stream and use JAXB UTF-8 writer |
aoqi@0 | 387 | OutputStream os = bridge.supportOutputStream() ? XMLStreamWriterUtil.getOutputStream(sw) : null; |
aoqi@0 | 388 | if (rawContext != null) { |
aoqi@0 | 389 | Marshaller m = rawContext.createMarshaller(); |
aoqi@0 | 390 | m.setProperty("jaxb.fragment", Boolean.TRUE); |
aoqi@0 | 391 | m.setAttachmentMarshaller(am); |
aoqi@0 | 392 | if (os != null) { |
aoqi@0 | 393 | m.marshal(jaxbObject, os); |
aoqi@0 | 394 | } else { |
aoqi@0 | 395 | m.marshal(jaxbObject, sw); |
aoqi@0 | 396 | } |
aoqi@0 | 397 | } else { |
aoqi@0 | 398 | if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) { |
aoqi@0 | 399 | bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), am); |
aoqi@0 | 400 | } else { |
aoqi@0 | 401 | bridge.marshal(jaxbObject, sw, am); |
aoqi@0 | 402 | } |
aoqi@0 | 403 | } |
aoqi@0 | 404 | //cleanup() is not needed since JAXB doesn't keep ref to AttachmentMarshaller |
aoqi@0 | 405 | //am.cleanup(); |
aoqi@0 | 406 | } catch (JAXBException e) { |
aoqi@0 | 407 | // bug 6449684, spec 4.3.4 |
aoqi@0 | 408 | throw new WebServiceException(e); |
aoqi@0 | 409 | } |
aoqi@0 | 410 | } |
aoqi@0 | 411 | |
aoqi@0 | 412 | @Override |
aoqi@0 | 413 | public Message copy() { |
aoqi@0 | 414 | return new JAXBMessage(this); |
aoqi@0 | 415 | } |
aoqi@0 | 416 | |
aoqi@0 | 417 | public XMLStreamReader readEnvelope() { |
aoqi@0 | 418 | int base = soapVersion.ordinal()*3; |
aoqi@0 | 419 | this.envelopeTag = DEFAULT_TAGS.get(base); |
aoqi@0 | 420 | this.bodyTag = DEFAULT_TAGS.get(base+2); |
aoqi@0 | 421 | List<XMLStreamReader> hReaders = new java.util.ArrayList<XMLStreamReader>(); |
aoqi@0 | 422 | ElemInfo envElem = new ElemInfo(envelopeTag, null); |
aoqi@0 | 423 | ElemInfo bdyElem = new ElemInfo(bodyTag, envElem); |
aoqi@0 | 424 | for (Header h : getHeaders().asList()) { |
aoqi@0 | 425 | try { |
aoqi@0 | 426 | hReaders.add(h.readHeader()); |
aoqi@0 | 427 | } catch (XMLStreamException e) { |
aoqi@0 | 428 | throw new RuntimeException(e); |
aoqi@0 | 429 | } |
aoqi@0 | 430 | } |
aoqi@0 | 431 | XMLStreamReader soapHeader = null; |
aoqi@0 | 432 | if(hReaders.size()>0) { |
aoqi@0 | 433 | headerTag = DEFAULT_TAGS.get(base+1); |
aoqi@0 | 434 | ElemInfo hdrElem = new ElemInfo(headerTag, envElem); |
aoqi@0 | 435 | soapHeader = new XMLReaderComposite(hdrElem, hReaders.toArray(new XMLStreamReader[hReaders.size()])); |
aoqi@0 | 436 | } |
aoqi@0 | 437 | try { |
aoqi@0 | 438 | XMLStreamReader payload= readPayload(); |
aoqi@0 | 439 | XMLStreamReader soapBody = new XMLReaderComposite(bdyElem, new XMLStreamReader[]{payload}); |
aoqi@0 | 440 | XMLStreamReader[] soapContent = (soapHeader != null) ? new XMLStreamReader[]{soapHeader, soapBody} : new XMLStreamReader[]{soapBody}; |
aoqi@0 | 441 | return new XMLReaderComposite(envElem, soapContent); |
aoqi@0 | 442 | } catch (XMLStreamException e) { |
aoqi@0 | 443 | throw new RuntimeException(e); |
aoqi@0 | 444 | } |
aoqi@0 | 445 | } |
aoqi@0 | 446 | } |