Example usage for org.apache.http.impl.client RequestWrapper setHeader

List of usage examples for org.apache.http.impl.client RequestWrapper setHeader

Introduction

In this page you can find the example usage for org.apache.http.impl.client RequestWrapper setHeader.

Prototype

public void setHeader(String str, String str2) 

Source Link

Usage

From source file:com.apigee.sdk.apm.http.impl.client.cache.ConditionalRequestBuilder.java

/**
 * When a {@link HttpCacheEntry} is stale but 'might' be used as a response
 * to an {@link HttpRequest} we will attempt to revalidate the entry with
 * the origin. Build the origin {@link HttpRequest} here and return it.
 * // ww w.  j  a v  a  2  s . c  o m
 * @param request
 *            the original request from the caller
 * @param cacheEntry
 *            the entry that needs to be revalidated
 * @return the wrapped request
 * @throws ProtocolException
 *             when I am unable to build a new origin request.
 */
public HttpRequest buildConditionalRequest(HttpRequest request, HttpCacheEntry cacheEntry)
        throws ProtocolException {
    RequestWrapper wrapperRequest = new RequestWrapper(request);
    wrapperRequest.resetHeaders();
    Header eTag = cacheEntry.getFirstHeader(HeaderConstants.ETAG);
    if (eTag != null) {
        wrapperRequest.setHeader(HeaderConstants.IF_NONE_MATCH, eTag.getValue());
    }
    Header lastModified = cacheEntry.getFirstHeader(HeaderConstants.LAST_MODIFIED);
    if (lastModified != null) {
        wrapperRequest.setHeader(HeaderConstants.IF_MODIFIED_SINCE, lastModified.getValue());
    }
    boolean mustRevalidate = false;
    for (Header h : cacheEntry.getHeaders(HeaderConstants.CACHE_CONTROL)) {
        for (HeaderElement elt : h.getElements()) {
            if (HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE.equalsIgnoreCase(elt.getName())
                    || HeaderConstants.CACHE_CONTROL_PROXY_REVALIDATE.equalsIgnoreCase(elt.getName())) {
                mustRevalidate = true;
                break;
            }
        }
    }
    if (mustRevalidate) {
        wrapperRequest.addHeader("Cache-Control", "max-age=0");
    }
    return wrapperRequest;

}

From source file:com.apigee.sdk.apm.http.impl.client.cache.ConditionalRequestBuilder.java

/**
 * When a {@link HttpCacheEntry} does not exist for a specific
 * {@link HttpRequest} we attempt to see if an existing
 * {@link HttpCacheEntry} is appropriate by building a conditional
 * {@link HttpRequest} using the variants' ETag values. If no such values
 * exist, the request is unmodified//from  ww  w.  j a  v a 2  s.c  o m
 * 
 * @param request
 *            the original request from the caller
 * @param cacheEntry
 *            the entry that needs to be revalidated
 * @return the wrapped request
 * @throws ProtocolException
 *             when I am unable to build a new origin request.
 */
public HttpRequest buildConditionalRequestFromVariants(HttpRequest request, Set<HttpCacheEntry> variantEntries)
        throws ProtocolException {
    RequestWrapper wrapperRequest = new RequestWrapper(request);
    wrapperRequest.resetHeaders();

    // we do not support partial content so all etags are used
    StringBuilder etags = new StringBuilder();
    boolean first = true;
    for (HttpCacheEntry entry : variantEntries) {
        Header etagHeader = entry.getFirstHeader(HeaderConstants.ETAG);
        if (etagHeader != null) {
            if (first) {
                etags.append(etagHeader.getValue());
                first = false;
            } else {
                etags.append(",").append(etagHeader.getValue());
            }
        }
    }
    // if first is still true than no variants had a cache entry, return
    // unmodified wrapped request
    if (first) {
        return wrapperRequest;
    }

    wrapperRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString());

    return wrapperRequest;
}

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

/**
 * When a {@link HttpCacheEntry} does not exist for a specific {@link HttpRequest}
 * we attempt to see if an existing {@link HttpCacheEntry} is appropriate by building
 * a conditional {@link HttpRequest} using the variants' ETag values.  If no such values
 * exist, the request is unmodified/*from w  w  w .  ja v a  2  s.c o m*/
 *
 * @param request the original request from the caller
 * @param variants
 * @return the wrapped request
 */
public HttpRequest buildConditionalRequestFromVariants(HttpRequest request, Map<String, Variant> variants) {
    RequestWrapper wrapperRequest;
    try {
        wrapperRequest = new RequestWrapper(request);
    } catch (ProtocolException pe) {
        log.warn("unable to build conditional request", pe);
        return request;
    }
    wrapperRequest.resetHeaders();

    // we do not support partial content so all etags are used
    StringBuilder etags = new StringBuilder();
    boolean first = true;
    for (String etag : variants.keySet()) {
        if (!first) {
            etags.append(",");
        }
        first = false;
        etags.append(etag);
    }

    wrapperRequest.setHeader(HeaderConstants.IF_NONE_MATCH, etags.toString());
    return wrapperRequest;
}