// cache the response (remember preemptive is off)
if (session.getExpires() > 0) {
SourceValidity[] validities = new SourceValidity[1];
validities[0] = session.getExpiresValidity();
CachedResponse response = new CachedResponse(validities, result);
session.getCacheStorageProxy().put(uri, response);
}
} else {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Streaming from cached response.");
}
// use the response from the cache
result = (byte[])object;
}
// stream the content
XMLDeserializer deserializer = null;
try {
deserializer = (XMLDeserializer)this.manager.lookup( XMLDeserializer.ROLE );
deserializer.setConsumer(handler);
deserializer.deserialize(result);
} catch (ComponentException ce) {
throw new SAXException("Unable to lookup xml deserializer.", ce);
} finally {
this.manager.release( deserializer );
}
return;
} else {
// we are not processing parallel
// first: test for a cached response
IncludeCacheStorageProxy storage = session.getCacheStorageProxy();
CachedResponse response = (CachedResponse)storage.get(uri);
if ( null != response) {
SourceValidity[] validities = response.getValidityObjects();
// if purging is turned off and either the cached response is valid or
// we are loading preemptive, then use the cached response
if ( !session.isPurging()
&& (session.isPreemptive() || validities[0].isValid() == SourceValidity.VALID)) {
// stream the content
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Streaming from cached response.");
}
XMLDeserializer deserializer = null;
try {
deserializer = (XMLDeserializer)this.manager.lookup( XMLDeserializer.ROLE );
deserializer.setConsumer(handler);
deserializer.deserialize(response.getResponse());
} catch (ComponentException ce) {
throw new SAXException("Unable to lookup xml deserializer.", ce);
} finally {
this.manager.release( deserializer );
}
// load preemptive if the response is not valid
if ( session.getExpires() > 0
&& session.isPreemptive()
&& validities[0].isValid() != SourceValidity.VALID) {
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Add uri to preemptive loader list " + uri);
}
if (!PreemptiveLoader.getInstance().alive) {
this.getLogger().error("Preemptive loader has not started yet.");
}
PreemptiveLoader.getInstance().add(session.getCacheStorageProxy(), uri, session.getExpires());
}
return;
} else {
// cached response is not valid
storage.remove(uri);
}
}
}
// we are not processing in parallel and have no (valid) cached response
XMLSerializer serializer = null;
try {
final Source source = session.resolveURI(uri, this.resolver);
// stream directly (and cache the response)
if (this.getLogger().isDebugEnabled()) {
this.getLogger().debug("Streaming directly from source.");
}
if (session.getExpires() > 0) {
serializer = (XMLSerializer)this.manager.lookup(XMLSerializer.ROLE);
XMLTeePipe tee = new XMLTeePipe(handler, serializer);
SourceUtil.toSAX(source, tee);
SourceValidity[] validities = new SourceValidity[1];
validities[0] = session.getExpiresValidity();
CachedResponse response = new CachedResponse(validities,
(byte[])serializer.getSAXFragment());
session.getCacheStorageProxy().put(uri, response);
} else {
SourceUtil.toSAX(source, handler);
}