Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/http/impl/DefaultHttpRequestRetryHandler.java


1   /*
2    * $HeadURL: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/tags/4.0-alpha2/src/java/org/apache/http/impl/DefaultHttpRequestRetryHandler.java $
3    * $Revision: 376961 $
4    * $Date: 2006-02-11 11:32:50 +0100 (Sat, 11 Feb 2006) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2006 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.http.impl;
31  
32  import java.io.IOException;
33  import java.io.InterruptedIOException;
34  import java.net.UnknownHostException;
35  
36  import javax.net.ssl.SSLHandshakeException;
37  
38  import org.apache.http.NoHttpResponseException;
39  import org.apache.http.protocol.HttpContext;
40  import org.apache.http.protocol.HttpExecutionContext;
41  import org.apache.http.protocol.HttpRequestRetryHandler;
42  
43  /**
44   * The default {@link HttpRequestRetryHandler} used by request executors.
45   * 
46   * @author Michael Becke
47   * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
48   */
49  public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
50  
51    /** the number of times a method will be retried */
52      private int retryCount;
53      
54      /** Whether or not methods that have successfully sent their request will be retried */
55      private boolean requestSentRetryEnabled;
56      
57      /**
58       * Default constructor
59       */
60      public DefaultHttpRequestRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
61          super();
62          this.retryCount = retryCount;
63          this.requestSentRetryEnabled = requestSentRetryEnabled;
64      }
65      
66      /**
67       * Default constructor
68       */
69      public DefaultHttpRequestRetryHandler() {
70          this(3, false);
71      }
72      /** 
73       * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
74       * if the given method should be retried.
75       */
76      public boolean retryRequest(
77              final IOException exception, 
78              int executionCount,
79              final HttpContext context) {
80          if (exception == null) {
81              throw new IllegalArgumentException("Exception parameter may not be null");
82          }
83          if (context == null) {
84              throw new IllegalArgumentException("HTTP context may not be null");
85          }
86          if (executionCount > this.retryCount) {
87              // Do not retry if over max retry count
88              return false;
89          }
90          if (exception instanceof NoHttpResponseException) {
91              // Retry if the server dropped connection on us
92              return true;
93          }
94          if (exception instanceof InterruptedIOException) {
95              // Timeout
96              return false;
97          }
98          if (exception instanceof UnknownHostException) {
99              // Unknown host
100             return false;
101         }
102         if (exception instanceof SSLHandshakeException) {
103             // SSL handshake exception
104             return false;
105         }
106         Boolean b = (Boolean) context.getAttribute(HttpExecutionContext.HTTP_REQ_SENT);
107         boolean sent = (b != null && b.booleanValue());
108         if (!sent || this.requestSentRetryEnabled) {
109             // Retry if the request has not been sent fully or
110             // if it's OK to retry methods that have been sent
111             return true;
112         }
113         // otherwise do not retry
114         return false;
115     }
116     
117     /**
118      * @return <code>true</code> if this handler will retry methods that have 
119      * successfully sent their request, <code>false</code> otherwise
120      */
121     public boolean isRequestSentRetryEnabled() {
122         return requestSentRetryEnabled;
123     }
124 
125     /**
126      * @return the maximum number of times a method will be retried
127      */
128     public int getRetryCount() {
129         return retryCount;
130     }
131 }