325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.xml.internal.ws.message;
325N/A
325N/Aimport com.sun.istack.internal.FragmentContentHandler;
325N/Aimport com.sun.xml.internal.bind.api.Bridge;
325N/Aimport com.sun.xml.internal.bind.unmarshaller.DOMScanner;
325N/Aimport com.sun.xml.internal.ws.api.SOAPVersion;
325N/Aimport com.sun.xml.internal.ws.api.message.HeaderList;
325N/Aimport com.sun.xml.internal.ws.api.message.Message;
325N/Aimport com.sun.xml.internal.ws.api.message.AttachmentSet;
325N/Aimport com.sun.xml.internal.ws.streaming.DOMStreamReader;
325N/Aimport com.sun.xml.internal.ws.util.DOMUtil;
325N/Aimport org.w3c.dom.Element;
325N/Aimport org.xml.sax.ContentHandler;
325N/Aimport org.xml.sax.ErrorHandler;
325N/Aimport org.xml.sax.SAXException;
325N/A
325N/Aimport javax.xml.bind.JAXBException;
325N/Aimport javax.xml.bind.Unmarshaller;
325N/Aimport javax.xml.stream.XMLStreamException;
325N/Aimport javax.xml.stream.XMLStreamReader;
325N/Aimport javax.xml.stream.XMLStreamWriter;
325N/Aimport javax.xml.transform.Source;
325N/Aimport javax.xml.transform.dom.DOMSource;
325N/Aimport javax.xml.ws.WebServiceException;
325N/A
325N/A/**
325N/A * {@link Message} backed by a DOM {@link Element} that represents the payload.
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic final class DOMMessage extends AbstractMessageImpl {
325N/A private HeaderList headers;
325N/A private final Element payload;
325N/A
325N/A public DOMMessage(SOAPVersion ver, Element payload) {
325N/A this(ver,null,payload);
325N/A }
325N/A
325N/A public DOMMessage(SOAPVersion ver, HeaderList headers, Element payload) {
325N/A this(ver,headers,payload,null);
325N/A }
325N/A
325N/A public DOMMessage(SOAPVersion ver, HeaderList headers, Element payload, AttachmentSet attachments) {
325N/A super(ver);
325N/A this.headers = headers;
325N/A this.payload = payload;
325N/A this.attachmentSet = attachments;
325N/A assert payload!=null;
325N/A }
325N/A /**
325N/A * This constructor is a convenience and called by the {@link #copy}
325N/A */
325N/A private DOMMessage(DOMMessage that) {
325N/A super(that);
325N/A this.headers = HeaderList.copy(that.headers);
325N/A this.payload = that.payload;
325N/A }
325N/A
325N/A public boolean hasHeaders() {
325N/A return getHeaders().size() > 0;
325N/A }
325N/A
325N/A public HeaderList getHeaders() {
325N/A if (headers == null)
325N/A headers = new HeaderList();
325N/A
325N/A return headers;
325N/A }
325N/A
325N/A public String getPayloadLocalPart() {
325N/A return payload.getLocalName();
325N/A }
325N/A
325N/A public String getPayloadNamespaceURI() {
325N/A return payload.getNamespaceURI();
325N/A }
325N/A
325N/A public boolean hasPayload() {
325N/A return true;
325N/A }
325N/A
325N/A public Source readPayloadAsSource() {
325N/A return new DOMSource(payload);
325N/A }
325N/A
325N/A public <T> T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
325N/A if(hasAttachments())
325N/A unmarshaller.setAttachmentUnmarshaller(new AttachmentUnmarshallerImpl(getAttachments()));
325N/A try {
325N/A return (T)unmarshaller.unmarshal(payload);
325N/A } finally{
325N/A unmarshaller.setAttachmentUnmarshaller(null);
325N/A }
325N/A }
325N/A
325N/A public <T> T readPayloadAsJAXB(Bridge<T> bridge) throws JAXBException {
325N/A return bridge.unmarshal(payload,
325N/A hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null);
325N/A }
325N/A
325N/A public XMLStreamReader readPayload() throws XMLStreamException {
325N/A DOMStreamReader dss = new DOMStreamReader();
325N/A dss.setCurrentNode(payload);
325N/A dss.nextTag();
325N/A assert dss.getEventType()==XMLStreamReader.START_ELEMENT;
325N/A return dss;
325N/A }
325N/A
325N/A public void writePayloadTo(XMLStreamWriter sw) {
325N/A try {
325N/A if (payload != null)
325N/A DOMUtil.serializeNode(payload, sw);
325N/A } catch (XMLStreamException e) {
325N/A throw new WebServiceException(e);
325N/A }
325N/A }
325N/A
325N/A protected void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException {
325N/A if(fragment)
325N/A contentHandler = new FragmentContentHandler(contentHandler);
325N/A DOMScanner ds = new DOMScanner();
325N/A ds.setContentHandler(contentHandler);
325N/A ds.scan(payload);
325N/A }
325N/A
325N/A public Message copy() {
325N/A return new DOMMessage(this);
325N/A }
325N/A}