Example usage for org.apache.http.client.cache HttpCacheEntry getStatusCode

List of usage examples for org.apache.http.client.cache HttpCacheEntry getStatusCode

Introduction

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

Prototype

public int getStatusCode() 

Source Link

Document

Returns the HTTP response code from the origin HttpResponse .

Usage

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

private CloseableHttpResponse handleCacheHit(final HttpRoute route, final HttpRequestWrapper request,
        final HttpClientContext context, final HttpExecutionAware execAware, final HttpCacheEntry entry)
        throws IOException, HttpException {
    final HttpHost target = context.getTargetHost();
    recordCacheHit(target, request);//from w  ww .j a va  2  s.  co  m
    CloseableHttpResponse out = null;
    final Date now = getCurrentDate();
    if (suitabilityChecker.canCachedResponseBeUsed(target, request, entry, now)) {
        log.debug("Cache hit");
        out = generateCachedResponse(request, context, entry, now);
    } else if (!mayCallBackend(request)) {
        log.debug("Cache entry not suitable but only-if-cached requested");
        out = generateGatewayTimeout(context);
    } else if (!(entry.getStatusCode() == HttpStatus.SC_NOT_MODIFIED
            && !suitabilityChecker.isConditional(request))) {
        log.debug("Revalidating cache entry");
        return revalidateCacheEntry(route, request, context, execAware, entry, now);
    } else {
        log.debug("Cache entry not usable; calling backend");
        return callBackend(route, request, context, execAware);
    }
    context.setAttribute(HttpClientContext.HTTP_ROUTE, route);
    context.setAttribute(HttpClientContext.HTTP_TARGET_HOST, target);
    context.setAttribute(HttpClientContext.HTTP_REQUEST, request);
    context.setAttribute(HttpClientContext.HTTP_RESPONSE, out);
    context.setAttribute(HttpClientContext.HTTP_REQ_SENT, Boolean.TRUE);
    return out;
}

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

private void handleCacheHit(final BasicFuture<HttpResponse> future, final HttpHost target,
        final HttpRequestWrapper request, final HttpCacheContext clientContext, final HttpCacheEntry entry)
        throws IOException {
    recordCacheHit(target, request);//  w w  w  .  j  av  a 2 s  . co m
    final HttpResponse out;
    final Date now = getCurrentDate();
    if (this.suitabilityChecker.canCachedResponseBeUsed(target, request, entry, now)) {
        log.debug("Cache hit");
        out = generateCachedResponse(request, clientContext, entry, now);
    } else if (!mayCallBackend(request)) {
        log.debug("Cache entry not suitable but only-if-cached requested");
        out = generateGatewayTimeout(clientContext);
    } else if (validityPolicy.isRevalidatable(entry) && !(entry.getStatusCode() == HttpStatus.SC_NOT_MODIFIED
            && !suitabilityChecker.isConditional(request))) {
        log.debug("Revalidating cache entry");
        revalidateCacheEntry(future, target, request, clientContext, entry, now);
        return;
    } else {
        log.debug("Cache entry not usable; calling backend");
        callBackend(future, target, request, clientContext);
        return;
    }
    clientContext.setAttribute(HttpClientContext.HTTP_ROUTE, new HttpRoute(target));
    clientContext.setAttribute(HttpCoreContext.HTTP_TARGET_HOST, target);
    clientContext.setAttribute(HttpCoreContext.HTTP_REQUEST, request);
    clientContext.setAttribute(HttpCoreContext.HTTP_RESPONSE, out);
    clientContext.setAttribute(HttpCoreContext.HTTP_REQ_SENT, Boolean.TRUE);
    future.completed(out);
}