src/share/jaxws_classes/com/sun/xml/internal/ws/handler/HandlerTube.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, 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.handler;
aoqi@0 27
aoqi@0 28 import com.sun.istack.internal.Nullable;
aoqi@0 29 import com.sun.xml.internal.ws.api.WSBinding;
aoqi@0 30 import com.sun.xml.internal.ws.api.message.Packet;
aoqi@0 31 import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort;
aoqi@0 32 import com.sun.xml.internal.ws.api.pipe.*;
aoqi@0 33 import com.sun.xml.internal.ws.api.pipe.helper.AbstractFilterTubeImpl;
aoqi@0 34 import com.sun.xml.internal.ws.binding.BindingImpl;
aoqi@0 35 import com.sun.xml.internal.ws.client.HandlerConfiguration;
aoqi@0 36
aoqi@0 37 import javax.xml.ws.handler.MessageContext;
aoqi@0 38 import javax.xml.ws.handler.Handler;
aoqi@0 39 import java.util.List;
aoqi@0 40
aoqi@0 41 /**
aoqi@0 42 * @author WS Development team
aoqi@0 43 */
aoqi@0 44
aoqi@0 45 public abstract class HandlerTube extends AbstractFilterTubeImpl {
aoqi@0 46 /**
aoqi@0 47 * handle hold reference to other Tube for inter-tube communication
aoqi@0 48 */
aoqi@0 49 HandlerTube cousinTube;
aoqi@0 50 protected List<Handler> handlers;
aoqi@0 51 HandlerProcessor processor;
aoqi@0 52 boolean remedyActionTaken = false;
aoqi@0 53 protected final @Nullable WSDLPort port;
aoqi@0 54 // flag used to decide whether to call close on cousinTube
aoqi@0 55 boolean requestProcessingSucessful = false;
aoqi@0 56 private WSBinding binding;
aoqi@0 57 private HandlerConfiguration hc;
aoqi@0 58
aoqi@0 59 public HandlerTube(Tube next, WSDLPort port, WSBinding binding) {
aoqi@0 60 super(next);
aoqi@0 61 this.port = port;
aoqi@0 62 this.binding = binding;
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 public HandlerTube(Tube next, HandlerTube cousinTube, WSBinding binding) {
aoqi@0 66 super(next);
aoqi@0 67 this.cousinTube = cousinTube;
aoqi@0 68 this.binding = binding;
aoqi@0 69 if(cousinTube != null) {
aoqi@0 70 this.port = cousinTube.port;
aoqi@0 71 } else {
aoqi@0 72 this.port = null;
aoqi@0 73 }
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 /**
aoqi@0 77 * Copy constructor for {@link Tube#copy(TubeCloner)}.
aoqi@0 78 */
aoqi@0 79 protected HandlerTube(HandlerTube that, TubeCloner cloner) {
aoqi@0 80 super(that,cloner);
aoqi@0 81 if(that.cousinTube != null) {
aoqi@0 82 this.cousinTube = cloner.copy(that.cousinTube);
aoqi@0 83 }
aoqi@0 84 this.port = that.port;
aoqi@0 85 this.binding = that.binding;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 protected WSBinding getBinding() {
aoqi@0 89 return binding;
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 @Override
aoqi@0 93 public NextAction processRequest(Packet request) {
aoqi@0 94 setupExchange();
aoqi@0 95 // This check is done to cover handler returning false in Oneway request
aoqi@0 96 if (isHandleFalse()) {
aoqi@0 97 // Cousin HandlerTube returned false during Oneway Request processing.
aoqi@0 98 // Don't call handlers and dispatch the message.
aoqi@0 99 remedyActionTaken = true;
aoqi@0 100 return doInvoke(super.next, request);
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 // This is done here instead of the constructor, since User can change
aoqi@0 104 // the roles and handlerchain after a stub/proxy is created.
aoqi@0 105 setUpProcessorInternal();
aoqi@0 106
aoqi@0 107 MessageUpdatableContext context = getContext(request);
aoqi@0 108 boolean isOneWay = checkOneWay(request);
aoqi@0 109 try {
aoqi@0 110 if (!isHandlerChainEmpty()) {
aoqi@0 111 // Call handlers on Request
aoqi@0 112 boolean handlerResult = callHandlersOnRequest(context, isOneWay);
aoqi@0 113 //Update Packet with user modifications
aoqi@0 114 context.updatePacket();
aoqi@0 115 // two-way case where no message is sent
aoqi@0 116 if (!isOneWay && !handlerResult) {
aoqi@0 117 return doReturnWith(request);
aoqi@0 118 }
aoqi@0 119 }
aoqi@0 120 requestProcessingSucessful = true;
aoqi@0 121 // Call next Tube
aoqi@0 122 return doInvoke(super.next, request);
aoqi@0 123 } catch (RuntimeException re) {
aoqi@0 124 if(isOneWay) {
aoqi@0 125 //Eat the exception, its already logged and close the transportBackChannel
aoqi@0 126 if(request.transportBackChannel != null ) {
aoqi@0 127 request.transportBackChannel.close();
aoqi@0 128 }
aoqi@0 129 request.setMessage(null);
aoqi@0 130 return doReturnWith(request);
aoqi@0 131 } else
aoqi@0 132 throw re;
aoqi@0 133 } finally {
aoqi@0 134 if(!requestProcessingSucessful) {
aoqi@0 135 initiateClosing(context.getMessageContext());
aoqi@0 136 }
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 }
aoqi@0 140
aoqi@0 141 @Override
aoqi@0 142 public NextAction processResponse(Packet response) {
aoqi@0 143 setupExchange();
aoqi@0 144 MessageUpdatableContext context = getContext(response);
aoqi@0 145 try {
aoqi@0 146 if (isHandleFalse() || (response.getMessage() == null)) {
aoqi@0 147 // Cousin HandlerTube returned false during Response processing.
aoqi@0 148 // or it is oneway request
aoqi@0 149 // or handler chain is empty
aoqi@0 150 // Don't call handlers.
aoqi@0 151 return doReturnWith(response);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 setUpProcessorInternal();
aoqi@0 155
aoqi@0 156 boolean isFault = isHandleFault(response);
aoqi@0 157 if (!isHandlerChainEmpty()) {
aoqi@0 158 // Call handlers on Response
aoqi@0 159 callHandlersOnResponse(context, isFault);
aoqi@0 160 }
aoqi@0 161 } finally {
aoqi@0 162 initiateClosing(context.getMessageContext());
aoqi@0 163 }
aoqi@0 164 //Update Packet with user modifications
aoqi@0 165 context.updatePacket();
aoqi@0 166
aoqi@0 167 return doReturnWith(response);
aoqi@0 168
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 @Override
aoqi@0 172 public NextAction processException(Throwable t) {
aoqi@0 173 try {
aoqi@0 174 return doThrow(t);
aoqi@0 175 } finally {
aoqi@0 176 Packet packet = Fiber.current().getPacket();
aoqi@0 177 MessageUpdatableContext context = getContext(packet);
aoqi@0 178 initiateClosing(context.getMessageContext());
aoqi@0 179 /* TODO revisit: commented this out as the modified packet is no longer used
aoqi@0 180 In future if the message is propagated even when an exception
aoqi@0 181 occurs, then uncomment context.updatePacket();
aoqi@0 182 */
aoqi@0 183 //Update Packet with user modifications
aoqi@0 184 //context.updatePacket();
aoqi@0 185
aoqi@0 186
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 /**
aoqi@0 191 * Must be overridden by HandlerTube that drives other handler tubes for processing a message.
aoqi@0 192 * On Client-side: ClientLogicalHandlerTube drives the Handler Processing.
aoqi@0 193 * On Server-side: In case SOAP Binding, ServerMessageHandlerTube drives the Handler Processing.
aoqi@0 194 * In case XML/HTTP Binding, ServerLogicalHandlerTube drives the Handler Processing.
aoqi@0 195 *
aoqi@0 196 *
aoqi@0 197 * If its a top HandlerTube, should override by calling #close(MessaggeContext);
aoqi@0 198 *
aoqi@0 199 */
aoqi@0 200
aoqi@0 201 protected void initiateClosing(MessageContext mc) {
aoqi@0 202 // Do nothing
aoqi@0 203
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 /**
aoqi@0 207 * Calls close on previously invoked handlers.
aoqi@0 208 * Also, Cleans up any state left over in the Tube instance from the current
aoqi@0 209 * invocation, as Tube instances can be reused after the completion of MEP.
aoqi@0 210 *
aoqi@0 211 * On Client, SOAPHandlers are closed first and then LogicalHandlers
aoqi@0 212 * On Server, LogicalHandlers are closed first and then SOAPHandlers
aoqi@0 213 */
aoqi@0 214 final public void close(MessageContext msgContext) {
aoqi@0 215 //assuming cousinTube is called if requestProcessingSucessful is true
aoqi@0 216 if (requestProcessingSucessful) {
aoqi@0 217 if (cousinTube != null) {
aoqi@0 218 cousinTube.close(msgContext);
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 }
aoqi@0 222 if (processor != null)
aoqi@0 223 closeHandlers(msgContext);
aoqi@0 224
aoqi@0 225 // Clean up the exchange for next invocation.
aoqi@0 226 exchange = null;
aoqi@0 227 requestProcessingSucessful = false;
aoqi@0 228
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 /**
aoqi@0 232 * On Client, Override by calling #closeClientHandlers(MessageContext mc)
aoqi@0 233 * On Server, Override by calling #closeServerHandlers(MessageContext mc)
aoqi@0 234 * The difference is the order in which they are closed.
aoqi@0 235 * @param mc
aoqi@0 236 */
aoqi@0 237 abstract void closeHandlers(MessageContext mc);
aoqi@0 238
aoqi@0 239 /**
aoqi@0 240 * Called by close(MessageContext mc) in a Client Handlertube
aoqi@0 241 */
aoqi@0 242 protected void closeClientsideHandlers(MessageContext msgContext) {
aoqi@0 243 if (processor == null)
aoqi@0 244 return;
aoqi@0 245 if (remedyActionTaken) {
aoqi@0 246 //Close only invoked handlers in the chain
aoqi@0 247
aoqi@0 248 //CLIENT-SIDE
aoqi@0 249 processor.closeHandlers(msgContext, processor.getIndex(), 0);
aoqi@0 250 processor.setIndex(-1);
aoqi@0 251 //reset remedyActionTaken
aoqi@0 252 remedyActionTaken = false;
aoqi@0 253 } else {
aoqi@0 254 //Close all handlers in the chain
aoqi@0 255
aoqi@0 256 //CLIENT-SIDE
aoqi@0 257 processor.closeHandlers(msgContext, handlers.size() - 1, 0);
aoqi@0 258
aoqi@0 259 }
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 /**
aoqi@0 263 * Called by close(MessageContext mc) in a Server Handlertube
aoqi@0 264 */
aoqi@0 265 protected void closeServersideHandlers(MessageContext msgContext) {
aoqi@0 266 if (processor == null)
aoqi@0 267 return;
aoqi@0 268 if (remedyActionTaken) {
aoqi@0 269 //Close only invoked handlers in the chain
aoqi@0 270
aoqi@0 271 //SERVER-SIDE
aoqi@0 272 processor.closeHandlers(msgContext, processor.getIndex(), handlers.size() - 1);
aoqi@0 273 processor.setIndex(-1);
aoqi@0 274 //reset remedyActionTaken
aoqi@0 275 remedyActionTaken = false;
aoqi@0 276 } else {
aoqi@0 277 //Close all handlers in the chain
aoqi@0 278
aoqi@0 279 //SERVER-SIDE
aoqi@0 280 processor.closeHandlers(msgContext, 0, handlers.size() - 1);
aoqi@0 281
aoqi@0 282 }
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 abstract void callHandlersOnResponse(MessageUpdatableContext context, boolean handleFault);
aoqi@0 286
aoqi@0 287 abstract boolean callHandlersOnRequest(MessageUpdatableContext context, boolean oneWay);
aoqi@0 288
aoqi@0 289 private boolean checkOneWay(Packet packet) {
aoqi@0 290 if (port != null) {
aoqi@0 291 /* we can determine this value from WSDL */
aoqi@0 292 return packet.getMessage().isOneWay(port);
aoqi@0 293 } else {
aoqi@0 294 /*
aoqi@0 295 otherwise use this value as an approximation, since this carries
aoqi@0 296 the application's intention --- whether it was invokeOneway vs invoke,etc.
aoqi@0 297 */
aoqi@0 298 return !(packet.expectReply != null && packet.expectReply);
aoqi@0 299 }
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 private void setUpProcessorInternal() {
aoqi@0 303 HandlerConfiguration hc = ((BindingImpl) binding).getHandlerConfig();
aoqi@0 304 if (hc != this.hc)
aoqi@0 305 resetProcessor();
aoqi@0 306 this.hc = hc;
aoqi@0 307
aoqi@0 308 setUpProcessor();
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 abstract void setUpProcessor();
aoqi@0 312
aoqi@0 313 protected void resetProcessor() {
aoqi@0 314 handlers = null;
aoqi@0 315 }
aoqi@0 316
aoqi@0 317 final public boolean isHandlerChainEmpty() {
aoqi@0 318 return handlers.isEmpty();
aoqi@0 319 }
aoqi@0 320 abstract MessageUpdatableContext getContext(Packet p);
aoqi@0 321
aoqi@0 322 private boolean isHandleFault(Packet packet) {
aoqi@0 323 if (cousinTube != null) {
aoqi@0 324 return exchange.isHandleFault();
aoqi@0 325 } else {
aoqi@0 326 boolean isFault = packet.getMessage().isFault();
aoqi@0 327 exchange.setHandleFault(isFault);
aoqi@0 328 return isFault;
aoqi@0 329 }
aoqi@0 330 }
aoqi@0 331
aoqi@0 332 final void setHandleFault() {
aoqi@0 333 exchange.setHandleFault(true);
aoqi@0 334 }
aoqi@0 335
aoqi@0 336 private boolean isHandleFalse() {
aoqi@0 337 return exchange.isHandleFalse();
aoqi@0 338 }
aoqi@0 339
aoqi@0 340 final void setHandleFalse() {
aoqi@0 341 exchange.setHandleFalse();
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 private void setupExchange() {
aoqi@0 345 if(exchange == null) {
aoqi@0 346 exchange = new HandlerTubeExchange();
aoqi@0 347 if(cousinTube != null) {
aoqi@0 348 cousinTube.exchange = exchange;
aoqi@0 349 }
aoqi@0 350 } else {
aoqi@0 351 if(cousinTube != null) {
aoqi@0 352 cousinTube.exchange = exchange;
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 }
aoqi@0 356 }
aoqi@0 357 private HandlerTubeExchange exchange;
aoqi@0 358
aoqi@0 359 /**
aoqi@0 360 * This class is used primarily to exchange information or status between
aoqi@0 361 * LogicalHandlerTube and SOAPHandlerTube
aoqi@0 362 */
aoqi@0 363 static final class HandlerTubeExchange {
aoqi@0 364 private boolean handleFalse;
aoqi@0 365 private boolean handleFault;
aoqi@0 366
aoqi@0 367 boolean isHandleFault() {
aoqi@0 368 return handleFault;
aoqi@0 369 }
aoqi@0 370
aoqi@0 371 void setHandleFault(boolean isFault) {
aoqi@0 372 this.handleFault = isFault;
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 public boolean isHandleFalse() {
aoqi@0 376 return handleFalse;
aoqi@0 377 }
aoqi@0 378
aoqi@0 379 void setHandleFalse() {
aoqi@0 380 this.handleFalse = true;
aoqi@0 381 }
aoqi@0 382 }
aoqi@0 383
aoqi@0 384 }

mercurial