001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import java.io.IOException;
034: import java.io.PrintStream;
035: import java.io.PrintWriter;
036: import java.lang.reflect.Method;
037:
038: /**
039: * Signals that an HTTP or HttpClient exception has occurred.
040: *
041: * @author Laura Werner
042: *
043: * @version $Revision: 480424 $ $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
044: */
045: public class HttpException extends IOException {
046:
047: /**
048: * Creates a new HttpException with a <tt>null</tt> detail message.
049: */
050: public HttpException() {
051: super ();
052: this .cause = null;
053: }
054:
055: /**
056: * Creates a new HttpException with the specified detail message.
057: *
058: * @param message the exception detail message
059: */
060: public HttpException(String message) {
061: super (message);
062: this .cause = null;
063: }
064:
065: /**
066: * Creates a new HttpException with the specified detail message and cause.
067: *
068: * @param message the exception detail message
069: * @param cause the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
070: * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
071: *
072: * @since 3.0
073: */
074: public HttpException(String message, Throwable cause) {
075: super (message);
076: this .cause = cause;
077:
078: // If we're running on JDK 1.4 or later, tell Throwable what the cause was
079: try {
080: Class[] paramsClasses = new Class[] { Throwable.class };
081: Method initCause = Throwable.class.getMethod("initCause",
082: paramsClasses);
083: initCause.invoke(this , new Object[] { cause });
084: } catch (Exception e) {
085: // The setCause method must not be available
086: }
087: }
088:
089: /**
090: * Return the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
091: * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>.
092: *
093: * @return the <tt>Throwable</tt> that caused this exception, or <tt>null</tt>
094: * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
095: *
096: * @since 3.0
097: */
098: public Throwable getCause() {
099: return cause;
100: }
101:
102: /**
103: * Print this HttpException and its stack trace to the standard error stream.
104: *
105: * @since 3.0
106: */
107: public void printStackTrace() {
108: printStackTrace(System.err);
109: }
110:
111: /**
112: * Print this HttpException and its stack trace to the specified print stream.
113: *
114: * @param s the <tt>PrintStream</tt> to which the exception and its stack trace
115: * should be written
116: *
117: * @since 3.0
118: */
119: public void printStackTrace(PrintStream s) {
120: try {
121: // JDK 1.4 has a nice printStackTrace method that prints the cause's stack
122: // trace too and prunes out duplicate stack frames. Call it if possible,
123: // which is determined by checking whether JDK 1.4's getStackTrace method is present
124: Class[] paramsClasses = new Class[] {};
125: this .getClass().getMethod("getStackTrace", paramsClasses);
126: super .printStackTrace(s);
127: } catch (Exception ex) {
128: // If that didn't work, print it out ourselves
129: // First print this exception's stack trace.
130: super .printStackTrace(s);
131: if (cause != null) {
132: // Print out the exception that caused this one.
133: // This will recurse if the cause is another HttpException.
134: s.print("Caused by: ");
135: cause.printStackTrace(s);
136: }
137: }
138: }
139:
140: /**
141: * Print this HttpException and its stack trace to the specified print writer.
142: *
143: * @param s the <tt>PrintWriter</tt> to which the exception and its stack trace
144: * should be written
145: *
146: * @since 3.0
147: */
148: public void printStackTrace(PrintWriter s) {
149: try {
150: // JDK 1.4 has a nice printStackTrace method that prints the cause's stack
151: // trace too and prunes out duplicate stack frames. Call it if possible,
152: // which is determined by checking whether JDK 1.4's getStackTrace method is present
153: Class[] paramsClasses = new Class[] {};
154: this .getClass().getMethod("getStackTrace", paramsClasses);
155: super .printStackTrace(s);
156: } catch (Exception ex) {
157: // If that didn't work, print it out ourselves
158: // First print this exception's stack trace.
159: super .printStackTrace(s);
160: if (cause != null) {
161: // Print out the exception that caused this one.
162: // This will recurse if the cause is another HttpException.
163: s.print("Caused by: ");
164: cause.printStackTrace(s);
165: }
166: }
167: }
168:
169: /**
170: * Sets the text description of the reason for an exception.
171: *
172: * @param reason The reason for the exception.
173: *
174: * @deprecated HttpClient no longer uses this for itself. It is only
175: * provided for compatibility with existing clients, and will be removed
176: * in a future release.
177: */
178: public void setReason(String reason) {
179: this .reason = reason;
180: }
181:
182: /**
183: * Get the text description of the reason for an exception.
184: *
185: * @deprecated HttpClient no longer uses this for itself. It is only
186: * provided for compatibility with existing clients, and will be removed
187: * in a future release.
188: */
189: public String getReason() {
190: return reason;
191: }
192:
193: /**
194: * Sets the status code description of the reason for an exception.
195: *
196: * @param code The reason for the exception. This is intended to be an
197: * HTTP status code.
198: *
199: * @deprecated HttpClient no longer uses this for itself. It is only
200: * provided for compatibility with existing clients, and will be removed
201: * in a future release.
202: */
203: public void setReasonCode(int code) {
204: reasonCode = code;
205: }
206:
207: /**
208: * Get the status code description of the reason for an exception.
209: *
210: * @deprecated HttpClient no longer uses this for itself. It is only
211: * provided for compatibility with existing clients, and will be removed
212: * in a future release.
213: */
214: public int getReasonCode() {
215: return this .reasonCode;
216: }
217:
218: /**
219: * A "reason" string provided for compatibility with older clients.
220: *
221: * @deprecated HttpClient no longer uses this field for itself. It
222: * is only provided for compatibility with existing clients.
223: */
224: private String reason;
225:
226: /**
227: * Reason code for compatibility with older clients.
228: *
229: * @deprecated HttpClient no longer uses this field for itself.
230: * It is only provided for compatibility with existing clients.
231: */
232: private int reasonCode = HttpStatus.SC_OK;
233:
234: /** The original Throwable representing the cause of this error */
235: private final Throwable cause;
236: }
|