olegk       2004/05/11 13:43:55

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        HttpConnection.java HttpMethod.java
                        HttpMethodBase.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestNoHost.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        SimpleHttpServer.java
                        SimpleHttpServerConnection.java
  Added:       httpclient/src/test/org/apache/commons/httpclient
                        TestMethodAbort.java
  Log:
  PR #20288 (httpMethod.abort needed)
  
  Added ability to abort the execution of HTTP methods
  
  Contributed by Oleg Kalnichevski
  Reviewed by Michael Becke
  
  Revision  Changes    Path
  1.90      +6 -4      
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.89
  retrieving revision 1.90
  diff -u -r1.89 -r1.90
  --- HttpConnection.java       18 Apr 2004 23:51:34 -0000      1.89
  +++ HttpConnection.java       11 May 2004 20:43:54 -0000      1.90
  @@ -1035,6 +1035,8 @@
       /**
        * Attempts to shutdown the [EMAIL PROTECTED] Socket}'s output, via 
Socket.shutdownOutput()
        * when running on JVM 1.3 or higher.
  +     * 
  +     * @deprecated unused
        */
       public void shutdownOutput() {
           LOG.trace("enter HttpConnection.shutdownOutput()");
  
  
  
  1.37      +12 -5     
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java
  
  Index: HttpMethod.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethod.java,v
  retrieving revision 1.36
  retrieving revision 1.37
  diff -u -r1.36 -r1.37
  --- HttpMethod.java   18 Apr 2004 23:51:35 -0000      1.36
  +++ HttpMethod.java   11 May 2004 20:43:54 -0000      1.37
  @@ -33,7 +33,7 @@
   import java.io.InputStream;
   
   import org.apache.commons.httpclient.auth.AuthState;
  -import org.apache.commons.httpclient.params.*;
  +import org.apache.commons.httpclient.params.HttpMethodParams;
   
   /**
    * <p>
  @@ -433,6 +433,13 @@
        */
       int execute(HttpState state, HttpConnection connection) 
           throws HttpException, IOException;
  +
  +    /**
  +     * Aborts the execution of the HTTP method.
  +     * 
  +     * @see #execute(HttpState, HttpConnection)
  +     */
  +    void abort();
   
       /**
        * Recycles the HTTP method so that it can be used again.
  
  
  
  1.206     +39 -8     
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
  
  Index: HttpMethodBase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
  retrieving revision 1.205
  retrieving revision 1.206
  diff -u -r1.205 -r1.206
  --- HttpMethodBase.java       8 May 2004 10:12:07 -0000       1.205
  +++ HttpMethodBase.java       11 May 2004 20:43:54 -0000      1.206
  @@ -168,6 +168,9 @@
       /** HTTP protocol version used for execution of this method. */
       private HttpVersion effectiveVersion = null;
   
  +    /** Whether the execution of this method has been aborted */
  +    private transient boolean aborted = false;
  +
       // ----------------------------------------------------------- Constructors
   
       /**
  @@ -917,10 +920,9 @@
           if (conn == null) {
               throw new IllegalArgumentException("HttpConnection parameter may not be 
null");
           }
  -        // TODO: do we care
  -//        if (hasBeenUsed()) {
  -//            throw new IllegalStateException("Already used, but not recycled.");
  -//        }
  +        if (this.aborted) {
  +            throw new IllegalStateException("Method has been aborted");
  +        }
           if (!validate()) {
               throw new ProtocolException("HttpMethodBase object not valid");
           }
  @@ -974,6 +976,21 @@
       }
   
       /**
  +     * Aborts the execution of this method.
  +     */
  +
  +    public void abort() {
  +        if (this.aborted) {
  +            return;
  +        }
  +        this.aborted = true;
  +        HttpConnection conn = this.responseConnection; 
  +        if (conn != null) {
  +            conn.close();
  +        }
  +    }
  +
  +    /**
        * Returns <tt>true</tt> if the HTTP method has been already [EMAIL PROTECTED] 
#execute executed},
        * but not [EMAIL PROTECTED] #recycle recycled}.
        * 
  @@ -1004,6 +1021,8 @@
           getResponseHeaderGroup().clear();
           getResponseTrailerHeaderGroup().clear();
           statusLine = null;
  +        effectiveVersion = null;
  +        aborted = false;
           used = false;
           params = new HttpMethodParams();
           responseBody = null;
  @@ -2288,4 +2307,16 @@
       public AuthState getProxyAuthState() {
           return this.proxyAuthState;
       }
  +    
  +    /**
  +     * Tests whether the execution of this method has been aborted
  +     * 
  +     * @return <tt>true</tt> if the execution of this method has been aborted,
  +     *  <tt>false</tt> otherwise
  +     * 
  +     * @since 3.0
  +     */
  +    public boolean isAborted() {
  +        return this.aborted;
  +    }    
   }
  
  
  
  1.38      +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.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- TestNoHost.java   9 May 2004 12:16:12 -0000       1.37
  +++ TestNoHost.java   11 May 2004 20:43:55 -0000      1.38
  @@ -89,6 +89,7 @@
           suite.addTest(TestBadContentLength.suite());
           suite.addTest(TestEquals.suite());
           suite.addTestSuite(TestIdleConnectionTimeout.class);
  +        suite.addTest(TestMethodAbort.suite());
           return suite;
       }
   
  
  
  
  1.1                  
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodAbort.java
  
  Index: TestMethodAbort.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestMethodAbort.java,v
 1.1 2004/05/11 20:43:55 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/05/11 20:43:55 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * 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;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.httpclient.methods.GetMethod;
  import org.apache.commons.httpclient.server.HttpRequestHandler;
  import org.apache.commons.httpclient.server.ResponseWriter;
  import org.apache.commons.httpclient.server.SimpleHttpServerConnection;
  import org.apache.commons.httpclient.server.SimpleRequest;
  
  /**
   * Tests ability to abort method execution.
   *
   * @author Oleg Kalnichevski
   * 
   * @version $Revision: 1.1 $
   */
  public class TestMethodAbort extends HttpClientTestBase {
  
      // ------------------------------------------------------------ Constructor
      public TestMethodAbort(String testName) {
          super(testName);
      }
  
      // ------------------------------------------------------------------- Main
      public static void main(String args[]) {
          String[] testCaseName = { TestMethodAbort.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestMethodAbort.class);
      }
  
      private class ProduceGarbageHandler implements HttpRequestHandler {
  
          public ProduceGarbageHandler() {
              super();
          }
  
          public boolean processRequest(
              final SimpleHttpServerConnection conn,
              final SimpleRequest request) throws IOException
          {
  
              final String garbage = "garbage!\r\n";  
              final long count = 1000000000;  
  
              String protocol = request.getRequestLine().getProtocol();
              ResponseWriter out = conn.getWriter();
              out.println(protocol + " 200 OK");
              out.println("Content-Type: text/plain");
              out.println("Content-Length: " + count * garbage.length()) ;
              out.println("Connection: close");
              out.println();
              for (int i = 0; i < count; i++) {
                  out.print(garbage);
              }
              return true;
          }
      }
  
      public void testAbortMethod() throws IOException {
          this.server.setRequestHandler(new ProduceGarbageHandler());
          final GetMethod httpget = new GetMethod("/test/");
          
          Thread thread = new Thread(new Runnable() {
              public void run() {            
                  try {
                      Thread.sleep(500);
                  } catch (InterruptedException e) {
                  }
                  httpget.abort();
              }
              
          });
          thread.setDaemon(true); 
          thread.start();
          
          try {
              this.client.executeMethod(httpget);
              BufferedReader in = new BufferedReader(new InputStreamReader(
                  httpget.getResponseBodyAsStream()));
              String line = null;
              while ((line = in.readLine()) != null) {  
              }
              fail("IOException must have been thrown");
          } catch (IOException e) {
              // expected
          } finally {
              httpget.releaseConnection();
          }
          assertTrue(httpget.isAborted());
      }
  
      public void testAbortedMethodExecute() throws IOException {
          final GetMethod httpget = new GetMethod("/test/");
          
          try {
              httpget.abort();
              try {
                  this.client.executeMethod(httpget);
                  fail("IllegalStateException must have been thrown");
              } catch (IllegalStateException e) {
              }
          } finally {
              httpget.releaseConnection();
          }
          assertTrue(httpget.isAborted());
      }
  }
  
  
  
  1.6       +6 -12     
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java
  
  Index: SimpleHttpServer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServer.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SimpleHttpServer.java     27 Feb 2004 19:01:34 -0000      1.5
  +++ SimpleHttpServer.java     11 May 2004 20:43:55 -0000      1.6
  @@ -35,7 +35,6 @@
   import java.net.InetAddress;
   import java.net.ServerSocket;
   import java.net.Socket;
  -import java.net.SocketException;
   import java.util.HashSet;
   import java.util.Iterator;
   import java.util.Set;
  @@ -277,20 +276,15 @@
                       t.setDaemon(true);
                       t.start();
                   } catch (IOException e) {
  -                    LOG.error("SimpleHttpServer error", e);
  -                    throw new RuntimeException(e.getMessage());
  +                    LOG.error("I/O error: " + e.getMessage());
                   }
                   Thread.sleep(100);
               }
           } catch (InterruptedException accept) {
  -        } catch (SocketException e) {
  +        } catch (IOException e) {
               if (!stopped) {
  -                LOG.error("SimpleHttpServer error", e);
  -                throw new RuntimeException(e.getMessage());
  +                LOG.error("I/O error: " + e.getMessage());
               }
  -        } catch (IOException e) {
  -            LOG.error("SimpleHttpServer error", e);
  -            throw new RuntimeException(e.getMessage());
           } finally {
               destroy();
           }
  
  
  
  1.8       +4 -7      
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java
  
  Index: SimpleHttpServerConnection.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SimpleHttpServerConnection.java   27 Feb 2004 19:01:34 -0000      1.7
  +++ SimpleHttpServerConnection.java   11 May 2004 20:43:55 -0000      1.8
  @@ -36,7 +36,6 @@
   import java.io.OutputStream;
   import java.io.UnsupportedEncodingException;
   import java.net.Socket;
  -import java.net.SocketException;
   
   import org.apache.commons.httpclient.HttpParser;
   import org.apache.commons.httpclient.HttpStatus;
  @@ -92,10 +91,8 @@
                   ++this.requestNo;
                   readRequest();
               } while (keepAlive);
  -        } catch (SocketException ignore) {
           } catch (IOException e) {
  -            LOG.error("ServerConnection read error", e);
  -            throw new RuntimeException(e.getMessage());
  +            LOG.error("I/O error: " + e.getMessage());
           } finally {
               destroy();
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to