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

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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.addressing;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.NotNull;
aoqi@0 29 import com.sun.istack.internal.Nullable;
aoqi@0 30 import com.sun.xml.internal.ws.addressing.model.ActionNotSupportedException;
aoqi@0 31 import com.sun.xml.internal.ws.addressing.model.InvalidAddressingHeaderException;
aoqi@0 32 import com.sun.xml.internal.ws.api.EndpointAddress;
aoqi@0 33 import com.sun.xml.internal.ws.api.SOAPVersion;
aoqi@0 34 import com.sun.xml.internal.ws.api.WSBinding;
aoqi@0 35 import com.sun.xml.internal.ws.api.addressing.AddressingVersion;
aoqi@0 36 import com.sun.xml.internal.ws.api.addressing.NonAnonymousResponseProcessor;
aoqi@0 37 import com.sun.xml.internal.ws.api.addressing.WSEndpointReference;
aoqi@0 38 import com.sun.xml.internal.ws.api.message.AddressingUtils;
aoqi@0 39 import com.sun.xml.internal.ws.api.message.Message;
aoqi@0 40 import com.sun.xml.internal.ws.api.message.MessageHeaders;
aoqi@0 41 import com.sun.xml.internal.ws.api.message.Messages;
aoqi@0 42 import com.sun.xml.internal.ws.api.message.Packet;
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.api.pipe.*;
aoqi@0 46 import com.sun.xml.internal.ws.api.server.WSEndpoint;
aoqi@0 47 import com.sun.xml.internal.ws.client.Stub;
aoqi@0 48 import com.sun.xml.internal.ws.developer.JAXWSProperties;
aoqi@0 49 import com.sun.xml.internal.ws.fault.SOAPFaultBuilder;
aoqi@0 50 import com.sun.xml.internal.ws.message.FaultDetailHeader;
aoqi@0 51 import com.sun.xml.internal.ws.resources.AddressingMessages;
aoqi@0 52
aoqi@0 53 import javax.xml.soap.SOAPFault;
aoqi@0 54 import javax.xml.ws.WebServiceException;
aoqi@0 55 import java.net.URI;
aoqi@0 56 import java.util.logging.Level;
aoqi@0 57 import java.util.logging.Logger;
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * Handles WS-Addressing for the server.
aoqi@0 61 *
aoqi@0 62 * @author Rama Pulavarthi
aoqi@0 63 * @author Kohsuke Kawaguchi
aoqi@0 64 * @author Arun Gupta
aoqi@0 65 */
aoqi@0 66 public class WsaServerTube extends WsaTube {
aoqi@0 67 private WSEndpoint endpoint;
aoqi@0 68 // store the replyTo/faultTo of the message currently being processed.
aoqi@0 69 // both will be set to non-null in processRequest
aoqi@0 70 private WSEndpointReference replyTo;
aoqi@0 71 private WSEndpointReference faultTo;
aoqi@0 72 private boolean isAnonymousRequired = false;
aoqi@0 73 // Used by subclasses to avoid this class closing the transport back
aoqi@0 74 // channel based on the ReplyTo/FaultTo addrs being non-anonymous. False
aoqi@0 75 // can be useful in cases where special back-channel handling is required.
aoqi@0 76 protected boolean isEarlyBackchannelCloseAllowed = true;
aoqi@0 77
aoqi@0 78 /**
aoqi@0 79 * WSDLBoundOperation calculated on the Request payload.
aoqi@0 80 * Used for determining ReplyTo or Fault Action for non-anonymous responses *
aoqi@0 81 */
aoqi@0 82 private WSDLBoundOperation wbo;
aoqi@0 83 public WsaServerTube(WSEndpoint endpoint, @NotNull WSDLPort wsdlPort, WSBinding binding, Tube next) {
aoqi@0 84 super(wsdlPort, binding, next);
aoqi@0 85 this.endpoint = endpoint;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 public WsaServerTube(WsaServerTube that, TubeCloner cloner) {
aoqi@0 89 super(that, cloner);
aoqi@0 90 endpoint = that.endpoint;
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 @Override
aoqi@0 94 public WsaServerTube copy(TubeCloner cloner) {
aoqi@0 95 return new WsaServerTube(this, cloner);
aoqi@0 96 }
aoqi@0 97
aoqi@0 98 @Override
aoqi@0 99 public @NotNull NextAction processRequest(Packet request) {
aoqi@0 100 Message msg = request.getMessage();
aoqi@0 101 if (msg == null) {
aoqi@0 102 return doInvoke(next,request);
aoqi@0 103 } // hmm?
aoqi@0 104
aoqi@0 105 // expose bunch of addressing related properties for advanced applications
aoqi@0 106 request.addSatellite(new WsaPropertyBag(addressingVersion,soapVersion,request));
aoqi@0 107
aoqi@0 108 // Store request ReplyTo and FaultTo in requestPacket.invocationProperties
aoqi@0 109 // so that they can be used after responsePacket is received.
aoqi@0 110 // These properties are used if a fault is thrown from the subsequent Pipe/Tubes.
aoqi@0 111
aoqi@0 112 MessageHeaders hl = request.getMessage().getHeaders();
aoqi@0 113 String msgId;
aoqi@0 114 try {
aoqi@0 115 replyTo = AddressingUtils.getReplyTo(hl, addressingVersion, soapVersion);
aoqi@0 116 faultTo = AddressingUtils.getFaultTo(hl, addressingVersion, soapVersion);
aoqi@0 117 msgId = AddressingUtils.getMessageID(hl, addressingVersion, soapVersion);
aoqi@0 118 } catch (InvalidAddressingHeaderException e) {
aoqi@0 119
aoqi@0 120 LOGGER.log(Level.WARNING, addressingVersion.getInvalidMapText()+", Problem header:" + e.getProblemHeader()+ ", Reason: "+ e.getSubsubcode(),e);
aoqi@0 121
aoqi@0 122 // problematic header must be removed since it can fail during Fault message processing
aoqi@0 123 hl.remove(e.getProblemHeader());
aoqi@0 124
aoqi@0 125 SOAPFault soapFault = helper.createInvalidAddressingHeaderFault(e, addressingVersion);
aoqi@0 126 // WS-A fault processing for one-way methods
aoqi@0 127 if ((wsdlPort!=null) && request.getMessage().isOneWay(wsdlPort)) {
aoqi@0 128 Packet response = request.createServerResponse(null, wsdlPort, null, binding);
aoqi@0 129 return doReturnWith(response);
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 Message m = Messages.create(soapFault);
aoqi@0 133 if (soapVersion == SOAPVersion.SOAP_11) {
aoqi@0 134 FaultDetailHeader s11FaultDetailHeader = new FaultDetailHeader(addressingVersion, addressingVersion.problemHeaderQNameTag.getLocalPart(), e.getProblemHeader());
aoqi@0 135 m.getHeaders().add(s11FaultDetailHeader);
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 Packet response = request.createServerResponse(m, wsdlPort, null, binding);
aoqi@0 139 return doReturnWith(response);
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 // defaulting
aoqi@0 143 if (replyTo == null) {
aoqi@0 144 replyTo = addressingVersion.anonymousEpr;
aoqi@0 145 }
aoqi@0 146 if (faultTo == null) {
aoqi@0 147 faultTo = replyTo;
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 // Save a copy into the packet such that we can save it with that
aoqi@0 151 // packet if we're going to deliver the response at a later time
aoqi@0 152 // (async from the request).
aoqi@0 153 request.put(WsaPropertyBag.WSA_REPLYTO_FROM_REQUEST, replyTo);
aoqi@0 154 request.put(WsaPropertyBag.WSA_FAULTTO_FROM_REQUEST, faultTo);
aoqi@0 155 request.put(WsaPropertyBag.WSA_MSGID_FROM_REQUEST, msgId);
aoqi@0 156
aoqi@0 157 wbo = getWSDLBoundOperation(request);
aoqi@0 158 isAnonymousRequired = isAnonymousRequired(wbo);
aoqi@0 159
aoqi@0 160 Packet p = validateInboundHeaders(request);
aoqi@0 161 // if one-way message and WS-A header processing fault has occurred,
aoqi@0 162 // then do no further processing
aoqi@0 163 if (p.getMessage() == null) {
aoqi@0 164 return doReturnWith(p);
aoqi@0 165 }
aoqi@0 166
aoqi@0 167 // if we find an error in addressing header, just turn around the direction here
aoqi@0 168 if (p.getMessage().isFault()) {
aoqi@0 169 // close the transportBackChannel if we know that
aoqi@0 170 // we'll never use them
aoqi@0 171 if (isEarlyBackchannelCloseAllowed &&
aoqi@0 172 !(isAnonymousRequired) &&
aoqi@0 173 !faultTo.isAnonymous() && request.transportBackChannel != null) {
aoqi@0 174 request.transportBackChannel.close();
aoqi@0 175 }
aoqi@0 176 return processResponse(p);
aoqi@0 177 }
aoqi@0 178 // close the transportBackChannel if we know that
aoqi@0 179 // we'll never use them
aoqi@0 180 if (isEarlyBackchannelCloseAllowed &&
aoqi@0 181 !(isAnonymousRequired) &&
aoqi@0 182 !replyTo.isAnonymous() && !faultTo.isAnonymous() &&
aoqi@0 183 request.transportBackChannel != null) {
aoqi@0 184 request.transportBackChannel.close();
aoqi@0 185 }
aoqi@0 186 return doInvoke(next,p);
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 protected boolean isAnonymousRequired(@Nullable WSDLBoundOperation wbo) {
aoqi@0 190 //this requirement can only be specified in W3C case, Override this in W3C case.
aoqi@0 191 return false;
aoqi@0 192 }
aoqi@0 193
aoqi@0 194 protected void checkAnonymousSemantics(WSDLBoundOperation wbo, WSEndpointReference replyTo, WSEndpointReference faultTo) {
aoqi@0 195 //this requirement can only be specified in W3C case, Override this in W3C case.
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 @Override
aoqi@0 199 public @NotNull NextAction processException(Throwable t) {
aoqi@0 200 final Packet response = Fiber.current().getPacket();
aoqi@0 201 ThrowableContainerPropertySet tc = response.getSatellite(ThrowableContainerPropertySet.class);
aoqi@0 202 if (tc == null) {
aoqi@0 203 tc = new ThrowableContainerPropertySet(t);
aoqi@0 204 response.addSatellite(tc);
aoqi@0 205 } else if (t != tc.getThrowable()) {
aoqi@0 206 // This is a pathological case where an exception happens after a previous exception.
aoqi@0 207 // Make sure you report the latest one.
aoqi@0 208 tc.setThrowable(t);
aoqi@0 209 }
aoqi@0 210 return processResponse(response.endpoint.createServiceResponseForException(tc, response, soapVersion, wsdlPort,
aoqi@0 211 response.endpoint.getSEIModel(),
aoqi@0 212 binding));
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 @Override
aoqi@0 216 public @NotNull NextAction processResponse(Packet response) {
aoqi@0 217 Message msg = response.getMessage();
aoqi@0 218 if (msg ==null) {
aoqi@0 219 return doReturnWith(response);
aoqi@0 220 } // one way message. Nothing to see here. Move on.
aoqi@0 221
aoqi@0 222 String to = AddressingUtils.getTo(msg.getHeaders(),
aoqi@0 223 addressingVersion, soapVersion);
aoqi@0 224 if (to != null) {
aoqi@0 225 replyTo = faultTo = new WSEndpointReference(to, addressingVersion);
aoqi@0 226 }
aoqi@0 227
aoqi@0 228 if (replyTo == null) {
aoqi@0 229 // This is an async response or we're not processing the response in
aoqi@0 230 // the same tube instance as we processed the request. Get the ReplyTo
aoqi@0 231 // now, from the properties we stored into the request packet. We
aoqi@0 232 // assume anyone that interrupted the request->response flow will have
aoqi@0 233 // saved the ReplyTo and put it back into the packet for our use.
aoqi@0 234 replyTo = (WSEndpointReference)response.
aoqi@0 235 get(WsaPropertyBag.WSA_REPLYTO_FROM_REQUEST);
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 if (faultTo == null) {
aoqi@0 239 // This is an async response or we're not processing the response in
aoqi@0 240 // the same tube instance as we processed the request. Get the FaultTo
aoqi@0 241 // now, from the properties we stored into the request packet. We
aoqi@0 242 // assume anyone that interrupted the request->response flow will have
aoqi@0 243 // saved the FaultTo and put it back into the packet for our use.
aoqi@0 244 faultTo = (WSEndpointReference)response.
aoqi@0 245 get(WsaPropertyBag.WSA_FAULTTO_FROM_REQUEST);
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 WSEndpointReference target = msg.isFault() ? faultTo : replyTo;
aoqi@0 249 if (target == null && response.proxy instanceof Stub) {
aoqi@0 250 target = ((Stub) response.proxy).getWSEndpointReference();
aoqi@0 251 }
aoqi@0 252 if (target == null || target.isAnonymous() || isAnonymousRequired) {
aoqi@0 253 return doReturnWith(response);
aoqi@0 254 }
aoqi@0 255 if (target.isNone()) {
aoqi@0 256 // the caller doesn't want to hear about it, so proceed like one-way
aoqi@0 257 response.setMessage(null);
aoqi@0 258 return doReturnWith(response);
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 if ((wsdlPort!=null) && response.getMessage().isOneWay(wsdlPort)) {
aoqi@0 262 // one way message but with replyTo. I believe this is a hack for WS-TX - KK.
aoqi@0 263 LOGGER.fine(AddressingMessages.NON_ANONYMOUS_RESPONSE_ONEWAY());
aoqi@0 264 return doReturnWith(response);
aoqi@0 265 }
aoqi@0 266
aoqi@0 267 // MTU: If we're not sending a response that corresponds to a WSDL op,
aoqi@0 268 // then take whatever soapAction is set on the packet (as allowing
aoqi@0 269 // helper.getOutputAction() will only result in a bogus 'unset'
aoqi@0 270 // action value.
aoqi@0 271 if (wbo != null || response.soapAction == null) {
aoqi@0 272 String action = response.getMessage().isFault() ?
aoqi@0 273 helper.getFaultAction(wbo, response) :
aoqi@0 274 helper.getOutputAction(wbo);
aoqi@0 275 //set the SOAPAction, as its got to be same as wsa:Action
aoqi@0 276 if (response.soapAction == null ||
aoqi@0 277 (action != null &&
aoqi@0 278 !action.equals(AddressingVersion.UNSET_OUTPUT_ACTION))) {
aoqi@0 279 response.soapAction = action;
aoqi@0 280 }
aoqi@0 281 }
aoqi@0 282 response.expectReply = false;
aoqi@0 283
aoqi@0 284 EndpointAddress adrs;
aoqi@0 285 try {
aoqi@0 286 adrs = new EndpointAddress(URI.create(target.getAddress()));
aoqi@0 287 } catch (NullPointerException e) {
aoqi@0 288 throw new WebServiceException(e);
aoqi@0 289 } catch (IllegalArgumentException e) {
aoqi@0 290 throw new WebServiceException(e);
aoqi@0 291 }
aoqi@0 292
aoqi@0 293 response.endpointAddress = adrs;
aoqi@0 294
aoqi@0 295 if (response.isAdapterDeliversNonAnonymousResponse) {
aoqi@0 296 return doReturnWith(response);
aoqi@0 297 }
aoqi@0 298
aoqi@0 299 return doReturnWith(NonAnonymousResponseProcessor.getDefault().process(response));
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 @Override
aoqi@0 303 protected void validateAction(Packet packet) {
aoqi@0 304 //There may not be a WSDL operation. There may not even be a WSDL.
aoqi@0 305 //For instance this may be a RM CreateSequence message.
aoqi@0 306 WSDLBoundOperation wsdlBoundOperation = getWSDLBoundOperation(packet);
aoqi@0 307
aoqi@0 308 if (wsdlBoundOperation == null) {
aoqi@0 309 return;
aoqi@0 310 }
aoqi@0 311
aoqi@0 312 String gotA = AddressingUtils.getAction(
aoqi@0 313 packet.getMessage().getHeaders(),
aoqi@0 314 addressingVersion, soapVersion);
aoqi@0 315
aoqi@0 316 if (gotA == null) {
aoqi@0 317 throw new WebServiceException(AddressingMessages.VALIDATION_SERVER_NULL_ACTION());
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 String expected = helper.getInputAction(packet);
aoqi@0 321 String soapAction = helper.getSOAPAction(packet);
aoqi@0 322 if (helper.isInputActionDefault(packet) && (soapAction != null && !soapAction.equals(""))) {
aoqi@0 323 expected = soapAction;
aoqi@0 324 }
aoqi@0 325
aoqi@0 326 if (expected != null && !gotA.equals(expected)) {
aoqi@0 327 throw new ActionNotSupportedException(gotA);
aoqi@0 328 }
aoqi@0 329 }
aoqi@0 330
aoqi@0 331 @Override
aoqi@0 332 protected void checkMessageAddressingProperties(Packet packet) {
aoqi@0 333 super.checkMessageAddressingProperties(packet);
aoqi@0 334
aoqi@0 335 // wsaw:Anonymous validation
aoqi@0 336 WSDLBoundOperation wsdlBoundOperation = getWSDLBoundOperation(packet);
aoqi@0 337 checkAnonymousSemantics(wsdlBoundOperation, replyTo, faultTo);
aoqi@0 338 // check if addresses are valid
aoqi@0 339 checkNonAnonymousAddresses(replyTo,faultTo);
aoqi@0 340 }
aoqi@0 341
aoqi@0 342 @SuppressWarnings("ResultOfObjectAllocationIgnored")
aoqi@0 343 private void checkNonAnonymousAddresses(WSEndpointReference replyTo, WSEndpointReference faultTo) {
aoqi@0 344 if (!replyTo.isAnonymous()) {
aoqi@0 345 try {
aoqi@0 346 new EndpointAddress(URI.create(replyTo.getAddress()));
aoqi@0 347 } catch (Exception e) {
aoqi@0 348 throw new InvalidAddressingHeaderException(addressingVersion.replyToTag, addressingVersion.invalidAddressTag);
aoqi@0 349 }
aoqi@0 350 }
aoqi@0 351 //for now only validate ReplyTo
aoqi@0 352 /*
aoqi@0 353 if (!faultTo.isAnonymous()) {
aoqi@0 354 try {
aoqi@0 355 new EndpointAddress(URI.create(faultTo.getAddress()));
aoqi@0 356 } catch (IllegalArgumentException e) {
aoqi@0 357 throw new InvalidAddressingHeaderException(addressingVersion.faultToTag, addressingVersion.invalidAddressTag);
aoqi@0 358 }
aoqi@0 359 }
aoqi@0 360 */
aoqi@0 361
aoqi@0 362 }
aoqi@0 363
aoqi@0 364 /**
aoqi@0 365 * @deprecated
aoqi@0 366 * Use {@link JAXWSProperties#ADDRESSING_MESSAGEID}.
aoqi@0 367 */
aoqi@0 368 public static final String REQUEST_MESSAGE_ID = "com.sun.xml.internal.ws.addressing.request.messageID";
aoqi@0 369
aoqi@0 370 private static final Logger LOGGER = Logger.getLogger(WsaServerTube.class.getName());
aoqi@0 371 }

mercurial