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
23  * questions.
24  */
25 
26 package com.sun.xml.internal.ws.message;
27 
28 import com.sun.istack.internal.FragmentContentHandler;
29 import com.sun.xml.internal.bind.api.Bridge;
30 import com.sun.xml.internal.bind.unmarshaller.DOMScanner;
31 import com.sun.xml.internal.ws.api.SOAPVersion;
32 import com.sun.xml.internal.ws.api.message.HeaderList;
33 import com.sun.xml.internal.ws.api.message.Message;
34 import com.sun.xml.internal.ws.api.message.AttachmentSet;
35 import com.sun.xml.internal.ws.api.message.MessageHeaders;
36 import com.sun.xml.internal.ws.streaming.DOMStreamReader;
37 import com.sun.xml.internal.ws.util.DOMUtil;
38 import org.w3c.dom.Element;
39 import org.xml.sax.ContentHandler;
40 import org.xml.sax.ErrorHandler;
41 import org.xml.sax.SAXException;
42 
43 import javax.xml.bind.JAXBException;
44 import javax.xml.bind.Unmarshaller;
45 import javax.xml.stream.XMLStreamException;
46 import javax.xml.stream.XMLStreamReader;
47 import javax.xml.stream.XMLStreamWriter;
48 import javax.xml.transform.Source;
49 import javax.xml.transform.dom.DOMSource;
50 import javax.xml.ws.WebServiceException;
51 
52 /**
53  * {@link Message} backed by a DOM {@link Element} that represents the payload.
54  *
55  * @author Kohsuke Kawaguchi
56  */
57 public final class DOMMessage extends AbstractMessageImpl {
58     private MessageHeaders headers;
59     private final Element payload;
60 
DOMMessage(SOAPVersion ver, Element payload)61     public DOMMessage(SOAPVersion ver, Element payload) {
62         this(ver,null,payload);
63     }
64 
DOMMessage(SOAPVersion ver, MessageHeaders headers, Element payload)65     public DOMMessage(SOAPVersion ver, MessageHeaders headers, Element payload) {
66         this(ver,headers,payload,null);
67     }
68 
DOMMessage(SOAPVersion ver, MessageHeaders headers, Element payload, AttachmentSet attachments)69     public DOMMessage(SOAPVersion ver, MessageHeaders headers, Element payload, AttachmentSet attachments) {
70         super(ver);
71         this.headers = headers;
72         this.payload = payload;
73         this.attachmentSet = attachments;
74         assert payload!=null;
75     }
76     /**
77      * This constructor is a convenience and called by the {@link #copy}
78      */
DOMMessage(DOMMessage that)79     private DOMMessage(DOMMessage that) {
80         super(that);
81         this.headers = HeaderList.copy(that.headers);
82         this.payload = that.payload;
83     }
84 
hasHeaders()85     public boolean hasHeaders() {
86         return getHeaders().hasHeaders();
87     }
88 
getHeaders()89     public MessageHeaders getHeaders() {
90         if (headers == null)
91             headers = new HeaderList(getSOAPVersion());
92 
93         return headers;
94     }
95 
getPayloadLocalPart()96     public String getPayloadLocalPart() {
97         return payload.getLocalName();
98     }
99 
getPayloadNamespaceURI()100     public String getPayloadNamespaceURI() {
101         return payload.getNamespaceURI();
102     }
103 
hasPayload()104     public boolean hasPayload() {
105         return true;
106     }
107 
readPayloadAsSource()108     public Source readPayloadAsSource() {
109         return new DOMSource(payload);
110     }
111 
readPayloadAsJAXB(Unmarshaller unmarshaller)112     public <T> T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
113         if(hasAttachments())
114             unmarshaller.setAttachmentUnmarshaller(new AttachmentUnmarshallerImpl(getAttachments()));
115         try {
116             return (T)unmarshaller.unmarshal(payload);
117         } finally{
118             unmarshaller.setAttachmentUnmarshaller(null);
119         }
120     }
121     /** @deprecated */
readPayloadAsJAXB(Bridge<T> bridge)122     public <T> T readPayloadAsJAXB(Bridge<T> bridge) throws JAXBException {
123         return bridge.unmarshal(payload,
124             hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null);
125     }
126 
readPayload()127     public XMLStreamReader readPayload() throws XMLStreamException {
128         DOMStreamReader dss = new DOMStreamReader();
129         dss.setCurrentNode(payload);
130         dss.nextTag();
131         assert dss.getEventType()==XMLStreamReader.START_ELEMENT;
132         return dss;
133     }
134 
writePayloadTo(XMLStreamWriter sw)135     public void writePayloadTo(XMLStreamWriter sw) {
136         try {
137             if (payload != null)
138                 DOMUtil.serializeNode(payload, sw);
139         } catch (XMLStreamException e) {
140             throw new WebServiceException(e);
141         }
142     }
143 
writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment)144     protected void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
145         if(fragment)
146             contentHandler = new FragmentContentHandler(contentHandler);
147         DOMScanner ds = new DOMScanner();
148         ds.setContentHandler(contentHandler);
149         ds.scan(payload);
150     }
151 
copy()152     public Message copy() {
153         return new DOMMessage(this);
154     }
155 
156 }
157