• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/module-client/src/main/java/org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java $
3  * $Revision: 652726 $
4  * $Date: 2008-05-01 18:16:51 -0700 (Thu, 01 May 2008) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.impl.client;
33 
34 import java.io.IOException;
35 import java.io.InterruptedIOException;
36 import java.net.UnknownHostException;
37 
38 import javax.net.ssl.SSLHandshakeException;
39 
40 import org.apache.http.NoHttpResponseException;
41 import org.apache.http.client.HttpRequestRetryHandler;
42 import org.apache.http.protocol.HttpContext;
43 import org.apache.http.protocol.ExecutionContext;
44 
45 /**
46  * The default {@link HttpRequestRetryHandler} used by request executors.
47  *
48  * @author Michael Becke
49  * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
50  */
51 public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
52 
53     /** the number of times a method will be retried */
54     private final int retryCount;
55 
56     /** Whether or not methods that have successfully sent their request will be retried */
57     private final boolean requestSentRetryEnabled;
58 
59     /**
60      * Default constructor
61      */
DefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled)62     public DefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
63         super();
64         this.retryCount = retryCount;
65         this.requestSentRetryEnabled = requestSentRetryEnabled;
66     }
67 
68     /**
69      * Default constructor
70      */
DefaultHttpRequestRetryHandler()71     public DefaultHttpRequestRetryHandler() {
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      */
retryRequest( final IOException exception, int executionCount, final HttpContext context)78     public boolean retryRequest(
79             final IOException exception,
80             int executionCount,
81             final HttpContext context) {
82         if (exception == null) {
83             throw new IllegalArgumentException("Exception parameter may not be null");
84         }
85         if (context == null) {
86             throw new IllegalArgumentException("HTTP context may not be null");
87         }
88         if (executionCount > this.retryCount) {
89             // Do not retry if over max retry count
90             return false;
91         }
92         if (exception instanceof NoHttpResponseException) {
93             // Retry if the server dropped connection on us
94             return true;
95         }
96         if (exception instanceof InterruptedIOException) {
97             // Timeout
98             return false;
99         }
100         if (exception instanceof UnknownHostException) {
101             // Unknown host
102             return false;
103         }
104         if (exception instanceof SSLHandshakeException) {
105             // SSL handshake exception
106             return false;
107         }
108         Boolean b = (Boolean)
109             context.getAttribute(ExecutionContext.HTTP_REQ_SENT);
110         boolean sent = (b != null && b.booleanValue());
111         if (!sent || this.requestSentRetryEnabled) {
112             // Retry if the request has not been sent fully or
113             // if it's OK to retry methods that have been sent
114             return true;
115         }
116         // otherwise do not retry
117         return false;
118     }
119 
120     /**
121      * @return <code>true</code> if this handler will retry methods that have
122      * successfully sent their request, <code>false</code> otherwise
123      */
isRequestSentRetryEnabled()124     public boolean isRequestSentRetryEnabled() {
125         return requestSentRetryEnabled;
126     }
127 
128     /**
129      * @return the maximum number of times a method will be retried
130      */
getRetryCount()131     public int getRetryCount() {
132         return retryCount;
133     }
134 }
135