httpclient-cache-4.3.5

16:36:39.070 INFO  jd.cli.Main - Decompiling httpclient-cache-4.3.5.jar
package org.apache.http.impl.client.cache;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import org.apache.http.annotation.Immutable;
import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.cache.HttpCacheEntrySerializationException;
import org.apache.http.client.cache.HttpCacheEntrySerializer;

@Immutable
public class DefaultHttpCacheEntrySerializer
  implements HttpCacheEntrySerializer
{
  public void writeTo(HttpCacheEntry cacheEntry, OutputStream os)
    throws IOException
  {
    ObjectOutputStream oos = new ObjectOutputStream(os);
    try
    {
      oos.writeObject(cacheEntry);
    }
    finally
    {
      oos.close();
    }
  }
  
  public HttpCacheEntry readFrom(InputStream is)
    throws IOException
  {
    ObjectInputStream ois = new ObjectInputStream(is);
    try
    {
      return (HttpCacheEntry)ois.readObject();
    }
    catch (ClassNotFoundException ex)
    {
      throw new HttpCacheEntrySerializationException("Class not found: " + ex.getMessage(), ex);
    }
    finally
    {
      ois.close();
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.client.cache.DefaultHttpCacheEntrySerializer
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.client.cache;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpMessage;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.annotation.Immutable;
import org.apache.http.client.utils.DateUtils;

@Immutable
class ResponseCachingPolicy
{
  private static final String[] AUTH_CACHEABLE_PARAMS = { "s-maxage", "must-revalidate", "public" };
  private final long maxObjectSizeBytes;
  private final boolean sharedCache;
  private final boolean neverCache1_0ResponsesWithQueryString;
  private final Log log = LogFactory.getLog(getClass());
  private static final Set<Integer> cacheableStatuses = new HashSet(Arrays.asList(new Integer[] { Integer.valueOf(200), Integer.valueOf(203), Integer.valueOf(300), Integer.valueOf(301), Integer.valueOf(410) }));
  private final Set<Integer> uncacheableStatuses;
  
  public ResponseCachingPolicy(long maxObjectSizeBytes, boolean sharedCache, boolean neverCache1_0ResponsesWithQueryString, boolean allow303Caching)
  {
    this.maxObjectSizeBytes = maxObjectSizeBytes;
    this.sharedCache = sharedCache;
    this.neverCache1_0ResponsesWithQueryString = neverCache1_0ResponsesWithQueryString;
    if (allow303Caching) {
      uncacheableStatuses = new HashSet(Arrays.asList(new Integer[] { Integer.valueOf(206) }));
    } else {
      uncacheableStatuses = new HashSet(Arrays.asList(new Integer[] { Integer.valueOf(206), Integer.valueOf(303) }));
    }
  }
  
  public boolean isResponseCacheable(String httpMethod, HttpResponse response)
  {
    boolean cacheable = false;
    if (!"GET".equals(httpMethod))
    {
      log.debug("Response was not cacheable.");
      return false;
    }
    int status = response.getStatusLine().getStatusCode();
    if (cacheableStatuses.contains(Integer.valueOf(status)))
    {
      cacheable = true;
    }
    else
    {
      if (uncacheableStatuses.contains(Integer.valueOf(status))) {
        return false;
      }
      if (unknownStatusCode(status)) {
        return false;
      }
    }
    Header contentLength = response.getFirstHeader("Content-Length");
    if (contentLength != null)
    {
      int contentLengthValue = Integer.parseInt(contentLength.getValue());
      if (contentLengthValue > maxObjectSizeBytes) {
        return false;
      }
    }
    Header[] ageHeaders = response.getHeaders("Age");
    if (ageHeaders.length > 1) {
      return false;
    }
    Header[] expiresHeaders = response.getHeaders("Expires");
    if (expiresHeaders.length > 1) {
      return false;
    }
    Header[] dateHeaders = response.getHeaders("Date");
    if (dateHeaders.length != 1) {
      return false;
    }
    Date date = DateUtils.parseDate(dateHeaders[0].getValue());
    if (date == null) {
      return false;
    }
    for (Header varyHdr : response.getHeaders("Vary")) {
      for (HeaderElement elem : varyHdr.getElements()) {
        if ("*".equals(elem.getName())) {
          return false;
        }
      }
    }
    if (isExplicitlyNonCacheable(response)) {
      return false;
    }
    return (cacheable) || (isExplicitlyCacheable(response));
  }
  
  private boolean unknownStatusCode(int status)
  {
    if ((status >= 100) && (status <= 101)) {
      return false;
    }
    if ((status >= 200) && (status <= 206)) {
      return false;
    }
    if ((status >= 300) && (status <= 307)) {
      return false;
    }
    if ((status >= 400) && (status <= 417)) {
      return false;
    }
    if ((status >= 500) && (status <= 505)) {
      return false;
    }
    return true;
  }
  
  protected boolean isExplicitlyNonCacheable(HttpResponse response)
  {
    Header[] cacheControlHeaders = response.getHeaders("Cache-Control");
    for (Header header : cacheControlHeaders) {
      for (HeaderElement elem : header.getElements()) {
        if (("no-store".equals(elem.getName())) || ("no-cache".equals(elem.getName())) || ((sharedCache) && ("private".equals(elem.getName())))) {
          return true;
        }
      }
    }
    return false;
  }
  
  protected boolean hasCacheControlParameterFrom(HttpMessage msg, String[] params)
  {
    Header[] cacheControlHeaders = msg.getHeaders("Cache-Control");
    for (Header header : cacheControlHeaders) {
      for (HeaderElement elem : header.getElements()) {
        for (String param : params) {
          if (param.equalsIgnoreCase(elem.getName())) {
            return true;
          }
        }
      }
    }
    return false;
  }
  
  protected boolean isExplicitlyCacheable(HttpResponse response)
  {
    if (response.getFirstHeader("Expires") != null) {
      return true;
    }
    String[] cacheableParams = { "max-age", "s-maxage", "must-revalidate", "proxy-revalidate", "public" };
    
    return hasCacheControlParameterFrom(response, cacheableParams);
  }
  
  public boolean isResponseCacheable(HttpRequest request, HttpResponse response)
  {
    if (requestProtocolGreaterThanAccepted(request))
    {
      log.debug("Response was not cacheable.");
      return false;
    }
    String[] uncacheableRequestDirectives = { "no-store" };
    if (hasCacheControlParameterFrom(request, uncacheableRequestDirectives)) {
      return false;
    }
    if (request.getRequestLine().getUri().contains("?"))
    {
      if ((neverCache1_0ResponsesWithQueryString) && (from1_0Origin(response)))
      {
        log.debug("Response was not cacheable as it had a query string.");
        return false;
      }
      if (!isExplicitlyCacheable(response))
      {
        log.debug("Response was not cacheable as it is missing explicit caching headers.");
        return false;
      }
    }
    if (expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl(response)) {
      return false;
    }
    if (sharedCache)
    {
      Header[] authNHeaders = request.getHeaders("Authorization");
      if ((authNHeaders != null) && (authNHeaders.length > 0) && (!hasCacheControlParameterFrom(response, AUTH_CACHEABLE_PARAMS))) {
        return false;
      }
    }
    String method = request.getRequestLine().getMethod();
    return isResponseCacheable(method, response);
  }
  
  private boolean expiresHeaderLessOrEqualToDateHeaderAndNoCacheControl(HttpResponse response)
  {
    if (response.getFirstHeader("Cache-Control") != null) {
      return false;
    }
    Header expiresHdr = response.getFirstHeader("Expires");
    Header dateHdr = response.getFirstHeader("Date");
    if ((expiresHdr == null) || (dateHdr == null)) {
      return false;
    }
    Date expires = DateUtils.parseDate(expiresHdr.getValue());
    Date date = DateUtils.parseDate(dateHdr.getValue());
    if ((expires == null) || (date == null)) {
      return false;
    }
    return (expires.equals(date)) || (expires.before(date));
  }
  
  private boolean from1_0Origin(HttpResponse response)
  {
    Header via = response.getFirstHeader("Via");
    if (via != null)
    {
      HeaderElement[] arr$ = via.getElements();int len$ = arr$.length;int i$ = 0;
      if (i$ < len$)
      {
        HeaderElement elt = arr$[i$];
        String proto = elt.toString().split("\\s")[0];
        if (proto.contains("/")) {
          return proto.equals("HTTP/1.0");
        }
        return proto.equals("1.0");
      }
    }
    return HttpVersion.HTTP_1_0.equals(response.getProtocolVersion());
  }
  
  private boolean requestProtocolGreaterThanAccepted(HttpRequest req)
  {
    return req.getProtocolVersion().compareToVersion(HttpVersion.HTTP_1_1) > 0;
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.client.cache.ResponseCachingPolicy
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.client.cache;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.annotation.Immutable;
import org.apache.http.client.cache.InputLimit;
import org.apache.http.client.cache.Resource;
import org.apache.http.client.cache.ResourceFactory;

@Immutable
public class HeapResourceFactory
  implements ResourceFactory
{
  public Resource generate(String requestId, InputStream instream, InputLimit limit)
    throws IOException
  {
    ByteArrayOutputStream outstream = new ByteArrayOutputStream();
    byte[] buf = new byte['?'];
    long total = 0L;
    int l;
    while ((l = instream.read(buf)) != -1)
    {
      outstream.write(buf, 0, l);
      total += l;
      if ((limit != null) && (total > limit.getValue())) {
        limit.reached();
      }
    }
    return createResource(outstream.toByteArray());
  }
  
  public Resource copy(String requestId, Resource resource)
    throws IOException
  {
    byte[] body;
    byte[] body;
    if ((resource instanceof HeapResource))
    {
      body = ((HeapResource)resource).getByteArray();
    }
    else
    {
      ByteArrayOutputStream outstream = new ByteArrayOutputStream();
      IOUtils.copyAndClose(resource.getInputStream(), outstream);
      body = outstream.toByteArray();
    }
    return createResource(body);
  }
  
  Resource createResource(byte[] buf)
  {
    return new HeapResource(buf);
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.client.cache.HeapResourceFactory
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.client.cache;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.RequestLine;
import org.apache.http.StatusLine;
import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.cache.HttpCacheInvalidator;
import org.apache.http.client.cache.HttpCacheStorage;
import org.apache.http.client.cache.HttpCacheUpdateCallback;
import org.apache.http.client.cache.HttpCacheUpdateException;
import org.apache.http.client.cache.Resource;
import org.apache.http.client.cache.ResourceFactory;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.message.BasicHttpResponse;

class BasicHttpCache
  implements HttpCache
{
  private static final Set<String> safeRequestMethods = new HashSet(Arrays.asList(new String[] { "HEAD", "GET", "OPTIONS", "TRACE" }));
  private final CacheKeyGenerator uriExtractor;
  private final ResourceFactory resourceFactory;
  private final long maxObjectSizeBytes;
  private final CacheEntryUpdater cacheEntryUpdater;
  private final CachedHttpResponseGenerator responseGenerator;
  private final HttpCacheInvalidator cacheInvalidator;
  private final HttpCacheStorage storage;
  private final Log log = LogFactory.getLog(getClass());
  
  public BasicHttpCache(ResourceFactory resourceFactory, HttpCacheStorage storage, CacheConfig config, CacheKeyGenerator uriExtractor, HttpCacheInvalidator cacheInvalidator)
  {
    this.resourceFactory = resourceFactory;
    this.uriExtractor = uriExtractor;
    cacheEntryUpdater = new CacheEntryUpdater(resourceFactory);
    maxObjectSizeBytes = config.getMaxObjectSize();
    responseGenerator = new CachedHttpResponseGenerator();
    this.storage = storage;
    this.cacheInvalidator = cacheInvalidator;
  }
  
  public BasicHttpCache(ResourceFactory resourceFactory, HttpCacheStorage storage, CacheConfig config, CacheKeyGenerator uriExtractor)
  {
    this(resourceFactory, storage, config, uriExtractor, new CacheInvalidator(uriExtractor, storage));
  }
  
  public BasicHttpCache(ResourceFactory resourceFactory, HttpCacheStorage storage, CacheConfig config)
  {
    this(resourceFactory, storage, config, new CacheKeyGenerator());
  }
  
  public BasicHttpCache(CacheConfig config)
  {
    this(new HeapResourceFactory(), new BasicHttpCacheStorage(config), config);
  }
  
  public BasicHttpCache()
  {
    this(CacheConfig.DEFAULT);
  }
  
  public void flushCacheEntriesFor(HttpHost host, HttpRequest request)
    throws IOException
  {
    if (!safeRequestMethods.contains(request.getRequestLine().getMethod()))
    {
      String uri = uriExtractor.getURI(host, request);
      storage.removeEntry(uri);
    }
  }
  
  public void flushInvalidatedCacheEntriesFor(HttpHost host, HttpRequest request, HttpResponse response)
  {
    if (!safeRequestMethods.contains(request.getRequestLine().getMethod())) {
      cacheInvalidator.flushInvalidatedCacheEntries(host, request, response);
    }
  }
  
  void storeInCache(HttpHost target, HttpRequest request, HttpCacheEntry entry)
    throws IOException
  {
    if (entry.hasVariants()) {
      storeVariantEntry(target, request, entry);
    } else {
      storeNonVariantEntry(target, request, entry);
    }
  }
  
  void storeNonVariantEntry(HttpHost target, HttpRequest req, HttpCacheEntry entry)
    throws IOException
  {
    String uri = uriExtractor.getURI(target, req);
    storage.putEntry(uri, entry);
  }
  
  void storeVariantEntry(HttpHost target, final HttpRequest req, final HttpCacheEntry entry)
    throws IOException
  {
    String parentURI = uriExtractor.getURI(target, req);
    final String variantURI = uriExtractor.getVariantURI(target, req, entry);
    storage.putEntry(variantURI, entry);
    
    HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback()
    {
      public HttpCacheEntry update(HttpCacheEntry existing)
        throws IOException
      {
        return doGetUpdatedParentEntry(req.getRequestLine().getUri(), existing, entry, uriExtractor.getVariantKey(req, entry), variantURI);
      }
    };
    try
    {
      storage.updateEntry(parentURI, callback);
    }
    catch (HttpCacheUpdateException e)
    {
      log.warn("Could not update key [" + parentURI + "]", e);
    }
  }
  
  public void reuseVariantEntryFor(HttpHost target, final HttpRequest req, Variant variant)
    throws IOException
  {
    String parentCacheKey = uriExtractor.getURI(target, req);
    final HttpCacheEntry entry = variant.getEntry();
    final String variantKey = uriExtractor.getVariantKey(req, entry);
    final String variantCacheKey = variant.getCacheKey();
    
    HttpCacheUpdateCallback callback = new HttpCacheUpdateCallback()
    {
      public HttpCacheEntry update(HttpCacheEntry existing)
        throws IOException
      {
        return doGetUpdatedParentEntry(req.getRequestLine().getUri(), existing, entry, variantKey, variantCacheKey);
      }
    };
    try
    {
      storage.updateEntry(parentCacheKey, callback);
    }
    catch (HttpCacheUpdateException e)
    {
      log.warn("Could not update key [" + parentCacheKey + "]", e);
    }
  }
  
  boolean isIncompleteResponse(HttpResponse resp, Resource resource)
  {
    int status = resp.getStatusLine().getStatusCode();
    if ((status != 200) && (status != 206)) {
      return false;
    }
    Header hdr = resp.getFirstHeader("Content-Length");
    if (hdr == null) {
      return false;
    }
    int contentLength;
    try
    {
      contentLength = Integer.parseInt(hdr.getValue());
    }
    catch (NumberFormatException nfe)
    {
      return false;
    }
    return resource.length() < contentLength;
  }
  
  CloseableHttpResponse generateIncompleteResponseError(HttpResponse response, Resource resource)
  {
    int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
    HttpResponse error = new BasicHttpResponse(HttpVersion.HTTP_1_1, 502, "Bad Gateway");
    
    error.setHeader("Content-Type", "text/plain;charset=UTF-8");
    String msg = String.format("Received incomplete response with Content-Length %d but actual body length %d", new Object[] { Integer.valueOf(contentLength), Long.valueOf(resource.length()) });
    
    byte[] msgBytes = msg.getBytes();
    error.setHeader("Content-Length", Integer.toString(msgBytes.length));
    error.setEntity(new ByteArrayEntity(msgBytes));
    return Proxies.enhanceResponse(error);
  }
  
  HttpCacheEntry doGetUpdatedParentEntry(String requestId, HttpCacheEntry existing, HttpCacheEntry entry, String variantKey, String variantCacheKey)
    throws IOException
  {
    HttpCacheEntry src = existing;
    if (src == null) {
      src = entry;
    }
    Resource resource = null;
    if (src.getResource() != null) {
      resource = resourceFactory.copy(requestId, src.getResource());
    }
    Map<String, String> variantMap = new HashMap(src.getVariantMap());
    variantMap.put(variantKey, variantCacheKey);
    return new HttpCacheEntry(src.getRequestDate(), src.getResponseDate(), src.getStatusLine(), src.getAllHeaders(), resource, variantMap);
  }
  
  public HttpCacheEntry updateCacheEntry(HttpHost target, HttpRequest request, HttpCacheEntry stale, HttpResponse originResponse, Date requestSent, Date responseReceived)
    throws IOException
  {
    HttpCacheEntry updatedEntry = cacheEntryUpdater.updateCacheEntry(request.getRequestLine().getUri(), stale, requestSent, responseReceived, originResponse);
    
    storeInCache(target, request, updatedEntry);
    return updatedEntry;
  }
  
  public HttpCacheEntry updateVariantCacheEntry(HttpHost target, HttpRequest request, HttpCacheEntry stale, HttpResponse originResponse, Date requestSent, Date responseReceived, String cacheKey)
    throws IOException
  {
    HttpCacheEntry updatedEntry = cacheEntryUpdater.updateCacheEntry(request.getRequestLine().getUri(), stale, requestSent, responseReceived, originResponse);
    
    storage.putEntry(cacheKey, updatedEntry);
    return updatedEntry;
  }
  
  public HttpResponse cacheAndReturnResponse(HttpHost host, HttpRequest request, HttpResponse originResponse, Date requestSent, Date responseReceived)
    throws IOException
  {
    return cacheAndReturnResponse(host, request, Proxies.enhanceResponse(originResponse), requestSent, responseReceived);
  }
  
  public CloseableHttpResponse cacheAndReturnResponse(HttpHost host, HttpRequest request, CloseableHttpResponse originResponse, Date requestSent, Date responseReceived)
    throws IOException
  {
    boolean closeOriginResponse = true;
    SizeLimitedResponseReader responseReader = getResponseReader(request, originResponse);
    try
    {
      responseReader.readResponse();
      if (responseReader.isLimitReached())
      {
        closeOriginResponse = false;
        return responseReader.getReconstructedResponse();
      }
      Resource resource = responseReader.getResource();
      if (isIncompleteResponse(originResponse, resource)) {
        return generateIncompleteResponseError(originResponse, resource);
      }
      HttpCacheEntry entry = new HttpCacheEntry(requestSent, responseReceived, originResponse.getStatusLine(), originResponse.getAllHeaders(), resource);
      
      storeInCache(host, request, entry);
      return responseGenerator.generateResponse(entry);
    }
    finally
    {
      if (closeOriginResponse) {
        originResponse.close();
      }
    }
  }
  
  SizeLimitedResponseReader getResponseReader(HttpRequest request, CloseableHttpResponse backEndResponse)
  {
    return new SizeLimitedResponseReader(resourceFactory, maxObjectSizeBytes, request, backEndResponse);
  }
  
  public HttpCacheEntry getCacheEntry(HttpHost host, HttpRequest request)
    throws IOException
  {
    HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request));
    if (root == null) {
      return null;
    }
    if (!root.hasVariants()) {
      return root;
    }
    String variantCacheKey = (String)root.getVariantMap().get(uriExtractor.getVariantKey(request, root));
    if (variantCacheKey == null) {
      return null;
    }
    return storage.getEntry(variantCacheKey);
  }
  
  public void flushInvalidatedCacheEntriesFor(HttpHost host, HttpRequest request)
    throws IOException
  {
    cacheInvalidator.flushInvalidatedCacheEntries(host, request);
  }
  
  public Map<String, Variant> getVariantCacheEntriesWithEtags(HttpHost host, HttpRequest request)
    throws IOException
  {
    Map<String, Variant> variants = new HashMap();
    HttpCacheEntry root = storage.getEntry(uriExtractor.getURI(host, request));
    if ((root == null) || (!root.hasVariants())) {
      return variants;
    }
    for (Map.Entry<String, String> variant : root.getVariantMap().entrySet())
    {
      String variantKey = (String)variant.getKey();
      String variantCacheKey = (String)variant.getValue();
      addVariantWithEtag(variantKey, variantCacheKey, variants);
    }
    return variants;
  }
  
  private void addVariantWithEtag(String variantKey, String variantCacheKey, Map<String, Variant> variants)
    throws IOException
  {
    HttpCacheEntry entry = storage.getEntry(variantCacheKey);
    if (entry == null) {
      return;
    }
    Header etagHeader = entry.getFirstHeader("ETag");
    if (etagHeader == null) {
      return;
    }
    variants.put(etagHeader.getValue(), new Variant(variantKey, variantCacheKey, entry));
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.client.cache.BasicHttpCache
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.client.cache;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.RequestLine;
import org.apache.http.annotation.Immutable;
import org.apache.http.client.cache.HttpCacheEntry;
import org.apache.http.client.utils.URIUtils;

@Immutable
class CacheKeyGenerator
{
  private static final URI BASE_URI = URI.create("http://example.com/");
  
  public String getURI(HttpHost host, HttpRequest req)
  {
    if (isRelativeRequest(req)) {
      return canonicalizeUri(String.format("%s%s", new Object[] { host.toString(), req.getRequestLine().getUri() }));
    }
    return canonicalizeUri(req.getRequestLine().getUri());
  }
  
  public String canonicalizeUri(String uri)
  {
    try
    {
      URI normalized = URIUtils.resolve(BASE_URI, uri);
      URL u = new URL(normalized.toASCIIString());
      String protocol = u.getProtocol();
      String hostname = u.getHost();
      int port = canonicalizePort(u.getPort(), protocol);
      String path = u.getPath();
      String query = u.getQuery();
      String file = query != null ? path + "?" + query : path;
      URL out = new URL(protocol, hostname, port, file);
      return out.toString();
    }
    catch (IllegalArgumentException e)
    {
      return uri;
    }
    catch (MalformedURLException e) {}
    return uri;
  }
  
  private int canonicalizePort(int port, String protocol)
  {
    if ((port == -1) && ("http".equalsIgnoreCase(protocol))) {
      return 80;
    }
    if ((port == -1) && ("https".equalsIgnoreCase(protocol))) {
      return 443;
    }
    return port;
  }
  
  private boolean isRelativeRequest(HttpRequest req)
  {
    String requestUri = req.getRequestLine().getUri();
    return ("*".equals(requestUri)) || (requestUri.startsWith("/"));
  }
  
  protected String getFullHeaderValue(Header[] headers)
  {
    if (headers == null) {
      return "";
    }
    StringBuilder buf = new StringBuilder("");
    boolean first = true;
    for (Header hdr : headers)
    {
      if (!first) {
        buf.append(", ");
      }
      buf.append(hdr.getValue().trim());
      first = false;
    }
    return buf.toString();
  }
  
  public String getVariantURI(HttpHost host, HttpRequest req, HttpCacheEntry entry)
  {
    if (!entry.hasVariants()) {
      return getURI(host, req);
    }
    return getVariantKey(req, entry) + getURI(host, req);
  }
  
  public String getVariantKey(HttpRequest req, HttpCacheEntry entry)
  {
    List<String> variantHeaderNames = new ArrayList();
    for (Header varyHdr : entry.getHeaders("Vary")) {
      for (HeaderElement elt : varyHdr.getElements()) {
        variantHeaderNames.add(elt.getName());
      }
    }
    Collections.sort(variantHeaderNames);
    StringBuilder buf;
    try
    {
      buf = new StringBuilder("{");
      boolean first = true;
      for (String headerName : variantHeaderNames)
      {
        if (!first) {
          buf.append("&");
        }
        buf.append(URLEncoder.encode(headerName, Consts.UTF_8.name()));
        buf.append("=");
        buf.append(URLEncoder.encode(getFullHeaderValue(req.getHeaders(headerName)), Consts.UTF_8.name()));
        
        first = false;
      }
      buf.append("}");
    }
    catch (UnsupportedEncodingException uee)
    {
      throw new RuntimeException("couldn't encode to UTF-8", uee);
    }
    return buf.toString();
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.client.cache.CacheKeyGenerator
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.client.cache;

import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.apache.http.annotation.ThreadSafe;

@ThreadSafe
public class DefaultFailureCache
  implements FailureCache
{
  static final int DEFAULT_MAX_SIZE = 1000;
  static final int MAX_UPDATE_TRIES = 10;
  private final int maxSize;
  private final ConcurrentMap<String, FailureCacheValue> storage;
  
  public DefaultFailureCache()
  {
    this(1000);
  }
  
  public DefaultFailureCache(int maxSize)
  {
    this.maxSize = maxSize;
    storage = new ConcurrentHashMap();
  }
  
  public int getErrorCount(String identifier)
  {
    if (identifier == null) {
      throw new IllegalArgumentException("identifier may not be null");
    }
    FailureCacheValue storedErrorCode = (FailureCacheValue)storage.get(identifier);
    return storedErrorCode != null ? storedErrorCode.getErrorCount() : 0;
  }
  
  public void resetErrorCount(String identifier)
  {
    if (identifier == null) {
      throw new IllegalArgumentException("identifier may not be null");
    }
    storage.remove(identifier);
  }
  
  public void increaseErrorCount(String identifier)
  {
    if (identifier == null) {
      throw new IllegalArgumentException("identifier may not be null");
    }
    updateValue(identifier);
    removeOldestEntryIfMapSizeExceeded();
  }
  
  private void updateValue(String identifier)
  {
    for (int i = 0; i < 10; i++)
    {
      FailureCacheValue oldValue = (FailureCacheValue)storage.get(identifier);
      if (oldValue == null)
      {
        FailureCacheValue newValue = new FailureCacheValue(identifier, 1);
        if (storage.putIfAbsent(identifier, newValue) == null) {
          return;
        }
      }
      else
      {
        int errorCount = oldValue.getErrorCount();
        if (errorCount == Integer.MAX_VALUE) {
          return;
        }
        FailureCacheValue newValue = new FailureCacheValue(identifier, errorCount + 1);
        if (storage.replace(identifier, oldValue, newValue)) {
          return;
        }
      }
    }
  }
  
  private void removeOldestEntryIfMapSizeExceeded()
  {
    if (storage.size() > maxSize)
    {
      FailureCacheValue valueWithOldestTimestamp = findValueWithOldestTimestamp();
      if (valueWithOldestTimestamp != null) {
        storage.remove(valueWithOldestTimestamp.getKey(), valueWithOldestTimestamp);
      }
    }
  }
  
  private FailureCacheValue findValueWithOldestTimestamp()
  {
    long oldestTimestamp = Long.MAX_VALUE;
    FailureCacheValue oldestValue = null;
    for (Map.Entry<String, FailureCacheValue> storageEntry : storage.entrySet())
    {
      FailureCacheValue value = (FailureCacheValue)storageEntry.getValue();
      long creationTimeInNanos = value.getCreationTimeInNanos();
      if (creationTimeInNanos < oldestTimestamp)
      {
        oldestTimestamp = creationTimeInNanos;
        oldestValue = (FailureCacheValue)storageEntry.getValue();
      }
    }
    return oldestValue;
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.client.cache.DefaultFailureCache
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.client.cache;

import java.io.File;
import org.apache.http.annotation.Immutable;
import org.apache.http.impl.client.CloseableHttpClient;

@Immutable
public class CachingHttpClients
{
  public static CachingHttpClientBuilder custom()
  {
    return CachingHttpClientBuilder.create();
  }
  
  public static CloseableHttpClient createMemoryBound()
  {
    return CachingHttpClientBuilder.create().build();
  }
  
  public static CloseableHttpClient createFileBound(File cacheDir)
  {
    return CachingHttpClientBuilder.create().setCacheDir(cacheDir).build();
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.client.cache.CachingHttpClients
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.client.cache;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.HttpRequest;
import org.apache.http.HttpVersion;
import org.apache.http.ProtocolVersion;
import org.apache.http.RequestLine;
import org.apache.http.annotation.Immutable;

@Immutable
class CacheableRequestPolicy
{
  private final Log log = LogFactory.getLog(getClass());
  
  public boolean isServableFromCache(HttpRequest request)
  {
    String method = request.getRequestLine().getMethod();
    
    ProtocolVersion pv = request.getRequestLine().getProtocolVersion();
    if (HttpVersion.HTTP_1_1.compareToVersion(pv) != 0)
    {
      log.trace("non-HTTP/1.1 request was not serveable from cache");
      return false;
    }
    if (!method.equals("GET"))
    {
      log.trace("non-GET request was not serveable from cache");
      return false;
    }
    if (request.getHeaders("Pragma").length > 0)
    {
      log.trace("request with Pragma header was not serveable from cache");
      return false;
    }
    Header[] cacheControlHeaders = request.getHeaders("Cache-Control");
    for (Header cacheControl : cacheControlHeaders) {
      for (HeaderElement cacheControlElement : cacheControl.getElements())
      {
        if ("no-store".equalsIgnoreCase(cacheControlElement.getName()))
        {
          log.trace("Request with no-store was not serveable from cache");
          return false;
        }
        if ("no-cache".equalsIgnoreCase(cacheControlElement.getName()))
        {
          log.trace("Request with no-cache was not serveable from cache");
          return false;
        }
      }
    }
    log.trace("Request was serveable from cache");
    return true;
  }
}

/* Location:
 * Qualified Name:     org.apache.http.impl.client.cache.CacheableRequestPolicy
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.http.impl.client.cache;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.Header;
import org.apache.http.client.utils.DateUtils;

class WarningValue
{
  private int offs;
  private int init_offs;
  private final String src;
  private int warnCode;
  private String warnAgent;
  private String warnText;
  private Date warnDate;
  private static final String TOPLABEL = "\\p{Alpha}([\\p{Alnum}-]*\\p{Alnum})?";
  private static final String DOMAINLABEL = "\\p{Alnum}([\\p{Alnum}-]*\\p{Alnum})?";
  private static final String HOSTNAME = "(\\p{Alnum}([\\p{Alnum}-]*\\p{Alnum})?\\.)*\\p{Alpha}([\\p{Alnum}-]*\\p{Alnum})?\\.?";
  private static final String IPV4ADDRESS = "\\d+\\.\\d+\\.\\d+\\.\\d+";
  private static final String HOST = "((\\p{Alnum}([\\p{Alnum}-]*\\p{Alnum})?\\.)*\\p{Alpha}([\\p{Alnum}-]*\\p{Alnum})?\\.?)|(\\d+\\.\\d+\\.\\d+\\.\\d+)";
  private static final String PORT = "\\d*";
  private static final String HOSTPORT = "(((\\p{Alnum}([\\p{Alnum}-]*\\p{Alnum})?\\.)*\\p{Alpha}([\\p{Alnum}-]*\\p{Alnum})?\\.?)|(\\d+\\.\\d+\\.\\d+\\.\\d+))(\\:\\d*)?";
  
  WarningValue(String s)
  {
    this(s, 0);
  }
  
  WarningValue(String s, int offs)
  {
    this.offs = (init_offs = offs);
    src = s;
    consumeWarnValue();
  }
  
  public static WarningValue[] getWarningValues(Header h)
  {
    List<WarningValue> out = new ArrayList();
    String src = h.getValue();
    int offs = 0;
    for (;;)
    {
      if (offs < src.length()) {
        try
        {
          WarningValue wv = new WarningValue(src, offs);
          out.add(wv);
          offs = offs;
        }
        catch (IllegalArgumentException e)
        {
          int nextComma = src.indexOf(',', offs);
          if (nextComma != -1) {
            offs = nextComma + 1;
          }
        }
      }
    }
    WarningValue[] wvs = new WarningValue[0];
    return (WarningValue[])out.toArray(wvs);
  }
  
  protected void consumeLinearWhitespace()
  {
    while (offs < src.length())
    {
      switch (src.charAt(offs))
      {
      case '\r': 
        if ((offs + 2 >= src.length()) || (src.charAt(offs + 1) != '\n') || ((src.charAt(offs + 2) != ' ') && (src.charAt(offs + 2) != '\t'))) {
          return;
        }
        offs += 2;
        break;
      case '\t': 
      case ' ': 
        break;
      default: 
        return;
      }
      offs += 1;
    }
  }
  
  private boolean isChar(char c)
  {
    int i = c;
    return (i >= 0) && (i <= 127);
  }
  
  private boolean isControl(char c)
  {
    int i = c;
    return (i == 127) || ((i >= 0) && (i <= 31));
  }
  
  private boolean isSeparator(char c)
  {
    return (c == '(') || (c == ')') || (c == '<') || (c == '>') || (c == '@') || (c == ',') || (c == ';') || (c == ':') || (c == '\\') || (c == '"') || (c == '/') || (c == '[') || (c == ']') || (c == '?') || (c == '=') || (c == '{') || (c == '}') || (c == ' ') || (c == '\t');
  }
  
  protected void consumeToken()
  {
    if (!isTokenChar(src.charAt(offs))) {
      parseError();
    }
    while ((offs < src.length()) && 
      (isTokenChar(src.charAt(offs)))) {
      offs += 1;
    }
  }
  
  private boolean isTokenChar(char c)
  {
    return (isChar(c)) && (!isControl(c)) && (!isSeparator(c));
  }
  
  private static final Pattern HOSTPORT_PATTERN = Pattern.compile("(((\\p{Alnum}([\\p{Alnum}-]*\\p{Alnum})?\\.)*\\p{Alpha}([\\p{Alnum}-]*\\p{Alnum})?\\.?)|(\\d+\\.\\d+\\.\\d+\\.\\d+))(\\:\\d*)?");
  private static final String MONTH = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
  private static final String WEEKDAY = "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday";
  private static final String WKDAY = "Mon|Tue|Wed|Thu|Fri|Sat|Sun";
  private static final String TIME = "\\d{2}:\\d{2}:\\d{2}";
  private static final String DATE3 = "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( |\\d)\\d";
  private static final String DATE2 = "\\d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{2}";
  private static final String DATE1 = "\\d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d{4}";
  private static final String ASCTIME_DATE = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun) ((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( |\\d)\\d) (\\d{2}:\\d{2}:\\d{2}) \\d{4}";
  private static final String RFC850_DATE = "(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\\d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{2}) (\\d{2}:\\d{2}:\\d{2}) GMT";
  private static final String RFC1123_DATE = "(Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\\d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d{4}) (\\d{2}:\\d{2}:\\d{2}) GMT";
  private static final String HTTP_DATE = "((Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\\d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d{4}) (\\d{2}:\\d{2}:\\d{2}) GMT)|((Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\\d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{2}) (\\d{2}:\\d{2}:\\d{2}) GMT)|((Mon|Tue|Wed|Thu|Fri|Sat|Sun) ((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( |\\d)\\d) (\\d{2}:\\d{2}:\\d{2}) \\d{4})";
  private static final String WARN_DATE = "\"(((Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\\d{2} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d{4}) (\\d{2}:\\d{2}:\\d{2}) GMT)|((Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (\\d{2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\\d{2}) (\\d{2}:\\d{2}:\\d{2}) GMT)|((Mon|Tue|Wed|Thu|Fri|Sat|Sun) ((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ( |\\d)\\d) (\\d{2}:\\d{2}:\\d{2}) \\d{4}))\"";
  
  protected void consumeHostPort()
  {
    Matcher m = HOSTPORT_PATTERN.matcher(src.substring(offs));
    if (!m.find()) {
      parseError();
    }
    if (m.start() != 0) {
      parseError();
    }
    offs += m.end();
  }
  
  protected void consumeWarnAgent()
  {
    int curr_offs = offs;
    try
    {
      consumeHostPort();
      warnAgent = src.substring(curr_offs, offs);
      consumeCharacter(' ');
      return;
    }
    catch (IllegalArgumentException e)
    {
      offs = curr_offs;
      
      consumeToken();
      warnAgent = src.substring(curr_offs, offs);
      consumeCharacter(' ');
    }
  }
  
  protected void consumeQuotedString()
  {
    if (src.charAt(offs) != '"') {
      parseError();
    }
    offs += 1;
    boolean foundEnd = false;
    while ((offs < src.length()) && (!foundEnd))
    {
      char c = src.charAt(offs);
      if ((offs + 1 < src.length()) && (c == '\\') && (isChar(src.charAt(offs + 1))))
      {
        offs += 2;
      }
      else if (c == '"')
      {
        foundEnd = true;
        offs += 1;
      }
      else if ((c != '"') && (!isControl(c)))
      {
        offs += 1;
      }
      else
      {
        parseError();
      }
    }
    if (!foundEnd) {
      parseError();
    }
  }
  
  protected void consumeWarnText()
  {
    int curr = offs;
    consumeQuotedString();
    warnText = src.substring(curr, offs);
  }
  
  private static final Pattern WARN_DATE_PATTERN = Pattern.compile("\"(((Mon|Tue|Wed|Thu|Fri|Sat|Sun), (\\d{2} (Jan|Feb|Mar|Apr|May|Ju
1 2 3 4 5 6 7

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd