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.NoRouteToHostException;
34  import java.net.UnknownHostException;
35  import java.util.Arrays;
36  import java.util.Collection;
37  import java.util.HashSet;
38  import java.util.Set;
39  
40  import javax.net.ssl.SSLException;
41  
42  import org.apache.http.HttpEntityEnclosingRequest;
43  import org.apache.http.HttpRequest;
44  import org.apache.http.annotation.Contract;
45  import org.apache.http.annotation.ThreadingBehavior;
46  import org.apache.http.client.HttpRequestRetryHandler;
47  import org.apache.http.client.methods.HttpUriRequest;
48  import org.apache.http.client.protocol.HttpClientContext;
49  import org.apache.http.protocol.HttpContext;
50  import org.apache.http.util.Args;
51  
52  /**
53   * The default {@link HttpRequestRetryHandler} used by request executors.
54   *
55   * @since 4.0
56   */
57  @Contract(threading = ThreadingBehavior.IMMUTABLE)
58  public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler {
59  
60      public static final DefaultHttpRequestRetryHandlerHandler.html#DefaultHttpRequestRetryHandler">DefaultHttpRequestRetryHandler INSTANCE = new DefaultHttpRequestRetryHandler();
61  
62      /** the number of times a method will be retried */
63      private final int retryCount;
64  
65      /** Whether or not methods that have successfully sent their request will be retried */
66      private final boolean requestSentRetryEnabled;
67  
68      private final Set<Class<? extends IOException>> nonRetriableClasses;
69  
70      /**
71       * Create the request retry handler using the specified IOException classes
72       *
73       * @param retryCount how many times to retry; 0 means no retries
74       * @param requestSentRetryEnabled true if it's OK to retry requests that have been sent
75       * @param clazzes the IOException types that should not be retried
76       * @since 4.3
77       */
78      protected DefaultHttpRequestRetryHandler(
79              final int retryCount,
80              final boolean requestSentRetryEnabled,
81              final Collection<Class<? extends IOException>> clazzes) {
82          super();
83          this.retryCount = retryCount;
84          this.requestSentRetryEnabled = requestSentRetryEnabled;
85          this.nonRetriableClasses = new HashSet<Class<? extends IOException>>();
86          this.nonRetriableClasses.addAll(clazzes);
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 non-idempotent 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                 NoRouteToHostException.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         }
142         for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
143             if (rejectException.isInstance(exception)) {
144                 return false;
145             }
146         }
147         final HttpClientContext clientContext = HttpClientContext.adapt(context);
148         final HttpRequest request = clientContext.getRequest();
149 
150         if(requestIsAborted(request)){
151             return false;
152         }
153 
154         if (handleAsIdempotent(request)) {
155             // Retry if the request is considered idempotent
156             return true;
157         }
158 
159         if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) {
160             // Retry if the request has not been sent fully or
161             // if it's OK to retry methods that have been sent
162             return true;
163         }
164         // otherwise do not retry
165         return false;
166     }
167 
168     /**
169      * @return {@code true} if this handler will retry methods that have
170      * successfully sent their request, {@code false} otherwise
171      */
172     public boolean isRequestSentRetryEnabled() {
173         return requestSentRetryEnabled;
174     }
175 
176     /**
177      * @return the maximum number of times a method will be retried
178      */
179     public int getRetryCount() {
180         return retryCount;
181     }
182 
183     /**
184      * @since 4.2
185      */
186     protected boolean handleAsIdempotent(final HttpRequest request) {
187         return !(request instanceof HttpEntityEnclosingRequest);
188     }
189 
190     /**
191      * @since 4.2
192      *
193      * @deprecated (4.3)
194      */
195     @Deprecated
196     protected boolean requestIsAborted(final HttpRequest request) {
197         HttpRequest req = request;
198         if (request instanceof RequestWrapper) { // does not forward request to original
199             req = ((RequestWrapper) request).getOriginal();
200         }
201         return (req instanceof HttpUriRequest/../org/apache/http/client/methods/HttpUriRequest.html#HttpUriRequest">HttpUriRequest && ((HttpUriRequest)req).isAborted());
202     }
203 
204 }