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         }
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 trueif 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 && ((HttpUriRequest)req).isAborted());
202     }
203
204 }
205