List of usage examples for org.apache.http.client.methods CloseableHttpResponse removeHeaders
void removeHeaders(String str);
From source file:org.esigate.cache.CacheAdapter.java
public ClientExecChain wrapBackendHttpClient(final ClientExecChain wrapped) { return new ClientExecChain() { private boolean isCacheableStatus(int statusCode) { return (statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_NOT_FOUND || statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR || statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE || statusCode == HttpStatus.SC_NOT_MODIFIED || statusCode == HttpStatus.SC_GATEWAY_TIMEOUT); }/*from w w w . ja v a 2 s. com*/ /** * Fire pre-fetch and post-fetch events Enables cache for all GET requests if cache ttl was forced to a * certain duration in the configuration. This is done even for non 200 return codes! This is a very * aggressive but efficient caching policy. Adds "stale-while-revalidate" and "stale-if-error" cache-control * directives depending on the configuration. * * @throws HttpException * @throws IOException */ @Override public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext httpClientContext, HttpExecutionAware execAware) throws IOException, HttpException { OutgoingRequestContext context = OutgoingRequestContext.adapt(httpClientContext); CloseableHttpResponse response = wrapped.execute(route, request, context, execAware); String method = request.getRequestLine().getMethod(); int statusCode = response.getStatusLine().getStatusCode(); // If ttl is set, force caching even for error pages if (ttl > 0 && method.equalsIgnoreCase("GET") && isCacheableStatus(statusCode)) { response.removeHeaders("Date"); response.removeHeaders("Cache-control"); response.removeHeaders("Expires"); response.setHeader("Date", DateUtils.formatDate(new Date(System.currentTimeMillis()))); response.setHeader("Cache-control", "public, max-age=" + ttl); response.setHeader("Expires", DateUtils.formatDate(new Date(System.currentTimeMillis() + ((long) ttl) * 1000))); } if (request.getRequestLine().getMethod().equalsIgnoreCase("GET")) { String cacheControlHeader = ""; if (staleWhileRevalidate > 0) { cacheControlHeader += "stale-while-revalidate=" + staleWhileRevalidate; } if (staleIfError > 0) { if (cacheControlHeader.length() > 0) { cacheControlHeader += ","; } cacheControlHeader += "stale-if-error=" + staleIfError; } if (cacheControlHeader.length() > 0) { response.addHeader("Cache-control", cacheControlHeader); } } return response; } }; }
From source file:org.esigate.cache.CacheAdapter.java
public ClientExecChain wrapCachingHttpClient(final ClientExecChain wrapped) { return new ClientExecChain() { /**/*from ww w . j a v a 2 s. c om*/ * 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; } }; }