Lines 26-31
Link Here
|
26 |
import java.net.URLConnection; |
26 |
import java.net.URLConnection; |
27 |
import java.util.HashMap; |
27 |
import java.util.HashMap; |
28 |
import java.util.Map; |
28 |
import java.util.Map; |
|
|
29 |
import java.util.Date; |
30 |
import java.text.SimpleDateFormat; |
29 |
|
31 |
|
30 |
import org.apache.commons.httpclient.Header; |
32 |
import org.apache.commons.httpclient.Header; |
31 |
import org.apache.commons.httpclient.HttpMethod; |
33 |
import org.apache.commons.httpclient.HttpMethod; |
Lines 48-53
Link Here
|
48 |
|
50 |
|
49 |
public static final String CLEAR = "clearEachIteration"; // $NON-NLS-1$ |
51 |
public static final String CLEAR = "clearEachIteration"; // $NON-NLS-1$ |
50 |
|
52 |
|
|
|
53 |
private static final SimpleDateFormat expiresDateFormat = new java.text.SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); |
54 |
|
51 |
private transient ThreadLocal threadCache; |
55 |
private transient ThreadLocal threadCache; |
52 |
|
56 |
|
53 |
public CacheManager() { |
57 |
public CacheManager() { |
Lines 61-74
Link Here
|
61 |
*/ |
65 |
*/ |
62 |
private static class CacheEntry{ |
66 |
private static class CacheEntry{ |
63 |
private final String lastModified; |
67 |
private final String lastModified; |
|
|
68 |
private final String expires; |
64 |
private final String etag; |
69 |
private final String etag; |
65 |
public CacheEntry(String lastModified, String etag){ |
70 |
public CacheEntry(String lastModified, String expires, String etag){ |
66 |
this.lastModified = lastModified; |
71 |
this.lastModified = lastModified; |
|
|
72 |
this.expires = expires; |
67 |
this.etag = etag; |
73 |
this.etag = etag; |
68 |
} |
74 |
} |
69 |
public String getLastModified() { |
75 |
public String getLastModified() { |
70 |
return lastModified; |
76 |
return lastModified; |
71 |
} |
77 |
} |
|
|
78 |
public String getExpires() { |
79 |
return expires; |
80 |
} |
72 |
public String getEtag() { |
81 |
public String getEtag() { |
73 |
return etag; |
82 |
return etag; |
74 |
} |
83 |
} |
Lines 78-84
Link Here
|
78 |
} |
87 |
} |
79 |
|
88 |
|
80 |
/** |
89 |
/** |
81 |
* Save the Last-Modified and Etag headers if the result is cacheable. |
90 |
* Save the Last-Modified, Etag, and Expires headers if the result is cacheable. |
82 |
* |
91 |
* |
83 |
* @param conn connection |
92 |
* @param conn connection |
84 |
* @param res result |
93 |
* @param res result |
Lines 86-99
Link Here
|
86 |
public void saveDetails(URLConnection conn, SampleResult res){ |
95 |
public void saveDetails(URLConnection conn, SampleResult res){ |
87 |
if (isCacheable(res)){ |
96 |
if (isCacheable(res)){ |
88 |
String lastModified = conn.getHeaderField(HTTPConstantsInterface.LAST_MODIFIED); |
97 |
String lastModified = conn.getHeaderField(HTTPConstantsInterface.LAST_MODIFIED); |
|
|
98 |
String expires = conn.getHeaderField(HTTPConstantsInterface.EXPIRES); |
89 |
String etag = conn.getHeaderField(HTTPConstantsInterface.ETAG); |
99 |
String etag = conn.getHeaderField(HTTPConstantsInterface.ETAG); |
90 |
String url = conn.getURL().toString(); |
100 |
String url = conn.getURL().toString(); |
91 |
setCache(lastModified, etag, url); |
101 |
setCache(lastModified, expires, etag, url); |
92 |
} |
102 |
} |
93 |
} |
103 |
} |
94 |
|
104 |
|
95 |
/** |
105 |
/** |
96 |
* Save the Last-Modified and Etag headers if the result is cacheable. |
106 |
* Save the Last-Modified, Etag, and Expires headers if the result is cacheable. |
97 |
* |
107 |
* |
98 |
* @param method |
108 |
* @param method |
99 |
* @param res result |
109 |
* @param res result |
Lines 101-118
Link Here
|
101 |
public void saveDetails(HttpMethod method, SampleResult res) throws URIException{ |
111 |
public void saveDetails(HttpMethod method, SampleResult res) throws URIException{ |
102 |
if (isCacheable(res)){ |
112 |
if (isCacheable(res)){ |
103 |
String lastModified = getHeader(method ,HTTPConstantsInterface.LAST_MODIFIED); |
113 |
String lastModified = getHeader(method ,HTTPConstantsInterface.LAST_MODIFIED); |
|
|
114 |
String expires = getHeader(method ,HTTPConstantsInterface.EXPIRES); |
104 |
String etag = getHeader(method ,HTTPConstantsInterface.ETAG); |
115 |
String etag = getHeader(method ,HTTPConstantsInterface.ETAG); |
105 |
String url = method.getURI().toString(); |
116 |
String url = method.getURI().toString(); |
106 |
setCache(lastModified, etag, url); |
117 |
setCache(lastModified, expires, etag, url); |
107 |
} |
118 |
} |
108 |
} |
119 |
} |
109 |
|
120 |
|
110 |
// helper method to save the cache entry |
121 |
// helper method to save the cache entry |
111 |
private void setCache(String lastModified, String etag, String url) { |
122 |
private void setCache(String lastModified, String expires, String etag, String url) { |
112 |
if (log.isDebugEnabled()){ |
123 |
if (log.isDebugEnabled()){ |
113 |
log.debug("SET(both) "+url + " " + lastModified + " " + etag); |
124 |
log.debug("SET(both) "+url + " " + lastModified + " " + " " + expires + " " + etag); |
114 |
} |
125 |
} |
115 |
getCache().put(url, new CacheEntry(lastModified, etag)); |
126 |
getCache().put(url, new CacheEntry(lastModified, expires, etag)); |
116 |
} |
127 |
} |
117 |
|
128 |
|
118 |
// Helper method to deal with missing headers |
129 |
// Helper method to deal with missing headers |
Lines 179-184
Link Here
|
179 |
} |
190 |
} |
180 |
} |
191 |
} |
181 |
|
192 |
|
|
|
193 |
|
194 |
/** |
195 |
* Check the cache, if the entry has an expires header and the entry has not expired, return true<br/> |
196 |
* @param url URL to look up in cache |
197 |
*/ |
198 |
public boolean inCache(URL url) { |
199 |
CacheEntry entry = (CacheEntry) getCache().get(url.toString()); |
200 |
if (log.isDebugEnabled()){ |
201 |
log.debug("inCache "+url.toString()+" "+entry); |
202 |
} |
203 |
if (entry != null){ |
204 |
final String expires = entry.getExpires(); |
205 |
if (expires != null){ |
206 |
java.util.Date expiresDate = null; |
207 |
try { expiresDate = expiresDateFormat.parse(expires); } catch (Exception e) { } |
208 |
if (expiresDate != null) { |
209 |
if (expiresDate.after(new Date())) { |
210 |
if (log.isDebugEnabled()){ |
211 |
log.debug("Expires= " + expiresDate + " (Valid)"); |
212 |
} |
213 |
return true; |
214 |
} else if (log.isDebugEnabled()){ |
215 |
log.debug("Expires= " + expiresDate + " (Expired)"); |
216 |
} |
217 |
} |
218 |
} |
219 |
} |
220 |
return false; |
221 |
} |
222 |
|
182 |
private Map getCache(){ |
223 |
private Map getCache(){ |
183 |
return (Map) threadCache.get(); |
224 |
return (Map) threadCache.get(); |
184 |
} |
225 |
} |