src/share/jaxws_classes/com/sun/xml/internal/ws/encoding/XMLHTTPBindingCodec.java

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

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
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.encoding;
ohair@286 27
ohair@286 28 import com.sun.xml.internal.ws.api.SOAPVersion;
ohair@286 29 import com.sun.xml.internal.ws.api.WSFeatureList;
ohair@286 30 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 31 import com.sun.xml.internal.ws.api.pipe.Codec;
ohair@286 32 import com.sun.xml.internal.ws.api.pipe.ContentType;
ohair@286 33 import com.sun.xml.internal.ws.client.ContentNegotiation;
ohair@286 34 import com.sun.xml.internal.ws.encoding.xml.XMLCodec;
ohair@286 35 import com.sun.xml.internal.ws.encoding.xml.XMLMessage;
ohair@286 36 import com.sun.xml.internal.ws.encoding.xml.XMLMessage.MessageDataSource;
ohair@286 37 import com.sun.xml.internal.ws.encoding.xml.XMLMessage.UnknownContent;
ohair@286 38 import com.sun.xml.internal.ws.encoding.xml.XMLMessage.XMLMultiPart;
ohair@286 39 import com.sun.xml.internal.ws.resources.StreamingMessages;
ohair@286 40 import com.sun.xml.internal.ws.util.ByteArrayBuffer;
ohair@286 41
ohair@286 42 import javax.activation.DataSource;
ohair@286 43 import javax.xml.ws.WebServiceException;
ohair@286 44
ohair@286 45 import java.io.IOException;
ohair@286 46 import java.io.InputStream;
ohair@286 47 import java.io.OutputStream;
ohair@286 48 import java.lang.reflect.Method;
ohair@286 49 import java.nio.channels.WritableByteChannel;
ohair@286 50 import java.util.StringTokenizer;
ohair@286 51
ohair@286 52 /**
ohair@286 53 * XML (infoset) over HTTP binding {@link Codec}.
ohair@286 54 * <p>
ohair@286 55 * TODO: Support FI for multipart/related
ohair@286 56 * Support FI for MessageDataSource
ohair@286 57 *
ohair@286 58 * @author Jitendra Kotamraju
ohair@286 59 */
ohair@286 60 public final class XMLHTTPBindingCodec extends MimeCodec {
ohair@286 61 /**
ohair@286 62 * Base HTTP Accept request-header.
ohair@286 63 */
ohair@286 64 private static final String BASE_ACCEPT_VALUE =
ohair@286 65 "*";
ohair@286 66
ohair@286 67 /**
ohair@286 68 * Fast Infoset MIME type.
ohair@286 69 */
ohair@286 70 private static final String APPLICATION_FAST_INFOSET_MIME_TYPE =
ohair@286 71 "application/fastinfoset";
ohair@286 72
ohair@286 73 /**
ohair@286 74 * True if the Fast Infoset codec should be used
ohair@286 75 */
ohair@286 76 private boolean useFastInfosetForEncoding;
ohair@286 77
ohair@286 78 /**
ohair@286 79 * The XML codec
ohair@286 80 */
ohair@286 81 private final Codec xmlCodec;
ohair@286 82
ohair@286 83 /**
ohair@286 84 * The FI codec
ohair@286 85 */
ohair@286 86 private final Codec fiCodec;
ohair@286 87
ohair@286 88 /**
ohair@286 89 * The Accept header for XML encodings
ohair@286 90 */
ohair@286 91 private static final String xmlAccept = null;
ohair@286 92
ohair@286 93 /**
ohair@286 94 * The Accept header for Fast Infoset and XML encodings
ohair@286 95 */
ohair@286 96 private static final String fiXmlAccept = APPLICATION_FAST_INFOSET_MIME_TYPE + ", " + BASE_ACCEPT_VALUE;
ohair@286 97
alanb@368 98 private ContentTypeImpl setAcceptHeader(Packet p, ContentType c) {
alanb@368 99 ContentTypeImpl ctImpl = (ContentTypeImpl)c;
alanb@368 100 if (p.contentNegotiation == ContentNegotiation.optimistic
alanb@368 101 || p.contentNegotiation == ContentNegotiation.pessimistic) {
alanb@368 102 ctImpl.setAcceptHeader(fiXmlAccept);
alanb@368 103 } else {
alanb@368 104 ctImpl.setAcceptHeader(xmlAccept);
ohair@286 105 }
alanb@368 106 p.setContentType(ctImpl);
alanb@368 107 return ctImpl;
ohair@286 108 }
ohair@286 109
ohair@286 110 public XMLHTTPBindingCodec(WSFeatureList f) {
ohair@286 111 super(SOAPVersion.SOAP_11, f);
ohair@286 112
ohair@286 113 xmlCodec = new XMLCodec(f);
ohair@286 114
ohair@286 115 fiCodec = getFICodec();
ohair@286 116 }
ohair@286 117
alanb@368 118 @Override
ohair@286 119 public String getMimeType() {
ohair@286 120 return null;
ohair@286 121 }
ohair@286 122
ohair@286 123 @Override
ohair@286 124 public ContentType getStaticContentType(Packet packet) {
alanb@368 125 ContentType ct;
alanb@368 126 if (packet.getInternalMessage() instanceof MessageDataSource) {
alanb@368 127 final MessageDataSource mds = (MessageDataSource)packet.getInternalMessage();
ohair@286 128 if (mds.hasUnconsumedDataSource()) {
ohair@286 129 ct = getStaticContentType(mds);
ohair@286 130 return (ct != null)
alanb@368 131 ? setAcceptHeader(packet, ct) //_adaptingContentType.set(packet, ct)
ohair@286 132 : null;
ohair@286 133 }
ohair@286 134 }
ohair@286 135
ohair@286 136 ct = super.getStaticContentType(packet);
ohair@286 137 return (ct != null)
alanb@368 138 ? setAcceptHeader(packet, ct) //_adaptingContentType.set(packet, ct)
ohair@286 139 : null;
ohair@286 140 }
ohair@286 141
ohair@286 142 @Override
ohair@286 143 public ContentType encode(Packet packet, OutputStream out) throws IOException {
alanb@368 144 if (packet.getInternalMessage() instanceof MessageDataSource) {
alanb@368 145 final MessageDataSource mds = (MessageDataSource)packet.getInternalMessage();
ohair@286 146 if (mds.hasUnconsumedDataSource())
alanb@368 147 return setAcceptHeader(packet, encode(mds, out));
ohair@286 148 }
ohair@286 149
alanb@368 150 return setAcceptHeader(packet, super.encode(packet, out));
ohair@286 151 }
ohair@286 152
alanb@368 153 @Override
ohair@286 154 public ContentType encode(Packet packet, WritableByteChannel buffer) {
ohair@286 155 throw new UnsupportedOperationException();
ohair@286 156 }
ohair@286 157
ohair@286 158 @Override
ohair@286 159 public void decode(InputStream in, String contentType, Packet packet) throws IOException {
ohair@286 160 /**
ohair@286 161 * Reset the encoding state when on the server side for each
ohair@286 162 * decode/encode step.
ohair@286 163 */
ohair@286 164 if (packet.contentNegotiation == null)
ohair@286 165 useFastInfosetForEncoding = false;
ohair@286 166
ohair@286 167 if (contentType == null) {
ohair@286 168 xmlCodec.decode(in, contentType, packet);
ohair@286 169 } else if (isMultipartRelated(contentType)) {
ohair@286 170 packet.setMessage(new XMLMultiPart(contentType, in, features));
ohair@286 171 } else if(isFastInfoset(contentType)) {
ohair@286 172 if (fiCodec == null) {
ohair@286 173 throw new RuntimeException(StreamingMessages.FASTINFOSET_NO_IMPLEMENTATION());
ohair@286 174 }
ohair@286 175
ohair@286 176 useFastInfosetForEncoding = true;
ohair@286 177 fiCodec.decode(in, contentType, packet);
ohair@286 178 } else if (isXml(contentType)) {
ohair@286 179 xmlCodec.decode(in, contentType, packet);
ohair@286 180 } else {
ohair@286 181 packet.setMessage(new UnknownContent(contentType, in));
ohair@286 182 }
ohair@286 183
ohair@286 184 if (!useFastInfosetForEncoding) {
ohair@286 185 useFastInfosetForEncoding = isFastInfosetAcceptable(packet.acceptableMimeTypes);
ohair@286 186 }
ohair@286 187 }
ohair@286 188
alanb@368 189 @Override
ohair@286 190 protected void decode(MimeMultipartParser mpp, Packet packet) throws IOException {
ohair@286 191 // This method will never be invoked
ohair@286 192 }
ohair@286 193
alanb@368 194 @Override
ohair@286 195 public MimeCodec copy() {
ohair@286 196 return new XMLHTTPBindingCodec(features);
ohair@286 197 }
ohair@286 198
ohair@286 199 private boolean isMultipartRelated(String contentType) {
ohair@286 200 return compareStrings(contentType, MimeCodec.MULTIPART_RELATED_MIME_TYPE);
ohair@286 201 }
ohair@286 202
ohair@286 203 private boolean isXml(String contentType) {
ohair@286 204 return compareStrings(contentType, XMLCodec.XML_APPLICATION_MIME_TYPE)
ohair@286 205 || compareStrings(contentType, XMLCodec.XML_TEXT_MIME_TYPE)
ohair@286 206 || (compareStrings(contentType, "application/")&&(contentType.toLowerCase().indexOf("+xml") != -1));
ohair@286 207 }
ohair@286 208
ohair@286 209 private boolean isFastInfoset(String contentType) {
ohair@286 210 return compareStrings(contentType, APPLICATION_FAST_INFOSET_MIME_TYPE);
ohair@286 211 }
ohair@286 212
ohair@286 213 private boolean compareStrings(String a, String b) {
ohair@286 214 return a.length() >= b.length() &&
ohair@286 215 b.equalsIgnoreCase(
ohair@286 216 a.substring(0,
ohair@286 217 b.length()));
ohair@286 218 }
ohair@286 219
ohair@286 220 private boolean isFastInfosetAcceptable(String accept) {
ohair@286 221 if (accept == null) return false;
ohair@286 222
ohair@286 223 StringTokenizer st = new StringTokenizer(accept, ",");
ohair@286 224 while (st.hasMoreTokens()) {
ohair@286 225 final String token = st.nextToken().trim();
ohair@286 226 if (token.equalsIgnoreCase(APPLICATION_FAST_INFOSET_MIME_TYPE)) {
ohair@286 227 return true;
ohair@286 228 }
ohair@286 229 }
ohair@286 230 return false;
ohair@286 231 }
ohair@286 232
ohair@286 233 private ContentType getStaticContentType(MessageDataSource mds) {
ohair@286 234 final String contentType = mds.getDataSource().getContentType();
ohair@286 235 final boolean isFastInfoset = XMLMessage.isFastInfoset(contentType);
ohair@286 236
ohair@286 237 if (!requiresTransformationOfDataSource(isFastInfoset,
ohair@286 238 useFastInfosetForEncoding)) {
ohair@286 239 return new ContentTypeImpl(contentType);
ohair@286 240 } else {
ohair@286 241 return null;
ohair@286 242 }
ohair@286 243 }
ohair@286 244
ohair@286 245 private ContentType encode(MessageDataSource mds, OutputStream out) {
ohair@286 246 try {
ohair@286 247 final boolean isFastInfoset = XMLMessage.isFastInfoset(
ohair@286 248 mds.getDataSource().getContentType());
ohair@286 249 DataSource ds = transformDataSource(mds.getDataSource(),
ohair@286 250 isFastInfoset, useFastInfosetForEncoding, features);
ohair@286 251
ohair@286 252 InputStream is = ds.getInputStream();
ohair@286 253 byte[] buf = new byte[1024];
ohair@286 254 int count;
ohair@286 255 while((count=is.read(buf)) != -1) {
ohair@286 256 out.write(buf, 0, count);
ohair@286 257 }
ohair@286 258 return new ContentTypeImpl(ds.getContentType());
ohair@286 259 } catch(IOException ioe) {
ohair@286 260 throw new WebServiceException(ioe);
ohair@286 261 }
ohair@286 262 }
ohair@286 263
alanb@368 264 @Override
alanb@368 265 protected Codec getMimeRootCodec(Packet p) {
ohair@286 266 /**
ohair@286 267 * The following logic is only for outbound packets
ohair@286 268 * to be encoded by client.
ohair@286 269 * On the server the p.contentNegotiation == null.
ohair@286 270 */
ohair@286 271 if (p.contentNegotiation == ContentNegotiation.none) {
ohair@286 272 // The client may have changed the negotiation property from
ohair@286 273 // pessismistic to none between invocations
ohair@286 274 useFastInfosetForEncoding = false;
ohair@286 275 } else if (p.contentNegotiation == ContentNegotiation.optimistic) {
ohair@286 276 // Always encode using Fast Infoset if in optimisitic mode
ohair@286 277 useFastInfosetForEncoding = true;
ohair@286 278 }
ohair@286 279
alanb@368 280 return (useFastInfosetForEncoding && fiCodec != null)? fiCodec : xmlCodec;
ohair@286 281 }
ohair@286 282
ohair@286 283 public static boolean requiresTransformationOfDataSource(
ohair@286 284 boolean isFastInfoset, boolean useFastInfoset) {
ohair@286 285 return (isFastInfoset && !useFastInfoset) || (!isFastInfoset && useFastInfoset);
ohair@286 286 }
ohair@286 287
ohair@286 288 public static DataSource transformDataSource(DataSource in,
ohair@286 289 boolean isFastInfoset, boolean useFastInfoset, WSFeatureList f) {
ohair@286 290 try {
ohair@286 291 if (isFastInfoset && !useFastInfoset) {
ohair@286 292 // Convert from Fast Infoset to XML
ohair@286 293 Codec codec = new XMLHTTPBindingCodec(f);
ohair@286 294 Packet p = new Packet();
ohair@286 295 codec.decode(in.getInputStream(), in.getContentType(), p);
ohair@286 296
ohair@286 297 p.getMessage().getAttachments();
ohair@286 298 codec.getStaticContentType(p);
ohair@286 299
ohair@286 300 ByteArrayBuffer bos = new ByteArrayBuffer();
ohair@286 301 ContentType ct = codec.encode(p, bos);
ohair@286 302 return XMLMessage.createDataSource(ct.getContentType(), bos.newInputStream());
ohair@286 303 } else if (!isFastInfoset && useFastInfoset) {
ohair@286 304 // Convert from XML to Fast Infoset
ohair@286 305 Codec codec = new XMLHTTPBindingCodec(f);
ohair@286 306 Packet p = new Packet();
ohair@286 307 codec.decode(in.getInputStream(), in.getContentType(), p);
ohair@286 308
ohair@286 309 p.contentNegotiation = ContentNegotiation.optimistic;
ohair@286 310 p.getMessage().getAttachments();
ohair@286 311 codec.getStaticContentType(p);
ohair@286 312
ohair@286 313 ByteArrayBuffer bos = new ByteArrayBuffer();
ohair@286 314 com.sun.xml.internal.ws.api.pipe.ContentType ct = codec.encode(p, bos);
ohair@286 315 return XMLMessage.createDataSource(ct.getContentType(), bos.newInputStream());
ohair@286 316 }
ohair@286 317 } catch(Exception ex) {
ohair@286 318 throw new WebServiceException(ex);
ohair@286 319 }
ohair@286 320
ohair@286 321 return in;
ohair@286 322 }
ohair@286 323
ohair@286 324 /**
ohair@286 325 * Obtain an FI SOAP codec instance using reflection.
ohair@286 326 */
ohair@286 327 private static Codec getFICodec() {
ohair@286 328 try {
ohair@286 329 Class c = Class.forName("com.sun.xml.internal.ws.encoding.fastinfoset.FastInfosetCodec");
ohair@286 330 Method m = c.getMethod("create");
ohair@286 331 return (Codec)m.invoke(null);
ohair@286 332 } catch (Exception e) {
ohair@286 333 return null;
ohair@286 334 }
ohair@286 335 }
ohair@286 336 }

mercurial