1 8 9 package org.jboss.axis; 10 11 import org.jboss.axis.encoding.SerializationContext; 12 import org.jboss.axis.encoding.Serializer; 13 import org.jboss.axis.message.SOAPEnvelopeAxisImpl; 14 import org.jboss.axis.message.SOAPFaultImpl; 15 import org.jboss.axis.message.SOAPHeaderElementAxisImpl; 16 import org.jboss.axis.soap.SOAPConstants; 17 import org.jboss.axis.utils.JavaUtils; 18 import org.jboss.axis.utils.XMLUtils; 19 import org.jboss.logging.Logger; 20 import org.w3c.dom.Document ; 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Text ; 23 24 import javax.xml.namespace.QName ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.rpc.JAXRPCException ; 27 import java.io.PrintStream ; 28 import java.lang.reflect.InvocationTargetException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.Vector ; 32 33 48 49 public class AxisFault extends java.rmi.RemoteException 50 { 51 52 private static Logger log = Logger.getLogger(AxisFault.class.getName()); 53 54 protected QName faultCode; 55 58 protected Vector faultSubCode; 59 protected String faultString = ""; 60 protected String faultActor; 61 protected Vector faultDetails; protected String faultNode; 63 64 67 protected ArrayList faultHeaders; 68 69 76 public static AxisFault makeFault(Exception e) 77 { 78 if (e instanceof InvocationTargetException ) 79 { 80 InvocationTargetException ite = (InvocationTargetException )e; 81 Throwable t = ite.getTargetException(); 82 if (t instanceof Exception ) 83 e = (Exception )t; 84 } 85 86 if (e instanceof JAXRPCException ) 87 { 88 JAXRPCException rte = (JAXRPCException )e; 89 Throwable t = rte.getLinkedCause(); 90 if (t instanceof Exception ) 91 e = (Exception )t; 92 } 93 94 if (e instanceof AxisFault) 95 { 96 return (AxisFault)e; 97 } 98 99 return new AxisFault(e); 100 } 101 102 111 public AxisFault(String code, String faultString, String actor, Element [] details) 112 { 113 this(new QName (Constants.NS_URI_AXIS, code), 114 faultString, actor, details); 115 } 116 117 126 public AxisFault(QName code, String faultString, String actor, Element [] details) 127 { 128 super(faultString); 129 setFaultCode(code); 130 setFaultString(faultString); 131 setFaultActor(actor); 132 setFaultDetail(details); 133 } 134 135 147 public AxisFault(QName code, QName [] subcodes, String faultString, 148 String actor, String node, Element [] details) 149 { 150 super(faultString); 151 setFaultCode(code); 152 if (subcodes != null) 153 { 154 for (int i = 0; i < subcodes.length; i++) 155 { 156 addFaultSubCode(subcodes[i]); 157 } 158 } 159 setFaultString(faultString); 160 setFaultActor(actor); 161 setFaultNode(node); 162 setFaultDetail(details); 163 } 164 165 169 protected AxisFault(Exception target) 170 { 171 super(target.getMessage(), assertTarget(target)); 172 setFaultCodeAsString(Constants.FAULT_SERVER_USER); 174 initFromException(target); 175 } 176 177 181 private static Throwable assertTarget(Throwable target) 182 { 183 return target; 186 } 187 188 194 public AxisFault(String message) 195 { 196 super(message); 197 setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL); 198 setFaultString(message); 199 } 200 201 204 public AxisFault() 205 { 206 super(); 207 setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL); 208 } 209 210 218 public AxisFault(String message, Throwable target) 219 { 220 super(message, assertTarget(target)); 221 setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL); 222 setFaultString(getMessage()); 223 } 224 225 233 private void initFromException(Exception target) 234 { 235 assertTarget(target); 236 237 String faultString = target.getMessage(); 239 setFaultString(faultString != null ? faultString : target.toString()); 240 241 Throwable cause = target; 242 if (target instanceof AxisFault) 243 cause = ((AxisFault)target).detail; 244 245 if (cause != null) 247 { 248 addFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE, JavaUtils.stackToString(cause)); 250 } 251 } 252 253 257 private void initFaultDetails() 258 { 259 if (faultDetails == null) 260 { 261 faultDetails = new Vector (); 262 } 263 } 264 265 268 public void clearFaultDetails() 269 { 270 faultDetails = null; 271 } 272 273 276 public void dump() 277 { 278 log.debug(dumpToString()); 279 } 280 281 282 289 public String dumpToString() 290 { 291 String details = new String (); 292 293 if (faultDetails != null) 294 { 295 for (int i = 0; i < faultDetails.size(); i++) 296 { 297 Element e = (Element )faultDetails.get(i); 298 String namespace = e.getNamespaceURI(); 299 if (namespace == null) 300 { 301 namespace = ""; 302 } 303 String partname = e.getLocalName(); 304 if (partname == null) 305 { 306 partname = e.getNodeName(); 307 } 308 details += JavaUtils.LS 309 + "\t{" + namespace + "}" 310 + partname + ": " 311 + XMLUtils.getInnerXMLString(e); 312 } 313 } 314 315 String subCodes = new String (); 316 if (faultSubCode != null) 317 { 318 for (int i = 0; i < faultSubCode.size(); i++) 319 { 320 subCodes += JavaUtils.LS 321 + (QName )faultSubCode.elementAt(i); 322 } 323 } 324 String code = XMLUtils.xmlEncodeString(faultCode.toString()); 327 String errorString = XMLUtils.xmlEncodeString(faultString); 328 String actor = XMLUtils.xmlEncodeString(faultActor); 329 String node = XMLUtils.xmlEncodeString(faultNode); 330 331 332 return "AxisFault" + JavaUtils.LS 333 + " faultCode: " + code + JavaUtils.LS 334 + " faultSubcode: " + subCodes + JavaUtils.LS 335 + " faultString: " + errorString + JavaUtils.LS 336 + " faultActor: " + actor + JavaUtils.LS 337 + " faultNode: " + node + JavaUtils.LS 338 + " faultDetail: " + details + JavaUtils.LS 339 ; 340 } 341 342 347 public void setFaultCode(QName code) 348 { 349 faultCode = code; 350 } 351 352 358 359 public void setFaultCode(String code) 360 { 361 setFaultCodeAsString(code); 362 } 363 364 370 public void setFaultCodeAsString(String code) 371 { 372 SOAPConstants soapConstants = MessageContext.getCurrentContext() == null ? 373 SOAPConstants.SOAP11_CONSTANTS : 374 MessageContext.getCurrentContext().getSOAPConstants(); 375 376 faultCode = new QName (soapConstants.getEnvelopeURI(), code); 377 } 378 379 384 public QName getFaultCode() 385 { 386 return (faultCode); 387 } 388 389 394 public void addFaultSubCodeAsString(String code) 395 { 396 initFaultSubCodes(); 397 faultSubCode.add(new QName (Constants.NS_URI_AXIS, code)); 398 } 399 400 404 protected void initFaultSubCodes() 405 { 406 if (faultSubCode == null) 407 { 408 faultSubCode = new Vector (); 409 } 410 } 411 412 417 public void addFaultSubCode(QName code) 418 { 419 initFaultSubCodes(); 420 faultSubCode.add(code); 421 } 422 423 428 public void clearFaultSubCodes() 429 { 430 faultSubCode = null; 431 } 432 433 439 public QName [] getFaultSubCodes() 440 { 441 if (faultSubCode == null) 442 { 443 return null; 444 } 445 QName [] q = new QName [faultSubCode.size()]; 446 return (QName [])faultSubCode.toArray(q); 447 } 448 449 450 455 public void setFaultString(String str) 456 { 457 if (str != null) 458 { 459 faultString = str; 460 } 461 else 462 { 463 faultString = ""; 464 } 465 } 466 467 473 public String getFaultString() 474 { 475 return (faultString); 476 } 477 478 483 public void setFaultReason(String str) 484 { 485 setFaultString(str); 486 } 487 488 494 public String getFaultReason() 495 { 496 return getFaultString(); 497 } 498 499 504 public void setFaultActor(String actor) 505 { 506 faultActor = actor; 507 } 508 509 514 public String getFaultActor() 515 { 516 return (faultActor); 517 } 518 519 525 public String getFaultRole() 526 { 527 return getFaultActor(); 528 } 529 530 535 public void setFaultRole(String role) 536 { 537 setFaultActor(role); 538 } 539 540 546 public String getFaultNode() 547 { 548 return (faultNode); 549 } 550 551 556 public void setFaultNode(String node) 557 { 558 faultNode = node; 559 } 560 561 566 public void setFaultDetail(Element [] details) 567 { 568 if (details == null) 569 { 570 faultDetails = null; 571 return; 572 } 573 574 faultDetails = new Vector (details.length); 575 for (int loop = 0; loop < details.length; loop++) 576 { 577 faultDetails.add(details[loop]); 578 } 579 } 580 581 586 public void setFaultDetailString(String details) 587 { 588 clearFaultDetails(); 589 addFaultDetailString(details); 590 } 591 592 597 public void addFaultDetailString(String detail) 598 { 599 initFaultDetails(); 600 try 601 { 602 Document doc = XMLUtils.newDocument(); 603 Element element = doc.createElement("string"); 604 Text text = doc.createTextNode(detail); 605 element.appendChild(text); 606 faultDetails.add(element); 607 } 608 catch (ParserConfigurationException e) 609 { 610 throw new InternalException(e); 612 } 613 } 614 615 621 public void addFaultDetail(Element detail) 622 { 623 initFaultDetails(); 624 faultDetails.add(detail); 625 } 626 627 633 public void addFaultDetail(QName qname, String body) 634 { 635 Element detail = XMLUtils.StringToElement(qname.getNamespaceURI(), 636 qname.getLocalPart(), 637 body); 638 639 addFaultDetail(detail); 640 } 641 642 647 public Element [] getFaultDetails() 648 { 649 if (faultDetails == null) 650 { 651 return null; 652 } 653 Element result[] = new Element [faultDetails.size()]; 654 for (int i = 0; i < result.length; i++) 655 result[i] = (Element )faultDetails.elementAt(i); 656 return result; 657 } 658 659 666 public Element lookupFaultDetail(QName qname) 667 { 668 if (faultDetails != null) 669 { 670 String searchNamespace = qname.getNamespaceURI(); 673 String searchLocalpart = qname.getLocalPart(); 674 Iterator it = faultDetails.iterator(); 676 while (it.hasNext()) 677 { 678 Element e = (Element )it.next(); 679 String localpart = e.getLocalName(); 680 if (localpart == null) 681 { 682 localpart = e.getNodeName(); 683 } 684 String namespace = e.getNamespaceURI(); 685 if (namespace == null) 686 { 687 namespace = ""; 688 } 689 if (searchNamespace.equals(namespace) 692 && searchLocalpart.equals(localpart)) 693 { 694 return e; 695 } 696 } 697 } 698 return null; 699 } 700 701 708 public boolean removeFaultDetail(QName qname) 709 { 710 Element elt = lookupFaultDetail(qname); 711 if (elt == null) 712 { 713 return false; 714 } 715 else 716 { 717 return faultDetails.remove(elt); 718 } 719 } 720 721 727 public void output(SerializationContext context) throws Exception 728 { 729 730 SOAPConstants soapConstants = Constants.DEFAULT_SOAP_VERSION; 731 if (context.getMessageContext() != null) 732 soapConstants = context.getMessageContext().getSOAPConstants(); 733 734 SOAPEnvelopeAxisImpl envelope = new SOAPEnvelopeAxisImpl(soapConstants); 735 736 SOAPFaultImpl fault = new SOAPFaultImpl(this); 737 envelope.addBodyElement(fault); 738 739 if (faultHeaders != null) 741 { 742 for (Iterator i = faultHeaders.iterator(); i.hasNext();) 743 { 744 SOAPHeaderElementAxisImpl header = (SOAPHeaderElementAxisImpl)i.next(); 745 envelope.addHeader(header); 746 } 747 } 748 749 envelope.output(context); 750 } 751 752 757 public String toString() 758 { 759 return getClass().getName() + ": " + faultString; 760 } 761 762 768 public void printStackTrace(PrintStream ps) 769 { 770 ps.println(dumpToString()); 771 super.printStackTrace(ps); 772 } 773 774 780 public void printStackTrace(java.io.PrintWriter pw) 781 { 782 pw.println(dumpToString()); 783 super.printStackTrace(pw); 784 } 785 786 791 public void addHeader(SOAPHeaderElementAxisImpl header) 792 { 793 if (faultHeaders == null) 794 { 795 faultHeaders = new ArrayList (); 796 } 797 faultHeaders.add(header); 798 } 799 800 805 public ArrayList getHeaders() 806 { 807 return faultHeaders; 808 } 809 810 813 public void clearHeaders() 814 { 815 faultHeaders = null; 816 } 817 818 819 825 public void writeDetails(QName qname, SerializationContext context) throws java.io.IOException 826 { 827 828 Object detailObject = this.detail; 829 if (detailObject == null) 830 return; 831 832 Serializer ser = context.getSerializerForJavaType(detailObject.getClass()); 833 if (ser != null) 834 { 835 boolean oldMR = context.getDoMultiRefs(); 836 context.setDoMultiRefs(false); 837 context.serialize(qname, null, detailObject); 838 context.setDoMultiRefs(oldMR); 839 } 840 } 841 } 842 | Popular Tags |