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.UnknownHostException;
35
36 /***
37 * The default {@link HttpMethodRetryHandler} used by {@link HttpMethod}s.
38 *
39 * @author Michael Becke
40 * @author <a href="mailto:oleg -at- ural.ru">Oleg Kalnichevski</a>
41 */
42 public class DefaultHttpMethodRetryHandler implements HttpMethodRetryHandler {
43
44
45 private static Class SSL_HANDSHAKE_EXCEPTION = null;
46
47 static {
48 try {
49 SSL_HANDSHAKE_EXCEPTION = Class.forName("javax.net.ssl.SSLHandshakeException");
50 } catch (ClassNotFoundException ignore) {
51 }
52 }
53 /*** the number of times a method will be retried */
54 private int retryCount;
55
56 /*** Whether or not methods that have successfully sent their request will be retried */
57 private boolean requestSentRetryEnabled;
58
59 /***
60 * Default constructor
61 */
62 public DefaultHttpMethodRetryHandler(int retryCount, boolean requestSentRetryEnabled) {
63 super();
64 this.retryCount = retryCount;
65 this.requestSentRetryEnabled = requestSentRetryEnabled;
66 }
67
68 /***
69 * Default constructor
70 */
71 public DefaultHttpMethodRetryHandler() {
72 this(3, false);
73 }
74 /***
75 * Used <code>retryCount</code> and <code>requestSentRetryEnabled</code> to determine
76 * if the given method should be retried.
77 *
78 * @see HttpMethodRetryHandler#retryMethod(HttpMethod, IOException, int)
79 */
80 public boolean retryMethod(
81 final HttpMethod method,
82 final IOException exception,
83 int executionCount) {
84 if (method == null) {
85 throw new IllegalArgumentException("HTTP method may not be null");
86 }
87 if (exception == null) {
88 throw new IllegalArgumentException("Exception parameter may not be null");
89 }
90 if (executionCount > this.retryCount) {
91
92 return false;
93 }
94 if (exception instanceof NoHttpResponseException) {
95
96 return true;
97 }
98 if (exception instanceof InterruptedIOException) {
99
100 return false;
101 }
102 if (exception instanceof UnknownHostException) {
103
104 return false;
105 }
106 if (SSL_HANDSHAKE_EXCEPTION != null && SSL_HANDSHAKE_EXCEPTION.isInstance(exception)) {
107
108 return false;
109 }
110 if (!method.isRequestSent() || this.requestSentRetryEnabled) {
111
112
113 return true;
114 }
115
116 return false;
117 }
118
119 /***
120 * @return <code>true</code> if this handler will retry methods that have
121 * successfully sent their request, <code>false</code> otherwise
122 */
123 public boolean isRequestSentRetryEnabled() {
124 return requestSentRetryEnabled;
125 }
126
127 /***
128 * @return the maximum number of times a method will be retried
129 */
130 public int getRetryCount() {
131 return retryCount;
132 }
133 }