View Javadoc

1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/DefaultHttpMethodRetryHandler.java,v 1.3 2004/12/20 11:47:46 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.InterruptedIOException;
34  
35  /***
36   * The default {@link HttpMethodRetryHandler} used by {@link HttpMethod}s.
37   * 
38   * @author Michael Becke
39   * @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
40   */
41  public class DefaultHttpMethodRetryHandler implements HttpMethodRetryHandler {
42  
43  
44  	private static Class SSL_HANDSHAKE_EXCEPTION = null;
45  	
46  	static {
47  		try {
48  			SSL_HANDSHAKE_EXCEPTION = Class.forName("javax.net.ssl.SSLHandshakeException");
49  		} catch (ClassNotFoundException ignore) {			
50  		}
51  	}
52  	/*** the number of times a method will be retried */
53      private int retryCount;
54      
55      /*** Whether or not methods that have successfully sent their request will be retried */
56      private boolean requestSentRetryEnabled;
57      
58      /***
59       * Default constructor
60       */
61      public DefaultHttpMethodRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
62          super();
63          this.retryCount = retryCount;
64          this.requestSentRetryEnabled = requestSentRetryEnabled;
65      }
66      
67      /***
68       * Default constructor
69       */
70      public DefaultHttpMethodRetryHandler() {
71          this(3, false);
72      }
73      /*** 
74       * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
75       * if the given method should be retried.
76       * 
77       * @see HttpMethodRetryHandler#retryMethod(HttpMethod, IOException, int)
78       */
79      public boolean retryMethod(
80          final HttpMethod method, 
81          final IOException exception, 
82          int executionCount) {
83          if (method == null) {
84              throw new IllegalArgumentException("HTTP method may not be null");
85          }
86          if (exception == null) {
87              throw new IllegalArgumentException("Exception parameter may not be null");
88          }
89          if (executionCount > this.retryCount) {
90              // Do not retry if over max retry count
91              return false;
92          }
93          if (exception instanceof NoHttpResponseException) {
94              // Retry if the server dropped connection on us
95              return true;
96          }
97          if (exception instanceof InterruptedIOException) {
98              // Timeout
99              return false;
100         }
101         if (SSL_HANDSHAKE_EXCEPTION != null && SSL_HANDSHAKE_EXCEPTION.isInstance(exception)) {
102             // SSL handshake exception
103             return false;
104         }
105         if (!method.isRequestSent() || this.requestSentRetryEnabled) {
106             // Retry if the request has not been sent fully or
107             // if it's OK to retry methods that have been sent
108             return true;
109         }
110         // otherwise do not retry
111         return false;
112     }
113     
114     /***
115      * @return <code>true</code> if this handler will retry methods that have 
116      * successfully sent their request, <code>false</code> otherwise
117      */
118     public boolean isRequestSentRetryEnabled() {
119         return requestSentRetryEnabled;
120     }
121 
122     /***
123      * @return the maximum number of times a method will be retried
124      */
125     public int getRetryCount() {
126         return retryCount;
127     }
128 }