src/share/jaxws_classes/com/sun/xml/internal/ws/message/jaxb/JAXBMessage.java

Print this page
rev 472 : 8036030: Update JAX-WS RI integration to latest version
   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  29 import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
  30 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
  31 import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult;
  32 import com.sun.xml.internal.ws.api.SOAPVersion;
  33 import com.sun.xml.internal.ws.api.message.AttachmentSet;
  34 import com.sun.xml.internal.ws.api.message.Header;
  35 import com.sun.xml.internal.ws.api.message.HeaderList;
  36 import com.sun.xml.internal.ws.api.message.Message;
  37 import com.sun.xml.internal.ws.api.message.MessageHeaders;
  38 import com.sun.xml.internal.ws.api.message.StreamingSOAP;
  39 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
  40 import com.sun.xml.internal.ws.message.AbstractMessageImpl;
  41 import com.sun.xml.internal.ws.message.AttachmentSetImpl;
  42 import com.sun.xml.internal.ws.message.RootElementSniffer;
  43 import com.sun.xml.internal.ws.message.stream.StreamMessage;
  44 import com.sun.xml.internal.ws.spi.db.BindingContext;
  45 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
  46 import com.sun.xml.internal.ws.spi.db.XMLBridge;
  47 import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
  48 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
  49 import com.sun.xml.internal.ws.streaming.MtomStreamWriter;
  50 import com.sun.xml.internal.ws.util.xml.XMLReaderComposite;
  51 import com.sun.xml.internal.ws.util.xml.XMLReaderComposite.ElemInfo;
  52 
  53 import org.xml.sax.ContentHandler;
  54 import org.xml.sax.ErrorHandler;
  55 import org.xml.sax.SAXException;
  56 
  57 import javax.xml.bind.JAXBContext;
  58 import javax.xml.bind.JAXBElement;
  59 import javax.xml.bind.JAXBException;
  60 import javax.xml.bind.Marshaller;
  61 import javax.xml.bind.Unmarshaller;
  62 import javax.xml.bind.attachment.AttachmentMarshaller;
  63 import javax.xml.bind.annotation.XmlRootElement;
  64 import javax.xml.bind.util.JAXBResult;
  65 import javax.xml.namespace.QName;
  66 import javax.xml.stream.XMLStreamException;
  67 import javax.xml.stream.XMLStreamReader;
  68 import javax.xml.stream.XMLStreamWriter;
  69 import static javax.xml.stream.XMLStreamConstants.START_DOCUMENT;


 223         this.jaxbObject = jaxbObject;
 224         QName tagName = bridge.getTypeInfo().tagName;
 225         this.nsUri = tagName.getNamespaceURI();
 226         this.localName = tagName.getLocalPart();
 227         this.attachmentSet = new AttachmentSetImpl();
 228     }
 229 
 230     /**
 231      * Copy constructor.
 232      */
 233     public JAXBMessage(JAXBMessage that) {
 234         super(that);
 235         this.headers = that.headers;
 236         if(this.headers!=null)
 237             this.headers = new HeaderList(this.headers);
 238         this.attachmentSet = that.attachmentSet;
 239 
 240         this.jaxbObject = that.jaxbObject;
 241         this.bridge = that.bridge;
 242         this.rawContext = that.rawContext;

 243     }
 244 
 245     @Override
 246     public boolean hasHeaders() {
 247         return headers!=null && headers.hasHeaders();
 248     }
 249 
 250     @Override
 251     public MessageHeaders getHeaders() {
 252         if(headers==null)
 253             headers = new HeaderList(getSOAPVersion());
 254         return headers;
 255     }
 256 
 257     @Override
 258     public String getPayloadLocalPart() {
 259         if(localName==null)
 260             sniff();
 261         return localName;
 262     }


 394                 } else {
 395                     m.marshal(jaxbObject, sw);
 396                 }
 397             } else {
 398                 if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) {
 399                     bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), am);
 400                 } else {
 401                     bridge.marshal(jaxbObject, sw, am);
 402                 }
 403             }
 404             //cleanup() is not needed since JAXB doesn't keep ref to AttachmentMarshaller
 405             //am.cleanup();
 406         } catch (JAXBException e) {
 407             // bug 6449684, spec 4.3.4
 408             throw new WebServiceException(e);
 409         }
 410     }
 411 
 412     @Override
 413     public Message copy() {
 414         return new JAXBMessage(this);
 415     }
 416 
 417     public XMLStreamReader readEnvelope() {
 418         int base = soapVersion.ordinal()*3;
 419         this.envelopeTag = DEFAULT_TAGS.get(base);
 420         this.bodyTag = DEFAULT_TAGS.get(base+2);
 421         List<XMLStreamReader> hReaders = new java.util.ArrayList<XMLStreamReader>();
 422         ElemInfo envElem =  new ElemInfo(envelopeTag, null);
 423         ElemInfo bdyElem =  new ElemInfo(bodyTag, envElem);
 424         for (Header h : getHeaders().asList()) {
 425             try {
 426                 hReaders.add(h.readHeader());
 427             } catch (XMLStreamException e) {
 428                 throw new RuntimeException(e);
 429             }
 430         }
 431         XMLStreamReader soapHeader = null;
 432         if(hReaders.size()>0) {
 433             headerTag = DEFAULT_TAGS.get(base+1);
 434             ElemInfo hdrElem = new ElemInfo(headerTag, envElem);
 435             soapHeader = new XMLReaderComposite(hdrElem, hReaders.toArray(new XMLStreamReader[hReaders.size()]));
 436         }
 437         try {
 438             XMLStreamReader payload= readPayload();
 439             XMLStreamReader soapBody = new XMLReaderComposite(bdyElem, new XMLStreamReader[]{payload});
 440             XMLStreamReader[] soapContent = (soapHeader != null) ? new XMLStreamReader[]{soapHeader, soapBody} : new XMLStreamReader[]{soapBody};
 441             return new XMLReaderComposite(envElem, soapContent);
 442         } catch (XMLStreamException e) {
 443             throw new RuntimeException(e);
 444         }
 445     }































 446 }
   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  29 import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer;
  30 import com.sun.xml.internal.stream.buffer.XMLStreamBuffer;
  31 import com.sun.xml.internal.stream.buffer.XMLStreamBufferResult;
  32 import com.sun.xml.internal.ws.api.SOAPVersion;
  33 import com.sun.xml.internal.ws.api.message.AttachmentSet;
  34 import com.sun.xml.internal.ws.api.message.Header;
  35 import com.sun.xml.internal.ws.api.message.HeaderList;
  36 import com.sun.xml.internal.ws.api.message.Message;
  37 import com.sun.xml.internal.ws.api.message.MessageHeaders;
  38 import com.sun.xml.internal.ws.api.message.StreamingSOAP;
  39 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
  40 import com.sun.xml.internal.ws.message.AbstractMessageImpl;
  41 import com.sun.xml.internal.ws.message.AttachmentSetImpl;
  42 import com.sun.xml.internal.ws.message.RootElementSniffer;
  43 import com.sun.xml.internal.ws.message.stream.StreamMessage;
  44 import com.sun.xml.internal.ws.spi.db.BindingContext;
  45 import com.sun.xml.internal.ws.spi.db.BindingContextFactory;
  46 import com.sun.xml.internal.ws.spi.db.XMLBridge;
  47 import com.sun.xml.internal.ws.streaming.XMLStreamWriterUtil;
  48 import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil;
  49 import com.sun.xml.internal.org.jvnet.staxex.util.MtomStreamWriter;
  50 import com.sun.xml.internal.ws.util.xml.XMLReaderComposite;
  51 import com.sun.xml.internal.ws.util.xml.XMLReaderComposite.ElemInfo;
  52 
  53 import org.xml.sax.ContentHandler;
  54 import org.xml.sax.ErrorHandler;
  55 import org.xml.sax.SAXException;
  56 
  57 import javax.xml.bind.JAXBContext;
  58 import javax.xml.bind.JAXBElement;
  59 import javax.xml.bind.JAXBException;
  60 import javax.xml.bind.Marshaller;
  61 import javax.xml.bind.Unmarshaller;
  62 import javax.xml.bind.attachment.AttachmentMarshaller;
  63 import javax.xml.bind.annotation.XmlRootElement;
  64 import javax.xml.bind.util.JAXBResult;
  65 import javax.xml.namespace.QName;
  66 import javax.xml.stream.XMLStreamException;
  67 import javax.xml.stream.XMLStreamReader;
  68 import javax.xml.stream.XMLStreamWriter;
  69 import static javax.xml.stream.XMLStreamConstants.START_DOCUMENT;


 223         this.jaxbObject = jaxbObject;
 224         QName tagName = bridge.getTypeInfo().tagName;
 225         this.nsUri = tagName.getNamespaceURI();
 226         this.localName = tagName.getLocalPart();
 227         this.attachmentSet = new AttachmentSetImpl();
 228     }
 229 
 230     /**
 231      * Copy constructor.
 232      */
 233     public JAXBMessage(JAXBMessage that) {
 234         super(that);
 235         this.headers = that.headers;
 236         if(this.headers!=null)
 237             this.headers = new HeaderList(this.headers);
 238         this.attachmentSet = that.attachmentSet;
 239 
 240         this.jaxbObject = that.jaxbObject;
 241         this.bridge = that.bridge;
 242         this.rawContext = that.rawContext;
 243         this.copyFrom(that);
 244     }
 245 
 246     @Override
 247     public boolean hasHeaders() {
 248         return headers!=null && headers.hasHeaders();
 249     }
 250 
 251     @Override
 252     public MessageHeaders getHeaders() {
 253         if(headers==null)
 254             headers = new HeaderList(getSOAPVersion());
 255         return headers;
 256     }
 257 
 258     @Override
 259     public String getPayloadLocalPart() {
 260         if(localName==null)
 261             sniff();
 262         return localName;
 263     }


 395                 } else {
 396                     m.marshal(jaxbObject, sw);
 397                 }
 398             } else {
 399                 if (os != null && encoding != null && encoding.equalsIgnoreCase(SOAPBindingCodec.UTF8_ENCODING)) {
 400                     bridge.marshal(jaxbObject, os, sw.getNamespaceContext(), am);
 401                 } else {
 402                     bridge.marshal(jaxbObject, sw, am);
 403                 }
 404             }
 405             //cleanup() is not needed since JAXB doesn't keep ref to AttachmentMarshaller
 406             //am.cleanup();
 407         } catch (JAXBException e) {
 408             // bug 6449684, spec 4.3.4
 409             throw new WebServiceException(e);
 410         }
 411     }
 412 
 413     @Override
 414     public Message copy() {
 415         return new JAXBMessage(this).copyFrom(this);
 416     }
 417 
 418     public XMLStreamReader readEnvelope() {
 419         int base = soapVersion.ordinal()*3;
 420         this.envelopeTag = DEFAULT_TAGS.get(base);
 421         this.bodyTag = DEFAULT_TAGS.get(base+2);
 422         List<XMLStreamReader> hReaders = new java.util.ArrayList<XMLStreamReader>();
 423         ElemInfo envElem =  new ElemInfo(envelopeTag, null);
 424         ElemInfo bdyElem =  new ElemInfo(bodyTag, envElem);
 425         for (Header h : getHeaders().asList()) {
 426             try {
 427                 hReaders.add(h.readHeader());
 428             } catch (XMLStreamException e) {
 429                 throw new RuntimeException(e);
 430             }
 431         }
 432         XMLStreamReader soapHeader = null;
 433         if(hReaders.size()>0) {
 434             headerTag = DEFAULT_TAGS.get(base+1);
 435             ElemInfo hdrElem = new ElemInfo(headerTag, envElem);
 436             soapHeader = new XMLReaderComposite(hdrElem, hReaders.toArray(new XMLStreamReader[hReaders.size()]));
 437         }
 438         try {
 439             XMLStreamReader payload= readPayload();
 440             XMLStreamReader soapBody = new XMLReaderComposite(bdyElem, new XMLStreamReader[]{payload});
 441             XMLStreamReader[] soapContent = (soapHeader != null) ? new XMLStreamReader[]{soapHeader, soapBody} : new XMLStreamReader[]{soapBody};
 442             return new XMLReaderComposite(envElem, soapContent);
 443         } catch (XMLStreamException e) {
 444             throw new RuntimeException(e);
 445         }
 446     }
 447 
 448     public boolean isPayloadStreamReader() { return false; }
 449 
 450     public QName getPayloadQName() {
 451         return new QName(getPayloadNamespaceURI(), getPayloadLocalPart());
 452     }
 453 
 454     public XMLStreamReader readToBodyStarTag() {
 455         int base = soapVersion.ordinal()*3;
 456         this.envelopeTag = DEFAULT_TAGS.get(base);
 457         this.bodyTag = DEFAULT_TAGS.get(base+2);
 458         List<XMLStreamReader> hReaders = new java.util.ArrayList<XMLStreamReader>();
 459         ElemInfo envElem =  new ElemInfo(envelopeTag, null);
 460         ElemInfo bdyElem =  new ElemInfo(bodyTag, envElem);
 461         for (Header h : getHeaders().asList()) {
 462             try {
 463                 hReaders.add(h.readHeader());
 464             } catch (XMLStreamException e) {
 465                 throw new RuntimeException(e);
 466             }
 467         }
 468         XMLStreamReader soapHeader = null;
 469         if(hReaders.size()>0) {
 470             headerTag = DEFAULT_TAGS.get(base+1);
 471             ElemInfo hdrElem = new ElemInfo(headerTag, envElem);
 472             soapHeader = new XMLReaderComposite(hdrElem, hReaders.toArray(new XMLStreamReader[hReaders.size()]));
 473         }
 474         XMLStreamReader soapBody = new XMLReaderComposite(bdyElem, new XMLStreamReader[]{});
 475         XMLStreamReader[] soapContent = (soapHeader != null) ? new XMLStreamReader[]{soapHeader, soapBody} : new XMLStreamReader[]{soapBody};
 476         return new XMLReaderComposite(envElem, soapContent);
 477     }
 478 }