com.sun.xml.ws.message.AbstractMessageImpl Maven / Gradle / Ivy
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the license at
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at https://glassfish.dev.java.net/public/CDDLv1.0.html.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* you own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Copyright 2006 Sun Microsystems Inc. All Rights Reserved
*/
package com.sun.xml.ws.message;
import com.sun.xml.bind.api.Bridge;
import com.sun.xml.bind.marshaller.SAX2DOMEx;
import com.sun.xml.ws.api.SOAPVersion;
import com.sun.xml.ws.api.message.Attachment;
import com.sun.xml.ws.api.message.HeaderList;
import com.sun.xml.ws.api.message.Message;
import com.sun.xml.ws.api.message.Packet;
import com.sun.xml.ws.util.xml.XmlUtil;
import javax.xml.soap.AttachmentPart;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.LocatorImpl;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.MimeHeader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import java.util.List;
import java.util.Map;
/**
* Partial {@link Message} implementation.
*
*
* This class implements some of the {@link Message} methods.
* The idea is that those implementations may be non-optimal but
* it may save effort in implementing {@link Message} and reduce
* the code size.
*
*
* {@link Message} classes that are used more commonly should
* examine carefully which method can be implemented faster,
* and override them accordingly.
*
* @author Kohsuke Kawaguchi
*/
public abstract class AbstractMessageImpl extends Message {
/**
* SOAP version of this message.
* Used to implement some of the methods, but nothing more than that.
*
*
* So if you aren't using those methods that use this field,
* this can be null.
*/
protected final SOAPVersion soapVersion;
protected AbstractMessageImpl(SOAPVersion soapVersion) {
this.soapVersion = soapVersion;
}
/**
* Copy constructor.
*/
protected AbstractMessageImpl(AbstractMessageImpl that) {
this.soapVersion = that.soapVersion;
}
public Source readEnvelopeAsSource() {
return new SAXSource(new XMLReaderImpl(this), XMLReaderImpl.THE_SOURCE);
}
public T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
if(hasAttachments())
unmarshaller.setAttachmentUnmarshaller(new AttachmentUnmarshallerImpl(getAttachments()));
try {
return (T) unmarshaller.unmarshal(readPayloadAsSource());
} finally{
unmarshaller.setAttachmentUnmarshaller(null);
}
}
public T readPayloadAsJAXB(Bridge bridge) throws JAXBException {
return bridge.unmarshal(readPayloadAsSource(),
hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null );
}
/**
* Default implementation that relies on {@link #writePayloadTo(XMLStreamWriter)}
*/
public void writeTo(XMLStreamWriter w) throws XMLStreamException {
String soapNsUri = soapVersion.nsUri;
w.writeStartDocument();
w.writeStartElement("S","Envelope",soapNsUri);
w.writeNamespace("S",soapNsUri);
if(hasHeaders()) {
w.writeStartElement("S","Header",soapNsUri);
HeaderList headers = getHeaders();
int len = headers.size();
for( int i=0; i');
msg.addAttachmentPart(part);
}
return msg;
}
/**
*
*/
public SOAPMessage readAsSOAPMessage(Packet packet, boolean inbound) throws SOAPException {
SOAPMessage msg = readAsSOAPMessage();
Map> headers = null;
String key = inbound ? Packet.INBOUND_TRANSPORT_HEADERS : Packet.OUTBOUND_TRANSPORT_HEADERS;
if (packet.supports(key)) {
headers = (Map>)packet.get(key);
}
if (headers != null) {
for(Map.Entry> e : headers.entrySet()) {
if (!e.getKey().equalsIgnoreCase("Content-Type")) {
for(String value : e.getValue()) {
msg.getMimeHeaders().addHeader(e.getKey(), value);
}
}
}
}
msg.saveChanges();
return msg;
}
protected static final AttributesImpl EMPTY_ATTS = new AttributesImpl();
protected static final LocatorImpl NULL_LOCATOR = new LocatorImpl();
}