public class FileCachingHttpClientBuilder extends HttpClientBuilder
HttpClient
builder that supports RFC 2616 caching.
Cached content is written to disk. Special care should be taken so that multiple clients do not share a single cache directory unintentionally. This could result senstive data being available in ways it should not be.
When using the single-arg constructor variant to wrap an existing instance of
CachingHttpClientBuilder
, there are several caveats of which to be aware:
HttpClientBuilder
.
buildClient()
is called:
CacheConfig
This is due to the unfortunate fact that the Apache builder does not currently provide accessor methods to obtain the default instances currently set on the builder. Therefore, if you need to set any default cache config parameters which are not exposed by this builder, then you must use the Apache builder directly and may not use this builder.
Modifier and Type | Class and Description |
---|---|
static class |
FileCachingHttpClientBuilder.StorageMaintenanceTask
Scheduled task to manage an instance of
ManagedHttpCacheStorage . |
static class |
FileCachingHttpClientBuilder.StorageManagingHttpClient
Class which wraps a caching instance of
CloseableHttpClient and its associated
ManagedHttpCacheStorage , and manages the scheduled maintenance and lifecycle of the latter. |
Modifier and Type | Field and Description |
---|---|
private File |
cacheDir
Directory in which cached content will be stored.
|
private long |
maintentanceTaskInterval
Interval at which the storage maintenance task whould run.
|
private org.apache.http.impl.client.cache.ManagedHttpCacheStorage |
managedStorage
The current managed storage instance.
|
private int |
maxCacheEntries
The maximum number of cached responses.
|
private long |
maxCacheEntrySize
The maximum response body size, in bytes, that will be eligible for caching.
|
Constructor and Description |
---|
FileCachingHttpClientBuilder()
Constructor.
|
FileCachingHttpClientBuilder(org.apache.http.impl.client.cache.CachingHttpClientBuilder builder)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
org.apache.http.client.HttpClient |
buildClient()
Constructs an
HttpClient using the settings of this builder. |
protected void |
decorateApacheBuilder()
Decorate the Apache builder as determined by this builder's parameters.
|
File |
getCacheDirectory()
Gets the directory in which cached content will be stored.
|
long |
getMaintentanceTaskInterval()
Get the interval at which the storage maintenance task should run.
|
int |
getMaxCacheEntries()
Gets the maximum number of cached responses.
|
long |
getMaxCacheEntrySize()
Gets the maximum response body size, in bytes, that will be eligible for caching.
|
void |
setCacheDirectory(File directory)
Sets the directory in which cached content will be stored.
|
void |
setCacheDirectory(String directoryPath)
Sets the directory in which cached content will be stored.
|
void |
setMaintentanceTaskInterval(long value)
Set the interval at which the storage maintenance task should run.
|
void |
setMaxCacheEntries(int maxEntries)
Sets the maximum number of cached responses.
|
void |
setMaxCacheEntrySize(long size)
Sets the maximum response body size, in bytes, that will be eligible for caching.
|
getApacheBuilder, getConnectionProxyHost, getConnectionProxyPassword, getConnectionProxyPort, getConnectionProxyUsername, getConnectionTimeout, getHttpContentCharSet, getHttpRequestRetryHandler, getSocketBufferSize, getSocketLocalAddress, getSocketTimeout, getTLSSocketFactory, getUserAgent, isConnectionCloseAfterResponse, isConnectionDisregardTLSCertificate, isConnectionStalecheck, isHttpFollowRedirects, resetDefaults, setConnectionCloseAfterResponse, setConnectionDisregardTLSCertificate, setConnectionProxyHost, setConnectionProxyPassword, setConnectionProxyPort, setConnectionProxyUsername, setConnectionStalecheck, setConnectionTimeout, setHttpContentCharSet, setHttpFollowRedirects, setHttpRequestRetryHandler, setSocketBufferSize, setSocketLocalAddress, setSocketLocalAddress, setSocketTimeout, setTLSSocketFactory, setUserAgent
private File cacheDir
System.getProperty("java.io.tmpdir") + File.separator + "wwwcache"
private int maxCacheEntries
private long maxCacheEntrySize
@Duration private long maintentanceTaskInterval
private org.apache.http.impl.client.cache.ManagedHttpCacheStorage managedStorage
public FileCachingHttpClientBuilder()
public FileCachingHttpClientBuilder(@Nonnull org.apache.http.impl.client.cache.CachingHttpClientBuilder builder)
builder
- builder of clients used to fetch data from remote serverspublic File getCacheDirectory()
public void setCacheDirectory(@Nonnull @NotEmpty String directoryPath)
directoryPath
- filesystem path to the directorypublic void setCacheDirectory(@Nonnull File directory)
directory
- the directorypublic int getMaxCacheEntries()
public void setMaxCacheEntries(int maxEntries)
maxEntries
- maximum number of cached responses, must be greater than zeropublic long getMaxCacheEntrySize()
public void setMaxCacheEntrySize(long size)
size
- maximum response body size that will be eligible for caching, must be greater than zeropublic long getMaintentanceTaskInterval()
public void setMaintentanceTaskInterval(long value)
value
- the new maintenance task interval, in millisecondsprotected void decorateApacheBuilder() throws Exception
decorateApacheBuilder
in class HttpClientBuilder
Exception
- if there is a problem decorating the Apache builderpublic org.apache.http.client.HttpClient buildClient() throws Exception
HttpClient
using the settings of this builder.buildClient
in class HttpClientBuilder
Exception
- if there is any problem building the new client instanceCopyright © 1999–2018. All rights reserved.