1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/encoding/SOAPBindingCodec.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,529 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.ws.encoding; 1.30 + 1.31 +import com.sun.xml.internal.ws.api.SOAPVersion; 1.32 +import com.sun.xml.internal.ws.api.WSFeatureList; 1.33 +import com.sun.xml.internal.ws.api.client.SelectOptimalEncodingFeature; 1.34 +import com.sun.xml.internal.ws.api.fastinfoset.FastInfosetFeature; 1.35 +import com.sun.xml.internal.ws.api.message.Message; 1.36 +import com.sun.xml.internal.ws.api.message.Packet; 1.37 +import com.sun.xml.internal.ws.api.message.ExceptionHasMessage; 1.38 +import com.sun.xml.internal.ws.api.pipe.Codec; 1.39 +import com.sun.xml.internal.ws.api.pipe.Codecs; 1.40 +import com.sun.xml.internal.ws.api.pipe.ContentType; 1.41 +import com.sun.xml.internal.ws.api.pipe.StreamSOAPCodec; 1.42 +import com.sun.xml.internal.ws.client.ContentNegotiation; 1.43 +import com.sun.xml.internal.ws.protocol.soap.MessageCreationException; 1.44 +import com.sun.xml.internal.ws.resources.StreamingMessages; 1.45 +import com.sun.xml.internal.ws.server.UnsupportedMediaException; 1.46 +import static com.sun.xml.internal.ws.binding.WebServiceFeatureList.getSoapVersion; 1.47 + 1.48 +import javax.xml.ws.WebServiceException; 1.49 +import javax.xml.ws.WebServiceFeature; 1.50 +import javax.xml.ws.soap.MTOMFeature; 1.51 +import java.io.IOException; 1.52 +import java.io.InputStream; 1.53 +import java.io.OutputStream; 1.54 +import java.lang.reflect.Method; 1.55 +import java.nio.channels.ReadableByteChannel; 1.56 +import java.nio.channels.WritableByteChannel; 1.57 +import java.util.StringTokenizer; 1.58 + 1.59 +/** 1.60 + * SOAP binding {@link Codec} that can handle MTOM, SwA, and SOAP messages 1.61 + * encoded using XML or Fast Infoset. 1.62 + * 1.63 + * <p> 1.64 + * This is used when we need to determine the encoding from what we received (for decoding) 1.65 + * and from configuration and {@link Message} contents (for encoding) 1.66 + * 1.67 + * <p> 1.68 + * TODO: Split this Codec into two, one that supports FI and one that does not. 1.69 + * Then further split the FI Codec into two, one for client and one for 1.70 + * server. This will simplify the logic and make it easier to understand/maintain. 1.71 + * 1.72 + * @author Vivek Pandey 1.73 + * @author Kohsuke Kawaguchi 1.74 + */ 1.75 +public class SOAPBindingCodec extends MimeCodec implements com.sun.xml.internal.ws.api.pipe.SOAPBindingCodec { 1.76 + 1.77 + public static final String UTF8_ENCODING = "utf-8"; 1.78 + public static final String DEFAULT_ENCODING = UTF8_ENCODING; 1.79 + 1.80 + /** 1.81 + * Based on request's Accept header this is set. 1.82 + * Currently only set if MTOMFeature is enabled. 1.83 + * 1.84 + * Should be used on server-side, for encoding the response. 1.85 + */ 1.86 + private boolean acceptMtomMessages; 1.87 + 1.88 + /** 1.89 + * If the request's Content-Type is multipart/related; type=application/xop+xml, then this set to to true 1.90 + * 1.91 + * Used on server-side, for encoding the repsonse. 1.92 + */ 1.93 + private boolean isRequestMtomMessage; 1.94 + 1.95 + private enum TriState {UNSET,TRUE,FALSE} 1.96 + 1.97 + /** 1.98 + * This captures is decode is called before encode, 1.99 + * if true, infers that this is being used on Server-side 1.100 + */ 1.101 + private TriState decodeFirst = TriState.UNSET; 1.102 + 1.103 + /** 1.104 + * True if Fast Infoset functionality has been 1.105 + * configured to be disabled, or the Fast Infoset 1.106 + * runtime is not available. 1.107 + */ 1.108 + private boolean isFastInfosetDisabled; 1.109 + 1.110 + /** 1.111 + * True if the Fast Infoset codec should be used for encoding. 1.112 + */ 1.113 + private boolean useFastInfosetForEncoding; 1.114 + 1.115 + /** 1.116 + * True if the content negotiation property should 1.117 + * be ignored by the client. This will be used in 1.118 + * the case of Fast Infoset being configured to be 1.119 + * disabled or automatically selected. 1.120 + */ 1.121 + private boolean ignoreContentNegotiationProperty; 1.122 + 1.123 + // The XML SOAP codec 1.124 + private final StreamSOAPCodec xmlSoapCodec; 1.125 + 1.126 + // The Fast Infoset SOAP codec 1.127 + private final Codec fiSoapCodec; 1.128 + 1.129 + // The XML MTOM codec 1.130 + private final MimeCodec xmlMtomCodec; 1.131 + 1.132 + // The XML SWA codec 1.133 + private final MimeCodec xmlSwaCodec; 1.134 + 1.135 + // The Fast Infoset SWA codec 1.136 + private final MimeCodec fiSwaCodec; 1.137 + 1.138 +// private final SOAPBindingImpl binding; 1.139 +// private final WebServiceFeature[] feature; 1.140 + 1.141 + /** 1.142 + * The XML SOAP MIME type 1.143 + */ 1.144 + private final String xmlMimeType; 1.145 + 1.146 + /** 1.147 + * The Fast Infoset SOAP MIME type 1.148 + */ 1.149 + private final String fiMimeType; 1.150 + 1.151 + /** 1.152 + * The Accept header for XML encodings 1.153 + */ 1.154 + private final String xmlAccept; 1.155 + 1.156 + /** 1.157 + * The Accept header for Fast Infoset and XML encodings 1.158 + */ 1.159 + private final String connegXmlAccept; 1.160 + 1.161 + public StreamSOAPCodec getXMLCodec() { 1.162 + return xmlSoapCodec; 1.163 + } 1.164 + 1.165 + private class AcceptContentType implements ContentType { 1.166 + private ContentType _c; 1.167 + private String _accept; 1.168 + 1.169 + public AcceptContentType set(Packet p, ContentType c) { 1.170 + if (!ignoreContentNegotiationProperty && p.contentNegotiation != ContentNegotiation.none) { 1.171 + _accept = connegXmlAccept; 1.172 + } else { 1.173 + _accept = xmlAccept; 1.174 + } 1.175 + _c = c; 1.176 + return this; 1.177 + } 1.178 + 1.179 + public String getContentType() { 1.180 + return _c.getContentType(); 1.181 + } 1.182 + 1.183 + public String getSOAPActionHeader() { 1.184 + return _c.getSOAPActionHeader(); 1.185 + } 1.186 + 1.187 + public String getAcceptHeader() { 1.188 + return _accept; 1.189 + } 1.190 + } 1.191 + 1.192 + private AcceptContentType _adaptingContentType = new AcceptContentType(); 1.193 + 1.194 + public SOAPBindingCodec(WSFeatureList features) { 1.195 + this(features, Codecs.createSOAPEnvelopeXmlCodec(features)); 1.196 + } 1.197 + 1.198 + public SOAPBindingCodec(WSFeatureList features, StreamSOAPCodec xmlSoapCodec) { 1.199 + super(getSoapVersion(features), features); 1.200 + 1.201 + this.xmlSoapCodec = xmlSoapCodec; 1.202 + xmlMimeType = xmlSoapCodec.getMimeType(); 1.203 + 1.204 + xmlMtomCodec = new MtomCodec(version, xmlSoapCodec, features); 1.205 + 1.206 + xmlSwaCodec = new SwACodec(version, features, xmlSoapCodec); 1.207 + 1.208 + String clientAcceptedContentTypes = xmlSoapCodec.getMimeType() + ", " + 1.209 + xmlMtomCodec.getMimeType(); 1.210 + 1.211 + WebServiceFeature fi = features.get(FastInfosetFeature.class); 1.212 + isFastInfosetDisabled = (fi != null && !fi.isEnabled()); 1.213 + if (!isFastInfosetDisabled) { 1.214 + fiSoapCodec = getFICodec(xmlSoapCodec, version); 1.215 + if (fiSoapCodec != null) { 1.216 + fiMimeType = fiSoapCodec.getMimeType(); 1.217 + fiSwaCodec = new SwACodec(version, features, fiSoapCodec); 1.218 + connegXmlAccept = fiMimeType + ", " + clientAcceptedContentTypes; 1.219 + 1.220 + /** 1.221 + * This feature will only be present on the client side. 1.222 + * 1.223 + * Fast Infoset is enabled on the client if the service 1.224 + * explicitly supports Fast Infoset. 1.225 + */ 1.226 + WebServiceFeature select = features.get(SelectOptimalEncodingFeature.class); 1.227 + if (select != null) { // if the client FI feature is set - ignore negotiation property 1.228 + ignoreContentNegotiationProperty = true; 1.229 + if (select.isEnabled()) { 1.230 + // If the client's FI encoding feature is enabled, and server's is not disabled 1.231 + if (fi != null) { // if server's FI feature also enabled 1.232 + useFastInfosetForEncoding = true; 1.233 + } 1.234 + 1.235 + clientAcceptedContentTypes = connegXmlAccept; 1.236 + } else { // If client FI feature is disabled 1.237 + isFastInfosetDisabled = true; 1.238 + } 1.239 + } 1.240 + } else { 1.241 + // Fast Infoset could not be loaded by the runtime 1.242 + isFastInfosetDisabled = true; 1.243 + fiSwaCodec = null; 1.244 + fiMimeType = ""; 1.245 + connegXmlAccept = clientAcceptedContentTypes; 1.246 + ignoreContentNegotiationProperty = true; 1.247 + } 1.248 + } else { 1.249 + // Fast Infoset is explicitly not supported by the service 1.250 + fiSoapCodec = fiSwaCodec = null; 1.251 + fiMimeType = ""; 1.252 + connegXmlAccept = clientAcceptedContentTypes; 1.253 + ignoreContentNegotiationProperty = true; 1.254 + } 1.255 + 1.256 + xmlAccept = clientAcceptedContentTypes; 1.257 + 1.258 +// if(!(binding instanceof SOAPBindingImpl)) 1.259 + if(getSoapVersion(features) == null) 1.260 + throw new WebServiceException("Expecting a SOAP binding but found "); 1.261 +// this.binding = (SOAPBindingImpl)binding; 1.262 + } 1.263 + 1.264 + public String getMimeType() { 1.265 + return null; 1.266 + } 1.267 + 1.268 + public ContentType getStaticContentType(Packet packet) { 1.269 + ContentType toAdapt = getEncoder(packet).getStaticContentType(packet); 1.270 + return (toAdapt != null) ? _adaptingContentType.set(packet, toAdapt) : null; 1.271 + } 1.272 + 1.273 + public ContentType encode(Packet packet, OutputStream out) throws IOException { 1.274 + preEncode(packet); 1.275 + ContentType ct = _adaptingContentType.set(packet, getEncoder(packet).encode(packet, out)); 1.276 + postEncode(); 1.277 + return ct; 1.278 + } 1.279 + 1.280 + public ContentType encode(Packet packet, WritableByteChannel buffer) { 1.281 + preEncode(packet); 1.282 + ContentType ct = _adaptingContentType.set(packet, getEncoder(packet).encode(packet, buffer)); 1.283 + postEncode(); 1.284 + return ct; 1.285 + } 1.286 + 1.287 + /** 1.288 + * Should be called before encode(). 1.289 + * Set the state so that such state is used by encode process. 1.290 + */ 1.291 + private void preEncode(Packet p) { 1.292 + if (decodeFirst == TriState.UNSET) 1.293 + decodeFirst = TriState.FALSE; 1.294 + } 1.295 + 1.296 + /** 1.297 + * Should be called after encode() 1.298 + * Reset the encoding state. 1.299 + */ 1.300 + private void postEncode() { 1.301 + decodeFirst = TriState.UNSET; 1.302 + acceptMtomMessages = false; 1.303 + isRequestMtomMessage = false; 1.304 + } 1.305 + 1.306 + /** 1.307 + * Should be called before decode(). 1.308 + * Set the state so that such state is used by decode(). 1.309 + */ 1.310 + private void preDecode(Packet p) { 1.311 + if (p.contentNegotiation == null) 1.312 + useFastInfosetForEncoding = false; 1.313 + } 1.314 + 1.315 + /** 1.316 + * Should be called after decode(). 1.317 + * Set the state so that such state is used by encode(). 1.318 + */ 1.319 + private void postDecode(Packet p) { 1.320 + if(decodeFirst == TriState.UNSET) 1.321 + decodeFirst = TriState.TRUE; 1.322 + if(features.isEnabled(MTOMFeature.class)) 1.323 + acceptMtomMessages =isMtomAcceptable(p.acceptableMimeTypes); 1.324 + if (!useFastInfosetForEncoding) { 1.325 + useFastInfosetForEncoding = isFastInfosetAcceptable(p.acceptableMimeTypes); 1.326 + } 1.327 + } 1.328 + 1.329 + 1.330 + private boolean isServerSide() { 1.331 + return decodeFirst == TriState.TRUE; 1.332 + } 1.333 + 1.334 + public void decode(InputStream in, String contentType, Packet packet) throws IOException { 1.335 + if (contentType == null) { 1.336 + contentType = xmlMimeType; 1.337 + } 1.338 + 1.339 + preDecode(packet); 1.340 + try { 1.341 + if(isMultipartRelated(contentType)) 1.342 + // parse the multipart portion and then decide whether it's MTOM or SwA 1.343 + super.decode(in, contentType, packet); 1.344 + else if(isFastInfoset(contentType)) { 1.345 + if (!ignoreContentNegotiationProperty && packet.contentNegotiation == ContentNegotiation.none) 1.346 + throw noFastInfosetForDecoding(); 1.347 + 1.348 + useFastInfosetForEncoding = true; 1.349 + fiSoapCodec.decode(in, contentType, packet); 1.350 + } else 1.351 + xmlSoapCodec.decode(in, contentType, packet); 1.352 + } catch(RuntimeException we) { 1.353 + if (we instanceof ExceptionHasMessage || we instanceof UnsupportedMediaException) { 1.354 + throw we; 1.355 + } else { 1.356 + throw new MessageCreationException(version, we); 1.357 + } 1.358 + } 1.359 + postDecode(packet); 1.360 + } 1.361 + 1.362 + public void decode(ReadableByteChannel in, String contentType, Packet packet) { 1.363 + if (contentType == null) { 1.364 + throw new UnsupportedMediaException(); 1.365 + } 1.366 + 1.367 + preDecode(packet); 1.368 + try { 1.369 + if(isMultipartRelated(contentType)) 1.370 + super.decode(in, contentType, packet); 1.371 + else if(isFastInfoset(contentType)) { 1.372 + if (packet.contentNegotiation == ContentNegotiation.none) 1.373 + throw noFastInfosetForDecoding(); 1.374 + 1.375 + useFastInfosetForEncoding = true; 1.376 + fiSoapCodec.decode(in, contentType, packet); 1.377 + } else 1.378 + xmlSoapCodec.decode(in, contentType, packet); 1.379 + } catch(RuntimeException we) { 1.380 + if (we instanceof ExceptionHasMessage || we instanceof UnsupportedMediaException) { 1.381 + throw we; 1.382 + } else { 1.383 + throw new MessageCreationException(version, we); 1.384 + } 1.385 + } 1.386 + postDecode(packet); 1.387 + } 1.388 + 1.389 + public SOAPBindingCodec copy() { 1.390 + return new SOAPBindingCodec(features, (StreamSOAPCodec)xmlSoapCodec.copy()); 1.391 + } 1.392 + 1.393 + @Override 1.394 + protected void decode(MimeMultipartParser mpp, Packet packet) throws IOException { 1.395 + // is this SwA or XOP? 1.396 + final String rootContentType = mpp.getRootPart().getContentType(); 1.397 + 1.398 + if(isApplicationXopXml(rootContentType)) { 1.399 + isRequestMtomMessage = true; 1.400 + xmlMtomCodec.decode(mpp,packet); 1.401 + } else if (isFastInfoset(rootContentType)) { 1.402 + if (packet.contentNegotiation == ContentNegotiation.none) 1.403 + throw noFastInfosetForDecoding(); 1.404 + 1.405 + useFastInfosetForEncoding = true; 1.406 + fiSwaCodec.decode(mpp,packet); 1.407 + } else if (isXml(rootContentType)) 1.408 + xmlSwaCodec.decode(mpp,packet); 1.409 + else { 1.410 + // TODO localize exception 1.411 + throw new IOException(""); 1.412 + } 1.413 +// checkDuplicateKnownHeaders(packet); 1.414 + } 1.415 + 1.416 + private boolean isMultipartRelated(String contentType) { 1.417 + return compareStrings(contentType, MimeCodec.MULTIPART_RELATED_MIME_TYPE); 1.418 + } 1.419 + 1.420 + private boolean isApplicationXopXml(String contentType) { 1.421 + return compareStrings(contentType, MtomCodec.XOP_XML_MIME_TYPE); 1.422 + } 1.423 + 1.424 + private boolean isXml(String contentType) { 1.425 + return compareStrings(contentType, xmlMimeType); 1.426 + } 1.427 + 1.428 + private boolean isFastInfoset(String contentType) { 1.429 + if (isFastInfosetDisabled) return false; 1.430 + 1.431 + return compareStrings(contentType, fiMimeType); 1.432 + } 1.433 + 1.434 + private boolean compareStrings(String a, String b) { 1.435 + return a.length() >= b.length() && 1.436 + b.equalsIgnoreCase( 1.437 + a.substring(0, 1.438 + b.length())); 1.439 + } 1.440 + 1.441 + private boolean isFastInfosetAcceptable(String accept) { 1.442 + if (accept == null || isFastInfosetDisabled) return false; 1.443 + 1.444 + StringTokenizer st = new StringTokenizer(accept, ","); 1.445 + while (st.hasMoreTokens()) { 1.446 + final String token = st.nextToken().trim(); 1.447 + if (token.equalsIgnoreCase(fiMimeType)) { 1.448 + return true; 1.449 + } 1.450 + } 1.451 + return false; 1.452 + } 1.453 + 1.454 + /* 1.455 + * Just check if the Accept header contains application/xop+xml, 1.456 + * no need to worry about q values. 1.457 + */ 1.458 + private boolean isMtomAcceptable(String accept) { 1.459 + if (accept == null || isFastInfosetDisabled) return false; 1.460 + 1.461 + StringTokenizer st = new StringTokenizer(accept, ","); 1.462 + while (st.hasMoreTokens()) { 1.463 + final String token = st.nextToken().trim(); 1.464 + if (token.toLowerCase().contains(MtomCodec.XOP_XML_MIME_TYPE)) { 1.465 + return true; 1.466 + } 1.467 + } 1.468 + return false; 1.469 + } 1.470 + 1.471 + /** 1.472 + * Determines the encoding codec. 1.473 + */ 1.474 + private Codec getEncoder(Packet p) { 1.475 + /** 1.476 + * The following logic is only for outbound packets 1.477 + * to be encoded by a client. 1.478 + * For a server the p.contentNegotiation == null. 1.479 + */ 1.480 + if (!ignoreContentNegotiationProperty) { 1.481 + if (p.contentNegotiation == ContentNegotiation.none) { 1.482 + // The client may have changed the negotiation property from 1.483 + // pessismistic to none between invocations 1.484 + useFastInfosetForEncoding = false; 1.485 + } else if (p.contentNegotiation == ContentNegotiation.optimistic) { 1.486 + // Always encode using Fast Infoset if in optimisitic mode 1.487 + useFastInfosetForEncoding = true; 1.488 + } 1.489 + } 1.490 + 1.491 + // Override the MTOM binding for now 1.492 + // Note: Using FI with MTOM does not make sense 1.493 + if (useFastInfosetForEncoding) { 1.494 + final Message m = p.getMessage(); 1.495 + if(m==null || m.getAttachments().isEmpty() || features.isEnabled(MTOMFeature.class)) 1.496 + return fiSoapCodec; 1.497 + else 1.498 + return fiSwaCodec; 1.499 + } 1.500 + 1.501 + if(features.isEnabled(MTOMFeature.class)) { 1.502 + //On client, always use XOP encoding if MTOM is enabled 1.503 + // On Server, use XOP encoding if either request is XOP encoded or client accepts XOP encoding 1.504 + if(!isServerSide() || isRequestMtomMessage || acceptMtomMessages) 1.505 + return xmlMtomCodec; 1.506 + } 1.507 + 1.508 + Message m = p.getMessage(); 1.509 + if(m==null || m.getAttachments().isEmpty()) 1.510 + return xmlSoapCodec; 1.511 + else 1.512 + return xmlSwaCodec; 1.513 + } 1.514 + 1.515 + private RuntimeException noFastInfosetForDecoding() { 1.516 + return new RuntimeException(StreamingMessages.FASTINFOSET_DECODING_NOT_ACCEPTED()); 1.517 + } 1.518 + 1.519 + /** 1.520 + * Obtain an FI SOAP codec instance using reflection. 1.521 + */ 1.522 + private static Codec getFICodec(StreamSOAPCodec soapCodec, SOAPVersion version) { 1.523 + try { 1.524 + Class c = Class.forName("com.sun.xml.internal.ws.encoding.fastinfoset.FastInfosetStreamSOAPCodec"); 1.525 + Method m = c.getMethod("create", StreamSOAPCodec.class, SOAPVersion.class); 1.526 + return (Codec)m.invoke(null, soapCodec, version); 1.527 + } catch (Exception e) { 1.528 + // TODO Log that FI cannot be loaded 1.529 + return null; 1.530 + } 1.531 + } 1.532 +}