this.readerRole,
readerKey)
);
// now we have the key to get the cached object
CachedResponse cachedObject = this.cache.get(pcKey);
if (cachedObject != null) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("Found cached response for '" +
environment.getURI() + "' using key: " + pcKey);
}
SourceValidity[] validities = cachedObject.getValidityObjects();
if (validities == null || validities.length != 1) {
// to avoid getting here again and again, we delete it
this.cache.remove( pcKey );
if (getLogger().isDebugEnabled()) {
getLogger().debug("Cached response for '" + environment.getURI() +
"' using key: " + pcKey + " is invalid.");
}
this.cachedResponse = null;
} else {
SourceValidity cachedValidity = validities[0];
int result = cachedValidity.isValid();
boolean valid = false;
if ( result == 0 ) {
// get reader validity and compare
if (isCacheableProcessingComponent) {
readerValidity = ((CacheableProcessingComponent)super.reader).getValidity();
} else {
CacheValidity cv = ((Cacheable)super.reader).generateValidity();
if ( cv != null ) {
readerValidity = CacheValidityToSourceValidity.createValidity( cv );
}
}
if (readerValidity != null) {
result = cachedValidity.isValid(readerValidity);
if ( result == 0 ) {
readerValidity = null;
} else {
valid = (result == 1);
}
}
} else {
valid = (result > 0);
}
if (valid) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("processReader: using valid cached content for '" +
environment.getURI() + "'.");
}
byte[] response = cachedObject.getResponse();
if (response.length > 0) {
usedCache = true;
if ( cachedObject.getContentType() != null ) {
environment.setContentType(cachedObject.getContentType());
} else {
this.setMimeTypeForReader(environment);
}
outputStream = environment.getOutputStream(0);
environment.setContentLength(response.length);
outputStream.write(response);
}
} else {
if (getLogger().isDebugEnabled()) {
getLogger().debug("processReader: cached content is invalid for '" +
environment.getURI() + "'.");
}
// remove invalid cached object
this.cache.remove(pcKey);
}
}
}
}
if (!usedCache) {
if (pcKey != null) {
if (getLogger().isDebugEnabled()) {
getLogger().debug("processReader: caching content for further requests of '" +
environment.getURI() + "'.");
}
if (readerValidity == null) {
if (isCacheableProcessingComponent) {
readerValidity = ((CacheableProcessingComponent)super.reader).getValidity();
} else {
CacheValidity cv = ((Cacheable)super.reader).generateValidity();
if ( cv != null ) {
readerValidity = CacheValidityToSourceValidity.createValidity( cv );
}
}
}
if (readerValidity != null) {
outputStream = environment.getOutputStream(this.outputBufferSize);
outputStream = new CachingOutputStream(outputStream);
} else {
pcKey = null;
}
}
setMimeTypeForReader(environment);
if (this.reader.shouldSetContentLength()) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
this.reader.setOutputStream(os);
this.reader.generate();
environment.setContentLength(os.size());
if (outputStream == null) {
outputStream = environment.getOutputStream(0);
}
os.writeTo(outputStream);
} else {
if (outputStream == null) {
outputStream = environment.getOutputStream(this.outputBufferSize);
}
this.reader.setOutputStream(outputStream);
this.reader.generate();
}
// store the response
if (pcKey != null) {
final CachedResponse res = new CachedResponse(new SourceValidity[] {readerValidity},
((CachingOutputStream)outputStream).getContent());
res.setContentType(environment.getContentType());
this.cache.store(pcKey, res);
}
}
} catch (Exception e) {
handleException(e);