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 package org.apache.http.impl.client.cache;
28
29 import java.io.IOException;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.http.Header;
34 import org.apache.http.HttpException;
35 import org.apache.http.HttpResponse;
36 import org.apache.http.client.cache.HeaderConstants;
37 import org.apache.http.client.cache.HttpCacheEntry;
38 import org.apache.http.client.methods.HttpExecutionAware;
39 import org.apache.http.client.methods.HttpRequestWrapper;
40 import org.apache.http.client.methods.CloseableHttpResponse;
41 import org.apache.http.client.protocol.HttpClientContext;
42 import org.apache.http.conn.routing.HttpRoute;
43
44
45
46
47
48 public class AsynchronousValidationRequest implements Runnable {
49 private final AsynchronousValidator parent;
50 private final CachingExec cachingExec;
51 private final HttpRoute route;
52 private final HttpRequestWrapper request;
53 private final HttpClientContext context;
54 private final HttpExecutionAware execAware;
55 private final HttpCacheEntry cacheEntry;
56 private final String identifier;
57 private final int consecutiveFailedAttempts;
58
59 private final Log log = LogFactory.getLog(getClass());
60
61
62
63
64
65
66
67
68
69
70 AsynchronousValidationRequest(
71 final AsynchronousValidator parent,
72 final CachingExec cachingExec,
73 final HttpRoute route,
74 final HttpRequestWrapper request,
75 final HttpClientContext context,
76 final HttpExecutionAware execAware,
77 final HttpCacheEntry cacheEntry,
78 final String identifier,
79 final int consecutiveFailedAttempts) {
80 this.parent = parent;
81 this.cachingExec = cachingExec;
82 this.route = route;
83 this.request = request;
84 this.context = context;
85 this.execAware = execAware;
86 this.cacheEntry = cacheEntry;
87 this.identifier = identifier;
88 this.consecutiveFailedAttempts = consecutiveFailedAttempts;
89 }
90
91 @Override
92 public void run() {
93 try {
94 if (revalidateCacheEntry()) {
95 parent.jobSuccessful(identifier);
96 } else {
97 parent.jobFailed(identifier);
98 }
99 } finally {
100 parent.markComplete(identifier);
101 }
102 }
103
104
105
106
107
108
109
110
111 private boolean revalidateCacheEntry() {
112 try {
113 final CloseableHttpResponse httpResponse = cachingExec.revalidateCacheEntry(route, request, context, execAware, cacheEntry);
114 try {
115 final int statusCode = httpResponse.getStatusLine().getStatusCode();
116 return isNotServerError(statusCode) && isNotStale(httpResponse);
117 } finally {
118 httpResponse.close();
119 }
120 } catch (final IOException ioe) {
121 log.debug("Asynchronous revalidation failed due to I/O error", ioe);
122 return false;
123 } catch (final HttpException pe) {
124 log.error("HTTP protocol exception during asynchronous revalidation", pe);
125 return false;
126 } catch (final RuntimeException re) {
127 log.error("RuntimeException thrown during asynchronous revalidation: " + re);
128 return false;
129 }
130 }
131
132
133
134
135
136
137 private boolean isNotServerError(final int statusCode) {
138 return statusCode < 500;
139 }
140
141
142
143
144
145
146 private boolean isNotStale(final HttpResponse httpResponse) {
147 final Header[] warnings = httpResponse.getHeaders(HeaderConstants.WARNING);
148 if (warnings != null)
149 {
150 for (final Header warning : warnings)
151 {
152
153
154
155
156
157 final String warningValue = warning.getValue();
158 if (warningValue.startsWith("110") || warningValue.startsWith("111"))
159 {
160 return false;
161 }
162 }
163 }
164 return true;
165 }
166
167 public String getIdentifier() {
168 return identifier;
169 }
170
171
172
173
174
175 public int getConsecutiveFailedAttempts() {
176 return consecutiveFailedAttempts;
177 }
178
179 }