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

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

Introduction

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

Prototype

public void addHeader(String str, String str2) 

Source Link

Usage

From source file:com.clarionmedia.infinitum.web.rest.impl.SharedSecretAuthentication.java

@Override
public void authenticate(RequestWrapper request) {
    if (isHeader()) {
        request.addHeader(getTokenName(), getToken());
    } else {/*from  ww  w. j a v  a2s .c  om*/
        String uri = request.getURI().toString();
        if (uri.contains("?"))
            uri += "&" + getTokenName() + "=" + getToken();
        request.setURI(URI.create(uri));
    }
}

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

/**
 * Returns a request to unconditionally validate a cache entry with the
 * origin. In certain cases (due to multiple intervening caches) our cache
 * may actually receive a response to a normal conditional validation where
 * the Date header is actually older than that of our current cache entry.
 * In this case, the protocol recommendation is to retry the validation and
 * force syncup with the origin./*from  w  w  w.j a  va 2  s.c  o m*/
 * 
 * @param request
 *            client request we are trying to satisfy
 * @param entry
 *            existing cache entry we are trying to validate
 * @return an unconditional validation request
 * @throws ProtocolException
 */
public HttpRequest buildUnconditionalRequest(HttpRequest request, HttpCacheEntry entry)
        throws ProtocolException {
    RequestWrapper wrapped = new RequestWrapper(request);
    wrapped.resetHeaders();
    wrapped.addHeader("Cache-Control", "no-cache");
    wrapped.addHeader("Pragma", "no-cache");
    wrapped.removeHeaders("If-Range");
    wrapped.removeHeaders("If-Match");
    wrapped.removeHeaders("If-None-Match");
    wrapped.removeHeaders("If-Unmodified-Since");
    wrapped.removeHeaders("If-Modified-Since");
    return wrapped;
}

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.
 * /*from www  .j  a  v  a2  s.c om*/
 * @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:org.apache.http.impl.client.cache.ConditionalRequestBuilder.java

/**
 * Returns a request to unconditionally validate a cache entry with
 * the origin. In certain cases (due to multiple intervening caches)
 * our cache may actually receive a response to a normal conditional
 * validation where the Date header is actually older than that of
 * our current cache entry. In this case, the protocol recommendation
 * is to retry the validation and force syncup with the origin.
 * @param request client request we are trying to satisfy
 * @param entry existing cache entry we are trying to validate
 * @return an unconditional validation request
 *///from w w  w .ja v a  2s  . c om
public HttpRequest buildUnconditionalRequest(HttpRequest request, HttpCacheEntry entry) {
    RequestWrapper wrapped;
    try {
        wrapped = new RequestWrapper(request);
    } catch (ProtocolException e) {
        log.warn("unable to build proper unconditional request", e);
        return request;
    }
    wrapped.resetHeaders();
    wrapped.addHeader("Cache-Control", "no-cache");
    wrapped.addHeader("Pragma", "no-cache");
    wrapped.removeHeaders("If-Range");
    wrapped.removeHeaders("If-Match");
    wrapped.removeHeaders("If-None-Match");
    wrapped.removeHeaders("If-Unmodified-Since");
    wrapped.removeHeaders("If-Modified-Since");
    return wrapped;
}