aoqi@0: /*
aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0: *
aoqi@0: * This code is free software; you can redistribute it and/or modify it
aoqi@0: * under the terms of the GNU General Public License version 2 only, as
aoqi@0: * published by the Free Software Foundation. Oracle designates this
aoqi@0: * particular file as subject to the "Classpath" exception as provided
aoqi@0: * by Oracle in the LICENSE file that accompanied this code.
aoqi@0: *
aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0: * accompanied this code).
aoqi@0: *
aoqi@0: * You should have received a copy of the GNU General Public License version
aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0: *
aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0: * or visit www.oracle.com if you need additional information or have any
aoqi@0: * questions.
aoqi@0: */
aoqi@0:
aoqi@0: package com.sun.xml.internal.ws.message;
aoqi@0:
aoqi@0: import com.sun.istack.internal.NotNull;
aoqi@0: import com.sun.xml.internal.bind.api.Bridge;
aoqi@0: import com.sun.xml.internal.ws.api.SOAPVersion;
aoqi@0: import com.sun.xml.internal.ws.api.message.Header;
aoqi@0: import com.sun.xml.internal.ws.api.message.Message;
aoqi@0: import com.sun.xml.internal.ws.api.message.MessageHeaders;
aoqi@0: import com.sun.xml.internal.ws.api.message.MessageWritable;
aoqi@0: import com.sun.xml.internal.ws.api.message.Packet;
aoqi@0: import com.sun.xml.internal.ws.api.message.saaj.SAAJFactory;
aoqi@0: import com.sun.xml.internal.ws.encoding.TagInfoset;
aoqi@0: import com.sun.xml.internal.ws.message.saaj.SAAJMessage;
aoqi@0: import com.sun.xml.internal.ws.spi.db.XMLBridge;
aoqi@0: import java.util.ArrayList;
aoqi@0: import java.util.Collections;
aoqi@0: import org.xml.sax.ContentHandler;
aoqi@0: import org.xml.sax.ErrorHandler;
aoqi@0: import org.xml.sax.SAXException;
aoqi@0: import org.xml.sax.helpers.AttributesImpl;
aoqi@0: import org.xml.sax.helpers.LocatorImpl;
aoqi@0:
aoqi@0: import javax.xml.bind.JAXBException;
aoqi@0: import javax.xml.bind.Unmarshaller;
aoqi@0: import javax.xml.soap.SOAPException;
aoqi@0: import javax.xml.soap.SOAPMessage;
aoqi@0: import javax.xml.stream.XMLStreamException;
aoqi@0: import javax.xml.stream.XMLStreamWriter;
aoqi@0: import javax.xml.transform.Source;
aoqi@0: import javax.xml.transform.sax.SAXSource;
aoqi@0:
aoqi@0: import java.util.List;
aoqi@0: import java.util.Map;
aoqi@0:
aoqi@0: /**
aoqi@0: * Partial {@link Message} implementation.
aoqi@0: *
aoqi@0: *
aoqi@0: * This class implements some of the {@link Message} methods.
aoqi@0: * The idea is that those implementations may be non-optimal but
aoqi@0: * it may save effort in implementing {@link Message} and reduce
aoqi@0: * the code size.
aoqi@0: *
aoqi@0: *
aoqi@0: * {@link Message} classes that are used more commonly should
aoqi@0: * examine carefully which method can be implemented faster,
aoqi@0: * and override them accordingly.
aoqi@0: *
aoqi@0: * @author Kohsuke Kawaguchi
aoqi@0: */
aoqi@0: public abstract class AbstractMessageImpl extends Message {
aoqi@0: /**
aoqi@0: * SOAP version of this message.
aoqi@0: * Used to implement some of the methods, but nothing more than that.
aoqi@0: *
aoqi@0: *
aoqi@0: * So if you aren't using those methods that use this field,
aoqi@0: * this can be null.
aoqi@0: */
aoqi@0: protected final SOAPVersion soapVersion;
aoqi@0:
aoqi@0: protected @NotNull TagInfoset envelopeTag;
aoqi@0: protected @NotNull TagInfoset headerTag;
aoqi@0: protected @NotNull TagInfoset bodyTag;
aoqi@0:
aoqi@0: protected static final AttributesImpl EMPTY_ATTS;
aoqi@0: protected static final LocatorImpl NULL_LOCATOR = new LocatorImpl();
aoqi@0: protected static final List DEFAULT_TAGS;
aoqi@0:
aoqi@0: static void create(SOAPVersion v, List c) {
aoqi@0: int base = v.ordinal()*3;
aoqi@0: c.add(base, new TagInfoset(v.nsUri, "Envelope", "S", EMPTY_ATTS,"S", v.nsUri));
aoqi@0: c.add(base+1, new TagInfoset(v.nsUri, "Header", "S", EMPTY_ATTS));
aoqi@0: c.add(base+2, new TagInfoset(v.nsUri, "Body", "S", EMPTY_ATTS));
aoqi@0: }
aoqi@0:
aoqi@0: static {
aoqi@0: EMPTY_ATTS = new AttributesImpl();
aoqi@0: List tagList = new ArrayList();
aoqi@0: create(SOAPVersion.SOAP_11, tagList);
aoqi@0: create(SOAPVersion.SOAP_12, tagList);
aoqi@0: DEFAULT_TAGS = Collections.unmodifiableList(tagList);
aoqi@0: }
aoqi@0:
aoqi@0: protected AbstractMessageImpl(SOAPVersion soapVersion) {
aoqi@0: this.soapVersion = soapVersion;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public SOAPVersion getSOAPVersion() {
aoqi@0: return soapVersion;
aoqi@0: }
aoqi@0: /**
aoqi@0: * Copy constructor.
aoqi@0: */
aoqi@0: protected AbstractMessageImpl(AbstractMessageImpl that) {
aoqi@0: this.soapVersion = that.soapVersion;
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public Source readEnvelopeAsSource() {
aoqi@0: return new SAXSource(new XMLReaderImpl(this), XMLReaderImpl.THE_SOURCE);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
aoqi@0: if(hasAttachments())
aoqi@0: unmarshaller.setAttachmentUnmarshaller(new AttachmentUnmarshallerImpl(getAttachments()));
aoqi@0: try {
aoqi@0: return (T) unmarshaller.unmarshal(readPayloadAsSource());
aoqi@0: } finally{
aoqi@0: unmarshaller.setAttachmentUnmarshaller(null);
aoqi@0: }
aoqi@0: }
aoqi@0: /** @deprecated */
aoqi@0: @Override
aoqi@0: public T readPayloadAsJAXB(Bridge bridge) throws JAXBException {
aoqi@0: return bridge.unmarshal(readPayloadAsSource(),
aoqi@0: hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null );
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public T readPayloadAsJAXB(XMLBridge bridge) throws JAXBException {
aoqi@0: return bridge.unmarshal(readPayloadAsSource(),
aoqi@0: hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null );
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Default implementation that relies on {@link #writePayloadTo(XMLStreamWriter)}
aoqi@0: */
aoqi@0: @Override
aoqi@0: public void writeTo(XMLStreamWriter w) throws XMLStreamException {
aoqi@0: String soapNsUri = soapVersion.nsUri;
aoqi@0: w.writeStartDocument();
aoqi@0: w.writeStartElement("S","Envelope",soapNsUri);
aoqi@0: w.writeNamespace("S",soapNsUri);
aoqi@0: if(hasHeaders()) {
aoqi@0: w.writeStartElement("S","Header",soapNsUri);
aoqi@0: MessageHeaders headers = getHeaders();
aoqi@0: for (Header h : headers.asList()) {
aoqi@0: h.writeTo(w);
aoqi@0: }
aoqi@0: w.writeEndElement();
aoqi@0: }
aoqi@0: // write the body
aoqi@0: w.writeStartElement("S","Body",soapNsUri);
aoqi@0:
aoqi@0: writePayloadTo(w);
aoqi@0:
aoqi@0: w.writeEndElement();
aoqi@0: w.writeEndElement();
aoqi@0: w.writeEndDocument();
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Writes the whole envelope as SAX events.
aoqi@0: */
aoqi@0: @Override
aoqi@0: public void writeTo( ContentHandler contentHandler, ErrorHandler errorHandler ) throws SAXException {
aoqi@0: String soapNsUri = soapVersion.nsUri;
aoqi@0:
aoqi@0: contentHandler.setDocumentLocator(NULL_LOCATOR);
aoqi@0: contentHandler.startDocument();
aoqi@0: contentHandler.startPrefixMapping("S",soapNsUri);
aoqi@0: contentHandler.startElement(soapNsUri,"Envelope","S:Envelope",EMPTY_ATTS);
aoqi@0: if(hasHeaders()) {
aoqi@0: contentHandler.startElement(soapNsUri,"Header","S:Header",EMPTY_ATTS);
aoqi@0: MessageHeaders headers = getHeaders();
aoqi@0: for (Header h : headers.asList()) {
aoqi@0: h.writeTo(contentHandler,errorHandler);
aoqi@0: }
aoqi@0: contentHandler.endElement(soapNsUri,"Header","S:Header");
aoqi@0: }
aoqi@0: // write the body
aoqi@0: contentHandler.startElement(soapNsUri,"Body","S:Body",EMPTY_ATTS);
aoqi@0: writePayloadTo(contentHandler,errorHandler, true);
aoqi@0: contentHandler.endElement(soapNsUri,"Body","S:Body");
aoqi@0: contentHandler.endElement(soapNsUri,"Envelope","S:Envelope");
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Writes the payload to SAX events.
aoqi@0: *
aoqi@0: * @param fragment
aoqi@0: * if true, this method will fire SAX events without start/endDocument events,
aoqi@0: * suitable for embedding this into a bigger SAX event sequence.
aoqi@0: * if false, this method generaets a completely SAX event sequence on its own.
aoqi@0: */
aoqi@0: protected abstract void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException;
aoqi@0:
aoqi@0: public Message toSAAJ(Packet p, Boolean inbound) throws SOAPException {
aoqi@0: SAAJMessage message = SAAJFactory.read(p);
aoqi@0: if (message instanceof MessageWritable)
aoqi@0: ((MessageWritable) message)
aoqi@0: .setMTOMConfiguration(p.getMtomFeature());
aoqi@0: if (inbound != null) transportHeaders(p, inbound, message.readAsSOAPMessage());
aoqi@0: return message;
aoqi@0: }
aoqi@0:
aoqi@0: /**
aoqi@0: * Default implementation that uses {@link #writeTo(ContentHandler, ErrorHandler)}
aoqi@0: */
aoqi@0: @Override
aoqi@0: public SOAPMessage readAsSOAPMessage() throws SOAPException {
aoqi@0: return SAAJFactory.read(soapVersion, this);
aoqi@0: }
aoqi@0:
aoqi@0: @Override
aoqi@0: public SOAPMessage readAsSOAPMessage(Packet packet, boolean inbound) throws SOAPException {
aoqi@0: SOAPMessage msg = SAAJFactory.read(soapVersion, this, packet);
aoqi@0: transportHeaders(packet, inbound, msg);
aoqi@0: return msg;
aoqi@0: }
aoqi@0:
aoqi@0: private void transportHeaders(Packet packet, boolean inbound, SOAPMessage msg) throws SOAPException {
aoqi@0: Map> headers = getTransportHeaders(packet, inbound);
aoqi@0: if (headers != null) {
aoqi@0: addSOAPMimeHeaders(msg.getMimeHeaders(), headers);
aoqi@0: }
aoqi@0: if (msg.saveRequired()) msg.saveChanges();
aoqi@0: }
aoqi@0: }