1 16 17 package org.apache.axis ; 18 19 import java.io.PrintStream ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.Vector ; 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 27 import javax.xml.namespace.QName ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 import javax.xml.rpc.soap.SOAPFaultException ; 30 31 import org.apache.axis.components.logger.LogFactory; 32 import org.apache.axis.encoding.SerializationContext; 33 import org.apache.axis.message.SOAPEnvelope; 34 import org.apache.axis.message.SOAPFault; 35 import org.apache.axis.message.SOAPHeaderElement; 36 import org.apache.axis.soap.SOAPConstants; 37 import org.apache.axis.utils.JavaUtils; 38 import org.apache.axis.utils.XMLUtils; 39 import org.apache.axis.utils.NetworkUtils; 40 import org.apache.commons.logging.Log; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.Text ; 44 45 59 60 public class AxisFault extends java.rmi.RemoteException { 61 64 protected static Log log = 65 LogFactory.getLog(AxisFault.class.getName()); 66 67 protected QName faultCode ; 68 69 protected Vector faultSubCode ; 70 protected String faultString = ""; 71 protected String faultActor ; 72 protected Vector faultDetails ; protected String faultNode ; 74 75 76 protected ArrayList faultHeaders = null; 77 78 88 public static AxisFault makeFault(Exception e) 89 { 90 if (e instanceof InvocationTargetException ) { 91 Throwable t = ((InvocationTargetException )e).getTargetException(); 92 if (t instanceof Exception ) { 93 e = (Exception )t; 94 } 95 } 96 97 if (e instanceof AxisFault) { 98 return (AxisFault)e; 99 } 100 101 return new AxisFault(e); 102 } 103 104 113 public AxisFault(String code, String faultString, 114 String actor, Element[] details) { 115 this(new QName (Constants.NS_URI_AXIS, code), 116 faultString, actor, details); 117 } 118 119 128 public AxisFault(QName code, String faultString, 129 String actor, Element[] details) { 130 super (faultString); 131 setFaultCode( code ); 132 setFaultString( faultString ); 133 setFaultActor( actor ); 134 setFaultDetail( details ); 135 if (details == null) { 136 initFromException(this); 137 } 138 } 139 140 152 public AxisFault(QName code, QName [] subcodes, String faultString, 153 String actor, String node, Element[] details) { 154 super (faultString); 155 setFaultCode( code ); 156 if (subcodes != null) { 157 for (int i = 0; i < subcodes.length; i++) { 158 addFaultSubCode( subcodes[i] ); 159 } 160 } 161 setFaultString( faultString ); 162 setFaultActor( actor ); 163 setFaultNode( node ); 164 setFaultDetail( details ); 165 if (details == null) { 166 initFromException(this); 167 } 168 } 169 170 177 protected AxisFault(Exception target) { 178 super ("", target); 179 setFaultCodeAsString( Constants.FAULT_SERVER_USER ); 181 initFromException(target); 182 183 if ( target instanceof SOAPFaultException ) { 186 removeHostname(); 188 initFromSOAPFaultException((SOAPFaultException ) target); 189 addHostnameIfNeeded(); 191 } 192 193 } 194 195 200 public AxisFault(String message) 201 { 202 super (message); 203 setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL); 204 setFaultString(message); 205 initFromException(this); 206 } 207 208 211 public AxisFault() 212 { 213 super(); 214 setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL); 215 initFromException(this); 216 } 217 218 225 public AxisFault (String message, Throwable t) 226 { 227 super (message, t); 228 setFaultCodeAsString(Constants.FAULT_SERVER_GENERAL); 229 setFaultString(getMessage()); 230 addHostnameIfNeeded(); 231 } 232 233 240 private void initFromException(Exception target) 241 { 242 Element oldStackTrace = lookupFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE); 244 if (oldStackTrace != null) { 245 return; 247 } 248 249 setFaultString( target.toString() ); 251 252 253 if ((target instanceof AxisFault) && 262 (target.getClass() != AxisFault.class)) { 263 addFaultDetail(Constants.QNAME_FAULTDETAIL_EXCEPTIONNAME, 264 target.getClass().getName()); 265 } 266 267 if (target == this) { 269 addFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE, 273 getPlainStackTrace()); 274 } else { 275 addFaultDetail(Constants.QNAME_FAULTDETAIL_STACKTRACE, 276 JavaUtils.stackToString(target)); 277 } 278 279 addHostnameIfNeeded(); 281 } 282 283 287 private void initFromSOAPFaultException(SOAPFaultException fault) { 288 289 if ( fault.getFaultCode() != null ) { 291 setFaultCode(fault.getFaultCode()); 292 } 293 294 if ( fault.getFaultString() != null ) { 296 setFaultString(fault.getFaultString()); 297 } 298 299 if ( fault.getFaultActor() != null ) { 301 setFaultActor(fault.getFaultActor()); 302 } 303 304 if ( null == fault.getDetail() ) { 305 return; 306 } 307 308 Vector details = new Vector (); 310 Iterator detailIter = fault.getDetail().getChildElements(); 311 while (detailIter.hasNext()) { 312 details.add( detailIter.next()); 313 } 314 315 setFaultDetail( XMLUtils.asElementArray(details)); 317 } 318 319 323 private void initFaultDetails() { 324 if (faultDetails == null) { 325 faultDetails = new Vector (); 326 } 327 } 328 329 332 public void clearFaultDetails() { 333 faultDetails=null; 334 } 335 336 339 public void dump() { 340 log.debug(dumpToString()); 341 } 342 343 344 350 public String dumpToString() 351 { 352 StringBuffer buf = new StringBuffer ("AxisFault"); 353 buf.append(JavaUtils.LS); 354 buf.append(" faultCode: "); 355 buf.append(XMLUtils.xmlEncodeString(faultCode.toString())); 356 buf.append(JavaUtils.LS); 357 buf.append(" faultSubcode: "); 358 if (faultSubCode != null) { 359 for (int i = 0; i < faultSubCode.size(); i++) { 360 buf.append(JavaUtils.LS); 361 buf.append(faultSubCode.elementAt(i).toString()); 362 } 363 } 364 buf.append(JavaUtils.LS); 365 buf.append(" faultString: "); 366 buf.append(XMLUtils.xmlEncodeString(faultString)); 367 buf.append(JavaUtils.LS); 368 buf.append(" faultActor: "); 369 buf.append(XMLUtils.xmlEncodeString(faultActor)); 370 buf.append(JavaUtils.LS); 371 buf.append(" faultNode: "); 372 buf.append(XMLUtils.xmlEncodeString(faultNode)); 373 buf.append(JavaUtils.LS); 374 buf.append(" faultDetail: "); 375 if (faultDetails != null) { 376 for (int i=0; i < faultDetails.size(); i++) { 377 Element e = (Element) faultDetails.get(i); 378 buf.append(JavaUtils.LS); 379 buf.append("\t{"); 380 buf.append(null == e.getNamespaceURI() ? "" : e.getNamespaceURI()); 381 buf.append("}"); 382 buf.append(null == e.getLocalName() ? "" : e.getLocalName()); 383 buf.append(":"); 384 buf.append(XMLUtils.getInnerXMLString(e)); 385 } 386 } 387 buf.append(JavaUtils.LS); 388 return buf.toString(); 389 } 390 391 396 public void setFaultCode(QName code) { 397 faultCode = code ; 398 } 399 400 407 408 public void setFaultCode(String code) { 409 setFaultCodeAsString(code); 410 } 411 412 417 public void setFaultCodeAsString(String code) { 418 SOAPConstants soapConstants = MessageContext.getCurrentContext() == null ? 419 SOAPConstants.SOAP11_CONSTANTS : 420 MessageContext.getCurrentContext().getSOAPConstants(); 421 422 faultCode = new QName (soapConstants.getEnvelopeURI(), code); 423 } 424 425 430 public QName getFaultCode() { 431 return( faultCode ); 432 } 433 434 442 public void addFaultSubCodeAsString(String code) { 443 initFaultSubCodes(); 444 faultSubCode.add(new QName (Constants.NS_URI_AXIS, code)); 445 } 446 447 451 protected void initFaultSubCodes() { 452 if (faultSubCode == null) { 453 faultSubCode = new Vector (); 454 } 455 } 456 457 464 public void addFaultSubCode(QName code) { 465 initFaultSubCodes(); 466 faultSubCode.add(code); 467 } 468 469 475 public void clearFaultSubCodes() { 476 faultSubCode = null; 477 } 478 479 484 public QName [] getFaultSubCodes() { 485 if (faultSubCode == null) { 486 return null; 487 } 488 QName [] q = new QName [faultSubCode.size()]; 489 return (QName [])faultSubCode.toArray(q); 490 } 491 492 493 497 public void setFaultString(String str) { 498 if (str != null) { 499 faultString = str ; 500 } else { 501 faultString = ""; 502 } 503 } 504 505 511 public String getFaultString() { 512 return( faultString ); 513 } 514 515 521 public void setFaultReason(String str) { 522 setFaultString(str); 523 } 524 525 530 public String getFaultReason() { 531 return getFaultString(); 532 } 533 534 539 public void setFaultActor(String actor) { 540 faultActor = actor ; 541 } 542 543 547 public String getFaultActor() { 548 return( faultActor ); 549 } 550 551 556 public String getFaultRole() { 557 return getFaultActor(); 558 } 559 560 566 public void setFaultRole(String role) { 567 setFaultActor(role); 568 } 569 570 577 public String getFaultNode() { 578 return( faultNode ); 579 } 580 581 589 public void setFaultNode(String node) { 590 faultNode = node; 591 } 592 593 598 public void setFaultDetail(Element[] details) { 599 if ( details == null ) { 600 faultDetails=null; 601 return ; 602 } 603 faultDetails = new Vector ( details.length ); 604 for ( int loop = 0 ; loop < details.length ; loop++ ) { 605 faultDetails.add( details[loop] ); 606 } 607 } 608 609 613 public void setFaultDetailString(String details) { 614 clearFaultDetails(); 615 addFaultDetailString(details); 616 } 617 618 622 public void addFaultDetailString(String detail) { 623 initFaultDetails(); 624 try { 625 Document doc = XMLUtils.newDocument(); 626 Element element = doc.createElement("string"); 627 Text text = doc.createTextNode(detail); 628 element.appendChild(text); 629 faultDetails.add(element); 630 } catch (ParserConfigurationException e) { 631 throw new InternalException(e); 633 } 634 } 635 636 642 public void addFaultDetail(Element detail) { 643 initFaultDetails(); 644 faultDetails.add(detail); 645 } 646 647 653 public void addFaultDetail(QName qname,String body) { 654 Element detail = XMLUtils.StringToElement(qname.getNamespaceURI(), 655 qname.getLocalPart(), 656 body); 657 658 addFaultDetail(detail); 659 } 660 661 667 public Element[] getFaultDetails() { 668 if (faultDetails == null) { 669 return null; 670 } 671 Element result[] = new Element[faultDetails.size()]; 672 for (int i=0; i<result.length; i++) { 673 result[i] = (Element) faultDetails.elementAt(i); 674 } 675 return result; 676 } 677 678 684 public Element lookupFaultDetail(QName qname) { 685 if (faultDetails != null) { 686 String searchNamespace = qname.getNamespaceURI(); 689 String searchLocalpart = qname.getLocalPart(); 690 Iterator it=faultDetails.iterator(); 692 while (it.hasNext()) { 693 Element e = (Element) it.next(); 694 String localpart= e.getLocalName(); 695 if(localpart==null) { 696 localpart=e.getNodeName(); 697 } 698 String namespace= e.getNamespaceURI(); 699 if(namespace==null) { 700 namespace=""; 701 } 702 if(searchNamespace.equals(namespace) 705 && searchLocalpart.equals(localpart)) { 706 return e; 707 } 708 } 709 } 710 return null; 711 } 712 713 720 public boolean removeFaultDetail(QName qname) { 721 Element elt=lookupFaultDetail(qname); 722 if(elt==null) { 723 return false; 724 } else { 725 return faultDetails.remove(elt); 726 } 727 } 728 729 735 public void output(SerializationContext context) throws Exception { 736 737 SOAPConstants soapConstants = Constants.DEFAULT_SOAP_VERSION; 738 if (context.getMessageContext() != null) { 739 soapConstants = context.getMessageContext().getSOAPConstants(); 740 } 741 742 SOAPEnvelope envelope = new SOAPEnvelope(soapConstants); 743 744 SOAPFault fault = new SOAPFault(this); 745 envelope.addBodyElement(fault); 746 747 if (faultHeaders != null) { 749 for (Iterator i = faultHeaders.iterator(); i.hasNext();) { 750 SOAPHeaderElement header = (SOAPHeaderElement) i.next(); 751 envelope.addHeader(header); 752 } 753 } 754 755 envelope.output(context); 756 } 757 758 763 public String toString() { 764 return faultString; 765 } 766 767 770 private String getPlainStackTrace() { 771 StringWriter sw = new StringWriter (512); 772 PrintWriter pw = new PrintWriter (sw); 773 super.printStackTrace(pw); 774 pw.close(); 775 return sw.toString(); 776 } 777 778 784 public void printStackTrace(PrintStream ps) { 785 ps.println(dumpToString()); 786 super.printStackTrace(ps); 787 } 788 789 795 public void printStackTrace(java.io.PrintWriter pw) { 796 pw.println(dumpToString()); 797 super.printStackTrace(pw); 798 } 799 800 806 public void addHeader(SOAPHeaderElement header) { 807 if (faultHeaders == null) { 808 faultHeaders = new ArrayList (); 809 } 810 faultHeaders.add(header); 811 } 812 813 818 public ArrayList getHeaders() { 819 return faultHeaders; 820 } 821 822 825 public void clearHeaders() { 826 faultHeaders = null; 827 } 828 829 830 842 public void writeDetails(QName qname, SerializationContext context) 843 throws java.io.IOException { 844 Object detailObject = this.detail; 845 if (detailObject == null) { 846 return; 847 } 848 849 boolean haveSerializer = false; 850 try { 851 if (context.getTypeMapping().getSerializer(detailObject.getClass()) != null) { 852 haveSerializer = true; 853 } 854 } catch (Exception e) { 855 } 858 if (haveSerializer) { 859 boolean oldMR = context.getDoMultiRefs(); 860 context.setDoMultiRefs(false); 861 context.serialize(qname, null, detailObject); 862 context.setDoMultiRefs(oldMR); 863 } 864 } 865 866 871 public void addHostnameIfNeeded() { 872 if(lookupFaultDetail(Constants.QNAME_FAULTDETAIL_HOSTNAME)!=null) { 874 return; 876 } 877 addHostname(NetworkUtils.getLocalHostname()); 878 } 879 880 885 public void addHostname(String hostname) { 886 removeHostname(); 888 addFaultDetail(Constants.QNAME_FAULTDETAIL_HOSTNAME, 889 hostname); 890 } 891 892 896 public void removeHostname() { 897 removeFaultDetail(Constants.QNAME_FAULTDETAIL_HOSTNAME); 898 } 899 } 900 | Popular Tags |