1 7 package org.jboss.webservice.handler; 8 9 11 import org.jboss.axis.AxisFault; 12 import org.jboss.axis.Constants; 13 import org.jboss.axis.message.SOAPElementAxisImpl; 14 import org.jboss.logging.Logger; 15 16 import javax.xml.namespace.QName ; 17 import javax.xml.rpc.JAXRPCException ; 18 import javax.xml.rpc.handler.Handler ; 19 import javax.xml.rpc.handler.HandlerChain ; 20 import javax.xml.rpc.handler.HandlerInfo ; 21 import javax.xml.rpc.handler.MessageContext ; 22 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 23 import javax.xml.soap.SOAPEnvelope ; 24 import javax.xml.soap.SOAPException ; 25 import javax.xml.soap.SOAPHeader ; 26 import javax.xml.soap.SOAPHeaderElement ; 27 import javax.xml.soap.SOAPPart ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collection ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.ListIterator ; 35 import java.util.Map ; 36 import java.util.Set ; 37 38 47 public abstract class HandlerChainBaseImpl implements HandlerChain 48 { 49 private static Logger log = Logger.getLogger(HandlerChainBaseImpl.class); 50 51 public static final int STATE_DOES_NOT_EXIST = 0; 52 public static final int STATE_CREATED = 1; 53 public static final int STATE_READY = 2; 54 public static final int STATE_DESTROYED = 3; 55 56 protected ArrayList handlers = new ArrayList (); 58 protected HashSet roles = new HashSet (); 60 protected int falseIndex = -1; 62 protected int state; 64 65 68 public HandlerChainBaseImpl(List infos, Set roles) 69 { 70 log.debug("Create a handler chain for roles: " + roles); 71 addHandlersToChain(infos, roles); 72 } 73 74 79 private void addHandlersToChain(List infos, Set roleSet) 80 { 81 try 82 { 83 if (infos != null) 84 { 85 for (int i = 0; i < infos.size(); i++) 86 { 87 HandlerInfo info = (HandlerInfo )infos.get(i); 88 HandlerWrapper handler = new HandlerWrapper((Handler )info.getHandlerClass().newInstance()); 89 handlers.add(new Entry(handler, info)); 90 } 91 } 92 if (roleSet != null) 93 { 94 roles.addAll(roleSet); 95 } 96 } 97 catch (Exception e) 98 { 99 throw new JAXRPCException ("Cannot initialize handler chain", e); 100 } 101 102 state = STATE_CREATED; 104 } 105 106 109 public int getState() 110 { 111 return state; 112 } 113 114 120 public void init(Map config) 121 { 122 log.debug("init: [config=" + config + "]"); 123 for (int i = 0; i < handlers.size(); i++) 124 { 125 Entry entry = (Entry)handlers.get(i); 126 entry.handler.init(entry.info); 127 } 128 129 state = STATE_READY; 131 } 132 133 138 public void destroy() 139 { 140 log.debug("destroy"); 141 for (int i = 0; i < handlers.size(); i++) 142 { 143 Entry entry = (Entry)handlers.get(i); 144 entry.handler.destroy(); 145 } 146 handlers.clear(); 147 148 state = STATE_DESTROYED; 150 } 151 152 158 public String [] getRoles() 159 { 160 String [] arr = new String [roles.size()]; 161 roles.toArray(arr); 162 return arr; 163 } 164 165 175 public void setRoles(String [] soapActorNames) 176 { 177 List newRoles = Arrays.asList(soapActorNames); 178 log.debug("setRoles: " + newRoles); 179 180 roles.clear(); 181 roles.addAll(newRoles); 182 } 183 184 191 public boolean handleRequest(MessageContext msgContext) 192 { 193 boolean doNext = true; 194 195 log.debug("Enter: doHandleRequest"); 196 197 replaceDirtyHandlers(); 199 200 int handlerIndex = 0; 201 Handler currHandler = null; 202 try 203 { 204 for (; doNext && handlerIndex < handlers.size(); handlerIndex++) 205 { 206 String lastMessageTrace = null; 207 if (log.isTraceEnabled()) 208 { 209 org.jboss.axis.MessageContext msgCtx = (org.jboss.axis.MessageContext)msgContext; 210 SOAPPart soapPart = msgCtx.getRequestMessage().getSOAPPart(); 211 lastMessageTrace = traceSOAPPart(soapPart, lastMessageTrace); 212 } 213 214 currHandler = ((Entry)handlers.get(handlerIndex)).getHandler(); 215 log.debug("Handle request: " + currHandler); 216 doNext = currHandler.handleRequest(msgContext); 217 218 if (log.isTraceEnabled()) 219 { 220 org.jboss.axis.MessageContext msgCtx = (org.jboss.axis.MessageContext)msgContext; 221 SOAPPart soapPart = msgCtx.getRequestMessage().getSOAPPart(); 222 lastMessageTrace = traceSOAPPart(soapPart, lastMessageTrace); 223 } 224 } 225 } 226 catch (RuntimeException e) 227 { 228 log.error("RuntimeException in request handler", e); 229 doNext = false; 230 throw e; 231 } 232 finally 233 { 234 if (doNext == false) 236 falseIndex = handlerIndex; 237 238 log.debug("Exit: doHandleRequest with status: " + doNext); 239 } 240 241 return doNext; 242 } 243 244 254 public boolean handleResponse(MessageContext msgContext) 255 { 256 boolean doNext = true; 257 258 log.debug("Enter: handleResponse"); 259 260 int handlerIndex = handlers.size() - 1; 261 if (falseIndex != -1) 262 handlerIndex = falseIndex; 263 264 Handler currHandler = null; 265 try 266 { 267 for (; doNext && handlerIndex >= 0; handlerIndex--) 268 { 269 String lastMessageTrace = null; 270 if (log.isTraceEnabled()) 271 { 272 org.jboss.axis.MessageContext msgCtx = (org.jboss.axis.MessageContext)msgContext; 273 SOAPPart soapPart = msgCtx.getResponseMessage().getSOAPPart(); 274 lastMessageTrace = traceSOAPPart(soapPart, lastMessageTrace); 275 } 276 277 currHandler = ((Entry)handlers.get(handlerIndex)).getHandler(); 278 log.debug("Handle response: " + currHandler); 279 doNext = currHandler.handleResponse(msgContext); 280 281 if (log.isTraceEnabled()) 282 { 283 org.jboss.axis.MessageContext msgCtx = (org.jboss.axis.MessageContext)msgContext; 284 SOAPPart soapPart = msgCtx.getResponseMessage().getSOAPPart(); 285 lastMessageTrace = traceSOAPPart(soapPart, lastMessageTrace); 286 } 287 } 288 } 289 catch (RuntimeException rte) 290 { 291 log.error("RuntimeException in response handler", rte); 292 doNext = false; 293 throw rte; 294 } 295 finally 296 { 297 if (doNext == false) 299 falseIndex = handlerIndex; 300 301 log.debug("Exit: handleResponse with status: " + doNext); 302 } 303 304 return doNext; 305 } 306 307 317 public boolean handleFault(MessageContext msgContext) 318 { 319 boolean doNext = true; 320 321 int handlerIndex = handlers.size() - 1; 322 if (falseIndex != -1) 323 handlerIndex = falseIndex; 324 325 Handler currHandler = null; 326 for (; doNext && handlerIndex >= 0; handlerIndex--) 327 { 328 currHandler = ((Entry)handlers.get(handlerIndex)).getHandler(); 329 log.debug("Handle fault: " + currHandler); 330 doNext = currHandler.handleFault(msgContext); 331 } 332 333 return doNext; 334 } 335 336 339 protected String traceSOAPPart(SOAPPart soapPart, String lastMessageTrace) 340 { 341 try 342 { 343 SOAPEnvelope env = soapPart.getEnvelope(); 344 String envAsString = ((SOAPElementAxisImpl)env).getAsStringFromInternal(); 345 if (envAsString.equals(lastMessageTrace) == false) 346 { 347 log.trace(envAsString); 348 lastMessageTrace = envAsString; 349 } 350 return lastMessageTrace; 351 } 352 catch (SOAPException e) 353 { 354 log.error("Cannot get SOAPEnvelope", e); 355 return null; 356 } 357 } 358 359 362 protected void replaceDirtyHandlers() 363 { 364 for (int i = 0; i < handlers.size(); i++) 365 { 366 Entry entry = (Entry)handlers.get(i); 367 if (entry.handler.getState() == HandlerWrapper.DOES_NOT_EXIST) 368 { 369 log.debug("Replacing dirty handler: " + entry.handler); 370 try 371 { 372 HandlerWrapper handler = new HandlerWrapper((Handler )entry.info.getHandlerClass().newInstance()); 373 entry.handler = handler; 374 handler.init(entry.info); 375 } 376 catch (Exception e) 377 { 378 log.error("Cannot create handler instance for: " + entry.info); 379 } 380 } 381 } 382 } 383 384 387 protected Handler getHandlerAt(int pos) 388 { 389 if (pos < 0 || handlers.size() <= pos) 390 throw new IllegalArgumentException ("No handler at position: " + pos); 391 392 Entry entry = (Entry)handlers.get(pos); 393 return entry.handler; 394 } 395 396 399 protected void checkMustUnderstand(MessageContext msgContext) 400 { 401 String errorMsg = null; 402 403 try 404 { 405 SOAPMessageContext msgCtx = (SOAPMessageContext )msgContext; 406 SOAPPart soapPart = msgCtx.getMessage().getSOAPPart(); 407 SOAPHeader soapHeader = soapPart.getEnvelope().getHeader(); 408 if (soapHeader != null) 409 { 410 Iterator it = soapHeader.examineAllHeaderElements(); 411 while (errorMsg == null && it.hasNext()) 412 { 413 SOAPHeaderElement headerElement = (SOAPHeaderElement )it.next(); 414 if (headerElement.getMustUnderstand() == true) 415 { 416 QName headerName = new QName (headerElement.getNamespaceURI(), headerElement.getLocalName()); 417 418 String actor = headerElement.getActor(); 419 if (actor == null || Constants.URI_SOAP11_NEXT_ACTOR.equals(actor)) 420 { 421 errorMsg = "Unprocessed mustUnderstand header " + headerName; 422 break; 423 } 424 425 if (actor != null && roles.contains(actor)) 426 { 427 Iterator itHandlers = handlers.iterator(); 428 while (itHandlers.hasNext()) 429 { 430 Entry entry = (Entry)itHandlers.next(); 431 Handler handler = entry.getHandler(); 432 433 List headers = Arrays.asList(handler.getHeaders()); 435 if (headers.contains(headerName)) 436 { 437 errorMsg = "Unprocessed mustUnderstand header " + headerName; 438 break; 439 } 440 } 441 } 442 } 443 } 444 } 445 } 446 catch (SOAPException e) 447 { 448 log.error("Cannot check mustUnderstand for headers", e); 449 } 450 451 if (errorMsg != null) 452 { 453 AxisFault fault = new AxisFault(errorMsg); 454 fault.setFaultCode(Constants.FAULT_MUSTUNDERSTAND); 455 throw new JAXRPCException (fault); 456 } 457 } 458 459 462 private class Entry 463 { 464 private HandlerWrapper handler; 465 private HandlerInfo info; 466 467 public Entry(HandlerWrapper handler, HandlerInfo info) 468 { 469 this.handler = handler; 470 this.info = info; 471 } 472 473 public Handler getHandler() 474 { 475 return handler; 476 } 477 478 public HandlerInfo getInfo() 479 { 480 return info; 481 } 482 } 483 484 486 public boolean remove(Object o) 487 { 488 return handlers.remove(o); 489 } 490 491 public boolean containsAll(Collection c) 492 { 493 return handlers.containsAll(c); 494 } 495 496 public boolean removeAll(Collection c) 497 { 498 return handlers.removeAll(c); 499 } 500 501 public boolean retainAll(Collection c) 502 { 503 return handlers.retainAll(c); 504 } 505 506 public int hashCode() 507 { 508 return handlers.hashCode(); 509 } 510 511 public boolean equals(Object o) 512 { 513 return handlers.equals(o); 514 } 515 516 public Iterator iterator() 517 { 518 return handlers.iterator(); 519 } 520 521 public List subList(int fromIndex, int toIndex) 522 { 523 return handlers.subList(fromIndex, toIndex); 524 } 525 526 public ListIterator listIterator() 527 { 528 return handlers.listIterator(); 529 } 530 531 public ListIterator listIterator(int index) 532 { 533 return handlers.listIterator(index); 534 } 535 536 public int size() 537 { 538 return handlers.size(); 539 } 540 541 public void clear() 542 { 543 handlers.clear(); 544 } 545 546 public boolean isEmpty() 547 { 548 return handlers.isEmpty(); 549 } 550 551 public Object [] toArray() 552 { 553 return handlers.toArray(); 554 } 555 556 public Object get(int index) 557 { 558 return handlers.get(index); 559 } 560 561 public Object remove(int index) 562 { 563 return handlers.remove(index); 564 } 565 566 public void add(int index, Object element) 567 { 568 handlers.add(index, element); 569 } 570 571 public int indexOf(Object elem) 572 { 573 return handlers.indexOf(elem); 574 } 575 576 public int lastIndexOf(Object elem) 577 { 578 return handlers.lastIndexOf(elem); 579 } 580 581 public boolean add(Object o) 582 { 583 return handlers.add(o); 584 } 585 586 public boolean contains(Object elem) 587 { 588 return handlers.contains(elem); 589 } 590 591 public boolean addAll(int index, Collection c) 592 { 593 return handlers.addAll(index, c); 594 } 595 596 public boolean addAll(Collection c) 597 { 598 return handlers.addAll(c); 599 } 600 601 public Object set(int index, Object element) 602 { 603 return handlers.set(index, element); 604 } 605 606 public Object [] toArray(Object [] a) 607 { 608 return handlers.toArray(a); 609 } 610 } 611 | Popular Tags |