View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/HttpException.java,v 1.19 2004/09/30 18:53:20 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
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          // If we're running on JDK 1.4 or later, tell Throwable what the cause was
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              // The setCause method must not be available
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             // JDK 1.4 has a nice printStackTrace method that prints the cause's stack
120             // trace too and prunes out duplicate stack frames.  Call it if possible,
121             // which is determined by checking whether JDK 1.4's getStackTrace method is present 
122             Class[] paramsClasses = new Class[] {  };
123             this.getClass().getMethod("getStackTrace", paramsClasses);
124             super.printStackTrace(s);
125         } catch (Exception ex) {
126             // If that didn't work, print it out ourselves
127             // First print this exception's stack trace.
128             super.printStackTrace(s);
129             if (cause != null) {
130                 // Print out the exception that caused this one.
131                 // This will recurse if the cause is another HttpException.
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             // JDK 1.4 has a nice printStackTrace method that prints the cause's stack
149             // trace too and prunes out duplicate stack frames.  Call it if possible,
150             // which is determined by checking whether JDK 1.4's getStackTrace method is present 
151             Class[] paramsClasses = new Class[] {  };
152             this.getClass().getMethod("getStackTrace", paramsClasses);
153             super.printStackTrace(s);
154         } catch (Exception ex) {
155             // If that didn't work, print it out ourselves
156             // First print this exception's stack trace.
157             super.printStackTrace(s);
158             if (cause != null) {
159                 // Print out the exception that caused this one.
160                 // This will recurse if the cause is another HttpException.
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 }