Code with Finding: |
class CacheFilter {
public Reply ingoingFilter(Request request)
throws HttpException
{
// Has the filter been disabled ?
if ( request.hasState(STATE_NOCACHE) )
return null;
// Proceed:
if ( ! canUseCache(request) ) {
if ( ! isConnected() ) {
// Don't continue, if cache is disconnected.
if ( debug )
trace(request, "unavailable (disconnected).");
Reply reply = request.makeReply(HTTP.GATEWAY_TIMEOUT);
reply.setContent("The cache cannot be use for "
+ "<p><code>"+request.getMethod()+"</code> "
+ "<strong>"+request.getURL()+"</strong>"
+ ". <p>It is disconnected.");
return reply;
} else {
if ( debug )
trace(request, "can't use cache.");
// can't use cache, then we must invalidate the entry!
// according to the spec, we could do what we want
// either use OUR validator (then not invalidate it)
// or the one used for the request
// for now on, use only the on of the client
// ir invalidate cache entry
CachedResource ires = null;
try {
ires = loadResource(request.getURL().toExternalForm());
} catch (InvalidResourceException ex) {
ires = null;
}
if (ires != null) {
ires.setWillRevalidate(true);
}
request.setState(STATE_NOCACHE, Boolean.TRUE);
return null;
}
}
// Ok, we can try to use a cached version...
String url = request.getURL().toExternalForm();
CachedResource res = null;
try {
res = loadResource(url);
} catch (InvalidResourceException ex) {
res = null;
}
if ( res == null ) {
// This was an only-if-cached, don't continue:
if (request.checkOnlyIfCached() || ( ! isConnected()) ) {
if ( debug )
trace(request, "unavailable (disconnected).");
Reply reply = request.makeReply(HTTP.GATEWAY_TIMEOUT);
reply.setContent("The cache doesn't have an entry for "
+ "<p><strong>"+request.getURL()+"</strong>"
+ ". <p>And it is disconnected.");
return reply;
}
return null;
}
// Before going any further, we clone the original request:
Request reqorig = (Request) request.getClone();
request.setState(STATE_ORIGREQ, reqorig);
// Is this resource valid for answering this request ?
HttpWarning warning = null;
if ( ! res.isValid(request) ) {
if ( request.checkOnlyIfCached() || ! isConnected()) {
// We really want to use the cache, whatever
addWarning(request, WARN_DISCONNECTED);
} else {
if (debug)
System.out.println("Needs revalidation");
// Let's continue through the revalidation process:
request.setState(STATE_REVALIDATION, Boolean.TRUE);
return null;
}
}
// Yeah ! We win:
if ( debug )
trace(request, "uses cache.");
Reply reply = res.perform(reqorig);
// Add any warnings collected during processing to the reply:
setWarnings(request, reply);
request.setState(STATE_HOW, HOW_HIT);
return reply;
}
}
|