Skip to content

Commit

Permalink
client cache
Browse files Browse the repository at this point in the history
  • Loading branch information
patriot1burke committed Aug 6, 2012
1 parent 98f00b2 commit 296847e
Show file tree
Hide file tree
Showing 10 changed files with 885 additions and 0 deletions.
@@ -0,0 +1,60 @@
package org.jboss.resteasy.client.jaxrs.cache;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.io.Serializable;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public interface BrowserCache
{
public static class Header implements Serializable
{
private static final long serialVersionUID = 4145981086454860081L;

private String name;
private String value;

public Header(String name, String value)
{
this.name = name;
this.value = value;
}

public String getName()
{
return name;
}

public String getValue()
{
return value;
}
}

public static interface Entry
{
MultivaluedMap<String, String> getHeaders();

boolean expired();

Header[] getValidationHeaders();

byte[] getCached();

MediaType getMediaType();
}

Entry getAny(String key);

Entry get(String key, MediaType accept);

Entry put(String key, MediaType mediaType, MultivaluedMap<String, String> headers, byte[] cached, int expires, String etag, String lastModified);

Entry remove(String key, MediaType type);

void clear();

}
@@ -0,0 +1,32 @@
package org.jboss.resteasy.client.jaxrs.cache;

import javax.ws.rs.client.Configuration;
import javax.ws.rs.client.Feature;

/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public class BrowserCacheFeature implements Feature
{
protected BrowserCache cache;

public BrowserCache getCache()
{
return cache;
}

public void setCache(BrowserCache cache)
{
this.cache = cache;
}

@Override
public boolean onEnable(Configuration configuration)
{
if (cache == null) cache = new LightweightBrowserCache();
configuration.setProperty(BrowserCache.class.getName(), cache);
configuration.register(new CacheInterceptor(cache));
return true;
}
}
@@ -0,0 +1,93 @@
package org.jboss.resteasy.client.jaxrs.cache;

import org.jboss.resteasy.client.jaxrs.cache.BrowserCache.Entry;
import org.jboss.resteasy.client.jaxrs.cache.BrowserCache.Header;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CacheEntry implements Entry, Serializable
{
private static final long serialVersionUID = -1922521972113619372L;
private final String mediaType;
private final byte[] cached;
private final int expires;
private final long timestamp = System.currentTimeMillis();
private final MultivaluedMap<String, String> headers;
private Header[] validationHeaders = {};
private final String key;
private Map extendedProperties = new ConcurrentHashMap();

public CacheEntry(String key, MultivaluedMap<String, String> headers, byte[] cached, int expires, String etag, String lastModified, MediaType mediaType)
{
this.key = key;
this.cached = cached;
this.expires = expires;
this.mediaType = mediaType.toString();
this.headers = headers;

if (etag != null || lastModified != null)
{
if (etag != null && lastModified != null)
{
validationHeaders = new Header[2];
validationHeaders[0] = new Header("If-Modified-Since", lastModified);
validationHeaders[1] = new Header("If-None-Match", etag);
}
else if (etag != null)
{
validationHeaders = new Header[1];
validationHeaders[0] = new Header("If-None-Match", etag);
}
else if (lastModified != null)
{
validationHeaders = new Header[1];
validationHeaders[0] = new Header("If-Modified-Since", lastModified);
}

}
}

public String getKey()
{
return key;
}

public MultivaluedMap<String, String> getHeaders()
{
return headers;
}

public boolean expired()
{
return System.currentTimeMillis() - timestamp >= expires * 1000;
}

public Header[] getValidationHeaders()
{
return validationHeaders;
}

public byte[] getCached()
{
return cached;
}

public MediaType getMediaType()
{
return MediaType.valueOf(mediaType);
}

public void addExtendedProperty(Serializable key, Serializable value)
{
extendedProperties.put(key, value);
}

public Object getExtendedProperty(Serializable key)
{
return extendedProperties.get(key);
}
}

0 comments on commit 296847e

Please sign in to comment.