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