Skip to content

Commit 9e162ea

Browse files
committed
Support delegates in JarCacheStorage
In 4.2.5, it was simple to construct chained CachedHttpClient instances. In the new versions it does not appear simple or possible in general Hence, we are chaining the HttpCacheStorage objects together to replicate the behaviour we had previously.
1 parent f2b74ae commit 9e162ea

File tree

3 files changed

+69
-27
lines changed

3 files changed

+69
-27
lines changed

core/src/main/java/com/github/jsonldjava/core/DocumentLoader.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.http.client.protocol.RequestAcceptEncoding;
1313
import org.apache.http.client.protocol.ResponseContentEncoding;
1414
import org.apache.http.impl.client.CloseableHttpClient;
15+
import org.apache.http.impl.client.cache.BasicHttpCacheStorage;
1516
import org.apache.http.impl.client.cache.CacheConfig;
1617
import org.apache.http.impl.client.cache.CachingHttpClientBuilder;
1718

@@ -144,20 +145,27 @@ protected static CloseableHttpClient getDefaultHttpClient() {
144145
}
145146

146147
protected static CloseableHttpClient createDefaultHttpClient() {
147-
return CachingHttpClientBuilder
148+
// Common CacheConfig for both the JarCacheStorage and the underlying
149+
// BasicHttpCacheStorage
150+
final CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(1000)
151+
.setMaxObjectSize(1024 * 128).build();
152+
153+
CloseableHttpClient result = CachingHttpClientBuilder
148154
.create()
149155
// allow caching
150-
.setCacheConfig(
151-
CacheConfig.custom().setMaxCacheEntries(1000).setMaxObjectSize(1024 * 128)
152-
.build())
153-
// TODO: enable wrapping with JAR cache:
154-
.setHttpCacheStorage(new JarCacheStorage())
156+
.setCacheConfig(cacheConfig)
157+
// Wrap the local JarCacheStorage around a BasicHttpCacheStorage
158+
.setHttpCacheStorage(
159+
new JarCacheStorage(null, cacheConfig, new BasicHttpCacheStorage(
160+
cacheConfig)))
155161
// Support compressed data
156162
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/httpagent.html#d5e1238
157163
.addInterceptorFirst(new RequestAcceptEncoding())
158164
.addInterceptorFirst(new ResponseContentEncoding())
159165
// use system defaults for proxy etc.
160166
.useSystemProperties().build();
167+
168+
return result;
161169
}
162170

163171
public CloseableHttpClient getHttpClient() {

core/src/main/java/com/github/jsonldjava/utils/JarCacheStorage.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.http.client.cache.HttpCacheUpdateCallback;
2323
import org.apache.http.client.cache.HttpCacheUpdateException;
2424
import org.apache.http.client.cache.Resource;
25+
import org.apache.http.impl.client.cache.BasicHttpCacheStorage;
2526
import org.apache.http.impl.client.cache.CacheConfig;
2627
import org.apache.http.impl.cookie.DateUtils;
2728
import org.apache.http.message.BasicHeader;
@@ -40,9 +41,16 @@ public class JarCacheStorage implements HttpCacheStorage {
4041

4142
private final Logger log = LoggerFactory.getLogger(getClass());
4243

43-
private final CacheConfig cacheConfig = new CacheConfig();
44+
private final CacheConfig cacheConfig;
45+
// private final CacheConfig cacheConfig = new CacheConfig();
4446
private ClassLoader classLoader;
4547

48+
/**
49+
* All live caching that is not found locally is delegated to this
50+
* implementation.
51+
*/
52+
private HttpCacheStorage delegate;
53+
4654
public ClassLoader getClassLoader() {
4755
if (classLoader != null) {
4856
return classLoader;
@@ -54,22 +62,44 @@ public void setClassLoader(ClassLoader classLoader) {
5462
this.classLoader = classLoader;
5563
}
5664

65+
/**
66+
* @deprecated Use
67+
* {@link JarCacheStorage#JarCacheStorage(ClassLoader, CacheConfig)}
68+
* instead.
69+
*/
70+
@Deprecated
5771
public JarCacheStorage() {
58-
this(null);
72+
this(null, CacheConfig.DEFAULT);
5973
}
6074

75+
/**
76+
*
77+
* @param classLoader
78+
* The ClassLoader to use to locate JAR files and resources, or
79+
* null to use the Thread context class loader in each case.
80+
* @deprecated Use
81+
* {@link JarCacheStorage#JarCacheStorage(ClassLoader, CacheConfig)}
82+
* instead.
83+
*/
84+
@Deprecated
6185
public JarCacheStorage(ClassLoader classLoader) {
86+
this(classLoader, CacheConfig.DEFAULT);
87+
}
88+
89+
public JarCacheStorage(ClassLoader classLoader, CacheConfig cacheConfig) {
90+
this(classLoader, cacheConfig, new BasicHttpCacheStorage(cacheConfig));
91+
}
92+
93+
public JarCacheStorage(ClassLoader classLoader, CacheConfig cacheConfig,
94+
HttpCacheStorage delegate) {
6295
setClassLoader(classLoader);
63-
cacheConfig.setMaxObjectSize(0);
64-
cacheConfig.setMaxCacheEntries(0);
65-
cacheConfig.setMaxUpdateRetries(0);
66-
cacheConfig.getMaxCacheEntries();
96+
this.cacheConfig = cacheConfig;
97+
this.delegate = delegate;
6798
}
6899

69100
@Override
70101
public void putEntry(String key, HttpCacheEntry entry) throws IOException {
71-
// ignored
72-
102+
delegate.putEntry(key, entry);
73103
}
74104

75105
ObjectMapper mapper = new ObjectMapper();
@@ -107,7 +137,9 @@ public HttpCacheEntry getEntry(String key) throws IOException {
107137
}
108138
}
109139
}
110-
return null;
140+
// If we didn't find it in our cache, then attempt to find it in the
141+
// chained delegate
142+
return delegate.getEntry(key);
111143
}
112144

113145
private Enumeration<URL> getResources() throws IOException {
@@ -176,7 +208,7 @@ protected HttpCacheEntry cacheEntry(URI requestedUri, URL baseURL, JsonNode cach
176208
final List<Header> responseHeaders = new ArrayList<Header>();
177209
if (!cacheNode.has(HTTP.DATE_HEADER)) {
178210
responseHeaders
179-
.add(new BasicHeader(HTTP.DATE_HEADER, DateUtils.formatDate(new Date())));
211+
.add(new BasicHeader(HTTP.DATE_HEADER, DateUtils.formatDate(new Date())));
180212
}
181213
if (!cacheNode.has(HeaderConstants.CACHE_CONTROL)) {
182214
responseHeaders.add(new BasicHeader(HeaderConstants.CACHE_CONTROL,
@@ -197,13 +229,13 @@ protected HttpCacheEntry cacheEntry(URI requestedUri, URL baseURL, JsonNode cach
197229

198230
@Override
199231
public void removeEntry(String key) throws IOException {
200-
// Ignored
232+
delegate.removeEntry(key);
201233
}
202234

203235
@Override
204236
public void updateEntry(String key, HttpCacheUpdateCallback callback) throws IOException,
205-
HttpCacheUpdateException {
206-
// ignored
237+
HttpCacheUpdateException {
238+
delegate.updateEntry(key, callback);
207239
}
208240

209241
public CacheConfig getCacheConfig() {

core/src/test/java/com/github/jsonldjava/core/DocumentLoaderTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotEquals;
56
import static org.junit.Assert.assertNotNull;
67
import static org.junit.Assert.assertNotSame;
78
import static org.junit.Assert.assertSame;
@@ -110,17 +111,18 @@ public void fromURLCache() throws Exception {
110111

111112
// Now try to get it again and ensure it is
112113
// cached
113-
final HttpClient client = documentLoader.getHttpClient();
114-
final HttpUriRequest get = new HttpGet(url.toURI());
115-
get.setHeader("Accept", DocumentLoader.ACCEPT_HEADER);
116-
final HttpCacheContext localContext = HttpCacheContext.create();
117-
final HttpResponse respo = client.execute(get, localContext);
118-
EntityUtils.consume(respo.getEntity());
114+
final HttpClient clientCached = documentLoader.getHttpClient();
115+
final HttpUriRequest getCached = new HttpGet(url.toURI());
116+
getCached.setHeader("Accept", DocumentLoader.ACCEPT_HEADER);
117+
final HttpCacheContext localContextCached = HttpCacheContext.create();
118+
final HttpResponse respoCached = clientCached.execute(getCached, localContextCached);
119+
EntityUtils.consume(respoCached.getEntity());
119120

120121
// Check cache status
121122
// http://hc.apache.org/httpcomponents-client-ga/tutorial/html/caching.html
122-
final CacheResponseStatus responseStatus = localContext.getCacheResponseStatus();
123-
assertFalse(CacheResponseStatus.CACHE_MISS.equals(responseStatus));
123+
final CacheResponseStatus responseStatusCached = localContextCached
124+
.getCacheResponseStatus();
125+
assertNotEquals(CacheResponseStatus.CACHE_MISS, responseStatusCached);
124126
}
125127

126128
@Test

0 commit comments

Comments
 (0)