Example usage for org.apache.http.client.cache HttpCacheContext CACHE_RESPONSE_STATUS

List of usage examples for org.apache.http.client.cache HttpCacheContext CACHE_RESPONSE_STATUS

Introduction

In this page you can find the example usage for org.apache.http.client.cache HttpCacheContext CACHE_RESPONSE_STATUS.

Prototype

String CACHE_RESPONSE_STATUS

To view the source code for org.apache.http.client.cache HttpCacheContext CACHE_RESPONSE_STATUS.

Click Source Link

Document

This is the name under which the CacheResponseStatus of a request (for example, whether it resulted in a cache hit) will be recorded if an HttpContext is provided during execution.

Usage

From source file:org.esigate.extension.monitoring.Metric.java

@Override
public boolean event(EventDefinition id, Event event) {

    String timerName = MetricRegistry.name(this.getClass().getSimpleName(),
            driver.getConfiguration().getInstanceName(), id.getId());

    if (EventManager.EVENT_PROXY_POST.equals(id)) {
        if (((ProxyEvent) event).getErrorPage() != null) {
            String statusCode = String.valueOf(
                    ((ProxyEvent) event).getErrorPage().getHttpResponse().getStatusLine().getStatusCode());
            timerName = MetricRegistry.name(timerName, "error", statusCode);
        }//from   w  w w  . j a v a 2s  .c  om
    } else if (EventManager.EVENT_FETCH_POST.equals(id)) {
        // Retrieve HTTP response status code and cache status
        FetchEvent e = (FetchEvent) event;
        int statusCode = e.getHttpResponse().getStatusLine().getStatusCode();
        CacheResponseStatus cacheResponseStatus = (CacheResponseStatus) e.getHttpContext()
                .getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS);

        // Adding status code when error
        if (statusCode >= HttpStatus.SC_BAD_REQUEST) {
            timerName = MetricRegistry.name(timerName, "error", String.valueOf(statusCode));
        }
        // Adding cache if not MISS
        if (cacheResponseStatus != null && !cacheResponseStatus.equals(CacheResponseStatus.CACHE_MISS)) {
            timerName = MetricRegistry.name(timerName, cacheResponseStatus.name().toLowerCase());
        }
    }

    metric.meter(timerName).mark();

    return true;
}

From source file:org.esigate.cache.CacheAdapter.java

public ClientExecChain wrapCachingHttpClient(final ClientExecChain wrapped) {
    return new ClientExecChain() {

        /**/*w ww .j  av 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;
        }
    };
}

From source file:org.apache.http.impl.client.cache.CachingExec.java

private void setResponseStatus(final HttpContext context, final CacheResponseStatus value) {
    if (context != null) {
        context.setAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS, value);
    }/*  ww  w.j  a  v  a  2s .  c o m*/
}

From source file:org.apache.http.impl.client.cache.CachingHttpAsyncClient.java

private void setResponseStatus(final HttpCacheContext clientContext, final CacheResponseStatus value) {
    if (clientContext != null) {
        clientContext.setAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS, value);
    }/*w w w.j  a v a  2  s .  c o  m*/
}

From source file:org.esigate.extension.FragmentLogging.java

@Override
public boolean event(EventDefinition id, Event event) {

    FragmentEvent e = (FragmentEvent) event;

    if (EventManager.EVENT_FRAGMENT_PRE.equals(id)) {
        // Keep track of the start time.
        e.getHttpContext().setAttribute(TIME, System.currentTimeMillis(), true);
    } else {/*from www .jav  a 2s  .  c o  m*/
        int statusCode = e.getHttpResponse().getStatusLine().getStatusCode();

        // Log only if info or issue
        if (LOG.isInfoEnabled() || statusCode >= HttpStatus.SC_BAD_REQUEST) {

            // Log last result only
            HttpRequest httpRequest = e.getHttpContext().getSentRequest();

            // Create log message
            HttpHost targetHost = e.getHttpContext().getTargetHost();

            String requestLine = httpRequest.getRequestLine().toString();
            String statusLine = e.getHttpResponse().getStatusLine().toString();

            String reqHeaders = ArrayUtils.toString(httpRequest.getAllHeaders());
            String respHeaders = ArrayUtils.toString(e.getHttpResponse().getAllHeaders());

            String cache = "";
            CacheResponseStatus cacheResponseStatus = (CacheResponseStatus) e.getHttpContext()
                    .getAttribute(HttpCacheContext.CACHE_RESPONSE_STATUS);
            if (cacheResponseStatus != null) {
                cache = cacheResponseStatus.toString();
            }

            long time = System.currentTimeMillis() - (Long) e.getHttpContext().removeAttribute(TIME, true);

            StringBuilder logMessage = new StringBuilder(Parameters.SMALL_BUFFER_SIZE);
            logMessage.append(driver.getConfiguration().getInstanceName());
            logMessage.append(" ");
            // Display target host, protocol and port
            if (targetHost != null) {
                logMessage.append(targetHost.getSchemeName());
                logMessage.append("://");
                logMessage.append(targetHost.getHostName());

                if (targetHost.getPort() != -1) {
                    logMessage.append(":");
                    logMessage.append(targetHost.getPort());
                }

                logMessage.append(" - ");
            }

            logMessage.append(requestLine).append(" ").append(reqHeaders).append(" -> ").append(statusLine)
                    .append(" (").append(time).append(" ms) ").append(cache).append(" ").append(respHeaders);

            // Log level according to status code.
            if (statusCode >= HttpStatus.SC_BAD_REQUEST) {
                LOG.warn(logMessage.toString());
            } else {
                LOG.info(logMessage.toString());
            }
        }
    }

    // Continue processing
    return true;
}