1 package org.codehaus.xfire.fault; 2 3 import java.util.List; 4 import org.w3c.dom.Element; 5 6 /*** 7 * <p> 8 * In XFire, applications throw their own declared exceptions which are 9 * then turned into faults. The XFireFault class wraps these exceptions 10 * extracting out the details for the fault message. 11 * </p> 12 * <p> 13 * If the developer wishes to generate their own custom fault messages, 14 * they can either override XFireFault to provide the FaultHandlers with 15 * the necessary information or write a new FaultHandler. 16 * </p> 17 * <p> 18 * TODO Add i18n support 19 * </p> 20 * 21 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a> 22 * @since Feb 14, 2004 23 */ 24 public class XFireFault 25 extends Exception 26 { 27 /*** Fault codes. */ 28 public final static String VERSION_MISMATCH = "VersionMismatch"; 29 public final static String MUST_UNDERSTAND = "MustUnderstand"; 30 public final static String DATA_ENCODING_UNKNOWN = "DataEncodingUnknown"; 31 32 /*** 33 * "The message was incorrectly formed or did not contain the appropriate 34 * information in order to succeed." -- SOAP 1.2 Spec 35 */ 36 public final static String SENDER = "Sender"; 37 38 /*** 39 * "The message could not be processed for reasons attributable to the 40 * processing of the message rather than to the contents of the message itself." 41 * -- SOAP 1.2 Spec 42 */ 43 public final static String RECEIVER = "Receiver"; 44 45 private String faultCode; 46 47 private Element subCode; 48 49 private List detail; 50 51 private Exception exception; 52 53 /*** 54 * Create a fault. 55 * 56 * @param string The fault message. 57 * @param exception The exception which caused this fault. 58 * @param code The fault code. See XFireFault's static fields. 59 */ 60 public XFireFault( String message, 61 Exception exception, 62 String code ) 63 { 64 super( message, exception ); 65 this.faultCode = code; 66 } 67 68 /*** 69 * Create a fault for the specified exception. The faultCode is 70 * set to RECEIVER. 71 * @param exception 72 */ 73 public XFireFault( Exception exception ) 74 { 75 super( exception ); 76 this.faultCode = RECEIVER; 77 } 78 79 /*** 80 * Create a fault with the specified faultCode. The exception 81 * message is used for the fault message. 82 * 83 * @param exception The exception that caused this fault. 84 * @param code The fault code. See XFireFault's static fields. 85 */ 86 public XFireFault(Exception exception, String code) 87 { 88 super( exception.getMessage(), exception ); 89 this.faultCode = code; 90 } 91 92 /*** 93 * Create an exception wih the specified fault message 94 * and faultCode. 95 * 96 * @param message The fault message. 97 * @param code The fault code. See XFireFault's static fields. 98 */ 99 public XFireFault(String message, String code) 100 { 101 super( message ); 102 this.faultCode = code; 103 } 104 105 public static XFireFault createFault( Exception e ) 106 { 107 XFireFault fault = null; 108 109 if ( e instanceof XFireFault ) 110 { 111 fault = (XFireFault) e; 112 } 113 else 114 { 115 fault = new XFireFault( e ); 116 } 117 118 return fault; 119 } 120 121 /*** 122 * @return 123 */ 124 public String getCode() 125 { 126 return faultCode; 127 } 128 129 public String getReason() 130 { 131 return getMessage(); 132 } 133 134 /*** 135 * Returns the SubCode for the Fault Code. 136 * 137 * TODO: The spec seems unclear whether or not there can be multiple 138 * subcodes. Its an easy enough change to support multiple subcodes. 139 * 140 * @return The SubCode element as detailed by the SOAP 1.2 spec. 141 */ 142 public Element getSubCode() 143 { 144 return subCode; 145 } 146 147 public void setSubCode(Element subCode) 148 { 149 this.subCode = subCode; 150 } 151 152 /*** 153 * A list of Element's that provide the message detail. 154 * @return 155 */ 156 public List getDetail() 157 { 158 return detail; 159 } 160 161 public void setDetail( List detail ) 162 { 163 this.detail = detail; 164 } 165 }