src/share/jaxws_classes/com/sun/xml/internal/ws/binding/BindingImpl.java

Sun, 15 Dec 2013 23:35:45 +0100

author
mkos
date
Sun, 15 Dec 2013 23:35:45 +0100
changeset 494
2fcd3ddb57a6
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025152: Enhance activation set up
8028388: 9 jaxws tests failed in nightly build with java.lang.ClassCastException
Summary: fix also reviewed by Bill Shannon, Alexander Fomin
Reviewed-by: dfuchs, hawtin, mgrebac
Contributed-by: bill.shannon@oracle.com

ohair@286 1 /*
alanb@368 2 * Copyright (c) 1997, 2013, 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.binding;
ohair@286 27
alanb@368 28 import com.oracle.webservices.internal.api.message.MessageContextFactory;
ohair@286 29 import com.sun.istack.internal.NotNull;
ohair@286 30 import com.sun.istack.internal.Nullable;
ohair@286 31 import com.sun.xml.internal.ws.api.BindingID;
ohair@286 32 import com.sun.xml.internal.ws.api.SOAPVersion;
ohair@286 33 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 34 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
ohair@286 35 import com.sun.xml.internal.ws.api.pipe.Codec;
ohair@286 36 import com.sun.xml.internal.ws.client.HandlerConfiguration;
ohair@286 37 import com.sun.xml.internal.ws.developer.MemberSubmissionAddressingFeature;
ohair@286 38 import com.sun.xml.internal.ws.developer.BindingTypeFeature;
ohair@286 39
mkos@494 40 import javax.activation.CommandInfo;
mkos@494 41 import javax.activation.CommandMap;
mkos@494 42 import javax.activation.MailcapCommandMap;
ohair@286 43 import javax.xml.namespace.QName;
ohair@286 44 import javax.xml.ws.Service;
ohair@286 45 import javax.xml.ws.WebServiceFeature;
ohair@286 46 import javax.xml.ws.soap.AddressingFeature;
ohair@286 47 import javax.xml.ws.handler.Handler;
ohair@286 48 import java.util.Collections;
ohair@286 49 import java.util.HashMap;
alanb@368 50 import java.util.HashSet;
ohair@286 51 import java.util.List;
ohair@286 52 import java.util.Set;
ohair@286 53 import java.util.Map;
ohair@286 54
alanb@368 55
ohair@286 56 /**
ohair@286 57 * Instances are created by the service, which then
ohair@286 58 * sets the handler chain on the binding impl.
ohair@286 59 *
ohair@286 60 * <p>
ohair@286 61 * This class is made abstract as we don't see a situation when
ohair@286 62 * a BindingImpl has much meaning without binding id.
ohair@286 63 * IOW, for a specific binding there will be a class
ohair@286 64 * extending BindingImpl, for example SOAPBindingImpl.
ohair@286 65 *
ohair@286 66 * <p>
ohair@286 67 * The spi Binding interface extends Binding.
ohair@286 68 *
ohair@286 69 * @author WS Development Team
ohair@286 70 */
ohair@286 71 public abstract class BindingImpl implements WSBinding {
ohair@286 72
ohair@286 73 protected static final WebServiceFeature[] EMPTY_FEATURES = new WebServiceFeature[0];
ohair@286 74
ohair@286 75 //This is reset when ever Binding.setHandlerChain() or SOAPBinding.setRoles() is called.
alanb@368 76 private HandlerConfiguration handlerConfig;
alanb@368 77 private final Set<QName> addedHeaders = new HashSet<QName>();
alanb@368 78 private final Set<QName> knownHeaders = new HashSet<QName>();
alanb@368 79 private final Set<QName> unmodKnownHeaders = Collections.unmodifiableSet(knownHeaders);
ohair@286 80 private final BindingID bindingId;
ohair@286 81 // Features that are set(enabled/disabled) on the binding
alanb@368 82 protected final WebServiceFeatureList features;
ohair@286 83 // Features that are set(enabled/disabled) on the binding or an operation
ohair@286 84 protected final Map<QName, WebServiceFeatureList> operationFeatures = new HashMap<QName, WebServiceFeatureList>();
ohair@286 85 // Features that are set(enabled/disabled) on the binding, an operation or an input message
ohair@286 86 protected final Map<QName, WebServiceFeatureList> inputMessageFeatures = new HashMap<QName, WebServiceFeatureList>();
ohair@286 87 // Features that are set(enabled/disabled) on the binding, an operation or an output message
ohair@286 88 protected final Map<QName, WebServiceFeatureList> outputMessageFeatures = new HashMap<QName, WebServiceFeatureList>();
ohair@286 89 // Features that are set(enabled/disabled) on the binding, an operation or a fault message
ohair@286 90 protected final Map<MessageKey, WebServiceFeatureList> faultMessageFeatures = new HashMap<MessageKey, WebServiceFeatureList>();
ohair@286 91
ohair@286 92 protected javax.xml.ws.Service.Mode serviceMode = javax.xml.ws.Service.Mode.PAYLOAD;
ohair@286 93
alanb@368 94 protected MessageContextFactory messageContextFactory;
alanb@368 95
ohair@286 96 protected BindingImpl(BindingID bindingId, WebServiceFeature ... features) {
ohair@286 97 this.bindingId = bindingId;
ohair@286 98 handlerConfig = new HandlerConfiguration(Collections.<String>emptySet(), Collections.<Handler>emptyList());
alanb@368 99 if (handlerConfig.getHandlerKnownHeaders() != null)
alanb@368 100 knownHeaders.addAll(handlerConfig.getHandlerKnownHeaders());
alanb@368 101 this.features = new WebServiceFeatureList(features);
alanb@368 102 this.features.validate();
ohair@286 103 }
ohair@286 104
ohair@286 105 public
ohair@286 106 @NotNull
ohair@286 107 List<Handler> getHandlerChain() {
ohair@286 108 return handlerConfig.getHandlerChain();
ohair@286 109 }
ohair@286 110
ohair@286 111 public HandlerConfiguration getHandlerConfig() {
ohair@286 112 return handlerConfig;
ohair@286 113 }
ohair@286 114
alanb@368 115 protected void setHandlerConfig(HandlerConfiguration handlerConfig) {
alanb@368 116 this.handlerConfig = handlerConfig;
alanb@368 117 knownHeaders.clear();
alanb@368 118 knownHeaders.addAll(addedHeaders);
alanb@368 119 if (handlerConfig != null && handlerConfig.getHandlerKnownHeaders() != null)
alanb@368 120 knownHeaders.addAll(handlerConfig.getHandlerKnownHeaders());
alanb@368 121 }
ohair@286 122
ohair@286 123 public void setMode(@NotNull Service.Mode mode) {
ohair@286 124 this.serviceMode = mode;
ohair@286 125 }
ohair@286 126
ohair@286 127 public Set<QName> getKnownHeaders() {
alanb@368 128 return unmodKnownHeaders;
alanb@368 129 }
alanb@368 130
alanb@368 131 public boolean addKnownHeader(QName headerQName) {
alanb@368 132 addedHeaders.add(headerQName);
alanb@368 133 return knownHeaders.add(headerQName);
ohair@286 134 }
ohair@286 135
ohair@286 136 public
ohair@286 137 @NotNull
ohair@286 138 BindingID getBindingId() {
ohair@286 139 return bindingId;
ohair@286 140 }
ohair@286 141
ohair@286 142 public final SOAPVersion getSOAPVersion() {
ohair@286 143 return bindingId.getSOAPVersion();
ohair@286 144 }
ohair@286 145
ohair@286 146 public AddressingVersion getAddressingVersion() {
ohair@286 147 AddressingVersion addressingVersion;
ohair@286 148 if (features.isEnabled(AddressingFeature.class))
ohair@286 149 addressingVersion = AddressingVersion.W3C;
ohair@286 150 else if (features.isEnabled(MemberSubmissionAddressingFeature.class))
ohair@286 151 addressingVersion = AddressingVersion.MEMBER;
ohair@286 152 else
ohair@286 153 addressingVersion = null;
ohair@286 154 return addressingVersion;
ohair@286 155 }
ohair@286 156
ohair@286 157 @NotNull
mkos@494 158 public final Codec createCodec() {
mkos@494 159
mkos@494 160 // initialization from here should cover most of cases;
mkos@494 161 // if not, it would be necessary to call
mkos@494 162 // BindingImpl.initializeJavaActivationHandlers()
mkos@494 163 // explicitly by programmer
mkos@494 164 initializeJavaActivationHandlers();
mkos@494 165
ohair@286 166 return bindingId.createEncoder(this);
ohair@286 167 }
ohair@286 168
mkos@494 169 public static void initializeJavaActivationHandlers() {
mkos@494 170 // DataHandler.writeTo() may search for DCH. So adding some default ones.
mkos@494 171 try {
mkos@494 172 CommandMap map = CommandMap.getDefaultCommandMap();
mkos@494 173 if (map instanceof MailcapCommandMap) {
mkos@494 174 MailcapCommandMap mailMap = (MailcapCommandMap) map;
mkos@494 175
mkos@494 176 // registering our DCH since javamail's DCH doesn't handle
mkos@494 177 if (!cmdMapInitialized(mailMap)) {
mkos@494 178 mailMap.addMailcap("text/xml;;x-java-content-handler=com.sun.xml.internal.ws.encoding.XmlDataContentHandler");
mkos@494 179 mailMap.addMailcap("application/xml;;x-java-content-handler=com.sun.xml.internal.ws.encoding.XmlDataContentHandler");
mkos@494 180 mailMap.addMailcap("image/*;;x-java-content-handler=com.sun.xml.internal.ws.encoding.ImageDataContentHandler");
mkos@494 181 mailMap.addMailcap("text/plain;;x-java-content-handler=com.sun.xml.internal.ws.encoding.StringDataContentHandler");
mkos@494 182 }
mkos@494 183 }
mkos@494 184 } catch (Throwable t) {
mkos@494 185 // ignore the exception.
mkos@494 186 }
mkos@494 187 }
mkos@494 188
mkos@494 189 private static boolean cmdMapInitialized(MailcapCommandMap mailMap) {
mkos@494 190 CommandInfo[] commands = mailMap.getAllCommands("text/xml");
mkos@494 191 if (commands == null || commands.length == 0) {
mkos@494 192 return false;
mkos@494 193 }
mkos@494 194
mkos@494 195 // SAAJ RI implements it's own DataHandlers which can be used for JAX-WS too;
mkos@494 196 // see com.sun.xml.internal.messaging.saaj.soap.AttachmentPartImpl#initializeJavaActivationHandlers
mkos@494 197 // so if found any of SAAJ or our own handler registered, we are ok; anyway using SAAJ directly here
mkos@494 198 // is not good idea since we don't want standalone JAX-WS to depend on specific SAAJ impl.
mkos@494 199 // This is also reason for duplication of Handler's code by JAX-WS
mkos@494 200 String saajClassName = "com.sun.xml.internal.messaging.saaj.soap.XmlDataContentHandler";
mkos@494 201 String jaxwsClassName = "com.sun.xml.internal.ws.encoding.XmlDataContentHandler";
mkos@494 202 for (CommandInfo command : commands) {
mkos@494 203 String commandClass = command.getCommandClass();
mkos@494 204 if (saajClassName.equals(commandClass) ||
mkos@494 205 jaxwsClassName.equals(commandClass)) {
mkos@494 206 return true;
mkos@494 207 }
mkos@494 208 }
mkos@494 209 return false;
mkos@494 210 }
mkos@494 211
ohair@286 212 public static BindingImpl create(@NotNull BindingID bindingId) {
ohair@286 213 if (bindingId.equals(BindingID.XML_HTTP))
ohair@286 214 return new HTTPBindingImpl();
ohair@286 215 else
ohair@286 216 return new SOAPBindingImpl(bindingId);
ohair@286 217 }
ohair@286 218
ohair@286 219 public static BindingImpl create(@NotNull BindingID bindingId, WebServiceFeature[] features) {
ohair@286 220 // Override the BindingID from the features
ohair@286 221 for(WebServiceFeature feature : features) {
ohair@286 222 if (feature instanceof BindingTypeFeature) {
ohair@286 223 BindingTypeFeature f = (BindingTypeFeature)feature;
ohair@286 224 bindingId = BindingID.parse(f.getBindingId());
ohair@286 225 }
ohair@286 226 }
ohair@286 227 if (bindingId.equals(BindingID.XML_HTTP))
ohair@286 228 return new HTTPBindingImpl(features);
ohair@286 229 else
ohair@286 230 return new SOAPBindingImpl(bindingId, features);
ohair@286 231 }
ohair@286 232
ohair@286 233 public static WSBinding getDefaultBinding() {
ohair@286 234 return new SOAPBindingImpl(BindingID.SOAP11_HTTP);
ohair@286 235 }
ohair@286 236
ohair@286 237 public String getBindingID() {
ohair@286 238 return bindingId.toString();
ohair@286 239 }
ohair@286 240
ohair@286 241 public @Nullable <F extends WebServiceFeature> F getFeature(@NotNull Class<F> featureType){
ohair@286 242 return features.get(featureType);
ohair@286 243 }
ohair@286 244
ohair@286 245 public @Nullable <F extends WebServiceFeature> F getOperationFeature(@NotNull Class<F> featureType,
ohair@286 246 @NotNull final QName operationName) {
ohair@286 247 final WebServiceFeatureList operationFeatureList = this.operationFeatures.get(operationName);
ohair@286 248 return FeatureListUtil.mergeFeature(featureType, operationFeatureList, features);
ohair@286 249 }
ohair@286 250
ohair@286 251 public boolean isFeatureEnabled(@NotNull Class<? extends WebServiceFeature> feature){
ohair@286 252 return features.isEnabled(feature);
ohair@286 253 }
ohair@286 254
ohair@286 255 public boolean isOperationFeatureEnabled(@NotNull Class<? extends WebServiceFeature> featureType,
ohair@286 256 @NotNull final QName operationName) {
ohair@286 257 final WebServiceFeatureList operationFeatureList = this.operationFeatures.get(operationName);
ohair@286 258 return FeatureListUtil.isFeatureEnabled(featureType, operationFeatureList, features);
ohair@286 259 }
ohair@286 260
ohair@286 261 @NotNull
ohair@286 262 public WebServiceFeatureList getFeatures() {
ohair@286 263 //TODO scchen convert BindingID to WebServiceFeature[]
alanb@368 264 if(!isFeatureEnabled(com.oracle.webservices.internal.api.EnvelopeStyleFeature.class)) {
ohair@286 265 WebServiceFeature[] f = { getSOAPVersion().toFeature() };
ohair@286 266 features.mergeFeatures(f, false);
ohair@286 267 }
ohair@286 268 return features;
ohair@286 269 }
ohair@286 270
ohair@286 271 public @NotNull WebServiceFeatureList getOperationFeatures(@NotNull final QName operationName) {
ohair@286 272 final WebServiceFeatureList operationFeatureList = this.operationFeatures.get(operationName);
ohair@286 273 return FeatureListUtil.mergeList(operationFeatureList, features);
ohair@286 274 }
ohair@286 275
ohair@286 276 public @NotNull WebServiceFeatureList getInputMessageFeatures(@NotNull final QName operationName) {
ohair@286 277 final WebServiceFeatureList operationFeatureList = this.operationFeatures.get(operationName);
ohair@286 278 final WebServiceFeatureList messageFeatureList = this.inputMessageFeatures.get(operationName);
ohair@286 279 return FeatureListUtil.mergeList(operationFeatureList, messageFeatureList, features);
ohair@286 280
ohair@286 281 }
ohair@286 282
ohair@286 283 public @NotNull WebServiceFeatureList getOutputMessageFeatures(@NotNull final QName operationName) {
ohair@286 284 final WebServiceFeatureList operationFeatureList = this.operationFeatures.get(operationName);
ohair@286 285 final WebServiceFeatureList messageFeatureList = this.outputMessageFeatures.get(operationName);
ohair@286 286 return FeatureListUtil.mergeList(operationFeatureList, messageFeatureList, features);
ohair@286 287 }
ohair@286 288
ohair@286 289 public @NotNull WebServiceFeatureList getFaultMessageFeatures(@NotNull final QName operationName,
ohair@286 290 @NotNull final QName messageName) {
ohair@286 291 final WebServiceFeatureList operationFeatureList = this.operationFeatures.get(operationName);
ohair@286 292 final WebServiceFeatureList messageFeatureList = this.faultMessageFeatures.get(
ohair@286 293 new MessageKey(operationName, messageName));
ohair@286 294 return FeatureListUtil.mergeList(operationFeatureList, messageFeatureList, features);
ohair@286 295 }
ohair@286 296
ohair@286 297 public void setOperationFeatures(@NotNull final QName operationName, WebServiceFeature... newFeatures) {
ohair@286 298 if (newFeatures != null) {
ohair@286 299 WebServiceFeatureList featureList = operationFeatures.get(operationName);
ohair@286 300 if (featureList == null) {
ohair@286 301 featureList = new WebServiceFeatureList();
ohair@286 302 }
ohair@286 303 for (WebServiceFeature f : newFeatures) {
ohair@286 304 featureList.add(f);
ohair@286 305 }
ohair@286 306 operationFeatures.put(operationName, featureList);
ohair@286 307 }
ohair@286 308 }
ohair@286 309
ohair@286 310 public void setInputMessageFeatures(@NotNull final QName operationName, WebServiceFeature... newFeatures) {
ohair@286 311 if (newFeatures != null) {
ohair@286 312 WebServiceFeatureList featureList = inputMessageFeatures.get(operationName);
ohair@286 313 if (featureList == null) {
ohair@286 314 featureList = new WebServiceFeatureList();
ohair@286 315 }
ohair@286 316 for (WebServiceFeature f : newFeatures) {
ohair@286 317 featureList.add(f);
ohair@286 318 }
ohair@286 319 inputMessageFeatures.put(operationName, featureList);
ohair@286 320 }
ohair@286 321 }
ohair@286 322
ohair@286 323 public void setOutputMessageFeatures(@NotNull final QName operationName, WebServiceFeature... newFeatures) {
ohair@286 324 if (newFeatures != null) {
ohair@286 325 WebServiceFeatureList featureList = outputMessageFeatures.get(operationName);
ohair@286 326 if (featureList == null) {
ohair@286 327 featureList = new WebServiceFeatureList();
ohair@286 328 }
ohair@286 329 for (WebServiceFeature f : newFeatures) {
ohair@286 330 featureList.add(f);
ohair@286 331 }
ohair@286 332 outputMessageFeatures.put(operationName, featureList);
ohair@286 333 }
ohair@286 334 }
ohair@286 335
ohair@286 336 public void setFaultMessageFeatures(@NotNull final QName operationName, @NotNull final QName messageName, WebServiceFeature... newFeatures) {
ohair@286 337 if (newFeatures != null) {
ohair@286 338 final MessageKey key = new MessageKey(operationName, messageName);
ohair@286 339 WebServiceFeatureList featureList = faultMessageFeatures.get(key);
ohair@286 340 if (featureList == null) {
ohair@286 341 featureList = new WebServiceFeatureList();
ohair@286 342 }
ohair@286 343 for (WebServiceFeature f : newFeatures) {
ohair@286 344 featureList.add(f);
ohair@286 345 }
ohair@286 346 faultMessageFeatures.put(key, featureList);
ohair@286 347 }
ohair@286 348 }
ohair@286 349
alanb@368 350 public synchronized @NotNull com.oracle.webservices.internal.api.message.MessageContextFactory getMessageContextFactory () {
alanb@368 351 if (messageContextFactory == null) {
alanb@368 352 messageContextFactory = MessageContextFactory.createFactory(getFeatures().toArray());
alanb@368 353 }
alanb@368 354 return messageContextFactory;
ohair@286 355 }
ohair@286 356
ohair@286 357 /**
ohair@286 358 * Experimental: Identify messages based on the name of the message and the
ohair@286 359 * operation that uses this message.
ohair@286 360 */
ohair@286 361 protected static class MessageKey {
ohair@286 362
ohair@286 363 final private QName operationName;
ohair@286 364 final private QName messageName;
ohair@286 365
ohair@286 366 public MessageKey(final QName operationName, final QName messageName) {
ohair@286 367 this.operationName = operationName;
ohair@286 368 this.messageName = messageName;
ohair@286 369 }
ohair@286 370
ohair@286 371 @Override
ohair@286 372 public int hashCode() {
ohair@286 373 final int hashFirst = this.operationName != null ? this.operationName.hashCode() : 0;
ohair@286 374 final int hashSecond = this.messageName != null ? this.messageName.hashCode() : 0;
ohair@286 375
ohair@286 376 return (hashFirst + hashSecond) * hashSecond + hashFirst;
ohair@286 377 }
ohair@286 378
ohair@286 379 @Override
ohair@286 380 public boolean equals(Object obj) {
ohair@286 381 if (obj == null) {
ohair@286 382 return false;
ohair@286 383 }
ohair@286 384 if (getClass() != obj.getClass()) {
ohair@286 385 return false;
ohair@286 386 }
ohair@286 387 final MessageKey other = (MessageKey) obj;
ohair@286 388 if (this.operationName != other.operationName && (this.operationName == null || !this.operationName.equals(other.operationName))) {
ohair@286 389 return false;
ohair@286 390 }
ohair@286 391 if (this.messageName != other.messageName && (this.messageName == null || !this.messageName.equals(other.messageName))) {
ohair@286 392 return false;
ohair@286 393 }
ohair@286 394 return true;
ohair@286 395 }
ohair@286 396
ohair@286 397 @Override
ohair@286 398 public String toString() {
ohair@286 399 return "(" + this.operationName + ", " + this.messageName + ")";
ohair@286 400 }
ohair@286 401
ohair@286 402 }
ohair@286 403
ohair@286 404 }

mercurial