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       * @since 4.3
70       */
71      protected DefaultHttpRequestRetryHandler(
72              final int retryCount,
73              final boolean requestSentRetryEnabled,
74              final Collection<Class<? extends IOException>> clazzes) {
75          super();
76          this.retryCount = retryCount;
77          this.requestSentRetryEnabled = requestSentRetryEnabled;
78          this.nonRetriableClasses = new HashSet<Class<? extends IOException>>();
79          for (final Class<? extends IOException> clazz: clazzes) {
80              this.nonRetriableClasses.add(clazz);
81          }
82      }
83  
84      /**
85       * Default constructor
86       */
87      @SuppressWarnings("unchecked")
88      public DefaultHttpRequestRetryHandler(final int retryCount, final boolean requestSentRetryEnabled) {
89          this(retryCount, requestSentRetryEnabled, Arrays.asList(
90                  InterruptedIOException.class,
91                  UnknownHostException.class,
92                  ConnectException.class,
93                  SSLException.class));
94      }
95  
96      /**
97       * Default constructor
98       */
99      public DefaultHttpRequestRetryHandler() {
100         this(3, false);
101     }
102     /**
103      * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
104      * if the given method should be retried.
105      */
106     public boolean retryRequest(
107             final IOException exception,
108             final int executionCount,
109             final HttpContext context) {
110         Args.notNull(exception, "Exception parameter");
111         Args.notNull(context, "HTTP context");
112         if (executionCount > this.retryCount) {
113             // Do not retry if over max retry count
114             return false;
115         }
116         if (this.nonRetriableClasses.contains(exception.getClass())) {
117             return false;
118         } else {
119             for (final Class<? extends IOException> rejectException : this.nonRetriableClasses) {
120                 if (rejectException.isInstance(exception)) {
121                     return false;
122                 }
123             }
124         }
125         final HttpClientContext clientContext = HttpClientContext.adapt(context);
126         final HttpRequest request = clientContext.getRequest();
127 
128         if(requestIsAborted(request)){
129             return false;
130         }
131 
132         if (handleAsIdempotent(request)) {
133             // Retry if the request is considered idempotent
134             return true;
135         }
136 
137         if (!clientContext.isRequestSent() || this.requestSentRetryEnabled) {
138             // Retry if the request has not been sent fully or
139             // if it's OK to retry methods that have been sent
140             return true;
141         }
142         // otherwise do not retry
143         return false;
144     }
145 
146     /**
147      * @return <code>true</code> if this handler will retry methods that have
148      * successfully sent their request, <code>false</code> otherwise
149      */
150     public boolean isRequestSentRetryEnabled() {
151         return requestSentRetryEnabled;
152     }
153 
154     /**
155      * @return the maximum number of times a method will be retried
156      */
157     public int getRetryCount() {
158         return retryCount;
159     }
160 
161     /**
162      * @since 4.2
163      */
164     protected boolean handleAsIdempotent(final HttpRequest request) {
165         return !(request instanceof HttpEntityEnclosingRequest);
166     }
167 
168     /**
169      * @since 4.2
170      *
171      * @deprecated (4.3)
172      */
173     @Deprecated
174     protected boolean requestIsAborted(final HttpRequest request) {
175         HttpRequest req = request;
176         if (request instanceof RequestWrapper) { // does not forward request to original
177             req = ((RequestWrapper) request).getOriginal();
178         }
179         return (req instanceof HttpUriRequest && ((HttpUriRequest)req).isAborted());
180     }
181 
182 }