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.Contract;
44  import org.apache.http.annotation.ThreadingBehavior;
45  import org.apache.http.client.HttpRequestRetryHandler;
46  import org.apache.http.client.methods.HttpUriRequest;
47  import org.apache.http.client.protocol.HttpClientContext;
48  import org.apache.http.protocol.HttpContext;
49  import org.apache.http.util.Args;
50  
51  /**
52   * The default {@link HttpRequestRetryHandler} used by request executors.
53   *
54   * @since 4.0
55   */
56  @Contract(threading = ThreadingBehavior.IMMUTABLE)
57  public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
58  
59      public static final DefaultHttpRequestRetryHandler INSTANCE = new DefaultHttpRequestRetryHandler();
60  
61      /** the number of times a method will be retried */
62      private final int retryCount;
63  
64      /** Whether or not methods that have successfully sent their request will be retried */
65      private final boolean requestSentRetryEnabled;
66  
67      private final Set<Class<? extends IOException>> nonRetriableClasses;
68  
69      /**
70       * Create the request retry handler using the specified IOException classes
71       *
72       * @param retryCount how many times to retry; 0 means no retries
73       * @param requestSentRetryEnabled true if it's OK to retry requests that have been sent
74       * @param clazzes the IOException types that should not be retried
75       * @since 4.3
76       */
77      protected DefaultHttpRequestRetryHandler(
78              final int retryCount,
79              final boolean requestSentRetryEnabled,
80              final Collection<Class<? extends IOException>> clazzes) {
81          super();
82          this.retryCount = retryCount;
83          this.requestSentRetryEnabled = requestSentRetryEnabled;
84          this.nonRetriableClasses = new HashSet<Class<? extends IOException>>();
85          for (final Class<? extends IOException> clazz: clazzes) {
86              this.nonRetriableClasses.add(clazz);
87          }
88      }
89  
90      /**
91       * Create the request retry handler using the following list of
92       * non-retriable IOException classes: <br>
93       * <ul>
94       * <li>InterruptedIOException</li>
95       * <li>UnknownHostException</li>
96       * <li>ConnectException</li>
97       * <li>SSLException</li>
98       * </ul>
99       * @param retryCount how many times to retry; 0 means no retries
100      * @param requestSentRetryEnabled true if it's OK to retry non-idempotent requests that have been sent
101      */
102     @SuppressWarnings("unchecked")
103     public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled) {
104         this(retryCount, requestSentRetryEnabled, Arrays.asList(
105                 InterruptedIOException.class,
106                 UnknownHostException.class,
107                 ConnectException.class,
108                 SSLException.class));
109     }
110 
111     /**
112      * Create the request retry handler with a retry count of 3, requestSentRetryEnabled false
113      * and using the following list of non-retriable IOException classes: <br>
114      * <ul>
115      * <li>InterruptedIOException</li>
116      * <li>UnknownHostException</li>
117      * <li>ConnectException</li>
118      * <li>SSLException</li>
119      * </ul>
120      */
121     public DefaultHttpRequestRetryHandler() {
122         this(3, false);
123     }
124     /**
125      * Used {@code retryCount} and {@code requestSentRetryEnabled} to determine
126      * if the given method should be retried.
127      */
128     @Override
129     public boolean retryRequest(
130             final IOException exception,
131             final int executionCount,
132             final HttpContext context) {
133         Args.notNull(exception, "Exception parameter");
134         Args.notNull(context, "HTTP context");
135         if (executionCount > this.retryCount) {
136             // Do not retry if over max retry count
137             return false;
138         }
139         if (this.nonRetriableClasses.contains(exception.getClass())) {
140             return false;
141         } else {
142             for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
143                 if (rejectException.isInstance(exception)) {
144                     return false;
145                 }
146             }
147         }
148         final HttpClientContext clientContext = HttpClientContext.adapt(context);
149         final HttpRequest request = clientContext.getRequest();
150 
151         if(requestIsAborted(request)){
152             return false;
153         }
154 
155         if (handleAsIdempotent(request)) {
156             // Retry if the request is considered idempotent
157             return true;
158         }
159 
160         if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) {
161             // Retry if the request has not been sent fully or
162             // if it's OK to retry methods that have been sent
163             return true;
164         }
165         // otherwise do not retry
166         return false;
167     }
168 
169     /**
170      * @return {@code true} if this handler will retry methods that have
171      * successfully sent their request, {@code false} otherwise
172      */
173     public boolean isRequestSentRetryEnabled() {
174         return requestSentRetryEnabled;
175     }
176 
177     /**
178      * @return the maximum number of times a method will be retried
179      */
180     public int getRetryCount() {
181         return retryCount;
182     }
183 
184     /**
185      * @since 4.2
186      */
187     protected boolean handleAsIdempotent(final HttpRequest request) {
188         return !(request instanceof HttpEntityEnclosingRequest);
189     }
190 
191     /**
192      * @since 4.2
193      *
194      * @deprecated (4.3)
195      */
196     @Deprecated
197     protected boolean requestIsAborted(final HttpRequest request) {
198         HttpRequest req = request;
199         if (request instanceof RequestWrapper) { // does not forward request to original
200             req = ((RequestWrapper) request).getOriginal();
201         }
202         return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted());
203     }
204 
205 }