/** * Constructs a {@code CachingHttpClient} with the given caching options * that stores cache entries in the provided storage backend and uses * the given {@link HttpClient} for backend requests. * @param client used to make origin requests * @param storage where to store cache entries * @param config cache module options */ public CachingHttpClient( final HttpClient client, final HttpCacheStorage storage, final CacheConfig config) { this(client, new BasicHttpCache(new HeapResourceFactory(), storage, config), config); }
public CachingExec( final ClientExecChain backend, final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config) { this(backend, new BasicHttpCache(resourceFactory, storage, config), config); }
public BasicHttpCache( final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config, final CacheKeyGenerator uriExtractor, final HttpCacheInvalidator cacheInvalidator) { this.resourceFactory = resourceFactory; this.uriExtractor = uriExtractor; this.cacheEntryUpdater = new CacheEntryUpdater(resourceFactory); this.maxObjectSizeBytes = config.getMaxObjectSize(); this.responseGenerator = new CachedHttpResponseGenerator(); this.storage = storage; this.cacheInvalidator = cacheInvalidator; }
public BasicHttpCache( final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config, final CacheKeyGenerator uriExtractor) { this( resourceFactory, storage, config, uriExtractor, new CacheInvalidator(uriExtractor, storage)); }
@SuppressWarnings("unchecked") @Before public void setUp() { mockRequestPolicy = createNiceMock(CacheableRequestPolicy.class); mockValidityPolicy = createNiceMock(CacheValidityPolicy.class); mockBackend = createNiceMock(ClientExecChain.class); mockCache = createNiceMock(HttpCache.class); mockSuitabilityChecker = createNiceMock(CachedResponseSuitabilityChecker.class); mockResponsePolicy = createNiceMock(ResponseCachingPolicy.class); mockHandler = createNiceMock(ResponseHandler.class); mockUriRequest = createNiceMock(HttpUriRequest.class); mockCacheEntry = createNiceMock(HttpCacheEntry.class); mockResponseGenerator = createNiceMock(CachedHttpResponseGenerator.class); mockCachedResponse = createNiceMock(CloseableHttpResponse.class); mockConditionalRequestBuilder = createNiceMock(ConditionalRequestBuilder.class); mockConditionalRequest = createNiceMock(HttpRequest.class); mockStatusLine = createNiceMock(StatusLine.class); mockResponseProtocolCompliance = createNiceMock(ResponseProtocolCompliance.class); mockRequestProtocolCompliance = createNiceMock(RequestProtocolCompliance.class); mockStorage = createNiceMock(HttpCacheStorage.class); config = CacheConfig.DEFAULT; asyncValidator = new AsynchronousValidator(config); host = new HttpHost("foo.example.com", 80); route = new HttpRoute(host); request = HttpRequestWrapper.wrap(new BasicHttpRequest("GET", "/stuff", HttpVersion.HTTP_1_1)); context = HttpCacheContext.create(); context.setTargetHost(host); entry = HttpTestUtils.makeCacheEntry(); impl = createCachingExecChain(mockBackend, mockCache, mockValidityPolicy, mockResponsePolicy, mockResponseGenerator, mockRequestPolicy, mockSuitabilityChecker, mockConditionalRequestBuilder, mockResponseProtocolCompliance, mockRequestProtocolCompliance, config, asyncValidator); }
@Override @Before public void setUp() { super.setUp(); config = CacheConfig.custom().setMaxObjectSize(MAX_BYTES).build(); if (CACHE_MANAGER.cacheExists(TEST_EHCACHE_NAME)){ CACHE_MANAGER.removeCache(TEST_EHCACHE_NAME); } CACHE_MANAGER.addCache(TEST_EHCACHE_NAME); final HttpCacheStorage storage = new EhcacheHttpCacheStorage(CACHE_MANAGER.getCache(TEST_EHCACHE_NAME)); mockBackend = EasyMock.createNiceMock(ClientExecChain.class); impl = new CachingExec(mockBackend, new HeapResourceFactory(), storage, config); }
@Before public void setUp() { now = new Date(); tenSecondsAgo = new Date(now.getTime() - 10 * 1000L); host = new HttpHost("foo.example.com"); mockStorage = mock(HttpCacheStorage.class); cacheKeyGenerator = new CacheKeyGenerator(); mockEntry = mock(HttpCacheEntry.class); request = HttpTestUtils.makeDefaultRequest(); response = HttpTestUtils.make200Response(); impl = new CacheInvalidator(cacheKeyGenerator, mockStorage); }
private HttpCacheStorage createStorage() { CacheConfig config = new CacheConfig(); // if max cache entries value is not present the CacheConfig's default (CacheConfig.DEFAULT_MAX_CACHE_ENTRIES = 1000) will be used Integer maxCacheEntries = Integer.getInteger("bitbucket.client.cache.maxentries"); if (maxCacheEntries != null) { config.setMaxCacheEntries(maxCacheEntries); } return new BasicHttpCacheStorage(config); }
public BasicHttpCache( final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config) { this( resourceFactory, storage, config, new CacheKeyGenerator()); }
public final CachingHttpClientBuilder setHttpCacheStorage( final HttpCacheStorage storage) { this.storage = storage; return this; }
@Override protected ClientExecChain decorateMainExec(final ClientExecChain mainExec) { final CacheConfig config = this.cacheConfig != null ? this.cacheConfig : CacheConfig.DEFAULT; // We copy the instance fields to avoid changing them, and rename to avoid accidental use of the wrong version ResourceFactory resourceFactoryCopy = this.resourceFactory; if (resourceFactoryCopy == null) { if (this.cacheDir == null) { resourceFactoryCopy = new HeapResourceFactory(); } else { resourceFactoryCopy = new FileResourceFactory(cacheDir); } } HttpCacheStorage storageCopy = this.storage; if (storageCopy == null) { if (this.cacheDir == null) { storageCopy = new BasicHttpCacheStorage(config); } else { final ManagedHttpCacheStorage managedStorage = new ManagedHttpCacheStorage(config); if (this.deleteCache) { addCloseable(new Closeable() { @Override public void close() throws IOException { managedStorage.shutdown(); } }); } else { addCloseable(managedStorage); } storageCopy = managedStorage; } } final AsynchronousValidator revalidator = createAsynchronousRevalidator(config); final CacheKeyGenerator uriExtractor = new CacheKeyGenerator(); HttpCacheInvalidator cacheInvalidator = this.httpCacheInvalidator; if (cacheInvalidator == null) { cacheInvalidator = new CacheInvalidator(uriExtractor, storageCopy); } return new CachingExec(mainExec, new BasicHttpCache( resourceFactoryCopy, storageCopy, config, uriExtractor, cacheInvalidator), config, revalidator); }
@Test public void testIssue1147() throws Exception { final CacheConfig cacheConfig = CacheConfig.custom() .setSharedCache(true) .setMaxObjectSize(262144) //256kb .build(); final ResourceFactory resourceFactory = new FileResourceFactory(cacheDir); final HttpCacheStorage httpCacheStorage = new ManagedHttpCacheStorage(cacheConfig); final ClientExecChain backend = mock(ClientExecChain.class); final HttpRequestWrapper get = HttpRequestWrapper.wrap(new HttpGet("http://somehost/")); final HttpClientContext context = HttpClientContext.create(); final HttpHost target = new HttpHost("somehost", 80); final HttpRoute route = new HttpRoute(target); context.setTargetHost(target); final Date now = new Date(); final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L); final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); response.setEntity(HttpTestUtils.makeBody(128)); response.setHeader("Content-Length", "128"); response.setHeader("ETag", "\"etag\""); response.setHeader("Cache-Control", "public, max-age=3600"); response.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo)); when(backend.execute( eq(route), isA(HttpRequestWrapper.class), isA(HttpClientContext.class), (HttpExecutionAware) Matchers.isNull())).thenReturn(Proxies.enhanceResponse(response)); final BasicHttpCache cache = new BasicHttpCache(resourceFactory, httpCacheStorage, cacheConfig); final ClientExecChain t = createCachingExecChain(backend, cache, cacheConfig); final HttpResponse response1 = t.execute(route, get, context, null); Assert.assertEquals(200, response1.getStatusLine().getStatusCode()); IOUtils.consume(response1.getEntity()); verify(backend).execute( eq(route), isA(HttpRequestWrapper.class), isA(HttpClientContext.class), (HttpExecutionAware) Matchers.isNull()); removeCache(); reset(backend); when(backend.execute( eq(route), isA(HttpRequestWrapper.class), isA(HttpClientContext.class), (HttpExecutionAware) Matchers.isNull())).thenReturn(Proxies.enhanceResponse(response)); final HttpResponse response2 = t.execute(route, get, context, null); Assert.assertEquals(200, response2.getStatusLine().getStatusCode()); IOUtils.consume(response2.getEntity()); verify(backend).execute( eq(route), isA(HttpRequestWrapper.class), isA(HttpClientContext.class), (HttpExecutionAware) Matchers.isNull()); }
public EtagCachingHttpClient(HttpCacheStorage storage) { this(new DefaultHttpClient(), storage); }
public EtagCachingHttpClient(HttpClient httpClient, HttpCacheStorage storage) { this.backingHttpClient = httpClient; this.storage = storage; }
/** * Constructs a {@code CachingHttpClient} with the given caching options * that stores cache entries in the provided storage backend and uses * the given {@link HttpClient} for backend requests. However, cached * response bodies are managed using the given {@link ResourceFactory}. * @param client used to make origin requests * @param resourceFactory how to manage cached response bodies * @param storage where to store cache entries * @param config cache module options */ public CachingHttpClient( final HttpClient client, final ResourceFactory resourceFactory, final HttpCacheStorage storage, final CacheConfig config) { this(client, new BasicHttpCache(resourceFactory, storage, config), config); }
/** * Create a new {@link CacheInvalidator} for a given {@link HttpCache} and * {@link CacheKeyGenerator}. * * @param uriExtractor Provides identifiers for the keys to store cache entries * @param storage the cache to store items away in */ public CacheInvalidator( final CacheKeyGenerator uriExtractor, final HttpCacheStorage storage) { this.cacheKeyGenerator = uriExtractor; this.storage = storage; }