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; }
private CloseableHttpResponse generateCachedResponse(final HttpRequestWrapper request, final HttpContext context, final HttpCacheEntry entry, final Date now) { final CloseableHttpResponse cachedResponse; if (request.containsHeader(HeaderConstants.IF_NONE_MATCH) || request.containsHeader(HeaderConstants.IF_MODIFIED_SINCE)) { cachedResponse = responseGenerator.generateNotModifiedResponse(entry); } else { cachedResponse = responseGenerator.generateResponse(entry); } setResponseStatus(context, CacheResponseStatus.CACHE_HIT); if (validityPolicy.getStalenessSecs(entry, now) > 0L) { cachedResponse.addHeader(HeaderConstants.WARNING,"110 localhost \"Response is stale\""); } return cachedResponse; }
private void addMissingContentLengthHeader(HttpResponse response, HttpEntity entity) { if (transferEncodingIsPresent(response)) return; Header contentLength = response.getFirstHeader(HTTP.CONTENT_LEN); if (contentLength == null) { contentLength = new BasicHeader(HTTP.CONTENT_LEN, Long.toString(entity .getContentLength())); response.setHeader(contentLength); } }
private HttpResponse unvalidatedCacheHit(final HttpContext context, final HttpCacheEntry entry) { final HttpResponse cachedResponse = responseGenerator.generateResponse(entry); setResponseStatus(context, CacheResponseStatus.CACHE_HIT); cachedResponse.addHeader(HeaderConstants.WARNING, "111 localhost \"Revalidation failed\""); return cachedResponse; }
/** * If I was able to use a {@link CacheEntity} to response to the {@link org.apache.http.HttpRequest} then * generate an {@link HttpResponse} based on the cache entry. * @param entry * {@link CacheEntity} to transform into an {@link HttpResponse} * @return {@link HttpResponse} that was constructed */ HttpResponse generateResponse(HttpCacheEntry entry) { Date now = new Date(); HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, entry .getStatusCode(), entry.getReasonPhrase()); HttpEntity entity = new CacheEntity(entry); response.setHeaders(entry.getAllHeaders()); addMissingContentLengthHeader(response, entity); response.setEntity(entity); long age = this.validityStrategy.getCurrentAgeSecs(entry, now); if (age > 0) { if (age >= Integer.MAX_VALUE) { response.setHeader(HeaderConstants.AGE, "2147483648"); } else { response.setHeader(HeaderConstants.AGE, "" + ((int) age)); } } return response; }
private HttpResponse unvalidatedCacheHit(HttpContext context, HttpCacheEntry entry) { final HttpResponse cachedResponse = responseGenerator.generateResponse(entry); setResponseStatus(context, CacheResponseStatus.CACHE_HIT); cachedResponse.addHeader(HeaderConstants.WARNING, "111 localhost \"Revalidation failed\""); return cachedResponse; }
/** * If I was able to use a {@link CacheEntity} to response to the {@link org.apache.http.HttpRequest} then * generate an {@link HttpResponse} based on the cache entry. * @param entry * {@link CacheEntity} to transform into an {@link HttpResponse} * @return {@link HttpResponse} that was constructed */ CloseableHttpResponse generateResponse(final HttpCacheEntry entry) { final Date now = new Date(); final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, entry .getStatusCode(), entry.getReasonPhrase()); response.setHeaders(entry.getAllHeaders()); if (entry.getResource() != null) { final HttpEntity entity = new CacheEntity(entry); addMissingContentLengthHeader(response, entity); response.setEntity(entity); } final long age = this.validityStrategy.getCurrentAgeSecs(entry, now); if (age > 0) { if (age >= Integer.MAX_VALUE) { response.setHeader(HeaderConstants.AGE, "2147483648"); } else { response.setHeader(HeaderConstants.AGE, "" + ((int) age)); } } return Proxies.enhanceResponse(response); }
private HttpResponse generateCachedResponse(final HttpRequestWrapper request, final HttpContext context, final HttpCacheEntry entry, final Date now) { final HttpResponse cachedResponse; if (request.containsHeader(HeaderConstants.IF_NONE_MATCH) || request.containsHeader(HeaderConstants.IF_MODIFIED_SINCE)) { cachedResponse = responseGenerator.generateNotModifiedResponse(entry); } else { cachedResponse = responseGenerator.generateResponse(entry); } setResponseStatus(context, CacheResponseStatus.CACHE_HIT); if (validityPolicy.getStalenessSecs(entry, now) > 0L) { cachedResponse.addHeader(HeaderConstants.WARNING,"110 localhost \"Response is stale\""); } return cachedResponse; }
private CloseableHttpResponse unvalidatedCacheHit( final HttpContext context, final HttpCacheEntry entry) { final CloseableHttpResponse cachedResponse = responseGenerator.generateResponse(entry); setResponseStatus(context, CacheResponseStatus.CACHE_HIT); cachedResponse.addHeader(HeaderConstants.WARNING, "111 localhost \"Revalidation failed\""); return cachedResponse; }
public BasicHttpCache(ResourceFactory resourceFactory, HttpCacheStorage storage, CacheConfig config) { this.resourceFactory = resourceFactory; this.uriExtractor = new CacheKeyGenerator(); this.cacheEntryUpdater = new CacheEntryUpdater(resourceFactory); this.maxObjectSizeBytes = config.getMaxObjectSize(); this.responseGenerator = new CachedHttpResponseGenerator(); this.storage = storage; this.cacheInvalidator = new CacheInvalidator(this.uriExtractor, this.storage); }
private void addMissingContentLengthHeader(final HttpResponse response, final HttpEntity entity) { if (transferEncodingIsPresent(response)) { return; } Header contentLength = response.getFirstHeader(HTTP.CONTENT_LEN); if (contentLength == null) { contentLength = new BasicHeader(HTTP.CONTENT_LEN, Long.toString(entity .getContentLength())); response.setHeader(contentLength); } }
private HttpResponse generateCachedResponse(HttpRequest request, HttpContext context, HttpCacheEntry entry, Date now) { final HttpResponse cachedResponse; if (request.containsHeader(HeaderConstants.IF_NONE_MATCH) || request.containsHeader(HeaderConstants.IF_MODIFIED_SINCE)) { cachedResponse = responseGenerator.generateNotModifiedResponse(entry); } else { cachedResponse = responseGenerator.generateResponse(entry); } setResponseStatus(context, CacheResponseStatus.CACHE_HIT); if (validityPolicy.getStalenessSecs(entry, now) > 0L) { cachedResponse.addHeader(HeaderConstants.WARNING,"110 localhost \"Response is stale\""); } return cachedResponse; }
public HttpResponse cacheAndReturnResponse(HttpHost host, HttpRequest request, HttpResponse originResponse, Date requestSent, Date responseReceived) throws IOException { SizeLimitedResponseReader responseReader = getResponseReader(request, originResponse); responseReader.readResponse(); if (responseReader.isLimitReached()) { return responseReader.getReconstructedResponse(); } Resource resource = responseReader.getResource(); if (isIncompleteResponse(originResponse, resource)) { return generateIncompleteResponseError(originResponse, resource); } HttpCacheEntry entry = new HttpCacheEntry( requestSent, responseReceived, originResponse.getStatusLine(), originResponse.getAllHeaders(), resource); storeInCache(host, request, entry); return responseGenerator.generateResponse(entry); }
CachingHttpClient( HttpClient client, HttpCache cache, CacheConfig config) { super(); if (client == null) { throw new IllegalArgumentException("HttpClient may not be null"); } if (cache == null) { throw new IllegalArgumentException("HttpCache may not be null"); } if (config == null) { throw new IllegalArgumentException("CacheConfig may not be null"); } this.maxObjectSizeBytes = config.getMaxObjectSize(); this.sharedCache = config.isSharedCache(); this.backend = client; this.responseCache = cache; this.validityPolicy = new CacheValidityPolicy(); this.responseCachingPolicy = new ResponseCachingPolicy(maxObjectSizeBytes, sharedCache); this.responseGenerator = new CachedHttpResponseGenerator(this.validityPolicy); this.cacheableRequestPolicy = new CacheableRequestPolicy(); this.suitabilityChecker = new CachedResponseSuitabilityChecker(this.validityPolicy, config); this.conditionalRequestBuilder = new ConditionalRequestBuilder(); this.responseCompliance = new ResponseProtocolCompliance(); this.requestCompliance = new RequestProtocolCompliance(); this.asynchRevalidator = makeAsynchronousValidator(config); }
matchingVariant, matchedEntry); HttpResponse resp = responseGenerator.generateResponse(responseEntry); tryToUpdateVariantMap(target, request, matchingVariant); return responseGenerator.generateNotModifiedResponse(responseEntry);
resource); storeInCache(host, request, entry); return responseGenerator.generateResponse(entry); } finally { if (closeOriginResponse) {
public CachingExec( final ClientExecChain backend, final HttpCache cache, final CacheConfig config, final AsynchronousValidator asynchRevalidator) { super(); Args.notNull(backend, "HTTP backend"); Args.notNull(cache, "HttpCache"); this.cacheConfig = config != null ? config : CacheConfig.DEFAULT; this.backend = backend; this.responseCache = cache; this.validityPolicy = new CacheValidityPolicy(); this.responseGenerator = new CachedHttpResponseGenerator(this.validityPolicy); this.cacheableRequestPolicy = new CacheableRequestPolicy(); this.suitabilityChecker = new CachedResponseSuitabilityChecker(this.validityPolicy, this.cacheConfig); this.conditionalRequestBuilder = new ConditionalRequestBuilder(); this.responseCompliance = new ResponseProtocolCompliance(); this.requestCompliance = new RequestProtocolCompliance(this.cacheConfig.isWeakETagOnPutDeleteAllowed()); this.responseCachingPolicy = new ResponseCachingPolicy( this.cacheConfig.getMaxObjectSize(), this.cacheConfig.isSharedCache(), this.cacheConfig.isNeverCacheHTTP10ResponsesWithQuery(), this.cacheConfig.is303CachingEnabled()); this.asynchRevalidator = asynchRevalidator; }
if (suitabilityChecker.isConditional(request) && suitabilityChecker.allConditionalsMatch(request, updatedEntry, new Date())) { return responseGenerator.generateNotModifiedResponse(updatedEntry); return responseGenerator.generateResponse(updatedEntry); && !staleResponseNotAllowed(request, cacheEntry, getCurrentDate()) && validityPolicy.mayReturnStaleIfError(request, cacheEntry, responseDate)) { final HttpResponse cachedResponse = responseGenerator.generateResponse(cacheEntry); cachedResponse.addHeader(HeaderConstants.WARNING, "110 localhost \"Response is stale\""); HttpEntity errorBody = backendResponse.getEntity();
CachingHttpClient( final HttpClient client, final HttpCache cache, final CacheConfig config) { super(); Args.notNull(client, "HttpClient"); Args.notNull(cache, "HttpCache"); Args.notNull(config, "CacheConfig"); this.maxObjectSizeBytes = config.getMaxObjectSize(); this.sharedCache = config.isSharedCache(); this.backend = client; this.responseCache = cache; this.validityPolicy = new CacheValidityPolicy(); this.responseCachingPolicy = new ResponseCachingPolicy(maxObjectSizeBytes, sharedCache, config.isNeverCacheHTTP10ResponsesWithQuery(), config.is303CachingEnabled()); this.responseGenerator = new CachedHttpResponseGenerator(this.validityPolicy); this.cacheableRequestPolicy = new CacheableRequestPolicy(); this.suitabilityChecker = new CachedResponseSuitabilityChecker(this.validityPolicy, config); this.conditionalRequestBuilder = new ConditionalRequestBuilder(); this.responseCompliance = new ResponseProtocolCompliance(); this.requestCompliance = new RequestProtocolCompliance(config.isWeakETagOnPutDeleteAllowed()); this.asynchRevalidator = makeAsynchronousValidator(config); }
matchingVariant, matchedEntry); final HttpResponse resp = responseGenerator.generateResponse(responseEntry); tryToUpdateVariantMap(target, request, matchingVariant); return responseGenerator.generateNotModifiedResponse(responseEntry);