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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2012, 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.api;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.xml.internal.ws.api.message.Message;
ohair@286 30 import com.sun.xml.internal.ws.api.pipe.Codec;
ohair@286 31 import com.sun.xml.internal.ws.api.pipe.Tube;
ohair@286 32 import com.sun.xml.internal.ws.binding.BindingImpl;
ohair@286 33 import com.sun.xml.internal.ws.binding.SOAPBindingImpl;
ohair@286 34 import com.sun.xml.internal.ws.binding.WebServiceFeatureList;
ohair@286 35 import com.sun.xml.internal.ws.encoding.SOAPBindingCodec;
ohair@286 36 import com.sun.xml.internal.ws.encoding.XMLHTTPBindingCodec;
ohair@286 37 import com.sun.xml.internal.ws.encoding.soap.streaming.SOAPNamespaceConstants;
ohair@286 38 import com.sun.xml.internal.ws.util.ServiceFinder;
ohair@286 39 import com.sun.xml.internal.ws.developer.JAXWSProperties;
ohair@286 40
ohair@286 41 import javax.xml.ws.BindingType;
ohair@286 42 import javax.xml.ws.WebServiceException;
ohair@286 43 import javax.xml.ws.WebServiceFeature;
ohair@286 44 import javax.xml.ws.handler.Handler;
ohair@286 45 import javax.xml.ws.http.HTTPBinding;
ohair@286 46 import javax.xml.ws.soap.MTOMFeature;
ohair@286 47 import javax.xml.ws.soap.SOAPBinding;
ohair@286 48
ohair@286 49 import java.io.UnsupportedEncodingException;
ohair@286 50 import java.net.URL;
ohair@286 51 import java.net.URLDecoder;
ohair@286 52 import java.util.HashMap;
ohair@286 53 import java.util.Map;
ohair@286 54
ohair@286 55 /**
ohair@286 56 * Parsed binding ID string.
ohair@286 57 *
ohair@286 58 * <p>
ohair@286 59 * {@link BindingID} is an immutable object that represents a binding ID,
ohair@286 60 * much like how {@link URL} is a representation of an URL.
ohair@286 61 * Like {@link URL}, this class offers a bunch of methods that let you
ohair@286 62 * query various traits/properties of a binding ID.
ohair@286 63 *
ohair@286 64 * <p>
ohair@286 65 * {@link BindingID} is extensible; one can plug in a parser from
ohair@286 66 * {@link String} to {@link BindingID} to interpret binding IDs that
ohair@286 67 * the JAX-WS RI does no a-priori knowledge of.
ohair@286 68 * Technologies such as Tango uses this to make the JAX-WS RI understand
ohair@286 69 * binding IDs defined in their world.
ohair@286 70 *
ohair@286 71 * Such technologies are free to extend this class and expose more characterstics.
ohair@286 72 *
ohair@286 73 * <p>
ohair@286 74 * Even though this class defines a few well known constants, {@link BindingID}
ohair@286 75 * instances do not necessarily have singleton semantics. Use {@link #equals(Object)}
ohair@286 76 * for the comparison.
ohair@286 77 *
ohair@286 78 * <h3>{@link BindingID} and {@link WSBinding}</h3>
ohair@286 79 * <p>
ohair@286 80 * {@link WSBinding} is mutable and represents a particular "use" of a {@link BindingID}.
ohair@286 81 * As such, it has state like a list of {@link Handler}s, which are inherently local
ohair@286 82 * to a particular usage. For example, if you have two proxies, you need two instances.
ohair@286 83 *
ohair@286 84 * {@link BindingID}, OTOH, is immutable and thus the single instance
ohair@286 85 * that represents "SOAP1.2/HTTP" can be shared and reused by all proxies in the same VM.
ohair@286 86 *
ohair@286 87 * @author Kohsuke Kawaguchi
ohair@286 88 */
ohair@286 89 public abstract class BindingID {
ohair@286 90
ohair@286 91 /**
ohair@286 92 * Creates an instance of {@link WSBinding} (which is conceptually an "use"
ohair@286 93 * of {@link BindingID}) from a {@link BindingID}.
ohair@286 94 *
ohair@286 95 * @return
ohair@286 96 * Always a new instance.
ohair@286 97 */
ohair@286 98 public final @NotNull WSBinding createBinding() {
ohair@286 99 return BindingImpl.create(this);
ohair@286 100 }
ohair@286 101
ohair@286 102 /**
ohair@286 103 * Returns wsdl:binding@transport attribute. Sub classes
ohair@286 104 * are expected to override this method to provide their transport
ohair@286 105 * attribute.
ohair@286 106 *
ohair@286 107 * @return wsdl:binding@transport attribute
ohair@286 108 * @since JAX-WS RI 2.1.6
ohair@286 109 */
ohair@286 110 public @NotNull String getTransport() {
ohair@286 111 return SOAPNamespaceConstants.TRANSPORT_HTTP;
ohair@286 112 }
ohair@286 113
ohair@286 114 public final @NotNull WSBinding createBinding(WebServiceFeature... features) {
ohair@286 115 return BindingImpl.create(this, features);
ohair@286 116 }
ohair@286 117
ohair@286 118 public final @NotNull WSBinding createBinding(WSFeatureList features) {
ohair@286 119 return createBinding(features.toArray());
ohair@286 120 }
ohair@286 121
ohair@286 122 /**
ohair@286 123 * Gets the SOAP version of this binding.
ohair@286 124 *
ohair@286 125 * TODO: clarify what to do with XML/HTTP binding
ohair@286 126 *
ohair@286 127 * @return
ohair@286 128 * If the binding is using SOAP, this method returns
ohair@286 129 * a {@link SOAPVersion} constant.
ohair@286 130 *
ohair@286 131 * If the binding is not based on SOAP, this method
ohair@286 132 * returns null. See {@link Message} for how a non-SOAP
ohair@286 133 * binding shall be handled by {@link Tube}s.
ohair@286 134 */
ohair@286 135 public abstract SOAPVersion getSOAPVersion();
ohair@286 136
ohair@286 137 /**
ohair@286 138 * Creates a new {@link Codec} for this binding.
ohair@286 139 *
ohair@286 140 * @param binding
ohair@286 141 * Ocassionally some aspects of binding can be overridden by
ohair@286 142 * {@link WSBinding} at runtime by users, so some {@link Codec}s
ohair@286 143 * need to have access to {@link WSBinding} that it's working for.
ohair@286 144 */
ohair@286 145 public abstract @NotNull Codec createEncoder(@NotNull WSBinding binding);
ohair@286 146
ohair@286 147 /**
ohair@286 148 * Gets the binding ID, which uniquely identifies the binding.
ohair@286 149 *
ohair@286 150 * <p>
ohair@286 151 * The relevant specs define the binding IDs and what they mean.
ohair@286 152 * The ID is used in many places to identify the kind of binding
ohair@286 153 * (such as SOAP1.1, SOAP1.2, REST, ...)
ohair@286 154 *
ohair@286 155 * @return
ohair@286 156 * Always non-null same value.
ohair@286 157 */
alanb@368 158 @Override
ohair@286 159 public abstract String toString();
ohair@286 160
ohair@286 161 /**
ohair@286 162 * Returna a new {@link WebServiceFeatureList} instance
ohair@286 163 * that represents the features that are built into this binding ID.
ohair@286 164 *
ohair@286 165 * <p>
ohair@286 166 * For example, {@link BindingID} for
ohair@286 167 * <tt>"{@value SOAPBinding#SOAP11HTTP_MTOM_BINDING}"</tt>
ohair@286 168 * would always return a list that has {@link MTOMFeature} enabled.
ohair@286 169 */
ohair@286 170 public WebServiceFeatureList createBuiltinFeatureList() {
ohair@286 171 return new WebServiceFeatureList();
ohair@286 172 }
ohair@286 173
ohair@286 174 /**
ohair@286 175 * Returns true if this binding can generate WSDL.
ohair@286 176 *
ohair@286 177 * <p>
ohair@286 178 * For e.g.: SOAP 1.1 and "XSOAP 1.2" is supposed to return true
ohair@286 179 * from this method. For SOAP1.2, there is no standard WSDL, so the
ohair@286 180 * runtime is not generating one and it expects the WSDL is packaged.
ohair@286 181 *
ohair@286 182 */
ohair@286 183 public boolean canGenerateWSDL() {
ohair@286 184 return false;
ohair@286 185 }
ohair@286 186
ohair@286 187 /**
ohair@286 188 * Returns a parameter of this binding ID.
ohair@286 189 *
ohair@286 190 * <p>
ohair@286 191 * Some binding ID, such as those for SOAP/HTTP, uses the URL
ohair@286 192 * query syntax (like <tt>?mtom=true</tt>) to control
ohair@286 193 * the optional part of the binding. This method obtains
ohair@286 194 * the value for such optional parts.
ohair@286 195 *
ohair@286 196 * <p>
ohair@286 197 * For implementors of the derived classes, if your binding ID
ohair@286 198 * does not define such optional parts (such as the XML/HTTP binding ID),
ohair@286 199 * then you should simply return the specified default value
ohair@286 200 * (which is what this implementation does.)
ohair@286 201 *
ohair@286 202 * @param parameterName
ohair@286 203 * The parameter name, such as "mtom" in the above example.
ohair@286 204 * @param defaultValue
ohair@286 205 * If this binding ID doesn't have the specified parameter explicitly,
ohair@286 206 * this value will be returned.
ohair@286 207 *
ohair@286 208 * @return
ohair@286 209 * the value of the parameter, if it's present (such as "true"
ohair@286 210 * in the above example.) If not present, this method returns
ohair@286 211 * the {@code defaultValue}.
ohair@286 212 */
ohair@286 213 public String getParameter(String parameterName, String defaultValue) {
ohair@286 214 return defaultValue;
ohair@286 215 }
ohair@286 216
ohair@286 217 /**
ohair@286 218 * Compares the equality based on {@link #toString()}.
ohair@286 219 */
alanb@368 220 @Override
ohair@286 221 public boolean equals(Object obj) {
ohair@286 222 if(!(obj instanceof BindingID))
ohair@286 223 return false;
ohair@286 224 return toString().equals(obj.toString());
ohair@286 225 }
ohair@286 226
alanb@368 227 @Override
ohair@286 228 public int hashCode() {
ohair@286 229 return toString().hashCode();
ohair@286 230 }
ohair@286 231
ohair@286 232 /**
ohair@286 233 * Parses a binding ID string into a {@link BindingID} object.
ohair@286 234 *
ohair@286 235 * <p>
ohair@286 236 * This method first checks for a few known values and then delegate
ohair@286 237 * the parsing to {@link BindingIDFactory}.
ohair@286 238 *
ohair@286 239 * <p>
ohair@286 240 * If parsing succeeds this method returns a value. Otherwise
ohair@286 241 * throws {@link WebServiceException}.
ohair@286 242 *
ohair@286 243 * @throws WebServiceException
ohair@286 244 * If the binding ID is not understood.
ohair@286 245 */
ohair@286 246 public static @NotNull BindingID parse(String lexical) {
ohair@286 247 if(lexical.equals(XML_HTTP.toString()))
ohair@286 248 return XML_HTTP;
ohair@286 249 if(lexical.equals(REST_HTTP.toString()))
ohair@286 250 return REST_HTTP;
ohair@286 251 if(belongsTo(lexical,SOAP11_HTTP.toString()))
ohair@286 252 return customize(lexical,SOAP11_HTTP);
ohair@286 253 if(belongsTo(lexical,SOAP12_HTTP.toString()))
ohair@286 254 return customize(lexical,SOAP12_HTTP);
ohair@286 255 if(belongsTo(lexical,SOAPBindingImpl.X_SOAP12HTTP_BINDING))
ohair@286 256 return customize(lexical,X_SOAP12_HTTP);
ohair@286 257
ohair@286 258 // OK, it's none of the values JAX-WS understands.
ohair@286 259 for( BindingIDFactory f : ServiceFinder.find(BindingIDFactory.class) ) {
ohair@286 260 BindingID r = f.parse(lexical);
ohair@286 261 if(r!=null)
ohair@286 262 return r;
ohair@286 263 }
ohair@286 264
ohair@286 265 // nobody understood this value
ohair@286 266 throw new WebServiceException("Wrong binding ID: "+lexical);
ohair@286 267 }
ohair@286 268
ohair@286 269 private static boolean belongsTo(String lexical, String id) {
ohair@286 270 return lexical.equals(id) || lexical.startsWith(id+'?');
ohair@286 271 }
ohair@286 272
ohair@286 273 /**
ohair@286 274 * Parses parameter portion and returns appropriately populated {@link SOAPHTTPImpl}
ohair@286 275 */
ohair@286 276 private static SOAPHTTPImpl customize(String lexical, SOAPHTTPImpl base) {
ohair@286 277 if(lexical.equals(base.toString()))
ohair@286 278 return base;
ohair@286 279
ohair@286 280 // otherwise we must have query parameter
ohair@286 281 // we assume the spec won't define any tricky parameters that require
ohair@286 282 // complicated handling (such as %HH or non-ASCII char), so this parser
ohair@286 283 // is quite simple-minded.
ohair@286 284 SOAPHTTPImpl r = new SOAPHTTPImpl(base.getSOAPVersion(), lexical, base.canGenerateWSDL());
ohair@286 285 try {
ohair@286 286 // With X_SOAP12_HTTP, base != lexical and lexical does n't have any query string
ohair@286 287 if(lexical.indexOf('?') == -1) {
ohair@286 288 return r;
ohair@286 289 }
ohair@286 290 String query = URLDecoder.decode(lexical.substring(lexical.indexOf('?')+1),"UTF-8");
ohair@286 291 for( String token : query.split("&") ) {
ohair@286 292 int idx = token.indexOf('=');
ohair@286 293 if(idx<0)
ohair@286 294 throw new WebServiceException("Malformed binding ID (no '=' in "+token+")");
ohair@286 295 r.parameters.put(token.substring(0,idx),token.substring(idx+1));
ohair@286 296 }
ohair@286 297 } catch (UnsupportedEncodingException e) {
ohair@286 298 throw new AssertionError(e); // UTF-8 is supported everywhere
ohair@286 299 }
ohair@286 300
ohair@286 301 return r;
ohair@286 302 }
ohair@286 303
ohair@286 304
ohair@286 305 /**
ohair@286 306 * Figures out the binding from {@link BindingType} annotation.
ohair@286 307 *
ohair@286 308 * @return
ohair@286 309 * default to {@link BindingID#SOAP11_HTTP}, if no such annotation is present.
ohair@286 310 * @see #parse(String)
ohair@286 311 */
ohair@286 312 public static @NotNull BindingID parse(Class<?> implClass) {
ohair@286 313 BindingType bindingType = implClass.getAnnotation(BindingType.class);
ohair@286 314 if (bindingType != null) {
ohair@286 315 String bindingId = bindingType.value();
ohair@286 316 if (bindingId.length() > 0) {
ohair@286 317 return BindingID.parse(bindingId);
ohair@286 318 }
ohair@286 319 }
ohair@286 320 return SOAP11_HTTP;
ohair@286 321 }
ohair@286 322
ohair@286 323 /**
ohair@286 324 * Constant that represents implementation specific SOAP1.2/HTTP which is
ohair@286 325 * used to generate non-standard WSDLs
ohair@286 326 */
ohair@286 327 public static final SOAPHTTPImpl X_SOAP12_HTTP = new SOAPHTTPImpl(
ohair@286 328 SOAPVersion.SOAP_12, SOAPBindingImpl.X_SOAP12HTTP_BINDING, true);
ohair@286 329
ohair@286 330 /**
ohair@286 331 * Constant that represents SOAP1.2/HTTP.
ohair@286 332 */
ohair@286 333 public static final SOAPHTTPImpl SOAP12_HTTP = new SOAPHTTPImpl(
ohair@286 334 SOAPVersion.SOAP_12, SOAPBinding.SOAP12HTTP_BINDING, true);
ohair@286 335 /**
ohair@286 336 * Constant that represents SOAP1.1/HTTP.
ohair@286 337 */
ohair@286 338 public static final SOAPHTTPImpl SOAP11_HTTP = new SOAPHTTPImpl(
ohair@286 339 SOAPVersion.SOAP_11, SOAPBinding.SOAP11HTTP_BINDING, true);
ohair@286 340
ohair@286 341 /**
ohair@286 342 * Constant that represents SOAP1.2/HTTP.
ohair@286 343 */
ohair@286 344 public static final SOAPHTTPImpl SOAP12_HTTP_MTOM = new SOAPHTTPImpl(
ohair@286 345 SOAPVersion.SOAP_12, SOAPBinding.SOAP12HTTP_MTOM_BINDING, true, true);
ohair@286 346 /**
ohair@286 347 * Constant that represents SOAP1.1/HTTP.
ohair@286 348 */
ohair@286 349 public static final SOAPHTTPImpl SOAP11_HTTP_MTOM = new SOAPHTTPImpl(
ohair@286 350 SOAPVersion.SOAP_11, SOAPBinding.SOAP11HTTP_MTOM_BINDING, true, true);
ohair@286 351
ohair@286 352
ohair@286 353 /**
ohair@286 354 * Constant that represents REST.
ohair@286 355 */
ohair@286 356 public static final BindingID XML_HTTP = new Impl(SOAPVersion.SOAP_11, HTTPBinding.HTTP_BINDING,false) {
alanb@368 357 @Override
ohair@286 358 public Codec createEncoder(WSBinding binding) {
ohair@286 359 return new XMLHTTPBindingCodec(binding.getFeatures());
ohair@286 360 }
ohair@286 361 };
ohair@286 362
ohair@286 363 /**
ohair@286 364 * Constant that represents REST.
ohair@286 365 */
ohair@286 366 private static final BindingID REST_HTTP = new Impl(SOAPVersion.SOAP_11, JAXWSProperties.REST_BINDING,true) {
alanb@368 367 @Override
ohair@286 368 public Codec createEncoder(WSBinding binding) {
ohair@286 369 return new XMLHTTPBindingCodec(binding.getFeatures());
ohair@286 370 }
ohair@286 371 };
ohair@286 372
ohair@286 373 private static abstract class Impl extends BindingID {
ohair@286 374 final SOAPVersion version;
ohair@286 375 private final String lexical;
ohair@286 376 private final boolean canGenerateWSDL;
ohair@286 377
ohair@286 378 public Impl(SOAPVersion version, String lexical, boolean canGenerateWSDL) {
ohair@286 379 this.version = version;
ohair@286 380 this.lexical = lexical;
ohair@286 381 this.canGenerateWSDL = canGenerateWSDL;
ohair@286 382 }
ohair@286 383
alanb@368 384 @Override
ohair@286 385 public SOAPVersion getSOAPVersion() {
ohair@286 386 return version;
ohair@286 387 }
ohair@286 388
alanb@368 389 @Override
ohair@286 390 public String toString() {
ohair@286 391 return lexical;
ohair@286 392 }
ohair@286 393
ohair@286 394 @Deprecated
alanb@368 395 @Override
ohair@286 396 public boolean canGenerateWSDL() {
ohair@286 397 return canGenerateWSDL;
ohair@286 398 }
ohair@286 399 }
ohair@286 400
ohair@286 401 /**
ohair@286 402 * Internal implementation for SOAP/HTTP.
ohair@286 403 */
ohair@286 404 private static final class SOAPHTTPImpl extends Impl implements Cloneable {
ohair@286 405 /*final*/ Map<String,String> parameters = new HashMap<String,String>();
ohair@286 406
ohair@286 407 static final String MTOM_PARAM = "mtom";
ohair@286 408
ohair@286 409 public SOAPHTTPImpl(SOAPVersion version, String lexical, boolean canGenerateWSDL) {
ohair@286 410 super(version, lexical, canGenerateWSDL);
ohair@286 411 }
ohair@286 412
ohair@286 413 public SOAPHTTPImpl(SOAPVersion version, String lexical, boolean canGenerateWSDL,
ohair@286 414 boolean mtomEnabled) {
ohair@286 415 this(version, lexical, canGenerateWSDL);
ohair@286 416 String mtomStr = mtomEnabled ? "true" : "false";
ohair@286 417 parameters.put(MTOM_PARAM, mtomStr);
ohair@286 418 }
ohair@286 419
alanb@368 420 public @NotNull @Override Codec createEncoder(WSBinding binding) {
ohair@286 421 return new SOAPBindingCodec(binding.getFeatures());
ohair@286 422 }
ohair@286 423
ohair@286 424 private Boolean isMTOMEnabled() {
ohair@286 425 String mtom = parameters.get(MTOM_PARAM);
ohair@286 426 return mtom==null?null:Boolean.valueOf(mtom);
ohair@286 427 }
ohair@286 428
alanb@368 429 @Override
ohair@286 430 public WebServiceFeatureList createBuiltinFeatureList() {
ohair@286 431 WebServiceFeatureList r=super.createBuiltinFeatureList();
ohair@286 432 Boolean mtom = isMTOMEnabled();
ohair@286 433 if(mtom != null)
ohair@286 434 r.add(new MTOMFeature(mtom));
ohair@286 435 return r;
ohair@286 436 }
ohair@286 437
alanb@368 438 @Override
ohair@286 439 public String getParameter(String parameterName, String defaultValue) {
ohair@286 440 if (parameters.get(parameterName) == null)
ohair@286 441 return super.getParameter(parameterName, defaultValue);
ohair@286 442 return parameters.get(parameterName);
ohair@286 443 }
alanb@368 444
alanb@368 445 @Override
alanb@368 446 public SOAPHTTPImpl clone() throws CloneNotSupportedException {
alanb@368 447 return (SOAPHTTPImpl) super.clone();
alanb@368 448 }
ohair@286 449 }
ohair@286 450 }

mercurial