src/share/jaxws_classes/com/sun/xml/internal/ws/message/AbstractMessageImpl.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.ws.message;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.NotNull;
aoqi@0 29 import com.sun.xml.internal.bind.api.Bridge;
aoqi@0 30 import com.sun.xml.internal.ws.api.SOAPVersion;
aoqi@0 31 import com.sun.xml.internal.ws.api.message.Header;
aoqi@0 32 import com.sun.xml.internal.ws.api.message.Message;
aoqi@0 33 import com.sun.xml.internal.ws.api.message.MessageHeaders;
aoqi@0 34 import com.sun.xml.internal.ws.api.message.MessageWritable;
aoqi@0 35 import com.sun.xml.internal.ws.api.message.Packet;
aoqi@0 36 import com.sun.xml.internal.ws.api.message.saaj.SAAJFactory;
aoqi@0 37 import com.sun.xml.internal.ws.encoding.TagInfoset;
aoqi@0 38 import com.sun.xml.internal.ws.message.saaj.SAAJMessage;
aoqi@0 39 import com.sun.xml.internal.ws.spi.db.XMLBridge;
aoqi@0 40 import java.util.ArrayList;
aoqi@0 41 import java.util.Collections;
aoqi@0 42 import org.xml.sax.ContentHandler;
aoqi@0 43 import org.xml.sax.ErrorHandler;
aoqi@0 44 import org.xml.sax.SAXException;
aoqi@0 45 import org.xml.sax.helpers.AttributesImpl;
aoqi@0 46 import org.xml.sax.helpers.LocatorImpl;
aoqi@0 47
aoqi@0 48 import javax.xml.bind.JAXBException;
aoqi@0 49 import javax.xml.bind.Unmarshaller;
aoqi@0 50 import javax.xml.soap.SOAPException;
aoqi@0 51 import javax.xml.soap.SOAPMessage;
aoqi@0 52 import javax.xml.stream.XMLStreamException;
aoqi@0 53 import javax.xml.stream.XMLStreamWriter;
aoqi@0 54 import javax.xml.transform.Source;
aoqi@0 55 import javax.xml.transform.sax.SAXSource;
aoqi@0 56
aoqi@0 57 import java.util.List;
aoqi@0 58 import java.util.Map;
aoqi@0 59
aoqi@0 60 /**
aoqi@0 61 * Partial {@link Message} implementation.
aoqi@0 62 *
aoqi@0 63 * <p>
aoqi@0 64 * This class implements some of the {@link Message} methods.
aoqi@0 65 * The idea is that those implementations may be non-optimal but
aoqi@0 66 * it may save effort in implementing {@link Message} and reduce
aoqi@0 67 * the code size.
aoqi@0 68 *
aoqi@0 69 * <p>
aoqi@0 70 * {@link Message} classes that are used more commonly should
aoqi@0 71 * examine carefully which method can be implemented faster,
aoqi@0 72 * and override them accordingly.
aoqi@0 73 *
aoqi@0 74 * @author Kohsuke Kawaguchi
aoqi@0 75 */
aoqi@0 76 public abstract class AbstractMessageImpl extends Message {
aoqi@0 77 /**
aoqi@0 78 * SOAP version of this message.
aoqi@0 79 * Used to implement some of the methods, but nothing more than that.
aoqi@0 80 *
aoqi@0 81 * <p>
aoqi@0 82 * So if you aren't using those methods that use this field,
aoqi@0 83 * this can be null.
aoqi@0 84 */
aoqi@0 85 protected final SOAPVersion soapVersion;
aoqi@0 86
aoqi@0 87 protected @NotNull TagInfoset envelopeTag;
aoqi@0 88 protected @NotNull TagInfoset headerTag;
aoqi@0 89 protected @NotNull TagInfoset bodyTag;
aoqi@0 90
aoqi@0 91 protected static final AttributesImpl EMPTY_ATTS;
aoqi@0 92 protected static final LocatorImpl NULL_LOCATOR = new LocatorImpl();
aoqi@0 93 protected static final List<TagInfoset> DEFAULT_TAGS;
aoqi@0 94
aoqi@0 95 static void create(SOAPVersion v, List c) {
aoqi@0 96 int base = v.ordinal()*3;
aoqi@0 97 c.add(base, new TagInfoset(v.nsUri, "Envelope", "S", EMPTY_ATTS,"S", v.nsUri));
aoqi@0 98 c.add(base+1, new TagInfoset(v.nsUri, "Header", "S", EMPTY_ATTS));
aoqi@0 99 c.add(base+2, new TagInfoset(v.nsUri, "Body", "S", EMPTY_ATTS));
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 static {
aoqi@0 103 EMPTY_ATTS = new AttributesImpl();
aoqi@0 104 List<TagInfoset> tagList = new ArrayList<TagInfoset>();
aoqi@0 105 create(SOAPVersion.SOAP_11, tagList);
aoqi@0 106 create(SOAPVersion.SOAP_12, tagList);
aoqi@0 107 DEFAULT_TAGS = Collections.unmodifiableList(tagList);
aoqi@0 108 }
aoqi@0 109
aoqi@0 110 protected AbstractMessageImpl(SOAPVersion soapVersion) {
aoqi@0 111 this.soapVersion = soapVersion;
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 @Override
aoqi@0 115 public SOAPVersion getSOAPVersion() {
aoqi@0 116 return soapVersion;
aoqi@0 117 }
aoqi@0 118 /**
aoqi@0 119 * Copy constructor.
aoqi@0 120 */
aoqi@0 121 protected AbstractMessageImpl(AbstractMessageImpl that) {
aoqi@0 122 this.soapVersion = that.soapVersion;
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 @Override
aoqi@0 126 public Source readEnvelopeAsSource() {
aoqi@0 127 return new SAXSource(new XMLReaderImpl(this), XMLReaderImpl.THE_SOURCE);
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 @Override
aoqi@0 131 public <T> T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
aoqi@0 132 if(hasAttachments())
aoqi@0 133 unmarshaller.setAttachmentUnmarshaller(new AttachmentUnmarshallerImpl(getAttachments()));
aoqi@0 134 try {
aoqi@0 135 return (T) unmarshaller.unmarshal(readPayloadAsSource());
aoqi@0 136 } finally{
aoqi@0 137 unmarshaller.setAttachmentUnmarshaller(null);
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140 /** @deprecated */
aoqi@0 141 @Override
aoqi@0 142 public <T> T readPayloadAsJAXB(Bridge<T> bridge) throws JAXBException {
aoqi@0 143 return bridge.unmarshal(readPayloadAsSource(),
aoqi@0 144 hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null );
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 @Override
aoqi@0 148 public <T> T readPayloadAsJAXB(XMLBridge<T> bridge) throws JAXBException {
aoqi@0 149 return bridge.unmarshal(readPayloadAsSource(),
aoqi@0 150 hasAttachments()? new AttachmentUnmarshallerImpl(getAttachments()) : null );
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 /**
aoqi@0 154 * Default implementation that relies on {@link #writePayloadTo(XMLStreamWriter)}
aoqi@0 155 */
aoqi@0 156 @Override
aoqi@0 157 public void writeTo(XMLStreamWriter w) throws XMLStreamException {
aoqi@0 158 String soapNsUri = soapVersion.nsUri;
aoqi@0 159 w.writeStartDocument();
aoqi@0 160 w.writeStartElement("S","Envelope",soapNsUri);
aoqi@0 161 w.writeNamespace("S",soapNsUri);
aoqi@0 162 if(hasHeaders()) {
aoqi@0 163 w.writeStartElement("S","Header",soapNsUri);
aoqi@0 164 MessageHeaders headers = getHeaders();
aoqi@0 165 for (Header h : headers.asList()) {
aoqi@0 166 h.writeTo(w);
aoqi@0 167 }
aoqi@0 168 w.writeEndElement();
aoqi@0 169 }
aoqi@0 170 // write the body
aoqi@0 171 w.writeStartElement("S","Body",soapNsUri);
aoqi@0 172
aoqi@0 173 writePayloadTo(w);
aoqi@0 174
aoqi@0 175 w.writeEndElement();
aoqi@0 176 w.writeEndElement();
aoqi@0 177 w.writeEndDocument();
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 /**
aoqi@0 181 * Writes the whole envelope as SAX events.
aoqi@0 182 */
aoqi@0 183 @Override
aoqi@0 184 public void writeTo( ContentHandler contentHandler, ErrorHandler errorHandler ) throws SAXException {
aoqi@0 185 String soapNsUri = soapVersion.nsUri;
aoqi@0 186
aoqi@0 187 contentHandler.setDocumentLocator(NULL_LOCATOR);
aoqi@0 188 contentHandler.startDocument();
aoqi@0 189 contentHandler.startPrefixMapping("S",soapNsUri);
aoqi@0 190 contentHandler.startElement(soapNsUri,"Envelope","S:Envelope",EMPTY_ATTS);
aoqi@0 191 if(hasHeaders()) {
aoqi@0 192 contentHandler.startElement(soapNsUri,"Header","S:Header",EMPTY_ATTS);
aoqi@0 193 MessageHeaders headers = getHeaders();
aoqi@0 194 for (Header h : headers.asList()) {
aoqi@0 195 h.writeTo(contentHandler,errorHandler);
aoqi@0 196 }
aoqi@0 197 contentHandler.endElement(soapNsUri,"Header","S:Header");
aoqi@0 198 }
aoqi@0 199 // write the body
aoqi@0 200 contentHandler.startElement(soapNsUri,"Body","S:Body",EMPTY_ATTS);
aoqi@0 201 writePayloadTo(contentHandler,errorHandler, true);
aoqi@0 202 contentHandler.endElement(soapNsUri,"Body","S:Body");
aoqi@0 203 contentHandler.endElement(soapNsUri,"Envelope","S:Envelope");
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 /**
aoqi@0 207 * Writes the payload to SAX events.
aoqi@0 208 *
aoqi@0 209 * @param fragment
aoqi@0 210 * if true, this method will fire SAX events without start/endDocument events,
aoqi@0 211 * suitable for embedding this into a bigger SAX event sequence.
aoqi@0 212 * if false, this method generaets a completely SAX event sequence on its own.
aoqi@0 213 */
aoqi@0 214 protected abstract void writePayloadTo(ContentHandler contentHandler, ErrorHandler errorHandler, boolean fragment) throws SAXException;
aoqi@0 215
aoqi@0 216 public Message toSAAJ(Packet p, Boolean inbound) throws SOAPException {
aoqi@0 217 SAAJMessage message = SAAJFactory.read(p);
aoqi@0 218 if (message instanceof MessageWritable)
aoqi@0 219 ((MessageWritable) message)
aoqi@0 220 .setMTOMConfiguration(p.getMtomFeature());
aoqi@0 221 if (inbound != null) transportHeaders(p, inbound, message.readAsSOAPMessage());
aoqi@0 222 return message;
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 /**
aoqi@0 226 * Default implementation that uses {@link #writeTo(ContentHandler, ErrorHandler)}
aoqi@0 227 */
aoqi@0 228 @Override
aoqi@0 229 public SOAPMessage readAsSOAPMessage() throws SOAPException {
aoqi@0 230 return SAAJFactory.read(soapVersion, this);
aoqi@0 231 }
aoqi@0 232
aoqi@0 233 @Override
aoqi@0 234 public SOAPMessage readAsSOAPMessage(Packet packet, boolean inbound) throws SOAPException {
aoqi@0 235 SOAPMessage msg = SAAJFactory.read(soapVersion, this, packet);
aoqi@0 236 transportHeaders(packet, inbound, msg);
aoqi@0 237 return msg;
aoqi@0 238 }
aoqi@0 239
aoqi@0 240 private void transportHeaders(Packet packet, boolean inbound, SOAPMessage msg) throws SOAPException {
aoqi@0 241 Map<String, List<String>> headers = getTransportHeaders(packet, inbound);
aoqi@0 242 if (headers != null) {
aoqi@0 243 addSOAPMimeHeaders(msg.getMimeHeaders(), headers);
aoqi@0 244 }
aoqi@0 245 if (msg.saveRequired()) msg.saveChanges();
aoqi@0 246 }
aoqi@0 247 }

mercurial