trace(request, "*** Can't use cache");
}
// we will invalidate this resource, will do that only
// on real entity resource, not on negotiated ones
if (connected) {
CachedResource res = null;
EntityCachedResource invalidRes = null;
try {
String requrl = request.getURL().toExternalForm();
res = store.getCachedResourceReference(requrl);
if (res != null) {
invalidRes = (EntityCachedResource)
res.lookupResource(request);
}
} catch (InvalidCacheException ex) {
invalidRes = null;
}
if (invalidRes != null) {
invalidRes.setWillRevalidate(true);
}
request.setState(STATE_NOCACHE, Boolean.TRUE);
return null;
} else {
// disconnected, abort now!
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;
}
}
// let's try to get the resource!
String requrl = request.getURL().toExternalForm();
// in the pre-cache, wait for full download
// FIXME should be better than this behaviour...
// see EntityCachedResource perform's FIXME ;)
if (precache.containsKey(requrl)) {
if (debug)
System.out.println("*** Already downloading: "+ requrl);
try {
CachedResource cr = (CachedResource)precache.get(requrl);
return cr.perform(request);
} catch (Exception ex) {
// there was a problem with the previous request,
// it may be better to do it by ourself
}
}
CachedResource res = null;
try {
res = store.getCachedResourceReference(requrl);
} catch (InvalidCacheException ex) {
res = null;
}
// Is this a push resource ?
try {
if(PushCacheManager.instance().isPushResource(res)) {
EntityCachedResource ecr=(EntityCachedResource)
res.lookupResource(request);
if(ecr!=null) {
Reply reply = ecr.perform(request);
return reply;
}
}
}
catch(Exception e) {
e.printStackTrace();
}
// /PSLH
// are we disconnected?
if (request.checkOnlyIfCached() || !connected ) {
// and no entries...
EntityCachedResource ecr = null;
if (res != null) {
ecr = (EntityCachedResource) res.lookupResource(request);
}
if ((res == null) || (ecr == null)) {
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;
}
// yeah!
if (debug) {
trace(request, (connected) ? " hit - only if cached" :
" hit while disconneced" );
}
if (!validator.isValid(ecr, request)) {
addWarning(request, WARN_STALE);
}
addWarning(request, WARN_DISCONNECTED);
Reply reply = ecr.perform(request);
// Add any warnings collected during processing to the reply:
setWarnings(request, reply);
//FIXME request.setState(STATE_HOW, HOW_HIT);
return reply;
}
// in connected mode, we should now take care of revalidation and such
if (res != null) {
// if not fully loaded, ask for a revalidation FIXME
if ((res.getLoadState() == CachedResource.STATE_LOAD_PARTIAL) ||
(res.getLoadState() == CachedResource.STATE_LOAD_ERROR)) {
setRequestRevalidation(res, request);
return null;
}
if ( validator.isValid(res, request) ) {
try {
store.updateResourceGeneration(res);
} catch (InvalidCacheException ex) {
// should be ok so...
}
//FIXME request.setState(STATE_HOW, HOW_HIT);
Reply rep = res.perform(request);
return rep;
} else {
if (debug) {
System.out.println("*** Revalidation asked for " + requrl);
}
// ask for a revalidation
setRequestRevalidation(res, request);
return null;
}
}
// lock here while we are waiting for the download
while (uritable.containsKey(requrl)) {
synchronized (uritable) {
try {
uritable.wait();
} catch (InterruptedException ex) {}
}
if (precache.containsKey(requrl)) {
if (debug)
System.out.println("*** Already downloading: "+ requrl);
CachedResource cr = (CachedResource)precache.get(requrl);
return cr.perform(request);
}
uritable.put(requrl, requrl);
}
return null;
}