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

Wed, 12 Jun 2013 14:47:09 +0100

author
mkos
date
Wed, 12 Jun 2013 14:47:09 +0100
changeset 384
8f2986ff0235
parent 0
373ffda63c9a
permissions
-rw-r--r--

8013021: Rebase 8005432 & 8003542 against the latest jdk8/jaxws
8003542: Improve processing of MTOM attachments
8005432: Update access to JAX-WS
Reviewed-by: mullan

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, 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.api.message;
aoqi@0 27
aoqi@0 28 import java.util.Iterator;
aoqi@0 29
aoqi@0 30 import javax.xml.namespace.QName;
aoqi@0 31 import javax.xml.stream.XMLStreamException;
aoqi@0 32 import javax.xml.ws.WebServiceException;
aoqi@0 33 import javax.xml.ws.soap.SOAPBinding;
aoqi@0 34
aoqi@0 35 import com.sun.istack.internal.NotNull;
aoqi@0 36 import com.sun.xml.internal.ws.addressing.WsaTubeHelper;
aoqi@0 37 import com.sun.xml.internal.ws.api.SOAPVersion;
aoqi@0 38 import com.sun.xml.internal.ws.api.WSBinding;
aoqi@0 39 import com.sun.xml.internal.ws.api.addressing.AddressingPropertySet;
aoqi@0 40 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
aoqi@0 41 import com.sun.xml.internal.ws.api.addressing.OneWayFeature;
aoqi@0 42 import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
aoqi@0 43 import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation;
aoqi@0 44 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
aoqi@0 45 import com.sun.xml.internal.ws.message.RelatesToHeader;
aoqi@0 46 import com.sun.xml.internal.ws.message.StringHeader;
aoqi@0 47 import com.sun.xml.internal.ws.resources.AddressingMessages;
aoqi@0 48 import com.sun.xml.internal.ws.resources.ClientMessages;
aoqi@0 49
aoqi@0 50 public class AddressingUtils {
aoqi@0 51 //TODO is MessageHeaders to be implicitly assumed? Or moved to utility class and taken out from interface?
aoqi@0 52 public static void fillRequestAddressingHeaders(MessageHeaders headers, Packet packet, AddressingVersion av, SOAPVersion sv, boolean oneway, String action) {
aoqi@0 53 fillRequestAddressingHeaders(headers, packet, av, sv, oneway, action, false);
aoqi@0 54 }
aoqi@0 55 public static void fillRequestAddressingHeaders(MessageHeaders headers, Packet packet, AddressingVersion av, SOAPVersion sv, boolean oneway, String action, boolean mustUnderstand) {
aoqi@0 56 fillCommonAddressingHeaders(headers, packet, av, sv, action, mustUnderstand);
aoqi@0 57
aoqi@0 58 // wsa:ReplyTo
aoqi@0 59 // null or "true" is equivalent to request/response MEP
aoqi@0 60 if (!oneway) {
aoqi@0 61 WSEndpointReference epr = av.anonymousEpr;
aoqi@0 62 if (headers.get(av.replyToTag, false) == null) {
aoqi@0 63 headers.add(epr.createHeader(av.replyToTag));
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 // wsa:FaultTo
aoqi@0 67 if (headers.get(av.faultToTag, false) == null) {
aoqi@0 68 headers.add(epr.createHeader(av.faultToTag));
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 // wsa:MessageID
aoqi@0 72 if (packet.getMessage().getHeaders().get(av.messageIDTag, false) == null) {
aoqi@0 73 if (headers.get(av.messageIDTag, false) == null) {
aoqi@0 74 Header h = new StringHeader(av.messageIDTag, Message.generateMessageID());
aoqi@0 75 headers.add(h);
aoqi@0 76 }
aoqi@0 77 }
aoqi@0 78 }
aoqi@0 79 }
aoqi@0 80 // private void fillRequestAddressingHeaders(Packet packet, AddressingVersion av, SOAPVersion sv, OneWayFeature oneWayFeature, boolean oneway, String action);
aoqi@0 81 public static void fillRequestAddressingHeaders(MessageHeaders headers, WSDLPort wsdlPort, WSBinding binding, Packet packet) {
aoqi@0 82 if (binding == null) {
aoqi@0 83 throw new IllegalArgumentException(AddressingMessages.NULL_BINDING());
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 if (binding.isFeatureEnabled(SuppressAutomaticWSARequestHeadersFeature.class)) {
aoqi@0 87 return;
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 //See if WSA headers are already set by the user.
aoqi@0 91 MessageHeaders hl = packet.getMessage().getHeaders();
aoqi@0 92 String action = AddressingUtils.getAction(hl, binding.getAddressingVersion(), binding.getSOAPVersion());
aoqi@0 93 if (action != null) {
aoqi@0 94 //assume that all the WSA headers are set by the user
aoqi@0 95 return;
aoqi@0 96 }
aoqi@0 97 AddressingVersion addressingVersion = binding.getAddressingVersion();
aoqi@0 98 //seiModel is passed as null as it is not needed.
aoqi@0 99 WsaTubeHelper wsaHelper = addressingVersion.getWsaHelper(wsdlPort, null, binding);
aoqi@0 100
aoqi@0 101 // wsa:Action
aoqi@0 102 String effectiveInputAction = wsaHelper.getEffectiveInputAction(packet);
aoqi@0 103 if (effectiveInputAction == null || effectiveInputAction.equals("") && binding.getSOAPVersion() == SOAPVersion.SOAP_11) {
aoqi@0 104 throw new WebServiceException(ClientMessages.INVALID_SOAP_ACTION());
aoqi@0 105 }
aoqi@0 106 boolean oneway = !packet.expectReply;
aoqi@0 107 if (wsdlPort != null) {
aoqi@0 108 // if WSDL has <wsaw:Anonymous>prohibited</wsaw:Anonymous>, then throw an error
aoqi@0 109 // as anonymous ReplyTo MUST NOT be added in that case. BindingProvider need to
aoqi@0 110 // disable AddressingFeature and MemberSubmissionAddressingFeature and hand-craft
aoqi@0 111 // the SOAP message with non-anonymous ReplyTo/FaultTo.
aoqi@0 112 if (!oneway && packet.getMessage() != null && packet.getWSDLOperation() != null) {
aoqi@0 113 WSDLBoundOperation wbo = wsdlPort.getBinding().get(packet.getWSDLOperation());
aoqi@0 114 if (wbo != null && wbo.getAnonymous() == WSDLBoundOperation.ANONYMOUS.prohibited) {
aoqi@0 115 throw new WebServiceException(AddressingMessages.WSAW_ANONYMOUS_PROHIBITED());
aoqi@0 116 }
aoqi@0 117 }
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 OneWayFeature oneWayFeature = binding.getFeature(OneWayFeature.class);
aoqi@0 121 final AddressingPropertySet addressingPropertySet = packet.getSatellite(AddressingPropertySet.class);
aoqi@0 122 oneWayFeature = addressingPropertySet == null ? oneWayFeature : new OneWayFeature(addressingPropertySet, addressingVersion);
aoqi@0 123
aoqi@0 124 if (oneWayFeature == null || !oneWayFeature.isEnabled()) {
aoqi@0 125 // standard oneway
aoqi@0 126 fillRequestAddressingHeaders(headers, packet, addressingVersion, binding.getSOAPVersion(), oneway, effectiveInputAction, AddressingVersion.isRequired(binding));
aoqi@0 127 } else {
aoqi@0 128 // custom oneway
aoqi@0 129 fillRequestAddressingHeaders(headers, packet, addressingVersion, binding.getSOAPVersion(), oneWayFeature, oneway, effectiveInputAction);
aoqi@0 130 }
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 public static String getAction(@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
aoqi@0 134 if (av == null) {
aoqi@0 135 throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 String action = null;
aoqi@0 139 Header h = getFirstHeader(headers, av.actionTag, true, sv);
aoqi@0 140 if (h != null) {
aoqi@0 141 action = h.getStringContent();
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 return action;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 public static WSEndpointReference getFaultTo(@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
aoqi@0 148 if (av == null) {
aoqi@0 149 throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 Header h = getFirstHeader(headers, av.faultToTag, true, sv);
aoqi@0 153 WSEndpointReference faultTo = null;
aoqi@0 154 if (h != null) {
aoqi@0 155 try {
aoqi@0 156 faultTo = h.readAsEPR(av);
aoqi@0 157 } catch (XMLStreamException e) {
aoqi@0 158 throw new WebServiceException(AddressingMessages.FAULT_TO_CANNOT_PARSE(), e);
aoqi@0 159 }
aoqi@0 160 }
aoqi@0 161
aoqi@0 162 return faultTo;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 public static String getMessageID(@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
aoqi@0 166 if (av == null) {
aoqi@0 167 throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 Header h = getFirstHeader(headers, av.messageIDTag, true, sv);
aoqi@0 171 String messageId = null;
aoqi@0 172 if (h != null) {
aoqi@0 173 messageId = h.getStringContent();
aoqi@0 174 }
aoqi@0 175
aoqi@0 176 return messageId;
aoqi@0 177 }
aoqi@0 178 public static String getRelatesTo(@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
aoqi@0 179 if (av == null) {
aoqi@0 180 throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 Header h = getFirstHeader(headers, av.relatesToTag, true, sv);
aoqi@0 184 String relatesTo = null;
aoqi@0 185 if (h != null) {
aoqi@0 186 relatesTo = h.getStringContent();
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 return relatesTo;
aoqi@0 190 }
aoqi@0 191 public static WSEndpointReference getReplyTo(@NotNull MessageHeaders headers, @NotNull AddressingVersion av, @NotNull SOAPVersion sv) {
aoqi@0 192 if (av == null) {
aoqi@0 193 throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 Header h = getFirstHeader(headers, av.replyToTag, true, sv);
aoqi@0 197 WSEndpointReference replyTo;
aoqi@0 198 if (h != null) {
aoqi@0 199 try {
aoqi@0 200 replyTo = h.readAsEPR(av);
aoqi@0 201 } catch (XMLStreamException e) {
aoqi@0 202 throw new WebServiceException(AddressingMessages.REPLY_TO_CANNOT_PARSE(), e);
aoqi@0 203 }
aoqi@0 204 } else {
aoqi@0 205 replyTo = av.anonymousEpr;
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 return replyTo;
aoqi@0 209 }
aoqi@0 210 public static String getTo(MessageHeaders headers, AddressingVersion av, SOAPVersion sv) {
aoqi@0 211 if (av == null) {
aoqi@0 212 throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 Header h = getFirstHeader(headers, av.toTag, true, sv);
aoqi@0 216 String to;
aoqi@0 217 if (h != null) {
aoqi@0 218 to = h.getStringContent();
aoqi@0 219 } else {
aoqi@0 220 to = av.anonymousUri;
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 return to;
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 public static Header getFirstHeader(MessageHeaders headers, QName name, boolean markUnderstood, SOAPVersion sv) {
aoqi@0 227 if (sv == null) {
aoqi@0 228 throw new IllegalArgumentException(AddressingMessages.NULL_SOAP_VERSION());
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 Iterator<Header> iter = headers.getHeaders(name.getNamespaceURI(), name.getLocalPart(), markUnderstood);
aoqi@0 232 while (iter.hasNext()) {
aoqi@0 233 Header h = iter.next();
aoqi@0 234 if (h.getRole(sv).equals(sv.implicitRole)) {
aoqi@0 235 return h;
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 return null;
aoqi@0 240 }
aoqi@0 241
aoqi@0 242 private static void fillRequestAddressingHeaders(@NotNull MessageHeaders headers, @NotNull Packet packet, @NotNull AddressingVersion av, @NotNull SOAPVersion sv, @NotNull OneWayFeature oneWayFeature, boolean oneway, @NotNull String action) {
aoqi@0 243 if (!oneway&&!oneWayFeature.isUseAsyncWithSyncInvoke() && Boolean.TRUE.equals(packet.isSynchronousMEP)) {
aoqi@0 244 fillRequestAddressingHeaders(headers, packet, av, sv, oneway, action);
aoqi@0 245 } else {
aoqi@0 246 fillCommonAddressingHeaders(headers, packet, av, sv, action, false);
aoqi@0 247
aoqi@0 248 boolean isMessageIdAdded = false;
aoqi@0 249
aoqi@0 250 // wsa:ReplyTo
aoqi@0 251 // add if it doesn't already exist and OneWayFeature requests a specific ReplyTo
aoqi@0 252 if (headers.get(av.replyToTag, false) == null) {
aoqi@0 253 WSEndpointReference replyToEpr = oneWayFeature.getReplyTo();
aoqi@0 254 if (replyToEpr != null) {
aoqi@0 255 headers.add(replyToEpr.createHeader(av.replyToTag));
aoqi@0 256 // add wsa:MessageID only for non-null ReplyTo
aoqi@0 257 if (packet.getMessage().getHeaders().get(av.messageIDTag, false) == null) {
aoqi@0 258 // if header doesn't exist, method getID creates a new random id
aoqi@0 259 String newID = oneWayFeature.getMessageId() == null ? Message.generateMessageID() : oneWayFeature.getMessageId();
aoqi@0 260 headers.add(new StringHeader(av.messageIDTag, newID));
aoqi@0 261 isMessageIdAdded = true;
aoqi@0 262 }
aoqi@0 263 }
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 // If the user sets a messageId, use it.
aoqi@0 267 final String messageId = oneWayFeature.getMessageId();
aoqi@0 268 if (!isMessageIdAdded && messageId != null) {
aoqi@0 269 headers.add(new StringHeader(av.messageIDTag, messageId));
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 // wsa:FaultTo
aoqi@0 273 // add if it doesn't already exist and OneWayFeature requests a specific FaultTo
aoqi@0 274 if (headers.get(av.faultToTag, false) == null) {
aoqi@0 275 WSEndpointReference faultToEpr = oneWayFeature.getFaultTo();
aoqi@0 276 if (faultToEpr != null) {
aoqi@0 277 headers.add(faultToEpr.createHeader(av.faultToTag));
aoqi@0 278 // add wsa:MessageID only for non-null FaultTo
aoqi@0 279 if (headers.get(av.messageIDTag, false) == null) {
aoqi@0 280 headers.add(new StringHeader(av.messageIDTag, Message.generateMessageID()));
aoqi@0 281 }
aoqi@0 282 }
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 // wsa:From
aoqi@0 286 if (oneWayFeature.getFrom() != null) {
aoqi@0 287 headers.addOrReplace(oneWayFeature.getFrom().createHeader(av.fromTag));
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 // wsa:RelatesTo
aoqi@0 291 if (oneWayFeature.getRelatesToID() != null) {
aoqi@0 292 headers.addOrReplace(new RelatesToHeader(av.relatesToTag, oneWayFeature.getRelatesToID()));
aoqi@0 293 }
aoqi@0 294 }
aoqi@0 295 }
aoqi@0 296
aoqi@0 297 /**
aoqi@0 298 * Creates wsa:To, wsa:Action and wsa:MessageID header on the client
aoqi@0 299 *
aoqi@0 300 * @param packet request packet
aoqi@0 301 * @param av WS-Addressing version
aoqi@0 302 * @param sv SOAP version
aoqi@0 303 * @param action Action Message Addressing Property value
aoqi@0 304 * @throws IllegalArgumentException if any of the parameters is null.
aoqi@0 305 */
aoqi@0 306 private static void fillCommonAddressingHeaders(MessageHeaders headers, Packet packet, @NotNull AddressingVersion av, @NotNull SOAPVersion sv, @NotNull String action, boolean mustUnderstand) {
aoqi@0 307 if (packet == null) {
aoqi@0 308 throw new IllegalArgumentException(AddressingMessages.NULL_PACKET());
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 if (av == null) {
aoqi@0 312 throw new IllegalArgumentException(AddressingMessages.NULL_ADDRESSING_VERSION());
aoqi@0 313 }
aoqi@0 314
aoqi@0 315 if (sv == null) {
aoqi@0 316 throw new IllegalArgumentException(AddressingMessages.NULL_SOAP_VERSION());
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 if (action == null && !sv.httpBindingId.equals(SOAPBinding.SOAP12HTTP_BINDING)) {
aoqi@0 320 throw new IllegalArgumentException(AddressingMessages.NULL_ACTION());
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 // wsa:To
aoqi@0 324 if (headers.get(av.toTag, false) == null) {
aoqi@0 325 StringHeader h = new StringHeader(av.toTag, packet.endpointAddress.toString());
aoqi@0 326 headers.add(h);
aoqi@0 327 }
aoqi@0 328
aoqi@0 329 // wsa:Action
aoqi@0 330 if (action != null) {
aoqi@0 331 packet.soapAction = action;
aoqi@0 332 if (headers.get(av.actionTag, false) == null) {
aoqi@0 333 //As per WS-I BP 1.2/2.0, if one of the WSA headers is MU, then all WSA headers should be treated as MU.,
aoqi@0 334 // so just set MU on action header
aoqi@0 335 StringHeader h = new StringHeader(av.actionTag, action, sv, mustUnderstand);
aoqi@0 336 headers.add(h);
aoqi@0 337 }
aoqi@0 338 }
aoqi@0 339 }
aoqi@0 340
aoqi@0 341
aoqi@0 342 }

mercurial