src/share/jaxws_classes/com/sun/xml/internal/ws/addressing/WsaTube.java

Fri, 14 Feb 2014 11:13:45 +0100

author
mkos
date
Fri, 14 Feb 2014 11:13:45 +0100
changeset 515
6cd506508147
parent 368
0989ad8c0860
child 637
9c07ef4934dd
permissions
-rw-r--r--

8026188: Enhance envelope factory
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu

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.addressing;
ohair@286 27
ohair@286 28 import com.sun.istack.internal.NotNull;
ohair@286 29 import com.sun.xml.internal.ws.addressing.model.InvalidAddressingHeaderException;
ohair@286 30 import com.sun.xml.internal.ws.addressing.model.MissingAddressingHeaderException;
ohair@286 31 import com.sun.xml.internal.ws.api.SOAPVersion;
ohair@286 32 import com.sun.xml.internal.ws.api.WSBinding;
ohair@286 33 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
alanb@368 34 import com.sun.xml.internal.ws.api.message.AddressingUtils;
ohair@286 35 import com.sun.xml.internal.ws.api.message.Header;
ohair@286 36 import com.sun.xml.internal.ws.api.message.Message;
ohair@286 37 import com.sun.xml.internal.ws.api.message.Messages;
ohair@286 38 import com.sun.xml.internal.ws.api.message.Packet;
ohair@286 39 import com.sun.xml.internal.ws.api.model.wsdl.WSDLBoundOperation;
ohair@286 40 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
ohair@286 41 import com.sun.xml.internal.ws.api.pipe.NextAction;
ohair@286 42 import com.sun.xml.internal.ws.api.pipe.Tube;
ohair@286 43 import com.sun.xml.internal.ws.api.pipe.TubeCloner;
ohair@286 44 import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl;
ohair@286 45 import com.sun.xml.internal.ws.developer.MemberSubmissionAddressingFeature;
ohair@286 46 import com.sun.xml.internal.ws.message.FaultDetailHeader;
ohair@286 47 import com.sun.xml.internal.ws.resources.AddressingMessages;
ohair@286 48
ohair@286 49 import javax.xml.namespace.QName;
ohair@286 50 import javax.xml.soap.SOAPFault;
ohair@286 51 import javax.xml.stream.XMLStreamException;
ohair@286 52 import javax.xml.ws.WebServiceException;
ohair@286 53 import javax.xml.ws.soap.AddressingFeature;
ohair@286 54 import javax.xml.ws.soap.SOAPBinding;
ohair@286 55 import java.util.Iterator;
ohair@286 56 import java.util.logging.Logger;
ohair@286 57 import java.util.logging.Level;
ohair@286 58
ohair@286 59 /**
ohair@286 60 * WS-Addressing processing code shared between client and server.
ohair@286 61 *
ohair@286 62 * <p>
ohair@286 63 * This tube is used only when WS-Addressing is enabled.
ohair@286 64 *
ohair@286 65 * @author Rama Pulavarthi
ohair@286 66 * @author Arun Gupta
ohair@286 67 */
ohair@286 68 abstract class WsaTube extends AbstractFilterTubeImpl {
ohair@286 69 /**
ohair@286 70 * Port that we are processing.
ohair@286 71 */
ohair@286 72 protected final @NotNull WSDLPort wsdlPort;
ohair@286 73 protected final WSBinding binding;
ohair@286 74 final WsaTubeHelper helper;
ohair@286 75 protected final @NotNull AddressingVersion addressingVersion;
ohair@286 76 protected final SOAPVersion soapVersion;
ohair@286 77
ohair@286 78 /**
ohair@286 79 * True if the addressing headers are mandatory.
ohair@286 80 */
ohair@286 81 private final boolean addressingRequired;
ohair@286 82
ohair@286 83 public WsaTube(WSDLPort wsdlPort, WSBinding binding, Tube next) {
ohair@286 84 super(next);
ohair@286 85 this.wsdlPort = wsdlPort;
ohair@286 86 this.binding = binding;
ohair@286 87 addKnownHeadersToBinding(binding);
ohair@286 88 addressingVersion = binding.getAddressingVersion();
ohair@286 89 soapVersion = binding.getSOAPVersion();
ohair@286 90 helper = getTubeHelper();
ohair@286 91 addressingRequired = AddressingVersion.isRequired(binding);
ohair@286 92 }
ohair@286 93
ohair@286 94 public WsaTube(WsaTube that, TubeCloner cloner) {
ohair@286 95 super(that, cloner);
ohair@286 96 this.wsdlPort = that.wsdlPort;
ohair@286 97 this.binding = that.binding;
ohair@286 98 this.helper = that.helper;
ohair@286 99 addressingVersion = that.addressingVersion;
ohair@286 100 soapVersion = that.soapVersion;
ohair@286 101 addressingRequired = that.addressingRequired;
ohair@286 102 }
ohair@286 103
ohair@286 104 private void addKnownHeadersToBinding(WSBinding binding) {
ohair@286 105 for (AddressingVersion addrVersion: AddressingVersion.values()) {
alanb@368 106 binding.addKnownHeader(addrVersion.actionTag);
alanb@368 107 binding.addKnownHeader(addrVersion.faultDetailTag);
alanb@368 108 binding.addKnownHeader(addrVersion.faultToTag);
alanb@368 109 binding.addKnownHeader(addrVersion.fromTag);
alanb@368 110 binding.addKnownHeader(addrVersion.messageIDTag);
alanb@368 111 binding.addKnownHeader(addrVersion.relatesToTag);
alanb@368 112 binding.addKnownHeader(addrVersion.replyToTag);
alanb@368 113 binding.addKnownHeader(addrVersion.toTag);
ohair@286 114 }
ohair@286 115 }
ohair@286 116
ohair@286 117 @Override
ohair@286 118 public @NotNull NextAction processException(Throwable t) {
ohair@286 119 return super.processException(t);
ohair@286 120 }
ohair@286 121
ohair@286 122 protected WsaTubeHelper getTubeHelper() {
ohair@286 123 if(binding.isFeatureEnabled(AddressingFeature.class)) {
ohair@286 124 return new WsaTubeHelperImpl(wsdlPort, null, binding);
ohair@286 125 } else if(binding.isFeatureEnabled(MemberSubmissionAddressingFeature.class)) {
ohair@286 126 //seiModel is null as it is not needed.
ohair@286 127 return new com.sun.xml.internal.ws.addressing.v200408.WsaTubeHelperImpl(wsdlPort, null, binding);
ohair@286 128 } else {
ohair@286 129 // Addressing is not enabled, WsaTube should not be included in the pipeline
ohair@286 130 throw new WebServiceException(AddressingMessages.ADDRESSING_NOT_ENABLED(this.getClass().getSimpleName()));
ohair@286 131 }
ohair@286 132 }
ohair@286 133
ohair@286 134 /**
ohair@286 135 * Validates the inbound message. If an error is found, create
ohair@286 136 * a fault message and returns that. Otherwise
ohair@286 137 * it will pass through the parameter 'packet' object to the return value.
ohair@286 138 */
ohair@286 139 protected Packet validateInboundHeaders(Packet packet) {
ohair@286 140 SOAPFault soapFault;
ohair@286 141 FaultDetailHeader s11FaultDetailHeader;
ohair@286 142
ohair@286 143 try {
ohair@286 144 checkMessageAddressingProperties(packet);
ohair@286 145 return packet;
ohair@286 146 } catch (InvalidAddressingHeaderException e) {
ohair@286 147 LOGGER.log(Level.WARNING,
ohair@286 148 addressingVersion.getInvalidMapText()+", Problem header:" + e.getProblemHeader()+ ", Reason: "+ e.getSubsubcode(),e);
ohair@286 149 soapFault = helper.createInvalidAddressingHeaderFault(e, addressingVersion);
ohair@286 150 s11FaultDetailHeader = new FaultDetailHeader(addressingVersion, addressingVersion.problemHeaderQNameTag.getLocalPart(), e.getProblemHeader());
ohair@286 151 } catch (MissingAddressingHeaderException e) {
ohair@286 152 LOGGER.log(Level.WARNING,addressingVersion.getMapRequiredText()+", Problem header:"+ e.getMissingHeaderQName(),e);
ohair@286 153 soapFault = helper.newMapRequiredFault(e);
ohair@286 154 s11FaultDetailHeader = new FaultDetailHeader(addressingVersion, addressingVersion.problemHeaderQNameTag.getLocalPart(), e.getMissingHeaderQName());
ohair@286 155 }
ohair@286 156
ohair@286 157 if (soapFault != null) {
ohair@286 158 // WS-A fault processing for one-way methods
ohair@286 159 if ((wsdlPort !=null) && packet.getMessage().isOneWay(wsdlPort)) {
ohair@286 160 return packet.createServerResponse(null, wsdlPort, null, binding);
ohair@286 161 }
ohair@286 162
ohair@286 163 Message m = Messages.create(soapFault);
ohair@286 164 if (soapVersion == SOAPVersion.SOAP_11) {
ohair@286 165 m.getHeaders().add(s11FaultDetailHeader);
ohair@286 166 }
ohair@286 167
ohair@286 168 return packet.createServerResponse(m, wsdlPort, null, binding);
ohair@286 169 }
ohair@286 170
ohair@286 171 return packet;
ohair@286 172 }
ohair@286 173
ohair@286 174 /**
ohair@286 175 * This method checks all the WS-Addressing headers are valid and as per the spec definded rules.
ohair@286 176 * Mainly it checks the cardinality of the WSA headers and checks that mandatory headers exist.
ohair@286 177 * It also checks if the SOAPAction is equal to wsa:Action value when non-empty.
ohair@286 178 *
ohair@286 179 * Override this method if you need to additional checking of headers other than just existence of the headers.
ohair@286 180 * For ex: On server-side, check Anonymous and Non-Anonymous semantics in addition to checking cardinality.
ohair@286 181 *
ohair@286 182 * Override checkMandatoryHeaders(Packet p) to have different validation rules for different versions
ohair@286 183 *
ohair@286 184 * @param packet
ohair@286 185 */
ohair@286 186 protected void checkMessageAddressingProperties(Packet packet) {
ohair@286 187 checkCardinality(packet);
ohair@286 188 }
ohair@286 189
ohair@286 190 final boolean isAddressingEngagedOrRequired(Packet packet, WSBinding binding) {
ohair@286 191 if (AddressingVersion.isRequired(binding))
ohair@286 192 return true;
ohair@286 193
ohair@286 194 if (packet == null)
ohair@286 195 return false;
ohair@286 196
ohair@286 197 if (packet.getMessage() == null)
ohair@286 198 return false;
ohair@286 199
ohair@286 200 if (packet.getMessage().getHeaders() != null)
ohair@286 201 return false;
ohair@286 202
alanb@368 203 String action = AddressingUtils.getAction(
alanb@368 204 packet.getMessage().getHeaders(),
alanb@368 205 addressingVersion, soapVersion);
ohair@286 206 if (action == null)
ohair@286 207 return true;
ohair@286 208
ohair@286 209 return true;
ohair@286 210 }
ohair@286 211
ohair@286 212 /**
ohair@286 213 * Checks the cardinality of WS-Addressing headers on an inbound {@link Packet}. This method
ohair@286 214 * checks for the cardinality if WS-Addressing is engaged (detected by the presence of wsa:Action
ohair@286 215 * header) or wsdl:required=true.
ohair@286 216 *
ohair@286 217 * @param packet The inbound packet.
ohair@286 218 * @throws WebServiceException if:
ohair@286 219 * <ul>
ohair@286 220 * <li>there is an error reading ReplyTo or FaultTo</li>
ohair@286 221 * <li>WS-Addressing is required and {@link Message} within <code>packet</code> is null</li>
ohair@286 222 * <li>WS-Addressing is required and no headers are found in the {@link Message}</li>
ohair@286 223 * <li>an uknown WS-Addressing header is present</li>
ohair@286 224 * </ul>
ohair@286 225 */
ohair@286 226 protected void checkCardinality(Packet packet) {
ohair@286 227 Message message = packet.getMessage();
ohair@286 228 if (message == null) {
ohair@286 229 if (addressingRequired)
ohair@286 230 throw new WebServiceException(AddressingMessages.NULL_MESSAGE());
ohair@286 231 else
ohair@286 232 return;
ohair@286 233 }
ohair@286 234
ohair@286 235 Iterator<Header> hIter = message.getHeaders().getHeaders(addressingVersion.nsUri, true);
ohair@286 236
ohair@286 237 if (!hIter.hasNext()) {
ohair@286 238 // no WS-A headers are found
ohair@286 239 if (addressingRequired)
ohair@286 240 // if WS-A is required, then throw an exception looking for wsa:Action header
ohair@286 241 throw new MissingAddressingHeaderException(addressingVersion.actionTag,packet);
ohair@286 242 else
ohair@286 243 // else no need to process
ohair@286 244 return;
ohair@286 245 }
ohair@286 246
ohair@286 247 boolean foundFrom = false;
ohair@286 248 boolean foundTo = false;
ohair@286 249 boolean foundReplyTo = false;
ohair@286 250 boolean foundFaultTo = false;
ohair@286 251 boolean foundAction = false;
ohair@286 252 boolean foundMessageId = false;
ohair@286 253 boolean foundRelatesTo = false;
ohair@286 254 QName duplicateHeader = null;
ohair@286 255
ohair@286 256 while (hIter.hasNext()) {
ohair@286 257 Header h = hIter.next();
ohair@286 258
ohair@286 259 // check if the Header is in current role
ohair@286 260 if (!isInCurrentRole(h, binding)) {
ohair@286 261 continue;
ohair@286 262 }
ohair@286 263
ohair@286 264 String local = h.getLocalPart();
ohair@286 265 if (local.equals(addressingVersion.fromTag.getLocalPart())) {
ohair@286 266 if (foundFrom) {
ohair@286 267 duplicateHeader = addressingVersion.fromTag;
ohair@286 268 break;
ohair@286 269 }
ohair@286 270 foundFrom = true;
ohair@286 271 } else if (local.equals(addressingVersion.toTag.getLocalPart())) {
ohair@286 272 if (foundTo) {
ohair@286 273 duplicateHeader = addressingVersion.toTag;
ohair@286 274 break;
ohair@286 275 }
ohair@286 276 foundTo = true;
ohair@286 277 } else if (local.equals(addressingVersion.replyToTag.getLocalPart())) {
ohair@286 278 if (foundReplyTo) {
ohair@286 279 duplicateHeader = addressingVersion.replyToTag;
ohair@286 280 break;
ohair@286 281 }
ohair@286 282 foundReplyTo = true;
ohair@286 283 try { // verify that the header is in a good shape
ohair@286 284 h.readAsEPR(addressingVersion);
ohair@286 285 } catch (XMLStreamException e) {
ohair@286 286 throw new WebServiceException(AddressingMessages.REPLY_TO_CANNOT_PARSE(), e);
ohair@286 287 }
ohair@286 288 } else if (local.equals(addressingVersion.faultToTag.getLocalPart())) {
ohair@286 289 if (foundFaultTo) {
ohair@286 290 duplicateHeader = addressingVersion.faultToTag;
ohair@286 291 break;
ohair@286 292 }
ohair@286 293 foundFaultTo = true;
ohair@286 294 try { // verify that the header is in a good shape
ohair@286 295 h.readAsEPR(addressingVersion);
ohair@286 296 } catch (XMLStreamException e) {
ohair@286 297 throw new WebServiceException(AddressingMessages.FAULT_TO_CANNOT_PARSE(), e);
ohair@286 298 }
ohair@286 299 } else if (local.equals(addressingVersion.actionTag.getLocalPart())) {
ohair@286 300 if (foundAction) {
ohair@286 301 duplicateHeader = addressingVersion.actionTag;
ohair@286 302 break;
ohair@286 303 }
ohair@286 304 foundAction = true;
ohair@286 305 } else if (local.equals(addressingVersion.messageIDTag.getLocalPart())) {
ohair@286 306 if (foundMessageId) {
ohair@286 307 duplicateHeader = addressingVersion.messageIDTag;
ohair@286 308 break;
ohair@286 309 }
ohair@286 310 foundMessageId = true;
ohair@286 311 } else if (local.equals(addressingVersion.relatesToTag.getLocalPart())) {
ohair@286 312 foundRelatesTo = true;
ohair@286 313 } else if (local.equals(addressingVersion.faultDetailTag.getLocalPart())) {
ohair@286 314 // TODO: should anything be done here ?
ohair@286 315 // TODO: fault detail element - only for SOAP 1.1
ohair@286 316 } else {
ohair@286 317 System.err.println(AddressingMessages.UNKNOWN_WSA_HEADER());
ohair@286 318 }
ohair@286 319 }
ohair@286 320
ohair@286 321 // check for invalid cardinality first before checking for mandatory headers
ohair@286 322 if (duplicateHeader != null) {
ohair@286 323 throw new InvalidAddressingHeaderException(duplicateHeader, addressingVersion.invalidCardinalityTag);
ohair@286 324 }
ohair@286 325
ohair@286 326 // WS-A is engaged if wsa:Action header is found
ohair@286 327 boolean engaged = foundAction;
ohair@286 328
ohair@286 329 // check for mandatory set of headers only if:
ohair@286 330 // 1. WS-A is engaged or
ohair@286 331 // 2. wsdl:required=true
ohair@286 332 // Both wsa:Action and wsa:To MUST be present on request (for oneway MEP) and
ohair@286 333 // response messages (for oneway and request/response MEP only)
ohair@286 334 if (engaged || addressingRequired) {
ohair@286 335 // Check for mandatory headers always (even for Protocol messages).
ohair@286 336 // If it breaks any interop scenarios, Remove the comments.
ohair@286 337 /*
ohair@286 338 WSDLBoundOperation wbo = getWSDLBoundOperation(packet);
ohair@286 339 // no need to check for for non-application messages
ohair@286 340 if (wbo == null)
ohair@286 341 return;
ohair@286 342 */
ohair@286 343 checkMandatoryHeaders(packet, foundAction, foundTo, foundReplyTo,
ohair@286 344 foundFaultTo, foundMessageId, foundRelatesTo);
ohair@286 345 }
ohair@286 346 }
ohair@286 347
ohair@286 348 final boolean isInCurrentRole(Header header, WSBinding binding) {
ohair@286 349 // TODO: binding will be null for protocol messages
ohair@286 350 // TODO: returning true assumes that protocol messages are
ohair@286 351 // TODO: always in current role, this may not to be fixed.
ohair@286 352 if (binding == null)
ohair@286 353 return true;
ohair@286 354 return ((SOAPBinding)binding).getRoles().contains(header.getRole(soapVersion));
ohair@286 355
ohair@286 356 }
ohair@286 357
ohair@286 358 protected final WSDLBoundOperation getWSDLBoundOperation(Packet packet) {
ohair@286 359 //we can find Req/Response or Oneway only with WSDLModel
ohair@286 360 if(wsdlPort == null)
ohair@286 361 return null;
ohair@286 362 QName opName = packet.getWSDLOperation();
ohair@286 363 if(opName != null)
ohair@286 364 return wsdlPort.getBinding().get(opName);
ohair@286 365 return null;
ohair@286 366 }
ohair@286 367
ohair@286 368 protected void validateSOAPAction(Packet packet) {
alanb@368 369 String gotA = AddressingUtils.getAction(
alanb@368 370 packet.getMessage().getHeaders(),
alanb@368 371 addressingVersion, soapVersion);
ohair@286 372 if (gotA == null)
ohair@286 373 throw new WebServiceException(AddressingMessages.VALIDATION_SERVER_NULL_ACTION());
ohair@286 374 if(packet.soapAction != null && !packet.soapAction.equals("\"\"") && !packet.soapAction.equals("\""+gotA+"\"")) {
ohair@286 375 throw new InvalidAddressingHeaderException(addressingVersion.actionTag, addressingVersion.actionMismatchTag);
ohair@286 376 }
ohair@286 377 }
ohair@286 378
ohair@286 379 protected abstract void validateAction(Packet packet);
ohair@286 380
ohair@286 381 /**
ohair@286 382 * This should be called only when Addressing is engaged.
ohair@286 383 *
ohair@286 384 * Checks only for presence of wsa:Action and validates that wsa:Action
ohair@286 385 * equals SOAPAction header when non-empty
ohair@286 386 * Should be overridden if other wsa headers need to be checked based on version.
ohair@286 387 *
ohair@286 388 * @param packet
ohair@286 389 * @param foundAction
ohair@286 390 * @param foundTo
ohair@286 391 * @param foundReplyTo
ohair@286 392 * @param foundFaultTo
ohair@286 393 * @param foundMessageId
ohair@286 394 * @param foundRelatesTo
ohair@286 395 */
ohair@286 396 protected void checkMandatoryHeaders(
ohair@286 397 Packet packet, boolean foundAction, boolean foundTo, boolean foundReplyTo,
ohair@286 398 boolean foundFaultTo, boolean foundMessageId, boolean foundRelatesTo) {
ohair@286 399 // if no wsa:Action header is found
ohair@286 400 if (!foundAction)
ohair@286 401 throw new MissingAddressingHeaderException(addressingVersion.actionTag,packet);
ohair@286 402 validateSOAPAction(packet);
ohair@286 403 }
ohair@286 404 private static final Logger LOGGER = Logger.getLogger(WsaTube.class.getName());
ohair@286 405 }

mercurial