org.apache.http.client.cache.HttpCacheContext Java Examples

The following examples show how to use org.apache.http.client.cache.HttpCacheContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeySetRetriever.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
private void logCacheResponseStatus(HttpCacheContext httpContext) {
	this.oidcRequests++;

	switch (httpContext.getCacheResponseStatus()) {
	case CACHE_HIT:
		this.oidcCacheHits++;
		break;
	case CACHE_MODULE_RESPONSE:
		this.oidcCacheModuleResponses++;
		break;
	case CACHE_MISS:
		this.oidcCacheMisses++;
		break;
	case VALIDATED:
		this.oidcCacheHitsValidated++;
		break;
	}

	long now = System.currentTimeMillis();

	if (this.oidcRequests >= 2 && now - lastCacheStatusLog > CACHE_STATUS_LOG_INTERVAL_MS) {
		log.info("Cache status for KeySetRetriever:\noidcCacheHits: " + oidcCacheHits + "\noidcCacheHitsValidated: "
				+ oidcCacheHitsValidated + "\noidcCacheModuleResponses: " + oidcCacheModuleResponses
				+ "\noidcCacheMisses: " + oidcCacheMisses);

		lastCacheStatusLog = now;
	}

}
 
Example #2
Source File: CacheAdapter.java    From esigate with Apache License 2.0 4 votes vote down vote up
public ClientExecChain wrapCachingHttpClient(final ClientExecChain wrapped) {
    return new ClientExecChain() {

        /**
         * Removes client http cache directives like "Cache-control" and "Pragma". Users must not be able to bypass
         * the cache just by making a refresh in the browser. Generates X-cache header.
         * 
         */
        @Override
        public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
                HttpClientContext httpClientContext, HttpExecutionAware execAware) throws IOException,
                HttpException {
            OutgoingRequestContext context = OutgoingRequestContext.adapt(httpClientContext);

            // Switch route for the cache to generate the right cache key
            CloseableHttpResponse response = wrapped.execute(route, request, context, execAware);

            // Remove previously added Cache-control header
            if (request.getRequestLine().getMethod().equalsIgnoreCase("GET")
                    && (staleWhileRevalidate > 0 || staleIfError > 0)) {
                response.removeHeader(response.getLastHeader("Cache-control"));
            }
            // Add X-cache header
            if (xCacheHeader) {
                if (context != null) {
                    CacheResponseStatus cacheResponseStatus =
                            (CacheResponseStatus) context.getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS);
                    String xCacheString;
                    if (cacheResponseStatus.equals(CacheResponseStatus.CACHE_HIT)) {
                        xCacheString = "HIT";
                    } else if (cacheResponseStatus.equals(CacheResponseStatus.VALIDATED)) {
                        xCacheString = "VALIDATED";
                    } else {
                        xCacheString = "MISS";
                    }
                    xCacheString += " from " + route.getTargetHost().toHostString();
                    xCacheString +=
                            " (" + request.getRequestLine().getMethod() + " " + request.getRequestLine().getUri()
                                    + ")";
                    response.addHeader("X-Cache", xCacheString);
                }
            }

            // Remove Via header
            if (!viaHeader && response.containsHeader("Via")) {
                response.removeHeaders("Via");
            }
            return response;
        }
    };
}
 
Example #3
Source File: TracingCachingHttpClientBuilder.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override boolean isRemote(HttpContext context, Span span) {
  boolean cacheHit = CacheResponseStatus.CACHE_HIT.equals(
    context.getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS));
  if (cacheHit) span.tag("http.cache_hit", "");
  return !cacheHit;
}