Java Reference
In-Depth Information
public
public void
void filter ( ClientRequestContext requestContext ,
ClientResponseContext responseContext )
throws
throws IOException ;
}
Let's use these two interfaces to implement a client-side cache. We want this cache to behave
like a browser's cache. This means we want it to honor the Cache-Control semantics dis-
cussed in Chapter 11 . We want cache entries to expire based on the metadata within Cache-
Control response headers. We want to perform conditional GETs if the client is requesting
an expired cache entry. Let's implement our ClientRequestFilter first:
import
import javax.ws.rs.client.ClientRequestFilter
javax.ws.rs.client.ClientRequestFilter ;
import
import javax.ws.rs.client.ClientRequestContext
javax.ws.rs.client.ClientRequestContext ;
public
public class
class ClientCacheRequestFilter
ClientCacheRequestFilter implements
implements ClientRequestFilter {
private
private Cache cache ;
public
public ClientCacheRequestFilter ( Cache cache ) {
this
this . cache = cache ;
}
public
public void
throws IOException {
iif (! ctx . getMethod (). equalsIgnoreCase ( "GET" )) return
void filter ( ClientRequestContext ctx ) throws
return ;
CacheEntry entry = cache . getEntry ( request . getUri ());
iif ( entry == null
null ) return
return ;
iif (! entry . isExpired ()) {
ByteArrayInputStream is = new
new ByteArrayInputStream ( entry . getContent ());
Response response = Response . ok ( is )
. type ( entry . getContentType ()). build ();
ctx . abortWith ( response );
return
return ;
}
String etag = entry . getETagHeader ();
String lastModified = entry . getLastModified ();
iif ( etag != null
null ) {
ctx . getHeaders . putSingle ( "If-None-Match" , etag );
}
iif ( lastModified != null
null ) {
ctx . getHeaders . putSingle ( "If-Modified-Since" , lastModified );
}
Search WWH ::




Custom Search