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