src/share/jaxws_classes/com/sun/xml/internal/ws/client/RequestContext.java

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

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.client;
ohair@286 27
alanb@368 28 import com.oracle.webservices.internal.api.message.BaseDistributedPropertySet;
ohair@286 29 import com.sun.istack.internal.NotNull;
ohair@286 30 import com.sun.xml.internal.ws.api.EndpointAddress;
ohair@286 31 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 32 import com.sun.xml.internal.ws.transport.Headers;
ohair@286 33
ohair@286 34 import javax.xml.ws.BindingProvider;
ohair@286 35 import java.util.HashMap;
ohair@286 36 import java.util.HashSet;
ohair@286 37 import java.util.List;
ohair@286 38 import java.util.Map;
ohair@286 39 import java.util.Map.Entry;
ohair@286 40 import java.util.Set;
ohair@286 41 import java.util.logging.Logger;
ohair@286 42
alanb@368 43
alanb@368 44 import static javax.xml.ws.BindingProvider.*;
alanb@368 45 import static javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS;
alanb@368 46
ohair@286 47 /**
ohair@286 48 * Request context implementation.
ohair@286 49 *
ohair@286 50 * <h2>Why a custom map?</h2>
ohair@286 51 * <p>
ohair@286 52 * The JAX-WS spec exposes properties as a {@link Map}, but if we just use
ohair@286 53 * an ordinary {@link HashMap} for this, it doesn't work as fast as we'd like
ohair@286 54 * it to be. Hence we have this class.
ohair@286 55 *
ohair@286 56 * <p>
ohair@286 57 * We expect the user to set a few properties and then use that same
ohair@286 58 * setting to make a bunch of invocations. So we'd like to take some hit
ohair@286 59 * when the user actually sets a property to do some computation,
ohair@286 60 * then use that computed value during a method invocation again and again.
ohair@286 61 *
ohair@286 62 * <p>
alanb@368 63 * For this goal, we use {@link com.sun.xml.internal.ws.api.PropertySet} and implement some properties
ohair@286 64 * as virtual properties backed by methods. This allows us to do the computation
ohair@286 65 * in the setter, and store it in a field.
ohair@286 66 *
ohair@286 67 * <p>
ohair@286 68 * These fields are used by {@link Stub#process} to populate a {@link Packet}.
ohair@286 69 *
ohair@286 70 * <h2>How it works?</h2>
ohair@286 71 * <p>
alanb@368 72 * For better performance, we wan't use strongly typed field as much as possible
alanb@368 73 * to avoid reflection and unnecessary collection iterations;
ohair@286 74 *
alanb@368 75 * Using {@link com.oracle.webservices.internal.api.message.BasePropertySet.MapView} implementation allows client to use {@link Map} interface
alanb@368 76 * in a way that all the strongly typed properties are reflected to the fields
alanb@368 77 * right away. Any additional (extending) properties can be added by client as well;
alanb@368 78 * those would be processed using iterating the {@link MapView} and their processing,
alanb@368 79 * of course, would be slower.
ohair@286 80 * <p>
alanb@368 81 * The previous implementation with fallback mode has been removed to simplify
alanb@368 82 * the code and remove the bugs.
ohair@286 83 *
ohair@286 84 * @author Kohsuke Kawaguchi
ohair@286 85 */
ohair@286 86 @SuppressWarnings({"SuspiciousMethodCalls"})
alanb@368 87 public final class RequestContext extends BaseDistributedPropertySet {
ohair@286 88 private static final Logger LOGGER = Logger.getLogger(RequestContext.class.getName());
ohair@286 89
ohair@286 90 /**
ohair@286 91 * The default value to be use for {@link #contentNegotiation} obtained
ohair@286 92 * from a system property.
ohair@286 93 * <p>
ohair@286 94 * This enables content negotiation to be easily switched on by setting
ohair@286 95 * a system property on the command line for testing purposes tests.
ohair@286 96 */
ohair@286 97 private static ContentNegotiation defaultContentNegotiation =
ohair@286 98 ContentNegotiation.obtainFromSystemProperty();
ohair@286 99
ohair@286 100 /**
alanb@368 101 * @deprecated
ohair@286 102 */
alanb@368 103 public void addSatellite(@NotNull com.sun.xml.internal.ws.api.PropertySet satellite) {
alanb@368 104 super.addSatellite(satellite);
alanb@368 105 }
ohair@286 106
ohair@286 107 /**
ohair@286 108 * The endpoint address to which this message is sent to.
ohair@286 109 *
ohair@286 110 * <p>
ohair@286 111 * This is the actual data store for {@link BindingProvider#ENDPOINT_ADDRESS_PROPERTY}.
ohair@286 112 */
ohair@286 113 private @NotNull EndpointAddress endpointAddress;
ohair@286 114
ohair@286 115 /**
ohair@286 116 * Creates {@link BindingProvider#ENDPOINT_ADDRESS_PROPERTY} view
ohair@286 117 * on top of {@link #endpointAddress}.
ohair@286 118 *
ohair@286 119 * @deprecated
ohair@286 120 * always access {@link #endpointAddress}.
ohair@286 121 */
alanb@368 122 @Property(ENDPOINT_ADDRESS_PROPERTY)
ohair@286 123 public String getEndPointAddressString() {
ohair@286 124 return endpointAddress != null ? endpointAddress.toString() : null;
ohair@286 125 }
ohair@286 126
ohair@286 127 public void setEndPointAddressString(String s) {
alanb@368 128 if (s == null) {
ohair@286 129 throw new IllegalArgumentException();
alanb@368 130 } else {
ohair@286 131 this.endpointAddress = EndpointAddress.create(s);
alanb@368 132 }
ohair@286 133 }
ohair@286 134
ohair@286 135 public void setEndpointAddress(@NotNull EndpointAddress epa) {
ohair@286 136 this.endpointAddress = epa;
ohair@286 137 }
ohair@286 138
ohair@286 139 public @NotNull EndpointAddress getEndpointAddress() {
ohair@286 140 return endpointAddress;
ohair@286 141 }
ohair@286 142
ohair@286 143 /**
ohair@286 144 * The value of {@link ContentNegotiation#PROPERTY}
ohair@286 145 * property.
ohair@286 146 */
ohair@286 147 public ContentNegotiation contentNegotiation = defaultContentNegotiation;
ohair@286 148
ohair@286 149 @Property(ContentNegotiation.PROPERTY)
ohair@286 150 public String getContentNegotiationString() {
ohair@286 151 return contentNegotiation.toString();
ohair@286 152 }
ohair@286 153
ohair@286 154 public void setContentNegotiationString(String s) {
alanb@368 155 if (s == null) {
ohair@286 156 contentNegotiation = ContentNegotiation.none;
alanb@368 157 } else {
ohair@286 158 try {
ohair@286 159 contentNegotiation = ContentNegotiation.valueOf(s);
ohair@286 160 } catch (IllegalArgumentException e) {
ohair@286 161 // If the value is not recognized default to none
ohair@286 162 contentNegotiation = ContentNegotiation.none;
ohair@286 163 }
ohair@286 164 }
ohair@286 165 }
alanb@368 166
ohair@286 167 /**
ohair@286 168 * The value of the SOAPAction header associated with the message.
ohair@286 169 *
ohair@286 170 * <p>
ohair@286 171 * For outgoing messages, the transport may sends out this value.
ohair@286 172 * If this field is null, the transport may choose to send <tt>""</tt>
ohair@286 173 * (quoted empty string.)
ohair@286 174 *
ohair@286 175 * For incoming messages, the transport will set this field.
ohair@286 176 * If the incoming message did not contain the SOAPAction header,
ohair@286 177 * the transport sets this field to null.
ohair@286 178 *
ohair@286 179 * <p>
ohair@286 180 * If the value is non-null, it must be always in the quoted form.
ohair@286 181 * The value can be null.
ohair@286 182 *
ohair@286 183 * <p>
ohair@286 184 * Note that the way the transport sends this value out depends on
ohair@286 185 * transport and SOAP version.
ohair@286 186 *
ohair@286 187 * For HTTP transport and SOAP 1.1, BP requires that SOAPAction
ohair@286 188 * header is present (See {@BP R2744} and {@BP R2745}.) For SOAP 1.2,
ohair@286 189 * this is moved to the parameter of the "application/soap+xml".
ohair@286 190 */
ohair@286 191
ohair@286 192 private String soapAction;
ohair@286 193
alanb@368 194 @Property(SOAPACTION_URI_PROPERTY)
alanb@368 195 public String getSoapAction() {
ohair@286 196 return soapAction;
ohair@286 197 }
alanb@368 198
alanb@368 199 public void setSoapAction(String sAction) {
ohair@286 200 soapAction = sAction;
ohair@286 201 }
ohair@286 202
ohair@286 203 /**
ohair@286 204 * This controls whether BindingProvider.SOAPACTION_URI_PROPERTY is used.
ohair@286 205 * See BindingProvider.SOAPACTION_USE_PROPERTY for details.
ohair@286 206 *
ohair@286 207 * This only control whether value of BindingProvider.SOAPACTION_URI_PROPERTY is used or not and not
ohair@286 208 * if it can be sent if it can be obtained by other means such as WSDL binding
ohair@286 209 */
ohair@286 210 private Boolean soapActionUse;
alanb@368 211
alanb@368 212 @Property(SOAPACTION_USE_PROPERTY)
alanb@368 213 public Boolean getSoapActionUse() {
ohair@286 214 return soapActionUse;
ohair@286 215 }
alanb@368 216
alanb@368 217 public void setSoapActionUse(Boolean sActionUse) {
ohair@286 218 soapActionUse = sActionUse;
ohair@286 219 }
ohair@286 220
ohair@286 221 /**
ohair@286 222 * Creates an empty {@link RequestContext}.
ohair@286 223 */
alanb@368 224 RequestContext() {
ohair@286 225 }
ohair@286 226
ohair@286 227 /**
ohair@286 228 * Copy constructor.
ohair@286 229 */
ohair@286 230 private RequestContext(RequestContext that) {
alanb@368 231 for (Map.Entry<String, Object> entry : that.asMapLocal().entrySet()) {
alanb@368 232 if (!propMap.containsKey(entry.getKey())) {
alanb@368 233 asMap().put(entry.getKey(), entry.getValue());
alanb@368 234 }
alanb@368 235 }
ohair@286 236 endpointAddress = that.endpointAddress;
ohair@286 237 soapAction = that.soapAction;
alanb@368 238 soapActionUse = that.soapActionUse;
ohair@286 239 contentNegotiation = that.contentNegotiation;
ohair@286 240 that.copySatelliteInto(this);
ohair@286 241 }
ohair@286 242
ohair@286 243 /**
ohair@286 244 * The efficient get method that reads from {@link RequestContext}.
ohair@286 245 */
alanb@368 246 @Override
ohair@286 247 public Object get(Object key) {
alanb@368 248 if(supports(key)) {
ohair@286 249 return super.get(key);
alanb@368 250 } else {
alanb@368 251 // use mapView to get extending property
alanb@368 252 return asMap().get(key);
alanb@368 253 }
ohair@286 254 }
ohair@286 255
ohair@286 256 /**
ohair@286 257 * The efficient put method that updates {@link RequestContext}.
ohair@286 258 */
alanb@368 259 @Override
ohair@286 260 public Object put(String key, Object value) {
alanb@368 261
alanb@368 262 if(supports(key)) {
ohair@286 263 return super.put(key,value);
alanb@368 264 } else {
alanb@368 265 // use mapView to put extending property (if the map allows that)
alanb@368 266 return asMap().put(key, value);
alanb@368 267 }
ohair@286 268 }
ohair@286 269
ohair@286 270 /**
ohair@286 271 * Fill a {@link Packet} with values of this {@link RequestContext}.
alanb@368 272 *
alanb@368 273 * @param packet to be filled with context values
alanb@368 274 * @param isAddressingEnabled flag if addressing enabled (to provide warning if necessary)
ohair@286 275 */
alanb@368 276 @SuppressWarnings("unchecked")
ohair@286 277 public void fill(Packet packet, boolean isAddressingEnabled) {
ohair@286 278
alanb@368 279 // handling as many properties as possible (all in propMap.keySet())
alanb@368 280 // to avoid slow Packet.put()
alanb@368 281 if (endpointAddress != null) {
alanb@368 282 packet.endpointAddress = endpointAddress;
alanb@368 283 }
alanb@368 284 packet.contentNegotiation = contentNegotiation;
alanb@368 285 fillSOAPAction(packet, isAddressingEnabled);
alanb@368 286 mergeRequestHeaders(packet);
ohair@286 287
alanb@368 288 Set<String> handlerScopeNames = new HashSet<String>();
alanb@368 289
alanb@368 290 copySatelliteInto(packet);
alanb@368 291
alanb@368 292 // extending properties ...
alanb@368 293 for (String key : asMapLocal().keySet()) {
alanb@368 294
alanb@368 295 //if it is not standard property it defaults to Scope.HANDLER
alanb@368 296 if (!supportsLocal(key)) {
alanb@368 297 handlerScopeNames.add(key);
alanb@368 298 }
alanb@368 299
alanb@368 300 // to avoid slow Packet.put(), handle as small number of props as possible
alanb@368 301 // => only properties not from RequestContext object
alanb@368 302 if (!propMap.containsKey(key)) {
alanb@368 303 Object value = asMapLocal().get(key);
alanb@368 304 if (packet.supports(key)) {
alanb@368 305 // very slow operation - try to avoid it!
alanb@368 306 packet.put(key, value);
alanb@368 307 } else {
alanb@368 308 packet.invocationProperties.put(key, value);
ohair@286 309 }
ohair@286 310 }
alanb@368 311 }
ohair@286 312
alanb@368 313 if (!handlerScopeNames.isEmpty()) {
alanb@368 314 packet.getHandlerScopePropertyNames(false).addAll(handlerScopeNames);
alanb@368 315 }
alanb@368 316 }
ohair@286 317
alanb@368 318 @SuppressWarnings("unchecked")
alanb@368 319 private void mergeRequestHeaders(Packet packet) {
alanb@368 320 //for bug 12883765
alanb@368 321 //retrieve headers which is set in soap message
alanb@368 322 Headers packetHeaders = (Headers) packet.invocationProperties.get(HTTP_REQUEST_HEADERS);
alanb@368 323 //retrieve headers from request context
alanb@368 324 Map<String, List<String>> myHeaders = (Map<String, List<String>>) asMap().get(HTTP_REQUEST_HEADERS);
alanb@368 325 if ((packetHeaders != null) && (myHeaders != null)) {
alanb@368 326 //update the headers set in soap message with those in request context
alanb@368 327 for (Entry<String, List<String>> entry : myHeaders.entrySet()) {
alanb@368 328 String key = entry.getKey();
alanb@368 329 if (key != null && key.trim().length() != 0) {
alanb@368 330 List<String> listFromPacket = packetHeaders.get(key);
alanb@368 331 //if the two headers contain the same key, combine the value
alanb@368 332 if (listFromPacket != null) {
alanb@368 333 listFromPacket.addAll(entry.getValue());
alanb@368 334 } else {
alanb@368 335 //add the headers in request context to those set in soap message
alanb@368 336 packetHeaders.put(key, myHeaders.get(key));
ohair@286 337 }
ohair@286 338 }
ohair@286 339 }
alanb@368 340 // update headers in request context with those set in soap message since it may contain other properties..
alanb@368 341 asMap().put(HTTP_REQUEST_HEADERS, packetHeaders);
alanb@368 342 }
alanb@368 343 }
ohair@286 344
alanb@368 345 private void fillSOAPAction(Packet packet, boolean isAddressingEnabled) {
alanb@368 346 final boolean p = packet.packetTakesPriorityOverRequestContext;
alanb@368 347 final String localSoapAction = p ? packet.soapAction : soapAction;
alanb@368 348 final Boolean localSoapActionUse = p ? (Boolean) packet.invocationProperties.get(BindingProvider.SOAPACTION_USE_PROPERTY)
alanb@368 349 : soapActionUse;
alanb@368 350
alanb@368 351 //JAX-WS-596: Check the semantics of SOAPACTION_USE_PROPERTY before using the SOAPACTION_URI_PROPERTY for
alanb@368 352 // SoapAction as specified in the javadoc of BindingProvider. The spec seems to be little contradicting with
alanb@368 353 // javadoc and says that the use property effects the sending of SOAPAction property.
alanb@368 354 // Since the user has the capability to set the value as "" if needed, implement the javadoc behavior.
alanb@368 355 if ((localSoapActionUse != null && localSoapActionUse) || (localSoapActionUse == null && isAddressingEnabled)) {
alanb@368 356 if (localSoapAction != null) {
alanb@368 357 packet.soapAction = localSoapAction;
alanb@368 358 }
alanb@368 359 }
alanb@368 360
alanb@368 361 if ((!isAddressingEnabled && (localSoapActionUse == null || !localSoapActionUse)) && localSoapAction != null) {
alanb@368 362 LOGGER.warning("BindingProvider.SOAPACTION_URI_PROPERTY is set in the RequestContext but is ineffective," +
alanb@368 363 " Either set BindingProvider.SOAPACTION_USE_PROPERTY to true or enable AddressingFeature");
ohair@286 364 }
ohair@286 365 }
ohair@286 366
ohair@286 367 public RequestContext copy() {
ohair@286 368 return new RequestContext(this);
ohair@286 369 }
ohair@286 370
alanb@368 371 @Override
ohair@286 372 protected PropertyMap getPropertyMap() {
ohair@286 373 return propMap;
ohair@286 374 }
ohair@286 375
ohair@286 376 private static final PropertyMap propMap = parse(RequestContext.class);
alanb@368 377
alanb@368 378 @Override
alanb@368 379 protected boolean mapAllowsAdditionalProperties() {
alanb@368 380 return true;
alanb@368 381 }
ohair@286 382 }

mercurial