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

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/ws/client/RequestContext.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,382 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, 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.client;
    1.30 +
    1.31 +import com.oracle.webservices.internal.api.message.BaseDistributedPropertySet;
    1.32 +import com.sun.istack.internal.NotNull;
    1.33 +import com.sun.xml.internal.ws.api.EndpointAddress;
    1.34 +import com.sun.xml.internal.ws.api.message.Packet;
    1.35 +import com.sun.xml.internal.ws.transport.Headers;
    1.36 +
    1.37 +import javax.xml.ws.BindingProvider;
    1.38 +import java.util.HashMap;
    1.39 +import java.util.HashSet;
    1.40 +import java.util.List;
    1.41 +import java.util.Map;
    1.42 +import java.util.Map.Entry;
    1.43 +import java.util.Set;
    1.44 +import java.util.logging.Logger;
    1.45 +
    1.46 +
    1.47 +import static javax.xml.ws.BindingProvider.*;
    1.48 +import static javax.xml.ws.handler.MessageContext.HTTP_REQUEST_HEADERS;
    1.49 +
    1.50 +/**
    1.51 + * Request context implementation.
    1.52 + *
    1.53 + * <h2>Why a custom map?</h2>
    1.54 + * <p>
    1.55 + * The JAX-WS spec exposes properties as a {@link Map}, but if we just use
    1.56 + * an ordinary {@link HashMap} for this, it doesn't work as fast as we'd like
    1.57 + * it to be. Hence we have this class.
    1.58 + *
    1.59 + * <p>
    1.60 + * We expect the user to set a few properties and then use that same
    1.61 + * setting to make a bunch of invocations. So we'd like to take some hit
    1.62 + * when the user actually sets a property to do some computation,
    1.63 + * then use that computed value during a method invocation again and again.
    1.64 + *
    1.65 + * <p>
    1.66 + * For this goal, we use {@link com.sun.xml.internal.ws.api.PropertySet} and implement some properties
    1.67 + * as virtual properties backed by methods. This allows us to do the computation
    1.68 + * in the setter, and store it in a field.
    1.69 + *
    1.70 + * <p>
    1.71 + * These fields are used by {@link Stub#process} to populate a {@link Packet}.
    1.72 + *
    1.73 + * <h2>How it works?</h2>
    1.74 + * <p>
    1.75 + * For better performance, we wan't use strongly typed field as much as possible
    1.76 + * to avoid reflection and unnecessary collection iterations;
    1.77 + *
    1.78 + * Using {@link com.oracle.webservices.internal.api.message.BasePropertySet.MapView} implementation allows client to use {@link Map} interface
    1.79 + * in a way that all the strongly typed properties are reflected to the fields
    1.80 + * right away. Any additional (extending) properties can be added by client as well;
    1.81 + * those would be processed using iterating the {@link MapView} and their processing,
    1.82 + * of course, would be slower.
    1.83 + * <p>
    1.84 + * The previous implementation with fallback mode has been removed to simplify
    1.85 + * the code and remove the bugs.
    1.86 + *
    1.87 + * @author Kohsuke Kawaguchi
    1.88 + */
    1.89 +@SuppressWarnings({"SuspiciousMethodCalls"})
    1.90 +public final class RequestContext extends BaseDistributedPropertySet {
    1.91 +    private static final Logger LOGGER = Logger.getLogger(RequestContext.class.getName());
    1.92 +
    1.93 +    /**
    1.94 +     * The default value to be use for {@link #contentNegotiation} obtained
    1.95 +     * from a system property.
    1.96 +     * <p>
    1.97 +     * This enables content negotiation to be easily switched on by setting
    1.98 +     * a system property on the command line for testing purposes tests.
    1.99 +     */
   1.100 +    private static ContentNegotiation defaultContentNegotiation =
   1.101 +            ContentNegotiation.obtainFromSystemProperty();
   1.102 +
   1.103 +    /**
   1.104 +     * @deprecated
   1.105 +     */
   1.106 +    public void addSatellite(@NotNull com.sun.xml.internal.ws.api.PropertySet satellite) {
   1.107 +        super.addSatellite(satellite);
   1.108 +    }
   1.109 +
   1.110 +    /**
   1.111 +     * The endpoint address to which this message is sent to.
   1.112 +     *
   1.113 +     * <p>
   1.114 +     * This is the actual data store for {@link BindingProvider#ENDPOINT_ADDRESS_PROPERTY}.
   1.115 +     */
   1.116 +    private @NotNull EndpointAddress endpointAddress;
   1.117 +
   1.118 +    /**
   1.119 +     * Creates {@link BindingProvider#ENDPOINT_ADDRESS_PROPERTY} view
   1.120 +     * on top of {@link #endpointAddress}.
   1.121 +     *
   1.122 +     * @deprecated
   1.123 +     *      always access {@link #endpointAddress}.
   1.124 +     */
   1.125 +    @Property(ENDPOINT_ADDRESS_PROPERTY)
   1.126 +    public String getEndPointAddressString() {
   1.127 +        return endpointAddress != null ? endpointAddress.toString() : null;
   1.128 +    }
   1.129 +
   1.130 +    public void setEndPointAddressString(String s) {
   1.131 +        if (s == null) {
   1.132 +            throw new IllegalArgumentException();
   1.133 +        } else {
   1.134 +            this.endpointAddress = EndpointAddress.create(s);
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    public void setEndpointAddress(@NotNull EndpointAddress epa) {
   1.139 +        this.endpointAddress = epa;
   1.140 +    }
   1.141 +
   1.142 +    public @NotNull EndpointAddress getEndpointAddress() {
   1.143 +        return endpointAddress;
   1.144 +    }
   1.145 +
   1.146 +    /**
   1.147 +     * The value of {@link ContentNegotiation#PROPERTY}
   1.148 +     * property.
   1.149 +     */
   1.150 +    public ContentNegotiation contentNegotiation = defaultContentNegotiation;
   1.151 +
   1.152 +    @Property(ContentNegotiation.PROPERTY)
   1.153 +    public String getContentNegotiationString() {
   1.154 +        return contentNegotiation.toString();
   1.155 +    }
   1.156 +
   1.157 +    public void setContentNegotiationString(String s) {
   1.158 +        if (s == null) {
   1.159 +            contentNegotiation = ContentNegotiation.none;
   1.160 +        } else {
   1.161 +            try {
   1.162 +                contentNegotiation = ContentNegotiation.valueOf(s);
   1.163 +            } catch (IllegalArgumentException e) {
   1.164 +                // If the value is not recognized default to none
   1.165 +                contentNegotiation = ContentNegotiation.none;
   1.166 +            }
   1.167 +        }
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * The value of the SOAPAction header associated with the message.
   1.172 +     *
   1.173 +     * <p>
   1.174 +     * For outgoing messages, the transport may sends out this value.
   1.175 +     * If this field is null, the transport may choose to send <tt>""</tt>
   1.176 +     * (quoted empty string.)
   1.177 +     *
   1.178 +     * For incoming messages, the transport will set this field.
   1.179 +     * If the incoming message did not contain the SOAPAction header,
   1.180 +     * the transport sets this field to null.
   1.181 +     *
   1.182 +     * <p>
   1.183 +     * If the value is non-null, it must be always in the quoted form.
   1.184 +     * The value can be null.
   1.185 +     *
   1.186 +     * <p>
   1.187 +     * Note that the way the transport sends this value out depends on
   1.188 +     * transport and SOAP version.
   1.189 +     *
   1.190 +     * For HTTP transport and SOAP 1.1, BP requires that SOAPAction
   1.191 +     * header is present (See {@BP R2744} and {@BP R2745}.) For SOAP 1.2,
   1.192 +     * this is moved to the parameter of the "application/soap+xml".
   1.193 +     */
   1.194 +
   1.195 +    private String soapAction;
   1.196 +
   1.197 +    @Property(SOAPACTION_URI_PROPERTY)
   1.198 +    public String getSoapAction() {
   1.199 +        return soapAction;
   1.200 +    }
   1.201 +
   1.202 +    public void setSoapAction(String sAction) {
   1.203 +        soapAction = sAction;
   1.204 +    }
   1.205 +
   1.206 +    /**
   1.207 +     * This controls whether BindingProvider.SOAPACTION_URI_PROPERTY is used.
   1.208 +     * See BindingProvider.SOAPACTION_USE_PROPERTY for details.
   1.209 +     *
   1.210 +     * This only control whether value of BindingProvider.SOAPACTION_URI_PROPERTY is used or not and not
   1.211 +     * if it can be sent if it can be obtained by other means such as WSDL binding
   1.212 +     */
   1.213 +    private Boolean soapActionUse;
   1.214 +
   1.215 +    @Property(SOAPACTION_USE_PROPERTY)
   1.216 +    public Boolean getSoapActionUse() {
   1.217 +        return soapActionUse;
   1.218 +    }
   1.219 +
   1.220 +    public void setSoapActionUse(Boolean sActionUse) {
   1.221 +        soapActionUse = sActionUse;
   1.222 +    }
   1.223 +
   1.224 +    /**
   1.225 +     * Creates an empty {@link RequestContext}.
   1.226 +     */
   1.227 +    RequestContext() {
   1.228 +    }
   1.229 +
   1.230 +    /**
   1.231 +     * Copy constructor.
   1.232 +     */
   1.233 +    private RequestContext(RequestContext that) {
   1.234 +        for (Map.Entry<String, Object> entry : that.asMapLocal().entrySet()) {
   1.235 +            if (!propMap.containsKey(entry.getKey())) {
   1.236 +                asMap().put(entry.getKey(), entry.getValue());
   1.237 +            }
   1.238 +        }
   1.239 +        endpointAddress = that.endpointAddress;
   1.240 +        soapAction = that.soapAction;
   1.241 +        soapActionUse = that.soapActionUse;
   1.242 +        contentNegotiation = that.contentNegotiation;
   1.243 +        that.copySatelliteInto(this);
   1.244 +    }
   1.245 +
   1.246 +    /**
   1.247 +     * The efficient get method that reads from {@link RequestContext}.
   1.248 +     */
   1.249 +    @Override
   1.250 +    public Object get(Object key) {
   1.251 +        if(supports(key)) {
   1.252 +            return super.get(key);
   1.253 +        } else {
   1.254 +            // use mapView to get extending property
   1.255 +            return asMap().get(key);
   1.256 +        }
   1.257 +    }
   1.258 +
   1.259 +    /**
   1.260 +     * The efficient put method that updates {@link RequestContext}.
   1.261 +     */
   1.262 +    @Override
   1.263 +    public Object put(String key, Object value) {
   1.264 +
   1.265 +        if(supports(key)) {
   1.266 +            return super.put(key,value);
   1.267 +        } else {
   1.268 +            // use mapView to put extending property (if the map allows that)
   1.269 +            return asMap().put(key, value);
   1.270 +        }
   1.271 +    }
   1.272 +
   1.273 +    /**
   1.274 +     * Fill a {@link Packet} with values of this {@link RequestContext}.
   1.275 +     *
   1.276 +     * @param packet              to be filled with context values
   1.277 +     * @param isAddressingEnabled flag if addressing enabled (to provide warning if necessary)
   1.278 +     */
   1.279 +    @SuppressWarnings("unchecked")
   1.280 +    public void fill(Packet packet, boolean isAddressingEnabled) {
   1.281 +
   1.282 +        // handling as many properties as possible (all in propMap.keySet())
   1.283 +        // to avoid slow Packet.put()
   1.284 +        if (endpointAddress != null) {
   1.285 +            packet.endpointAddress = endpointAddress;
   1.286 +        }
   1.287 +        packet.contentNegotiation = contentNegotiation;
   1.288 +        fillSOAPAction(packet, isAddressingEnabled);
   1.289 +        mergeRequestHeaders(packet);
   1.290 +
   1.291 +        Set<String> handlerScopeNames = new HashSet<String>();
   1.292 +
   1.293 +        copySatelliteInto(packet);
   1.294 +
   1.295 +        // extending properties ...
   1.296 +        for (String key : asMapLocal().keySet()) {
   1.297 +
   1.298 +            //if it is not standard property it defaults to Scope.HANDLER
   1.299 +            if (!supportsLocal(key)) {
   1.300 +                handlerScopeNames.add(key);
   1.301 +            }
   1.302 +
   1.303 +            // to avoid slow Packet.put(), handle as small number of props as possible
   1.304 +            // => only properties not from RequestContext object
   1.305 +            if (!propMap.containsKey(key)) {
   1.306 +                Object value = asMapLocal().get(key);
   1.307 +                if (packet.supports(key)) {
   1.308 +                    // very slow operation - try to avoid it!
   1.309 +                    packet.put(key, value);
   1.310 +                } else {
   1.311 +                    packet.invocationProperties.put(key, value);
   1.312 +                }
   1.313 +            }
   1.314 +        }
   1.315 +
   1.316 +        if (!handlerScopeNames.isEmpty()) {
   1.317 +            packet.getHandlerScopePropertyNames(false).addAll(handlerScopeNames);
   1.318 +        }
   1.319 +    }
   1.320 +
   1.321 +    @SuppressWarnings("unchecked")
   1.322 +    private void mergeRequestHeaders(Packet packet) {
   1.323 +        //for bug 12883765
   1.324 +        //retrieve headers which is set in soap message
   1.325 +        Headers packetHeaders = (Headers) packet.invocationProperties.get(HTTP_REQUEST_HEADERS);
   1.326 +        //retrieve headers from request context
   1.327 +        Map<String, List<String>> myHeaders = (Map<String, List<String>>) asMap().get(HTTP_REQUEST_HEADERS);
   1.328 +        if ((packetHeaders != null) && (myHeaders != null)) {
   1.329 +            //update the headers set in soap message with those in request context
   1.330 +            for (Entry<String, List<String>> entry : myHeaders.entrySet()) {
   1.331 +                String key = entry.getKey();
   1.332 +                if (key != null && key.trim().length() != 0) {
   1.333 +                    List<String> listFromPacket = packetHeaders.get(key);
   1.334 +                    //if the two headers contain the same key, combine the value
   1.335 +                    if (listFromPacket != null) {
   1.336 +                        listFromPacket.addAll(entry.getValue());
   1.337 +                    } else {
   1.338 +                        //add the headers  in request context to those set in soap message
   1.339 +                        packetHeaders.put(key, myHeaders.get(key));
   1.340 +                    }
   1.341 +                }
   1.342 +            }
   1.343 +            // update headers in request context with those set in soap message since it may contain other properties..
   1.344 +            asMap().put(HTTP_REQUEST_HEADERS, packetHeaders);
   1.345 +        }
   1.346 +    }
   1.347 +
   1.348 +    private void fillSOAPAction(Packet packet, boolean isAddressingEnabled) {
   1.349 +        final boolean p = packet.packetTakesPriorityOverRequestContext;
   1.350 +        final String  localSoapAction    = p ? packet.soapAction : soapAction;
   1.351 +        final Boolean localSoapActionUse = p ? (Boolean) packet.invocationProperties.get(BindingProvider.SOAPACTION_USE_PROPERTY)
   1.352 +                                             : soapActionUse;
   1.353 +
   1.354 +        //JAX-WS-596: Check the semantics of SOAPACTION_USE_PROPERTY before using the SOAPACTION_URI_PROPERTY for
   1.355 +        // SoapAction as specified in the javadoc of BindingProvider. The spec seems to be little contradicting with
   1.356 +        //  javadoc and says that the use property effects the sending of SOAPAction property.
   1.357 +        // Since the user has the capability to set the value as "" if needed, implement the javadoc behavior.
   1.358 +        if ((localSoapActionUse != null && localSoapActionUse) || (localSoapActionUse == null && isAddressingEnabled)) {
   1.359 +            if (localSoapAction != null) {
   1.360 +                packet.soapAction = localSoapAction;
   1.361 +            }
   1.362 +        }
   1.363 +
   1.364 +        if ((!isAddressingEnabled && (localSoapActionUse == null || !localSoapActionUse)) && localSoapAction != null) {
   1.365 +            LOGGER.warning("BindingProvider.SOAPACTION_URI_PROPERTY is set in the RequestContext but is ineffective," +
   1.366 +                    " Either set BindingProvider.SOAPACTION_USE_PROPERTY to true or enable AddressingFeature");
   1.367 +        }
   1.368 +    }
   1.369 +
   1.370 +    public RequestContext copy() {
   1.371 +        return new RequestContext(this);
   1.372 +    }
   1.373 +
   1.374 +    @Override
   1.375 +    protected PropertyMap getPropertyMap() {
   1.376 +        return propMap;
   1.377 +    }
   1.378 +
   1.379 +    private static final PropertyMap propMap = parse(RequestContext.class);
   1.380 +
   1.381 +    @Override
   1.382 +    protected boolean mapAllowsAdditionalProperties() {
   1.383 +        return true;
   1.384 +    }
   1.385 +}

mercurial