1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient;
31
32 import java.io.IOException;
33 import java.io.InterruptedIOException;
34 import java.net.NoRouteToHostException;
35 import java.net.UnknownHostException;
36
37 /***
38 * The default {@link HttpMethodRetryHandler} used by {@link HttpMethod}s.
39 *
40 * @author Michael Becke
41 * @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
42 */
43 public class DefaultHttpMethodRetryHandler implements HttpMethodRetryHandler {
44
45
46 private static Class SSL_HANDSHAKE_EXCEPTION = null;
47
48 static {
49 try {
50 SSL_HANDSHAKE_EXCEPTION = Class.forName("javax.net.ssl.SSLHandshakeException");
51 } catch (ClassNotFoundException ignore) {
52 }
53 }
54 /*** the number of times a method will be retried */
55 private int retryCount;
56
57 /*** Whether or not methods that have successfully sent their request will be retried */
58 private boolean requestSentRetryEnabled;
59
60 /***
61 * Creates a new DefaultHttpMethodRetryHandler.
62 * @param retryCount the number of times a method will be retried
63 * @param requestSentRetryEnabled if true, methods that have successfully sent their request will be retried
64 */
65 public DefaultHttpMethodRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
66 super();
67 this.retryCount = retryCount;
68 this.requestSentRetryEnabled = requestSentRetryEnabled;
69 }
70
71 /***
72 * Creates a new DefaultHttpMethodRetryHandler that retries up to 3 times
73 * but does not retry methods that have successfully sent their requests.
74 */
75 public DefaultHttpMethodRetryHandler() {
76 this(3, false);
77 }
78 /***
79 * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
80 * if the given method should be retried.
81 *
82 * @see HttpMethodRetryHandler#retryMethod(HttpMethod, IOException, int)
83 */
84 public boolean retryMethod(
85 final HttpMethod method,
86 final IOException exception,
87 int executionCount) {
88 if (method == null) {
89 throw new IllegalArgumentException("HTTP method may not be null");
90 }
91 if (exception == null) {
92 throw new IllegalArgumentException("Exception parameter may not be null");
93 }
94
95 if (method instanceof HttpMethodBase) {
96 if (((HttpMethodBase)method).isAborted()) {
97 return false;
98 }
99 }
100 if (executionCount > this.retryCount) {
101
102 return false;
103 }
104 if (exception instanceof NoHttpResponseException) {
105
106 return true;
107 }
108 if (exception instanceof InterruptedIOException) {
109
110 return false;
111 }
112 if (exception instanceof UnknownHostException) {
113
114 return false;
115 }
116 if (exception instanceof NoRouteToHostException) {
117
118 return false;
119 }
120 if (SSL_HANDSHAKE_EXCEPTION != null && SSL_HANDSHAKE_EXCEPTION.isInstance(exception)) {
121
122 return false;
123 }
124 if (!method.isRequestSent() || this.requestSentRetryEnabled) {
125
126
127 return true;
128 }
129
130 return false;
131 }
132
133 /***
134 * @return <code>true</code> if this handler will retry methods that have
135 * successfully sent their request, <code>false</code> otherwise
136 */
137 public boolean isRequestSentRetryEnabled() {
138 return requestSentRetryEnabled;
139 }
140
141 /***
142 * @return the maximum number of times a method will be retried
143 */
144 public int getRetryCount() {
145 return retryCount;
146 }
147 }