View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.client;
29  
30  import java.io.IOException;
31  import java.io.InterruptedIOException;
32  import java.net.ConnectException;
33  import java.net.UnknownHostException;
34  import java.util.Arrays;
35  import java.util.Collection;
36  import java.util.HashSet;
37  import java.util.Set;
38  
39  import javax.net.ssl.SSLException;
40  
41  import org.apache.http.HttpEntityEnclosingRequest;
42  import org.apache.http.HttpRequest;
43  import org.apache.http.annotation.Immutable;
44  import org.apache.http.client.HttpRequestRetryHandler;
45  import org.apache.http.client.methods.HttpUriRequest;
46  import org.apache.http.client.protocol.HttpClientContext;
47  import org.apache.http.protocol.HttpContext;
48  import org.apache.http.util.Args;
49  
50  /**
51   * The default {@link HttpRequestRetryHandler} used by request executors.
52   *
53   * @since 4.0
54   */
55  @Immutable
56  public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
57  
58      public static final DefaultHttpRequestRetryHandler INSTANCE = new DefaultHttpRequestRetryHandler();
59  
60      /** the number of times a method will be retried */
61      private final int retryCount;
62  
63      /** Whether or not methods that have successfully sent their request will be retried */
64      private final boolean requestSentRetryEnabled;
65  
66      private final Set<Class<? extends IOException>> nonRetriableClasses;
67  
68      /**
69       * Create the request retry handler using the specified IOException classes
70       *
71       * @param retryCount how many times to retry; 0 means no retries
72       * @param requestSentRetryEnabled true if it's OK to retry requests that have been sent
73       * @param clazzes the IOException types that should not be retried
74       * @since 4.3
75       */
76      protected DefaultHttpRequestRetryHandler(
77              final int retryCount,
78              final boolean requestSentRetryEnabled,
79              final Collection<Class<? extends IOException>> clazzes) {
80          super();
81          this.retryCount = retryCount;
82          this.requestSentRetryEnabled = requestSentRetryEnabled;
83          this.nonRetriableClasses = new HashSet<Class<? extends IOException>>();
84          for (final Class<? extends IOException> clazz: clazzes) {
85              this.nonRetriableClasses.add(clazz);
86          }
87      }
88  
89      /**
90       * Create the request retry handler using the following list of
91       * non-retriable IOException classes: <br>
92       * <ul>
93       * <li>InterruptedIOException</li>
94       * <li>UnknownHostException</li>
95       * <li>ConnectException</li>
96       * <li>SSLException</li>
97       * </ul>
98       * @param retryCount how many times to retry; 0 means no retries
99       * @param requestSentRetryEnabled true if it's OK to retry requests that have been sent
100      */
101     @SuppressWarnings("unchecked")
102     public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled) {
103         this(retryCount, requestSentRetryEnabled, Arrays.asList(
104                 InterruptedIOException.class,
105                 UnknownHostException.class,
106                 ConnectException.class,
107                 SSLException.class));
108     }
109 
110     /**
111      * Create the request retry handler with a retry count of 3, requestSentRetryEnabled false
112      * and using the following list of non-retriable IOException classes: <br>
113      * <ul>
114      * <li>InterruptedIOException</li>
115      * <li>UnknownHostException</li>
116      * <li>ConnectException</li>
117      * <li>SSLException</li>
118      * </ul>
119      */
120     public DefaultHttpRequestRetryHandler() {
121         this(3, false);
122     }
123     /**
124      * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
125      * if the given method should be retried.
126      */
127     public boolean retryRequest(
128             final IOException exception,
129             final int executionCount,
130             final HttpContext context) {
131         Args.notNull(exception, "Exception parameter");
132         Args.notNull(context, "HTTP context");
133         if (executionCount > this.retryCount) {
134             // Do not retry if over max retry count
135             return false;
136         }
137         if (this.nonRetriableClasses.contains(exception.getClass())) {
138             return false;
139         } else {
140             for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
141                 if (rejectException.isInstance(exception)) {
142                     return false;
143                 }
144             }
145         }
146         final HttpClientContext clientContext = HttpClientContext.adapt(context);
147         final HttpRequest request = clientContext.getRequest();
148 
149         if(requestIsAborted(request)){
150             return false;
151         }
152 
153         if (handleAsIdempotent(request)) {
154             // Retry if the request is considered idempotent
155             return true;
156         }
157 
158         if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) {
159             // Retry if the request has not been sent fully or
160             // if it's OK to retry methods that have been sent
161             return true;
162         }
163         // otherwise do not retry
164         return false;
165     }
166 
167     /**
168      * @return <code>true</code> if this handler will retry methods that have
169      * successfully sent their request, <code>false</code> otherwise
170      */
171     public boolean isRequestSentRetryEnabled() {
172         return requestSentRetryEnabled;
173     }
174 
175     /**
176      * @return the maximum number of times a method will be retried
177      */
178     public int getRetryCount() {
179         return retryCount;
180     }
181 
182     /**
183      * @since 4.2
184      */
185     protected boolean handleAsIdempotent(final HttpRequest request) {
186         return !(request instanceof HttpEntityEnclosingRequest);
187     }
188 
189     /**
190      * @since 4.2
191      *
192      * @deprecated (4.3)
193      */
194     @Deprecated
195     protected boolean requestIsAborted(final HttpRequest request) {
196         HttpRequest req = request;
197         if (request instanceof RequestWrapper) { // does not forward request to original
198             req = ((RequestWrapper) request).getOriginal();
199         }
200         return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted());
201     }
202 
203 }