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

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

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

mercurial