001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.message;
038:
039: import com.sun.istack.FragmentContentHandler;
040: import com.sun.xml.bind.api.Bridge;
041: import com.sun.xml.bind.unmarshaller.DOMScanner;
042: import com.sun.xml.ws.api.SOAPVersion;
043: import com.sun.xml.ws.api.message.HeaderList;
044: import com.sun.xml.ws.api.message.Message;
045: import com.sun.xml.ws.streaming.DOMStreamReader;
046: import com.sun.xml.ws.util.DOMUtil;
047: import org.w3c.dom.Element;
048: import org.xml.sax.ContentHandler;
049: import org.xml.sax.ErrorHandler;
050: import org.xml.sax.SAXException;
051:
052: import javax.xml.bind.JAXBException;
053: import javax.xml.bind.Unmarshaller;
054: import javax.xml.stream.XMLStreamException;
055: import javax.xml.stream.XMLStreamReader;
056: import javax.xml.stream.XMLStreamWriter;
057: import javax.xml.transform.Source;
058: import javax.xml.transform.dom.DOMSource;
059: import javax.xml.ws.WebServiceException;
060:
061: /**
062: * {@link Message} backed by a DOM {@link Element} that represents the payload.
063: *
064: * @author Kohsuke Kawaguchi
065: */
066: public final class DOMMessage extends AbstractMessageImpl {
067: private HeaderList headers;
068: private final Element payload;
069:
070: public DOMMessage(SOAPVersion ver, Element payload) {
071: this (ver, null, payload);
072: }
073:
074: public DOMMessage(SOAPVersion ver, HeaderList headers,
075: Element payload) {
076: super (ver);
077: this .headers = headers;
078: this .payload = payload;
079: assert payload != null;
080: }
081:
082: /**
083: * This constructor is a convenience and called by the {@link #copy}
084: */
085: private DOMMessage(DOMMessage that) {
086: super (that);
087: this .headers = HeaderList.copy(that.headers);
088: this .payload = that.payload;
089: }
090:
091: public boolean hasHeaders() {
092: return getHeaders().size() > 0;
093: }
094:
095: public HeaderList getHeaders() {
096: if (headers == null)
097: headers = new HeaderList();
098:
099: return headers;
100: }
101:
102: public String getPayloadLocalPart() {
103: return payload.getLocalName();
104: }
105:
106: public String getPayloadNamespaceURI() {
107: return payload.getNamespaceURI();
108: }
109:
110: public boolean hasPayload() {
111: return true;
112: }
113:
114: public Source readPayloadAsSource() {
115: return new DOMSource(payload);
116: }
117:
118: public <T> T readPayloadAsJAXB(Unmarshaller unmarshaller)
119: throws JAXBException {
120: if (hasAttachments())
121: unmarshaller
122: .setAttachmentUnmarshaller(new AttachmentUnmarshallerImpl(
123: getAttachments()));
124: try {
125: return (T) unmarshaller.unmarshal(payload);
126: } finally {
127: unmarshaller.setAttachmentUnmarshaller(null);
128: }
129: }
130:
131: public <T> T readPayloadAsJAXB(Bridge<T> bridge)
132: throws JAXBException {
133: return bridge.unmarshal(payload,
134: hasAttachments() ? new AttachmentUnmarshallerImpl(
135: getAttachments()) : null);
136: }
137:
138: public XMLStreamReader readPayload() throws XMLStreamException {
139: DOMStreamReader dss = new DOMStreamReader();
140: dss.setCurrentNode(payload);
141: dss.nextTag();
142: assert dss.getEventType() == XMLStreamReader.START_ELEMENT;
143: return dss;
144: }
145:
146: public void writePayloadTo(XMLStreamWriter sw) {
147: try {
148: if (payload != null)
149: DOMUtil.serializeNode(payload, sw);
150: } catch (XMLStreamException e) {
151: throw new WebServiceException(e);
152: }
153: }
154:
155: protected void writePayloadTo(ContentHandler contentHandler,
156: ErrorHandler errorHandler, boolean fragment)
157: throws SAXException {
158: if (fragment)
159: contentHandler = new FragmentContentHandler(contentHandler);
160: DOMScanner ds = new DOMScanner();
161: ds.setContentHandler(contentHandler);
162: ds.scan(payload);
163: }
164:
165: public Message copy() {
166: return new DOMMessage(this);
167: }
168: }
|