HttpException.java
  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: 480424 $
  4   * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
  5   *
  6   * ====================================================================
  7   *
  8   *  Licensed to the Apache Software Foundation (ASF) under one or more
  9   *  contributor license agreements.  See the NOTICE file distributed with
 10   *  this work for additional information regarding copyright ownership.
 11   *  The ASF licenses this file to You under the Apache License, Version 2.0
 12   *  (the "License"); you may not use this file except in compliance with
 13   *  the License.  You may obtain a copy of the License at
 14   *
 15   *      http://www.apache.org/licenses/LICENSE-2.0
 16   *
 17   *  Unless required by applicable law or agreed to in writing, software
 18   *  distributed under the License is distributed on an "AS IS" BASIS,
 19   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 20   *  See the License for the specific language governing permissions and
 21   *  limitations under the License.
 22   * ====================================================================
 23   *
 24   * This software consists of voluntary contributions made by many
 25   * individuals on behalf of the Apache Software Foundation.  For more
 26   * information on the Apache Software Foundation, please see
 27   * <http://www.apache.org/>.
 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          // If we're running on JDK 1.4 or later, tell Throwable what the cause was
 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              // The setCause method must not be available
 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              // JDK 1.4 has a nice printStackTrace method that prints the cause's stack
121              // trace too and prunes out duplicate stack frames.  Call it if possible,
122              // which is determined by checking whether JDK 1.4's getStackTrace method is present 
123              Class[] paramsClasses = new Class[] {  };
124              this.getClass().getMethod("getStackTrace", paramsClasses);
125              super.printStackTrace(s);
126          } catch (Exception ex) {
127              // If that didn't work, print it out ourselves
128              // First print this exception's stack trace.
129              super.printStackTrace(s);
130              if (cause != null) {
131                  // Print out the exception that caused this one.
132                  // This will recurse if the cause is another HttpException.
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              // JDK 1.4 has a nice printStackTrace method that prints the cause's stack
150              // trace too and prunes out duplicate stack frames.  Call it if possible,
151              // which is determined by checking whether JDK 1.4's getStackTrace method is present 
152              Class[] paramsClasses = new Class[] {  };
153              this.getClass().getMethod("getStackTrace", paramsClasses);
154              super.printStackTrace(s);
155          } catch (Exception ex) {
156              // If that didn't work, print it out ourselves
157              // First print this exception's stack trace.
158              super.printStackTrace(s);
159              if (cause != null) {
160                  // Print out the exception that caused this one.
161                  // This will recurse if the cause is another HttpException.
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  }