1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  package org.apache.commons.httpclient;
32  
33  import java.io.IOException;
34  import java.io.PrintStream;
35  import java.io.PrintWriter;
36  import java.lang.reflect.Method;
37  
38  /**
39   * Signals that an HTTP or HttpClient exception has occurred.
40   * 
41   * @author Laura Werner
42   * 
43   * @version $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
44   */
45  public class HttpException extends IOException {
46  
47      /**
48       * Creates a new HttpException with a <tt>null</tt> detail message.
49       */
50      public HttpException() {
51          super();
52          this.cause = null;
53      }
54  
55      /**
56       * Creates a new HttpException with the specified detail message.
57       *
58       * @param message the exception detail message
59       */
60      public HttpException(String message) {
61          super(message);
62          this.cause = null;
63      }
64  
65      /**
66       * Creates a new HttpException with the specified detail message and cause.
67       * 
68       * @param message the exception detail message
69       * @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
70       * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
71       * 
72       * @since 3.0
73       */
74      public HttpException(String message, Throwable cause) {
75          super(message);
76          this.cause = cause;
77          
78          
79          try {
80              Class[] paramsClasses = new Class[] { Throwable.class };
81              Method initCause = Throwable.class.getMethod("initCause", paramsClasses);
82              initCause.invoke(this, new Object[] { cause });
83          } catch (Exception e) {
84              
85          }
86      }
87  
88      /**
89       * Return the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
90       *         if the cause is unavailable, unknown, or not a <tt>Throwable</tt>.
91       * 
92       * @return the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
93       *         if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
94       * 
95       * @since 3.0
96       */
97      public Throwable getCause() {
98          return cause;
99      }
100 
101     /**
102      * Print this HttpException and its stack trace to the standard error stream.
103      * 
104      * @since 3.0
105      */
106     public void printStackTrace() {
107         printStackTrace(System.err);
108     }
109 
110     /**
111      * Print this HttpException and its stack trace to the specified print stream.
112      * 
113      * @param s the <tt>PrintStream</tt> to which the exception and its stack trace
114      * should be written
115      * 
116      * @since 3.0
117      */
118     public void printStackTrace(PrintStream s) {
119         try {
120             
121             
122             
123             Class[] paramsClasses = new Class[] {  };
124             this.getClass().getMethod("getStackTrace", paramsClasses);
125             super.printStackTrace(s);
126         } catch (Exception ex) {
127             
128             
129             super.printStackTrace(s);
130             if (cause != null) {
131                 
132                 
133                 s.print("Caused by: ");
134                 cause.printStackTrace(s);
135             }
136         }
137     }
138 
139     /**
140      * Print this HttpException and its stack trace to the specified print writer.
141      * 
142      * @param s the <tt>PrintWriter</tt> to which the exception and its stack trace
143      * should be written
144      * 
145      * @since 3.0
146      */
147     public void printStackTrace(PrintWriter s) {
148         try {
149             
150             
151             
152             Class[] paramsClasses = new Class[] {  };
153             this.getClass().getMethod("getStackTrace", paramsClasses);
154             super.printStackTrace(s);
155         } catch (Exception ex) {
156             
157             
158             super.printStackTrace(s);
159             if (cause != null) {
160                 
161                 
162                 s.print("Caused by: ");
163                 cause.printStackTrace(s);
164             }
165         }
166     }
167 
168     /**
169      * Sets the text description of the reason for an exception.
170      *
171      * @param reason The reason for the exception.
172      *
173      * @deprecated HttpClient no longer uses this for itself.  It is only
174      * provided for compatibility with existing clients, and will be removed
175      * in a future release.
176      */
177     public void setReason(String reason) {
178         this.reason = reason;
179     }
180 
181     /**
182      * Get the text description of the reason for an exception.
183      *
184      * @deprecated HttpClient no longer uses this for itself.  It is only
185      * provided for compatibility with existing clients, and will be removed
186      * in a future release.
187      */
188     public String getReason() {
189         return reason;
190     }
191 
192     /**
193      * Sets the status code description of the reason for an exception.
194      *
195      * @param code The reason for the exception.  This is intended to be an
196      *  HTTP status code.
197      *
198      * @deprecated HttpClient no longer uses this for itself.  It is only
199      * provided for compatibility with existing clients, and will be removed
200      * in a future release.
201      */
202     public void setReasonCode(int code) {
203         reasonCode = code;
204     }
205 
206     /**
207      * Get the status code description of the reason for an exception.
208      *
209      * @deprecated HttpClient no longer uses this for itself.  It is only
210      * provided for compatibility with existing clients, and will be removed
211      * in a future release.
212      */
213     public int getReasonCode() {
214         return this.reasonCode;
215     }
216 
217     /**
218      * A "reason" string provided for compatibility with older clients.
219      *
220      * @deprecated HttpClient no longer uses this field for itself.  It
221      * is only provided for compatibility with existing clients.
222      */
223     private String reason;
224 
225     /**
226      * Reason code for compatibility with older clients.
227      *
228      * @deprecated  HttpClient no longer uses this field for itself.
229      *  It is only provided for compatibility with existing clients.
230      */
231     private int reasonCode = HttpStatus.SC_OK;
232 
233     /** The original Throwable representing the cause of this error */
234     private final Throwable cause;
235 }