src/share/jaxws_classes/com/sun/xml/internal/ws/api/SOAPVersion.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/api/SOAPVersion.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,279 @@
     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.api;
    1.30 +
    1.31 +import com.sun.xml.internal.bind.util.Which;
    1.32 +import com.sun.xml.internal.ws.api.message.saaj.SAAJFactory;
    1.33 +import com.sun.xml.internal.ws.encoding.soap.SOAP12Constants;
    1.34 +
    1.35 +import javax.xml.namespace.QName;
    1.36 +import javax.xml.soap.MessageFactory;
    1.37 +import javax.xml.soap.SOAPConstants;
    1.38 +import javax.xml.soap.SOAPException;
    1.39 +import javax.xml.soap.SOAPFactory;
    1.40 +import javax.xml.ws.soap.SOAPBinding;
    1.41 +
    1.42 +import com.sun.xml.internal.org.jvnet.ws.EnvelopeStyle;
    1.43 +import com.sun.xml.internal.org.jvnet.ws.EnvelopeStyleFeature;
    1.44 +
    1.45 +import java.util.Arrays;
    1.46 +import java.util.Collections;
    1.47 +import java.util.HashSet;
    1.48 +import java.util.Set;
    1.49 +
    1.50 +/**
    1.51 + * Version of SOAP (1.1 and 1.2).
    1.52 + *
    1.53 + * <p>
    1.54 + * This class defines various constants for SOAP 1.1 and SOAP 1.2,
    1.55 + * and also defines convenience methods to simplify the processing
    1.56 + * of multiple SOAP versions.
    1.57 + *
    1.58 + * <p>
    1.59 + * This constant alows you to do:
    1.60 + *
    1.61 + * <pre>
    1.62 + * SOAPVersion version = ...;
    1.63 + * version.someOp(...);
    1.64 + * </pre>
    1.65 + *
    1.66 + * As opposed to:
    1.67 + *
    1.68 + * <pre>
    1.69 + * if(binding is SOAP11) {
    1.70 + *   doSomeOp11(...);
    1.71 + * } else {
    1.72 + *   doSomeOp12(...);
    1.73 + * }
    1.74 + * </pre>
    1.75 + *
    1.76 + * @author Kohsuke Kawaguchi
    1.77 + */
    1.78 +public enum SOAPVersion {
    1.79 +    SOAP_11(SOAPBinding.SOAP11HTTP_BINDING,
    1.80 +            com.sun.xml.internal.ws.encoding.soap.SOAPConstants.URI_ENVELOPE,
    1.81 +            "text/xml",
    1.82 +            SOAPConstants.URI_SOAP_ACTOR_NEXT, "actor",
    1.83 +            javax.xml.soap.SOAPConstants.SOAP_1_1_PROTOCOL,
    1.84 +            new QName(com.sun.xml.internal.ws.encoding.soap.SOAPConstants.URI_ENVELOPE, "MustUnderstand"),
    1.85 +            "Client",
    1.86 +            "Server",
    1.87 +            Collections.singleton(SOAPConstants.URI_SOAP_ACTOR_NEXT)),
    1.88 +
    1.89 +    SOAP_12(SOAPBinding.SOAP12HTTP_BINDING,
    1.90 +            SOAP12Constants.URI_ENVELOPE,
    1.91 +            "application/soap+xml",
    1.92 +            SOAPConstants.URI_SOAP_1_2_ROLE_ULTIMATE_RECEIVER, "role",
    1.93 +            javax.xml.soap.SOAPConstants.SOAP_1_2_PROTOCOL,
    1.94 +            new QName(com.sun.xml.internal.ws.encoding.soap.SOAP12Constants.URI_ENVELOPE, "MustUnderstand"),
    1.95 +            "Sender",
    1.96 +            "Receiver",
    1.97 +            new HashSet<String>(Arrays.asList(SOAPConstants.URI_SOAP_1_2_ROLE_NEXT,SOAPConstants.URI_SOAP_1_2_ROLE_ULTIMATE_RECEIVER)));
    1.98 +
    1.99 +    /**
   1.100 +     * Binding ID for SOAP/HTTP binding of this SOAP version.
   1.101 +     *
   1.102 +     * <p>
   1.103 +     * Either {@link SOAPBinding#SOAP11HTTP_BINDING} or
   1.104 +     *  {@link SOAPBinding#SOAP12HTTP_BINDING}
   1.105 +     */
   1.106 +    public final String httpBindingId;
   1.107 +
   1.108 +    /**
   1.109 +     * SOAP envelope namespace URI.
   1.110 +     */
   1.111 +    public final String nsUri;
   1.112 +
   1.113 +    /**
   1.114 +     * Content-type. Either "text/xml" or "application/soap+xml".
   1.115 +     */
   1.116 +    public final String contentType;
   1.117 +
   1.118 +    /**
   1.119 +     * SOAP MustUnderstand FaultCode for this SOAP version
   1.120 +     */
   1.121 +    public final QName faultCodeMustUnderstand;
   1.122 +
   1.123 +    /**
   1.124 +     * SAAJ {@link MessageFactory} for this SOAP version.
   1.125 +     * @deprecated
   1.126 +     */
   1.127 +    public final MessageFactory saajMessageFactory;
   1.128 +
   1.129 +    /**
   1.130 +     * SAAJ {@link SOAPFactory} for this SOAP version.
   1.131 +     * @deprecated
   1.132 +     */
   1.133 +    public final SOAPFactory saajSoapFactory;
   1.134 +
   1.135 +    private final String saajFactoryString;
   1.136 +
   1.137 +    /**
   1.138 +     * If the actor/role attribute is absent, this SOAP version assumes this value.
   1.139 +     */
   1.140 +    public final String implicitRole;
   1.141 +
   1.142 +    /**
   1.143 +     * Singleton set that contains {@link #implicitRole}.
   1.144 +     */
   1.145 +    public final Set<String> implicitRoleSet;
   1.146 +
   1.147 +    /**
   1.148 +     * This represents the roles required to be assumed by SOAP binding implementation.
   1.149 +     */
   1.150 +    public final Set<String> requiredRoles;
   1.151 +
   1.152 +    /**
   1.153 +     * "role" (SOAP 1.2) or "actor" (SOAP 1.1)
   1.154 +     */
   1.155 +    public final String roleAttributeName;
   1.156 +
   1.157 +    /**
   1.158 +     * "{nsUri}Client" or "{nsUri}Sender"
   1.159 +     */
   1.160 +    public final QName faultCodeClient;
   1.161 +
   1.162 +    /**
   1.163 +     * "{nsUri}Server" or "{nsUri}Receiver"
   1.164 +     */
   1.165 +    public final QName faultCodeServer;
   1.166 +
   1.167 +    private SOAPVersion(String httpBindingId, String nsUri, String contentType, String implicitRole, String roleAttributeName,
   1.168 +                        String saajFactoryString, QName faultCodeMustUnderstand, String faultCodeClientLocalName,
   1.169 +                        String faultCodeServerLocalName,Set<String> requiredRoles) {
   1.170 +        this.httpBindingId = httpBindingId;
   1.171 +        this.nsUri = nsUri;
   1.172 +        this.contentType = contentType;
   1.173 +        this.implicitRole = implicitRole;
   1.174 +        this.implicitRoleSet = Collections.singleton(implicitRole);
   1.175 +        this.roleAttributeName = roleAttributeName;
   1.176 +        this.saajFactoryString = saajFactoryString;
   1.177 +        try {
   1.178 +            saajMessageFactory = MessageFactory.newInstance(saajFactoryString);
   1.179 +            saajSoapFactory = SOAPFactory.newInstance(saajFactoryString);
   1.180 +        } catch (SOAPException e) {
   1.181 +            throw new Error(e);
   1.182 +        } catch (NoSuchMethodError e) {
   1.183 +            // SAAJ 1.3 is not in the classpath
   1.184 +            LinkageError x = new LinkageError("You are loading old SAAJ from "+ Which.which(MessageFactory.class));
   1.185 +            x.initCause(e);
   1.186 +            throw x;
   1.187 +        }
   1.188 +        this.faultCodeMustUnderstand = faultCodeMustUnderstand;
   1.189 +        this.requiredRoles = requiredRoles;
   1.190 +        this.faultCodeClient = new QName(nsUri,faultCodeClientLocalName);
   1.191 +        this.faultCodeServer = new QName(nsUri,faultCodeServerLocalName);
   1.192 +    }
   1.193 +
   1.194 +    public SOAPFactory getSOAPFactory() {
   1.195 +        try {
   1.196 +                return SAAJFactory.getSOAPFactory(saajFactoryString);
   1.197 +        } catch (SOAPException e) {
   1.198 +            throw new Error(e);
   1.199 +        } catch (NoSuchMethodError e) {
   1.200 +            // SAAJ 1.3 is not in the classpath
   1.201 +            LinkageError x = new LinkageError("You are loading old SAAJ from "+ Which.which(MessageFactory.class));
   1.202 +            x.initCause(e);
   1.203 +            throw x;
   1.204 +        }
   1.205 +    }
   1.206 +
   1.207 +    public MessageFactory getMessageFactory() {
   1.208 +        try {
   1.209 +                return SAAJFactory.getMessageFactory(saajFactoryString);
   1.210 +        } catch (SOAPException e) {
   1.211 +            throw new Error(e);
   1.212 +        } catch (NoSuchMethodError e) {
   1.213 +            // SAAJ 1.3 is not in the classpath
   1.214 +            LinkageError x = new LinkageError("You are loading old SAAJ from "+ Which.which(MessageFactory.class));
   1.215 +            x.initCause(e);
   1.216 +            throw x;
   1.217 +        }
   1.218 +    }
   1.219 +
   1.220 +    public String toString() {
   1.221 +        return httpBindingId;
   1.222 +    }
   1.223 +
   1.224 +    /**
   1.225 +     * Returns {@link SOAPVersion} whose {@link #httpBindingId} equals to
   1.226 +     * the given string.
   1.227 +     *
   1.228 +     * This method does not perform input string validation.
   1.229 +     *
   1.230 +     * @param binding
   1.231 +     *      for historical reason, we treat null as {@link #SOAP_11},
   1.232 +     *      but you really shouldn't be passing null.
   1.233 +     * @return always non-null.
   1.234 +     */
   1.235 +    public static SOAPVersion fromHttpBinding(String binding) {
   1.236 +        if(binding==null)
   1.237 +            return SOAP_11;
   1.238 +
   1.239 +        if(binding.equals(SOAP_12.httpBindingId))
   1.240 +            return SOAP_12;
   1.241 +        else
   1.242 +            return SOAP_11;
   1.243 +    }
   1.244 +
   1.245 +    /**
   1.246 +     * Returns {@link SOAPVersion} whose {@link #nsUri} equals to
   1.247 +     * the given string.
   1.248 +     *
   1.249 +     * This method does not perform input string validation.
   1.250 +     *
   1.251 +     * @param nsUri
   1.252 +     *      must not be null.
   1.253 +     * @return always non-null.
   1.254 +     */
   1.255 +    public static SOAPVersion fromNsUri(String nsUri) {
   1.256 +        if(nsUri.equals(SOAP_12.nsUri))
   1.257 +            return SOAP_12;
   1.258 +        else
   1.259 +            return SOAP_11;
   1.260 +    }
   1.261 +
   1.262 +    public static SOAPVersion from(EnvelopeStyleFeature f) {
   1.263 +        EnvelopeStyle.Style[] style = f.getStyles();
   1.264 +        if (style.length != 1) throw new IllegalArgumentException ("The EnvelopingFeature must has exactly one Enveloping.Style");
   1.265 +        return from(style[0]);
   1.266 +    }
   1.267 +
   1.268 +    public static SOAPVersion from(EnvelopeStyle.Style style) {
   1.269 +        switch (style) {
   1.270 +        case SOAP11: return SOAP_11;
   1.271 +        case SOAP12: return SOAP_12;
   1.272 +        case XML: //ERROR??
   1.273 +        default: return SOAP_11;
   1.274 +        }
   1.275 +    }
   1.276 +
   1.277 +    public EnvelopeStyleFeature toFeature() {
   1.278 +        return SOAP_11.equals(this) ?
   1.279 +            new EnvelopeStyleFeature(new EnvelopeStyle.Style[]{EnvelopeStyle.Style.SOAP11}) :
   1.280 +            new EnvelopeStyleFeature(new EnvelopeStyle.Style[]{EnvelopeStyle.Style.SOAP12});
   1.281 +    }
   1.282 +}

mercurial