You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/08/05 21:29:30 UTC

cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestExceptions.java TestNoHost.java

olegk       2003/08/05 12:29:30

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        ChunkedInputStream.java ChunkedOutputStream.java
                        HttpConnection.java HttpException.java
                        HttpRecoverableException.java
                        HttpTimeoutException.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestExceptions.java TestNoHost.java
  Added:       httpclient/src/java/org/apache/commons/httpclient/util
                        ExceptionUtil.java
  Log:
  Bug fix #19868 (Exception handling in HttpClient requires redesign)
  
  Contributed by Oleg Kalnichevski
  Reviewed by Michael Becke & Laura Werner
  
  Revision  Changes    Path
  1.19      +7 -4      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedInputStream.java
  
  Index: ChunkedInputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedInputStream.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- ChunkedInputStream.java	16 Jul 2003 20:48:27 -0000	1.18
  +++ ChunkedInputStream.java	5 Aug 2003 19:29:30 -0000	1.19
  @@ -67,6 +67,7 @@
   import java.io.IOException;
   import java.io.InputStream;
   
  +import org.apache.commons.httpclient.util.ExceptionUtil;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -337,7 +338,9 @@
               footers = HttpParser.parseHeaders(in);
           } catch(HttpException e) {
               LOG.error("Error parsing trailer headers", e);
  -            throw new IOException(e.getMessage());
  +            IOException ioe = new IOException(e.getMessage());
  +            ExceptionUtil.initCause(ioe, e); 
  +            throw ioe;
           }
           
           for (int i = 0; i < footers.length; i++) {
  
  
  
  1.11      +5 -5      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java
  
  Index: ChunkedOutputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ChunkedOutputStream.java	19 Apr 2003 22:29:31 -0000	1.10
  +++ ChunkedOutputStream.java	5 Aug 2003 19:29:30 -0000	1.11
  @@ -121,7 +121,7 @@
        */
       public ChunkedOutputStream(OutputStream stream) {
           if (stream == null) {
  -            throw new NullPointerException("stream parameter is null");
  +            throw new IllegalArgumentException("Stream parameter may not be null");
           }
           this.stream = stream;
       }
  
  
  
  1.72      +5 -5      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java
  
  Index: HttpConnection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v
  retrieving revision 1.71
  retrieving revision 1.72
  diff -u -r1.71 -r1.72
  --- HttpConnection.java	31 Jul 2003 21:21:06 -0000	1.71
  +++ HttpConnection.java	5 Aug 2003 19:29:30 -0000	1.72
  @@ -1211,7 +1211,7 @@
                       "Output exception occurred on a used connection.  Will treat as recoverable.", 
                       ioe
                   );
  -                return new HttpRecoverableException(ioe.toString());                
  +                return new HttpRecoverableException(ioe.getMessage(), ioe);                
               } else {
                   return ioe;
               }            
  
  
  
  1.15      +7 -19     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpException.java
  
  Index: HttpException.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpException.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- HttpException.java	15 Jul 2003 02:19:58 -0000	1.14
  +++ HttpException.java	5 Aug 2003 19:29:30 -0000	1.15
  @@ -70,20 +70,9 @@
   
   /**
    * Signals that an HTTP or HttpClient exception has occurred.
  - * <p>
  - * The usage of the reserved status and reason codes
  - * <ul>
  - *  <li>  -x: Internal use
  - *  <li>   0: Unknown reason
  - *  <li>   x: Basic ill-prepared reason
  - *  <li> 1xx: Informational status
  - *  <li> 2xx: Success status
  - *  <li> 3xx: Redirection status
  - *  <li> 4xx: Client error
  - *  <li> 5xx: Server error
  - * </ul>
  - *
  - * @author Unascribed
  + * 
  + * @author Laura Werner
  + * 
    * @version $Revision$ $Date$
    */
   public class HttpException extends IOException {
  @@ -123,7 +112,6 @@
               Method initCause = Throwable.class.getMethod("initCause", paramsClasses);
               initCause.invoke(this, new Object[] { cause });
           } catch (Exception e) {
  -            e.printStackTrace();
               // The setCause method must not be available
           }
       }
  
  
  
  1.11      +8 -4      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpRecoverableException.java
  
  Index: HttpRecoverableException.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpRecoverableException.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- HttpRecoverableException.java	30 Jul 2003 21:11:17 -0000	1.10
  +++ HttpRecoverableException.java	5 Aug 2003 19:29:30 -0000	1.11
  @@ -65,6 +65,8 @@
   
   import java.io.IOException;
   
  +import org.apache.commons.httpclient.util.ExceptionUtil;
  +
   /**
    * <p>
    * Signals that an HTTP or HttpClient exception has occurred.  This
  @@ -102,5 +104,7 @@
        */
       public HttpRecoverableException(String message, Throwable cause) {
           super(message);
  +        // If we're running on JDK 1.4 or later, tell Throwable what the cause was
  +        ExceptionUtil.initCause(this, cause);
       }
   }
  
  
  
  1.2       +4 -4      jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpTimeoutException.java
  
  Index: HttpTimeoutException.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpTimeoutException.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HttpTimeoutException.java	15 Jul 2003 02:19:58 -0000	1.1
  +++ HttpTimeoutException.java	5 Aug 2003 19:29:30 -0000	1.2
  @@ -95,7 +95,7 @@
        * if the cause is unavailable, unknown, or not a <tt>Throwable</tt>
        */
       public HttpTimeoutException(String message, Throwable cause) {
  -        super(message);
  +        super(message, cause);
       }
   
   }
  
  
  
  1.1                  jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java
  
  Index: ExceptionUtil.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/util/ExceptionUtil.java,v 1.1 2003/08/05 19:29:30 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2003/08/05 19:29:30 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  package org.apache.commons.httpclient.util;
  
  import java.lang.reflect.Method;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  /**
   * The home for utility methods that handle various exception-related tasks.
   * 
   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
   * @author <a href="mailto:laura@lwerner.org">Laura Werner</a>
   * 
   * @since 2.1
   */
  public class ExceptionUtil {
  
      /** Log object for this class. */
      private static final Log LOG = LogFactory.getLog(ExceptionUtil.class);
  
      /** A reference to Throwable's initCause method, or null if it's not there in this JVM */
      static private final Method initCause = getInitCauseMethod();
  
      /**
       * Returns a <code>Method<code> allowing access to
       * {@link Throwable.initCause(Throwable) initCause} method of {@link Throwable},
       * or <code>null</code> if the method
       * does not exist.
       * 
       * @return A <code>Method<code> for <code>Throwable.initCause</code>, or
       * <code>null</code> if unavailable.
       */ 
      static private Method getInitCauseMethod() {
          try {
              Class[] paramsClasses = new Class[] { Throwable.class };
              return Throwable.class.getMethod("initCause", paramsClasses);
          } catch (NoSuchMethodException e) {
              return null;
          }
      }
      
      /** 
       * If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
       * 
       * @param  throwable The throwable.
       * @param  cause     The cause of the throwable.
       */
      public static void initCause(Throwable throwable, Throwable cause) {
          if (initCause != null) {
              try {
                  initCause.invoke(throwable, new Object[] { cause });
              } catch (Exception e) {
                  LOG.warn("Exception invoking Throwable.initCause", e);
              }
          }
      }
  }
  
  
  
  1.2       +20 -10    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestExceptions.java
  
  Index: TestExceptions.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestExceptions.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestExceptions.java	15 Jul 2003 02:19:58 -0000	1.1
  +++ TestExceptions.java	5 Aug 2003 19:29:30 -0000	1.2
  @@ -66,7 +66,9 @@
   import java.io.PrintWriter;
   import java.io.StringWriter;
   
  +import junit.framework.Test;
   import junit.framework.TestCase;
  +import junit.framework.TestSuite;
   
   /**
    * 
  @@ -75,13 +77,21 @@
   public class TestExceptions extends TestCase
   {
   
  -    /**
  -     * Constructor for TestExceptions.
  -     * @param arg0
  -     */
  -    public TestExceptions(String arg0)
  -    {
  -        super(arg0);
  +    // ------------------------------------------------------------ Constructor
  +    public TestExceptions(String testName) {
  +        super(testName);
  +    }
  +
  +    // ------------------------------------------------------------------- Main
  +    public static void main(String args[]) {
  +        String[] testCaseName = { TestChallengeParser.class.getName() };
  +        junit.textui.TestRunner.main(testCaseName);
  +    }
  +
  +    // ------------------------------------------------------- TestCase Methods
  +
  +    public static Test suite() {
  +        return new TestSuite(TestChallengeParser.class);
       }
   
       /** Make sure that you can retrieve the "cause" from an HttpException */
  
  
  
  1.25      +5 -4      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java
  
  Index: TestNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- TestNoHost.java	30 Jul 2003 21:11:17 -0000	1.24
  +++ TestNoHost.java	5 Aug 2003 19:29:30 -0000	1.25
  @@ -105,6 +105,7 @@
           suite.addTest(TestRequestLine.suite());
           suite.addTest(TestPartsNoHost.suite());
           suite.addTest(TestMethodCharEncoding.suite());
  +        suite.addTest(TestExceptions.suite());
           suite.addTest(TestHttpVersion.suite());
           return suite;
       }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org