Fri, 14 Feb 2014 11:13:45 +0100
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.encoding; |
ohair@286 | 27 | |
ohair@286 | 28 | import static com.sun.xml.internal.ws.binding.WebServiceFeatureList.getSoapVersion; |
ohair@286 | 29 | |
alanb@368 | 30 | import com.oracle.webservices.internal.impl.encoding.StreamDecoderImpl; |
alanb@368 | 31 | import com.oracle.webservices.internal.impl.internalspi.encoding.StreamDecoder; |
ohair@286 | 32 | import com.sun.istack.internal.NotNull; |
ohair@286 | 33 | import com.sun.istack.internal.Nullable; |
ohair@286 | 34 | import com.sun.xml.internal.stream.buffer.MutableXMLStreamBuffer; |
ohair@286 | 35 | import com.sun.xml.internal.stream.buffer.XMLStreamBuffer; |
ohair@286 | 36 | import com.sun.xml.internal.stream.buffer.XMLStreamBufferMark; |
ohair@286 | 37 | import com.sun.xml.internal.stream.buffer.stax.StreamReaderBufferCreator; |
ohair@286 | 38 | import com.sun.xml.internal.ws.api.SOAPVersion; |
ohair@286 | 39 | import com.sun.xml.internal.ws.api.WSBinding; |
ohair@286 | 40 | import com.sun.xml.internal.ws.api.WSFeatureList; |
ohair@286 | 41 | import com.sun.xml.internal.ws.api.message.AttachmentSet; |
alanb@368 | 42 | import com.sun.xml.internal.ws.api.message.Header; |
ohair@286 | 43 | import com.sun.xml.internal.ws.api.message.HeaderList; |
ohair@286 | 44 | import com.sun.xml.internal.ws.api.message.Message; |
ohair@286 | 45 | import com.sun.xml.internal.ws.api.message.Packet; |
ohair@286 | 46 | import com.sun.xml.internal.ws.api.pipe.ContentType; |
ohair@286 | 47 | import com.sun.xml.internal.ws.api.streaming.XMLStreamWriterFactory; |
ohair@286 | 48 | import com.sun.xml.internal.ws.developer.SerializationFeature; |
ohair@286 | 49 | import com.sun.xml.internal.ws.message.AttachmentSetImpl; |
ohair@286 | 50 | import com.sun.xml.internal.ws.message.stream.StreamMessage; |
ohair@286 | 51 | import com.sun.xml.internal.ws.protocol.soap.VersionMismatchException; |
ohair@286 | 52 | import com.sun.xml.internal.ws.server.UnsupportedMediaException; |
ohair@286 | 53 | import com.sun.xml.internal.ws.streaming.XMLStreamReaderUtil; |
alanb@368 | 54 | import com.sun.xml.internal.ws.util.ServiceFinder; |
ohair@286 | 55 | |
ohair@286 | 56 | import javax.xml.stream.XMLStreamConstants; |
ohair@286 | 57 | import javax.xml.stream.XMLStreamException; |
ohair@286 | 58 | import javax.xml.stream.XMLStreamReader; |
ohair@286 | 59 | import javax.xml.stream.XMLStreamWriter; |
ohair@286 | 60 | import javax.xml.ws.WebServiceException; |
ohair@286 | 61 | import java.io.IOException; |
ohair@286 | 62 | import java.io.InputStream; |
ohair@286 | 63 | import java.io.OutputStream; |
ohair@286 | 64 | import java.nio.channels.ReadableByteChannel; |
ohair@286 | 65 | import java.nio.channels.WritableByteChannel; |
ohair@286 | 66 | import java.nio.charset.Charset; |
ohair@286 | 67 | import java.util.HashMap; |
ohair@286 | 68 | import java.util.List; |
ohair@286 | 69 | import java.util.Map; |
ohair@286 | 70 | |
ohair@286 | 71 | /** |
ohair@286 | 72 | * A stream SOAP codec. |
ohair@286 | 73 | * |
ohair@286 | 74 | * @author Paul Sandoz |
ohair@286 | 75 | */ |
ohair@286 | 76 | @SuppressWarnings({"StringEquality"}) |
ohair@286 | 77 | public abstract class StreamSOAPCodec implements com.sun.xml.internal.ws.api.pipe.StreamSOAPCodec, RootOnlyCodec { |
ohair@286 | 78 | |
ohair@286 | 79 | private static final String SOAP_ENVELOPE = "Envelope"; |
ohair@286 | 80 | private static final String SOAP_HEADER = "Header"; |
ohair@286 | 81 | private static final String SOAP_BODY = "Body"; |
ohair@286 | 82 | |
ohair@286 | 83 | private final SOAPVersion soapVersion; |
ohair@286 | 84 | protected final SerializationFeature serializationFeature; |
ohair@286 | 85 | |
alanb@368 | 86 | private final StreamDecoder streamDecoder; |
alanb@368 | 87 | |
ohair@286 | 88 | // charset of last decoded message. Will be used for encoding server's |
ohair@286 | 89 | // response messages with the request message's encoding |
ohair@286 | 90 | // it will stored in the packet.invocationProperties |
ohair@286 | 91 | private final static String DECODED_MESSAGE_CHARSET = "decodedMessageCharset"; |
ohair@286 | 92 | |
ohair@286 | 93 | /*package*/ StreamSOAPCodec(SOAPVersion soapVersion) { |
ohair@286 | 94 | this(soapVersion, null); |
ohair@286 | 95 | } |
ohair@286 | 96 | |
ohair@286 | 97 | /*package*/ StreamSOAPCodec(WSBinding binding) { |
ohair@286 | 98 | this(binding.getSOAPVersion(), binding.getFeature(SerializationFeature.class)); |
ohair@286 | 99 | } |
ohair@286 | 100 | |
ohair@286 | 101 | StreamSOAPCodec(WSFeatureList features) { |
ohair@286 | 102 | this(getSoapVersion(features), features.get(SerializationFeature.class)); |
ohair@286 | 103 | } |
ohair@286 | 104 | |
ohair@286 | 105 | private StreamSOAPCodec(SOAPVersion soapVersion, @Nullable SerializationFeature sf) { |
ohair@286 | 106 | this.soapVersion = soapVersion; |
ohair@286 | 107 | this.serializationFeature = sf; |
alanb@368 | 108 | this.streamDecoder = selectStreamDecoder(); |
alanb@368 | 109 | } |
alanb@368 | 110 | |
alanb@368 | 111 | private StreamDecoder selectStreamDecoder() { |
alanb@368 | 112 | for (StreamDecoder sd : ServiceFinder.find(StreamDecoder.class)) { |
alanb@368 | 113 | return sd; |
alanb@368 | 114 | } |
alanb@368 | 115 | |
alanb@368 | 116 | return new StreamDecoderImpl(); |
ohair@286 | 117 | } |
ohair@286 | 118 | |
ohair@286 | 119 | public ContentType getStaticContentType(Packet packet) { |
ohair@286 | 120 | return getContentType(packet); |
ohair@286 | 121 | } |
ohair@286 | 122 | |
ohair@286 | 123 | public ContentType encode(Packet packet, OutputStream out) { |
ohair@286 | 124 | if (packet.getMessage() != null) { |
ohair@286 | 125 | String encoding = getPacketEncoding(packet); |
ohair@286 | 126 | packet.invocationProperties.remove(DECODED_MESSAGE_CHARSET); |
ohair@286 | 127 | XMLStreamWriter writer = XMLStreamWriterFactory.create(out, encoding); |
ohair@286 | 128 | try { |
ohair@286 | 129 | packet.getMessage().writeTo(writer); |
ohair@286 | 130 | writer.flush(); |
ohair@286 | 131 | } catch (XMLStreamException e) { |
ohair@286 | 132 | throw new WebServiceException(e); |
ohair@286 | 133 | } |
ohair@286 | 134 | XMLStreamWriterFactory.recycle(writer); |
ohair@286 | 135 | } |
ohair@286 | 136 | return getContentType(packet); |
ohair@286 | 137 | } |
ohair@286 | 138 | |
ohair@286 | 139 | protected abstract ContentType getContentType(Packet packet); |
ohair@286 | 140 | |
ohair@286 | 141 | protected abstract String getDefaultContentType(); |
ohair@286 | 142 | |
ohair@286 | 143 | public ContentType encode(Packet packet, WritableByteChannel buffer) { |
ohair@286 | 144 | //TODO: not yet implemented |
ohair@286 | 145 | throw new UnsupportedOperationException(); |
ohair@286 | 146 | } |
ohair@286 | 147 | |
ohair@286 | 148 | protected abstract List<String> getExpectedContentTypes(); |
ohair@286 | 149 | |
ohair@286 | 150 | public void decode(InputStream in, String contentType, Packet packet) throws IOException { |
ohair@286 | 151 | decode(in, contentType, packet, new AttachmentSetImpl()); |
ohair@286 | 152 | } |
ohair@286 | 153 | |
ohair@286 | 154 | /* |
ohair@286 | 155 | * Checks against expected Content-Type headers that is handled by a codec |
ohair@286 | 156 | * |
ohair@286 | 157 | * @param ct the Content-Type of the request |
ohair@286 | 158 | * @param expected expected Content-Types for a codec |
ohair@286 | 159 | * @return true if the codec supports this Content-Type |
ohair@286 | 160 | * false otherwise |
ohair@286 | 161 | */ |
ohair@286 | 162 | private static boolean isContentTypeSupported(String ct, List<String> expected) { |
ohair@286 | 163 | for(String contentType : expected) { |
ohair@286 | 164 | if (ct.contains(contentType)) { |
ohair@286 | 165 | return true; |
ohair@286 | 166 | } |
ohair@286 | 167 | } |
ohair@286 | 168 | return false; |
ohair@286 | 169 | } |
ohair@286 | 170 | |
ohair@286 | 171 | /** |
ohair@286 | 172 | * Decodes a message from {@link XMLStreamReader} that points to |
ohair@286 | 173 | * the beginning of a SOAP infoset. |
ohair@286 | 174 | * |
ohair@286 | 175 | * @param reader |
ohair@286 | 176 | * can point to the start document or the start element. |
ohair@286 | 177 | */ |
ohair@286 | 178 | public final @NotNull Message decode(@NotNull XMLStreamReader reader) { |
ohair@286 | 179 | return decode(reader,new AttachmentSetImpl()); |
ohair@286 | 180 | } |
ohair@286 | 181 | |
ohair@286 | 182 | /** |
ohair@286 | 183 | * Decodes a message from {@link XMLStreamReader} that points to |
ohair@286 | 184 | * the beginning of a SOAP infoset. |
ohair@286 | 185 | * |
ohair@286 | 186 | * @param reader |
ohair@286 | 187 | * can point to the start document or the start element. |
ohair@286 | 188 | * @param attachmentSet |
ohair@286 | 189 | * {@link StreamSOAPCodec} can take attachments parsed outside, |
ohair@286 | 190 | * so that this codec can be used as a part of a biggre codec |
ohair@286 | 191 | * (like MIME multipart codec.) |
ohair@286 | 192 | */ |
ohair@286 | 193 | public final Message decode(XMLStreamReader reader, @NotNull AttachmentSet attachmentSet) { |
alanb@368 | 194 | return decode(soapVersion, reader, attachmentSet); |
alanb@368 | 195 | } |
alanb@368 | 196 | |
mkos@397 | 197 | public static final Message decode(SOAPVersion soapVersion, XMLStreamReader reader, @NotNull AttachmentSet attachmentSet) { |
ohair@286 | 198 | // Move to soap:Envelope and verify |
ohair@286 | 199 | if(reader.getEventType()!=XMLStreamConstants.START_ELEMENT) |
ohair@286 | 200 | XMLStreamReaderUtil.nextElementContent(reader); |
ohair@286 | 201 | XMLStreamReaderUtil.verifyReaderState(reader,XMLStreamConstants.START_ELEMENT); |
alanb@368 | 202 | if (SOAP_ENVELOPE.equals(reader.getLocalName()) && !soapVersion.nsUri.equals(reader.getNamespaceURI())) { |
alanb@368 | 203 | throw new VersionMismatchException(soapVersion, soapVersion.nsUri, reader.getNamespaceURI()); |
ohair@286 | 204 | } |
alanb@368 | 205 | XMLStreamReaderUtil.verifyTag(reader, soapVersion.nsUri, SOAP_ENVELOPE); |
mkos@397 | 206 | return new StreamMessage(soapVersion, reader, attachmentSet); |
ohair@286 | 207 | } |
ohair@286 | 208 | |
ohair@286 | 209 | public void decode(ReadableByteChannel in, String contentType, Packet packet ) { |
ohair@286 | 210 | throw new UnsupportedOperationException(); |
ohair@286 | 211 | } |
ohair@286 | 212 | |
ohair@286 | 213 | public final StreamSOAPCodec copy() { |
ohair@286 | 214 | return this; |
ohair@286 | 215 | } |
ohair@286 | 216 | |
ohair@286 | 217 | public void decode(InputStream in, String contentType, Packet packet, AttachmentSet att ) throws IOException { |
ohair@286 | 218 | List<String> expectedContentTypes = getExpectedContentTypes(); |
ohair@286 | 219 | if (contentType != null && !isContentTypeSupported(contentType,expectedContentTypes)) { |
ohair@286 | 220 | throw new UnsupportedMediaException(contentType, expectedContentTypes); |
ohair@286 | 221 | } |
alanb@368 | 222 | com.oracle.webservices.internal.api.message.ContentType pct = packet.getInternalContentType(); |
alanb@368 | 223 | ContentTypeImpl cti = (pct != null && pct instanceof ContentTypeImpl) ? |
alanb@368 | 224 | (ContentTypeImpl)pct : new ContentTypeImpl(contentType); |
alanb@368 | 225 | String charset = cti.getCharSet(); |
ohair@286 | 226 | if (charset != null && !Charset.isSupported(charset)) { |
ohair@286 | 227 | throw new UnsupportedMediaException(charset); |
ohair@286 | 228 | } |
ohair@286 | 229 | if (charset != null) { |
ohair@286 | 230 | packet.invocationProperties.put(DECODED_MESSAGE_CHARSET, charset); |
ohair@286 | 231 | } else { |
ohair@286 | 232 | packet.invocationProperties.remove(DECODED_MESSAGE_CHARSET); |
ohair@286 | 233 | } |
alanb@368 | 234 | packet.setMessage(streamDecoder.decode(in, charset, att, soapVersion)); |
ohair@286 | 235 | } |
ohair@286 | 236 | |
ohair@286 | 237 | public void decode(ReadableByteChannel in, String contentType, Packet response, AttachmentSet att ) { |
ohair@286 | 238 | throw new UnsupportedOperationException(); |
ohair@286 | 239 | } |
ohair@286 | 240 | |
ohair@286 | 241 | /* |
ohair@286 | 242 | * Creates a new {@link StreamSOAPCodec} instance. |
ohair@286 | 243 | */ |
ohair@286 | 244 | public static StreamSOAPCodec create(SOAPVersion version) { |
ohair@286 | 245 | if(version==null) |
ohair@286 | 246 | // this decoder is for SOAP, not for XML/HTTP |
ohair@286 | 247 | throw new IllegalArgumentException(); |
ohair@286 | 248 | switch(version) { |
ohair@286 | 249 | case SOAP_11: |
ohair@286 | 250 | return new StreamSOAP11Codec(); |
ohair@286 | 251 | case SOAP_12: |
ohair@286 | 252 | return new StreamSOAP12Codec(); |
ohair@286 | 253 | default: |
ohair@286 | 254 | throw new AssertionError(); |
ohair@286 | 255 | } |
ohair@286 | 256 | } |
ohair@286 | 257 | |
ohair@286 | 258 | /* |
ohair@286 | 259 | * Creates a new {@link StreamSOAPCodec} instance using binding |
ohair@286 | 260 | */ |
ohair@286 | 261 | public static StreamSOAPCodec create(WSFeatureList features) { |
ohair@286 | 262 | SOAPVersion version = getSoapVersion(features); |
ohair@286 | 263 | if(version==null) |
ohair@286 | 264 | // this decoder is for SOAP, not for XML/HTTP |
ohair@286 | 265 | throw new IllegalArgumentException(); |
ohair@286 | 266 | switch(version) { |
ohair@286 | 267 | case SOAP_11: |
ohair@286 | 268 | return new StreamSOAP11Codec(features); |
ohair@286 | 269 | case SOAP_12: |
ohair@286 | 270 | return new StreamSOAP12Codec(features); |
ohair@286 | 271 | default: |
ohair@286 | 272 | throw new AssertionError(); |
ohair@286 | 273 | } |
ohair@286 | 274 | } |
ohair@286 | 275 | |
ohair@286 | 276 | /** |
ohair@286 | 277 | * Creates a new {@link StreamSOAPCodec} instance using binding |
ohair@286 | 278 | * |
ohair@286 | 279 | * @deprecated use {@link #create(WSFeatureList)} |
ohair@286 | 280 | */ |
ohair@286 | 281 | public static StreamSOAPCodec create(WSBinding binding) { |
ohair@286 | 282 | SOAPVersion version = binding.getSOAPVersion(); |
ohair@286 | 283 | if(version==null) |
ohair@286 | 284 | // this decoder is for SOAP, not for XML/HTTP |
ohair@286 | 285 | throw new IllegalArgumentException(); |
ohair@286 | 286 | switch(version) { |
ohair@286 | 287 | case SOAP_11: |
ohair@286 | 288 | return new StreamSOAP11Codec(binding); |
ohair@286 | 289 | case SOAP_12: |
ohair@286 | 290 | return new StreamSOAP12Codec(binding); |
ohair@286 | 291 | default: |
ohair@286 | 292 | throw new AssertionError(); |
ohair@286 | 293 | } |
ohair@286 | 294 | } |
ohair@286 | 295 | |
ohair@286 | 296 | private String getPacketEncoding(Packet packet) { |
ohair@286 | 297 | // If SerializationFeature is set, just use that encoding |
ohair@286 | 298 | if (serializationFeature != null && serializationFeature.getEncoding() != null) { |
ohair@286 | 299 | return serializationFeature.getEncoding().equals("") |
ohair@286 | 300 | ? SOAPBindingCodec.DEFAULT_ENCODING : serializationFeature.getEncoding(); |
ohair@286 | 301 | } |
ohair@286 | 302 | |
ohair@286 | 303 | if (packet != null && packet.endpoint != null) { |
ohair@286 | 304 | // Use request message's encoding for Server-side response messages |
ohair@286 | 305 | String charset = (String)packet.invocationProperties.get(DECODED_MESSAGE_CHARSET); |
ohair@286 | 306 | return charset == null |
ohair@286 | 307 | ? SOAPBindingCodec.DEFAULT_ENCODING : charset; |
ohair@286 | 308 | } |
ohair@286 | 309 | |
ohair@286 | 310 | // Use default encoding for client-side request messages |
ohair@286 | 311 | return SOAPBindingCodec.DEFAULT_ENCODING; |
ohair@286 | 312 | } |
ohair@286 | 313 | |
ohair@286 | 314 | protected ContentTypeImpl.Builder getContenTypeBuilder(Packet packet) { |
ohair@286 | 315 | ContentTypeImpl.Builder b = new ContentTypeImpl.Builder(); |
ohair@286 | 316 | String encoding = getPacketEncoding(packet); |
ohair@286 | 317 | if (SOAPBindingCodec.DEFAULT_ENCODING.equalsIgnoreCase(encoding)) { |
ohair@286 | 318 | b.contentType = getDefaultContentType(); |
ohair@286 | 319 | b.charset = SOAPBindingCodec.DEFAULT_ENCODING; |
ohair@286 | 320 | return b; |
ohair@286 | 321 | } |
ohair@286 | 322 | b.contentType = getMimeType()+" ;charset="+encoding; |
ohair@286 | 323 | b.charset = encoding; |
ohair@286 | 324 | return b; |
ohair@286 | 325 | } |
ohair@286 | 326 | |
ohair@286 | 327 | } |