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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     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	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,484 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, 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 +    /**
    1.82 +     * True if Fast Infoset functionality has been
    1.83 +     * configured to be disabled, or the Fast Infoset
    1.84 +     * runtime is not available.
    1.85 +     */
    1.86 +    private boolean isFastInfosetDisabled;
    1.87 +
    1.88 +    /**
    1.89 +     * True if the Fast Infoset codec should be used for encoding.
    1.90 +     */
    1.91 +    private boolean useFastInfosetForEncoding;
    1.92 +
    1.93 +    /**
    1.94 +     * True if the content negotiation property should
    1.95 +     * be ignored by the client. This will be used in
    1.96 +     * the case of Fast Infoset being configured to be
    1.97 +     * disabled or automatically selected.
    1.98 +     */
    1.99 +    private boolean ignoreContentNegotiationProperty;
   1.100 +
   1.101 +    // The XML SOAP codec
   1.102 +    private final StreamSOAPCodec xmlSoapCodec;
   1.103 +
   1.104 +    // The Fast Infoset SOAP codec
   1.105 +    private final Codec fiSoapCodec;
   1.106 +
   1.107 +    // The XML MTOM codec
   1.108 +    private final MimeCodec xmlMtomCodec;
   1.109 +
   1.110 +    // The XML SWA codec
   1.111 +    private final MimeCodec xmlSwaCodec;
   1.112 +
   1.113 +    // The Fast Infoset SWA codec
   1.114 +    private final MimeCodec fiSwaCodec;
   1.115 +
   1.116 +    /**
   1.117 +     * The XML SOAP MIME type
   1.118 +     */
   1.119 +    private final String xmlMimeType;
   1.120 +
   1.121 +    /**
   1.122 +     * The Fast Infoset SOAP MIME type
   1.123 +     */
   1.124 +    private final String fiMimeType;
   1.125 +
   1.126 +    /**
   1.127 +     * The Accept header for XML encodings
   1.128 +     */
   1.129 +    private final String xmlAccept;
   1.130 +
   1.131 +    /**
   1.132 +     * The Accept header for Fast Infoset and XML encodings
   1.133 +     */
   1.134 +    private final String connegXmlAccept;
   1.135 +
   1.136 +    public StreamSOAPCodec getXMLCodec() {
   1.137 +        return xmlSoapCodec;
   1.138 +    }
   1.139 +
   1.140 +    private ContentTypeImpl setAcceptHeader(Packet p, ContentTypeImpl c) {
   1.141 +        String _accept;
   1.142 +        if (!ignoreContentNegotiationProperty && p.contentNegotiation != ContentNegotiation.none) {
   1.143 +            _accept = connegXmlAccept;
   1.144 +        } else {
   1.145 +            _accept = xmlAccept;
   1.146 +        }
   1.147 +        c.setAcceptHeader(_accept);
   1.148 +        return c;
   1.149 +    }
   1.150 +
   1.151 +    public SOAPBindingCodec(WSFeatureList features) {
   1.152 +        this(features, Codecs.createSOAPEnvelopeXmlCodec(features));
   1.153 +    }
   1.154 +
   1.155 +    public SOAPBindingCodec(WSFeatureList features, StreamSOAPCodec xmlSoapCodec) {
   1.156 +        super(getSoapVersion(features), features);
   1.157 +
   1.158 +        this.xmlSoapCodec = xmlSoapCodec;
   1.159 +        xmlMimeType = xmlSoapCodec.getMimeType();
   1.160 +
   1.161 +        xmlMtomCodec = new MtomCodec(version, xmlSoapCodec, features);
   1.162 +
   1.163 +        xmlSwaCodec = new SwACodec(version, features, xmlSoapCodec);
   1.164 +
   1.165 +        String clientAcceptedContentTypes = xmlSoapCodec.getMimeType() + ", " +
   1.166 +                xmlMtomCodec.getMimeType();
   1.167 +
   1.168 +        WebServiceFeature fi = features.get(FastInfosetFeature.class);
   1.169 +        isFastInfosetDisabled = (fi != null && !fi.isEnabled());
   1.170 +        if (!isFastInfosetDisabled) {
   1.171 +            fiSoapCodec = getFICodec(xmlSoapCodec, version);
   1.172 +            if (fiSoapCodec != null) {
   1.173 +                fiMimeType = fiSoapCodec.getMimeType();
   1.174 +                fiSwaCodec = new SwACodec(version, features, fiSoapCodec);
   1.175 +                connegXmlAccept = fiMimeType + ", " + clientAcceptedContentTypes;
   1.176 +
   1.177 +                /**
   1.178 +                 * This feature will only be present on the client side.
   1.179 +                 *
   1.180 +                 * Fast Infoset is enabled on the client if the service
   1.181 +                 * explicitly supports Fast Infoset.
   1.182 +                 */
   1.183 +                WebServiceFeature select = features.get(SelectOptimalEncodingFeature.class);
   1.184 +                if (select != null) { // if the client FI feature is set - ignore negotiation property
   1.185 +                    ignoreContentNegotiationProperty = true;
   1.186 +                    if (select.isEnabled()) {
   1.187 +                        // If the client's FI encoding feature is enabled, and server's is not disabled
   1.188 +                        if (fi != null) {  // if server's FI feature also enabled
   1.189 +                            useFastInfosetForEncoding = true;
   1.190 +                        }
   1.191 +
   1.192 +                        clientAcceptedContentTypes = connegXmlAccept;
   1.193 +                    } else {  // If client FI feature is disabled
   1.194 +                        isFastInfosetDisabled = true;
   1.195 +                    }
   1.196 +                }
   1.197 +            } else {
   1.198 +                // Fast Infoset could not be loaded by the runtime
   1.199 +                isFastInfosetDisabled = true;
   1.200 +                fiSwaCodec = null;
   1.201 +                fiMimeType = "";
   1.202 +                connegXmlAccept = clientAcceptedContentTypes;
   1.203 +                ignoreContentNegotiationProperty = true;
   1.204 +            }
   1.205 +        } else {
   1.206 +            // Fast Infoset is explicitly not supported by the service
   1.207 +            fiSoapCodec = fiSwaCodec = null;
   1.208 +            fiMimeType = "";
   1.209 +            connegXmlAccept = clientAcceptedContentTypes;
   1.210 +            ignoreContentNegotiationProperty = true;
   1.211 +        }
   1.212 +
   1.213 +        xmlAccept = clientAcceptedContentTypes;
   1.214 +
   1.215 +        if(getSoapVersion(features) == null)
   1.216 +            throw new WebServiceException("Expecting a SOAP binding but found ");
   1.217 +    }
   1.218 +
   1.219 +    public String getMimeType() {
   1.220 +        return null;
   1.221 +    }
   1.222 +
   1.223 +    public ContentType getStaticContentType(Packet packet) {
   1.224 +        ContentType toAdapt = getEncoder(packet).getStaticContentType(packet);
   1.225 +        return setAcceptHeader(packet, (ContentTypeImpl)toAdapt);
   1.226 +    }
   1.227 +
   1.228 +    public ContentType encode(Packet packet, OutputStream out) throws IOException {
   1.229 +       preEncode(packet);
   1.230 +       ContentType ct = getEncoder(packet).encode(packet, out);
   1.231 +       ct = setAcceptHeader(packet, (ContentTypeImpl)ct);
   1.232 +       postEncode();
   1.233 +       return ct;
   1.234 +    }
   1.235 +
   1.236 +    public ContentType encode(Packet packet, WritableByteChannel buffer) {
   1.237 +        preEncode(packet);
   1.238 +        ContentType ct = getEncoder(packet).encode(packet, buffer);
   1.239 +        ct = setAcceptHeader(packet, (ContentTypeImpl)ct);
   1.240 +        postEncode();
   1.241 +        return ct;
   1.242 +    }
   1.243 +
   1.244 +    /**
   1.245 +     * Should be called before encode().
   1.246 +     * Set the state so that such state is used by encode process.
   1.247 +     */
   1.248 +    private void preEncode(Packet p) {
   1.249 +    }
   1.250 +
   1.251 +    /**
   1.252 +     * Should be called after encode()
   1.253 +     * Reset the encoding state.
   1.254 +     */
   1.255 +    private void postEncode() {
   1.256 +    }
   1.257 +
   1.258 +    /**
   1.259 +     * Should be called before decode().
   1.260 +     * Set the state so that such state is used by decode().
   1.261 +     */
   1.262 +    private void preDecode(Packet p) {
   1.263 +        if (p.contentNegotiation == null)
   1.264 +            useFastInfosetForEncoding = false;
   1.265 +    }
   1.266 +
   1.267 +    /**
   1.268 +     * Should be called after decode().
   1.269 +     * Set the state so that such state is used by encode().
   1.270 +     */
   1.271 +    private void postDecode(Packet p) {
   1.272 +        p.setFastInfosetDisabled(isFastInfosetDisabled);
   1.273 +        if(features.isEnabled(MTOMFeature.class)) p.checkMtomAcceptable();
   1.274 +//            p.setMtomAcceptable( isMtomAcceptable(p.acceptableMimeTypes) );
   1.275 +        MTOMFeature mtomFeature = features.get(MTOMFeature.class);
   1.276 +        if (mtomFeature != null) {
   1.277 +            p.setMtomFeature(mtomFeature);
   1.278 +        }
   1.279 +        if (!useFastInfosetForEncoding) {
   1.280 +            useFastInfosetForEncoding = p.getFastInfosetAcceptable(fiMimeType);
   1.281 +//          useFastInfosetForEncoding = isFastInfosetAcceptable(p.acceptableMimeTypes);
   1.282 +        }
   1.283 +    }
   1.284 +
   1.285 +    public void decode(InputStream in, String contentType, Packet packet) throws IOException {
   1.286 +        if (contentType == null) {
   1.287 +            contentType = xmlMimeType;
   1.288 +        }
   1.289 +        packet.setContentType(new ContentTypeImpl(contentType));
   1.290 +        preDecode(packet);
   1.291 +        try {
   1.292 +            if(isMultipartRelated(contentType))
   1.293 +                // parse the multipart portion and then decide whether it's MTOM or SwA
   1.294 +                super.decode(in, contentType, packet);
   1.295 +            else if(isFastInfoset(contentType)) {
   1.296 +                if (!ignoreContentNegotiationProperty && packet.contentNegotiation == ContentNegotiation.none)
   1.297 +                    throw noFastInfosetForDecoding();
   1.298 +
   1.299 +                useFastInfosetForEncoding = true;
   1.300 +                fiSoapCodec.decode(in, contentType, packet);
   1.301 +            } else
   1.302 +                xmlSoapCodec.decode(in, contentType, packet);
   1.303 +        } catch(RuntimeException we) {
   1.304 +            if (we instanceof ExceptionHasMessage || we instanceof UnsupportedMediaException) {
   1.305 +                throw we;
   1.306 +            } else {
   1.307 +                throw new MessageCreationException(version, we);
   1.308 +            }
   1.309 +        }
   1.310 +        postDecode(packet);
   1.311 +    }
   1.312 +
   1.313 +    public void decode(ReadableByteChannel in, String contentType, Packet packet) {
   1.314 +        if (contentType == null) {
   1.315 +            throw new UnsupportedMediaException();
   1.316 +        }
   1.317 +
   1.318 +        preDecode(packet);
   1.319 +        try {
   1.320 +            if(isMultipartRelated(contentType))
   1.321 +                super.decode(in, contentType, packet);
   1.322 +            else if(isFastInfoset(contentType)) {
   1.323 +                if (packet.contentNegotiation == ContentNegotiation.none)
   1.324 +                    throw noFastInfosetForDecoding();
   1.325 +
   1.326 +                useFastInfosetForEncoding = true;
   1.327 +                fiSoapCodec.decode(in, contentType, packet);
   1.328 +            } else
   1.329 +                xmlSoapCodec.decode(in, contentType, packet);
   1.330 +        } catch(RuntimeException we) {
   1.331 +            if (we instanceof ExceptionHasMessage || we instanceof UnsupportedMediaException) {
   1.332 +                throw we;
   1.333 +            } else {
   1.334 +                throw new MessageCreationException(version, we);
   1.335 +            }
   1.336 +        }
   1.337 +        postDecode(packet);
   1.338 +    }
   1.339 +
   1.340 +    public SOAPBindingCodec copy() {
   1.341 +        return new SOAPBindingCodec(features, (StreamSOAPCodec)xmlSoapCodec.copy());
   1.342 +    }
   1.343 +
   1.344 +    @Override
   1.345 +    protected void decode(MimeMultipartParser mpp, Packet packet) throws IOException {
   1.346 +        // is this SwA or XOP?
   1.347 +        final String rootContentType = mpp.getRootPart().getContentType();
   1.348 +        boolean isMTOM = isApplicationXopXml(rootContentType);
   1.349 +        packet.setMtomRequest(isMTOM);
   1.350 +        if(isMTOM) {
   1.351 +            xmlMtomCodec.decode(mpp,packet);
   1.352 +        } else if (isFastInfoset(rootContentType)) {
   1.353 +            if (packet.contentNegotiation == ContentNegotiation.none)
   1.354 +                throw noFastInfosetForDecoding();
   1.355 +
   1.356 +            useFastInfosetForEncoding = true;
   1.357 +            fiSwaCodec.decode(mpp,packet);
   1.358 +        } else if (isXml(rootContentType))
   1.359 +            xmlSwaCodec.decode(mpp,packet);
   1.360 +        else {
   1.361 +            // TODO localize exception
   1.362 +            throw new IOException("");
   1.363 +        }
   1.364 +//        checkDuplicateKnownHeaders(packet);
   1.365 +    }
   1.366 +
   1.367 +    private boolean isMultipartRelated(String contentType) {
   1.368 +        return compareStrings(contentType, MimeCodec.MULTIPART_RELATED_MIME_TYPE);
   1.369 +    }
   1.370 +
   1.371 +    private boolean isApplicationXopXml(String contentType) {
   1.372 +        return compareStrings(contentType, MtomCodec.XOP_XML_MIME_TYPE);
   1.373 +    }
   1.374 +
   1.375 +    private boolean isXml(String contentType) {
   1.376 +        return compareStrings(contentType, xmlMimeType);
   1.377 +    }
   1.378 +
   1.379 +    private boolean isFastInfoset(String contentType) {
   1.380 +        if (isFastInfosetDisabled) return false;
   1.381 +
   1.382 +        return compareStrings(contentType, fiMimeType);
   1.383 +    }
   1.384 +
   1.385 +    private boolean compareStrings(String a, String b) {
   1.386 +        return a.length() >= b.length() &&
   1.387 +                b.equalsIgnoreCase(
   1.388 +                a.substring(0,
   1.389 +                b.length()));
   1.390 +    }
   1.391 +
   1.392 +//    private boolean isFastInfosetAcceptable(String accept) {
   1.393 +//        if (accept == null || isFastInfosetDisabled) return false;
   1.394 +//
   1.395 +//        StringTokenizer st = new StringTokenizer(accept, ",");
   1.396 +//        while (st.hasMoreTokens()) {
   1.397 +//            final String token = st.nextToken().trim();
   1.398 +//            if (token.equalsIgnoreCase(fiMimeType)) {
   1.399 +//                return true;
   1.400 +//            }
   1.401 +//        }
   1.402 +//        return false;
   1.403 +//    }
   1.404 +
   1.405 +    /*
   1.406 +     * Just check if the Accept header contains application/xop+xml,
   1.407 +     * no need to worry about q values.
   1.408 +     */
   1.409 +//    private boolean isMtomAcceptable(String accept) {
   1.410 +//        if (accept == null || isFastInfosetDisabled) return false;
   1.411 +//        StringTokenizer st = new StringTokenizer(accept, ",");
   1.412 +//        while (st.hasMoreTokens()) {
   1.413 +//            final String token = st.nextToken().trim();
   1.414 +//            if (token.toLowerCase().contains(MtomCodec.XOP_XML_MIME_TYPE)) {
   1.415 +//                return true;
   1.416 +//            }
   1.417 +//        }
   1.418 +//        return false;
   1.419 +//    }
   1.420 +
   1.421 +    /**
   1.422 +     * Determines the encoding codec.
   1.423 +     */
   1.424 +    private Codec getEncoder(Packet p) {
   1.425 +        /**
   1.426 +         * The following logic is only for outbound packets
   1.427 +         * to be encoded by a client.
   1.428 +         * For a server the p.contentNegotiation == null.
   1.429 +         */
   1.430 +        if (!ignoreContentNegotiationProperty) {
   1.431 +            if (p.contentNegotiation == ContentNegotiation.none) {
   1.432 +                // The client may have changed the negotiation property from
   1.433 +                // pessismistic to none between invocations
   1.434 +                useFastInfosetForEncoding = false;
   1.435 +            } else if (p.contentNegotiation == ContentNegotiation.optimistic) {
   1.436 +                // Always encode using Fast Infoset if in optimisitic mode
   1.437 +                useFastInfosetForEncoding = true;
   1.438 +            }
   1.439 +        }
   1.440 +
   1.441 +        // Override the MTOM binding for now
   1.442 +        // Note: Using FI with MTOM does not make sense
   1.443 +        if (useFastInfosetForEncoding) {
   1.444 +            final Message m = p.getMessage();
   1.445 +            if(m==null || m.getAttachments().isEmpty() || features.isEnabled(MTOMFeature.class))
   1.446 +                return fiSoapCodec;
   1.447 +            else
   1.448 +                return fiSwaCodec;
   1.449 +        }
   1.450 +
   1.451 +        //If the packet does not have a binding, explicitly set the MTOMFeature
   1.452 +        //on the packet so that it has a way to determine whether to use MTOM
   1.453 +        if (p.getBinding() == null) {
   1.454 +            if (features != null) {
   1.455 +                p.setMtomFeature(features.get(MTOMFeature.class));
   1.456 +            }
   1.457 +        }
   1.458 +
   1.459 +        if (p.shouldUseMtom()) {
   1.460 +            return xmlMtomCodec;
   1.461 +        }
   1.462 +
   1.463 +        Message m = p.getMessage();
   1.464 +        if(m==null || m.getAttachments().isEmpty())
   1.465 +            return xmlSoapCodec;
   1.466 +        else
   1.467 +            return xmlSwaCodec;
   1.468 +    }
   1.469 +
   1.470 +    private RuntimeException noFastInfosetForDecoding() {
   1.471 +        return new RuntimeException(StreamingMessages.FASTINFOSET_DECODING_NOT_ACCEPTED());
   1.472 +    }
   1.473 +
   1.474 +    /**
   1.475 +     * Obtain an FI SOAP codec instance using reflection.
   1.476 +     */
   1.477 +    private static Codec getFICodec(StreamSOAPCodec soapCodec, SOAPVersion version) {
   1.478 +        try {
   1.479 +            Class c = Class.forName("com.sun.xml.internal.ws.encoding.fastinfoset.FastInfosetStreamSOAPCodec");
   1.480 +            Method m = c.getMethod("create", StreamSOAPCodec.class, SOAPVersion.class);
   1.481 +            return (Codec)m.invoke(null, soapCodec, version);
   1.482 +        } catch (Exception e) {
   1.483 +            // TODO Log that FI cannot be loaded
   1.484 +            return null;
   1.485 +        }
   1.486 +    }
   1.487 +}

mercurial