Example usage for org.apache.http.client.cache CacheResponseStatus equals

List of usage examples for org.apache.http.client.cache CacheResponseStatus equals

Introduction

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

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:it.tidalwave.bluemarine2.downloader.impl.DefaultDownloader.java

/*******************************************************************************************************************
 *
 *
 *
 ******************************************************************************************************************/
/* VisibleForTesting */ void onDownloadRequest(final @ListensTo @Nonnull DownloadRequest request)
        throws URISyntaxException {
    try {/*from  ww w . jav  a 2 s.c  o m*/
        log.info("onDownloadRequest({})", request);

        URL url = request.getUrl();

        for (;;) {
            final HttpCacheContext context = HttpCacheContext.create();
            @Cleanup
            final CloseableHttpResponse response = httpClient.execute(new HttpGet(url.toURI()), context);
            final byte[] bytes = bytesFrom(response);
            final CacheResponseStatus cacheResponseStatus = context.getCacheResponseStatus();
            log.debug(">>>> cacheResponseStatus: {}", cacheResponseStatus);

            final Origin origin = cacheResponseStatus.equals(CacheResponseStatus.CACHE_HIT) ? Origin.CACHE
                    : Origin.NETWORK;

            // FIXME: shouldn't do this by myself
            // FIXME: upon configuration, everything should be cached (needed for supporting integration tests)
            if (!origin.equals(Origin.CACHE)
                    && Arrays.asList(200, 303).contains(response.getStatusLine().getStatusCode())) {
                final Date date = new Date();
                final Resource resource = new HeapResource(bytes);
                cacheStorage.putEntry(url.toExternalForm(), new HttpCacheEntry(date, date,
                        response.getStatusLine(), response.getAllHeaders(), resource));
            }

            // FIXME: if the redirect were enabled, we could drop this check
            if (request.isOptionPresent(DownloadRequest.Option.FOLLOW_REDIRECT)
                    && response.getStatusLine().getStatusCode() == 303) // SEE_OTHER FIXME
            {
                url = new URL(response.getFirstHeader("Location").getValue());
                log.info(">>>> following 'see also' to {} ...", url);
            } else {
                messageBus.publish(new DownloadComplete(request.getUrl(),
                        response.getStatusLine().getStatusCode(), bytes, origin));
                return;
            }
        }
    } catch (IOException e) {
        log.error("{}: {}", request.getUrl(), e.toString());
        messageBus.publish(new DownloadComplete(request.getUrl(), -1, new byte[0], Origin.NETWORK));
    }
}

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

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

        /**//from   w  ww  .  ja  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;
        }
    };
}

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);
        }/*w  w w. j a  v a  2  s  . 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;
}