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

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 397
b99d7e355d4b
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

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

mercurial