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