org.w3c.www.protocol.http.cache
Class CachedResource

java.lang.Object
  |
  +--org.w3c.tools.resources.AttributeHolder
        |
        +--org.w3c.tools.resources.Resource
              |
              +--org.w3c.www.protocol.http.cache.CachedResource
Direct Known Subclasses:
VaryResource

public class CachedResource
extends org.w3c.tools.resources.Resource
implements TeeMonitor


Field Summary
protected static int ATTR_CACHE_FILTER
          Attribute index - The cache filter that manages us.
protected static int ATTR_CONTENT_LENGTH
          Attribute index - The resource content length.
protected static int ATTR_CONTENT_TYPE
          Attribute index - The resource content type.
protected static int ATTR_DATE
          Attribute index - The date at which the reply was emited.
protected static int ATTR_ETAG
          Attribute index - The entity tag (if any) associated with the resource.
protected static int ATTR_EXTRA_HEADERS
          Attribute index - The extension headers attribute.
protected static int ATTR_FILE
          Attribute index - The path of the file that has the resource content.
protected static int ATTR_GENERATION
          Attribute index - This cached resource generation.
protected static int ATTR_INITAGE
          Attribute index - The initial age of this resource.
protected static int ATTR_LAST_MODIFIED
          Attribute index - The last modified date of the cached content
protected static int ATTR_MAXAGE
          Attribute index - The resource's max age.
protected static int ATTR_RESOURCE_STORE
          Attribute index - The resource store associated with this resource.
protected static int ATTR_RESPONSE_TIME
          Attribute index - The response time of the cached response.
protected static int ATTR_REVALIDATE
          Attribute index - Does this cached resource should revalidate.
protected static int ATTR_STATUS
          Attribute index - The reply status.
static int COND_FAILED
          Condition check return code - Condition existed but failed.
static int COND_OK
          Condition check return code - Condition existed and succeeded.
protected static long DEFAULT_MAXAGE
          The default maxage, set to less then 24 hours, as specified in HTTP/1.1
 
Fields inherited from class org.w3c.tools.resources.Resource
ATTR_CONTEXT, ATTR_HELP_URL, ATTR_IDENTIFIER, ATTR_LAST_MODIFIED, ATTR_PARENT, ATTR_RESOURCE_FRAMES, ATTR_STORE_ENTRY, ATTR_URL
 
Fields inherited from class org.w3c.tools.resources.AttributeHolder
attributes, slowpickle, values
 
Constructor Summary
CachedResource()
           
 
Method Summary
 java.io.InputStream cacheContent(java.io.InputStream in)
          Cache this content, then return the corresponding stream.
 int checkIfMatch(Request request)
          Try to validate an If-Match request.
 int checkIfModifiedSince(Request request)
          Try to validate an If-Modified-Since request.
 int checkIfNoneMatch(Request request)
          Try to validate an If-None-Match request.
 int checkIfUnmodifiedSince(Request request)
          Try to validate an If-Unmodified-Since request.
protected  void cleanUpload()
          Mark that resource as now being loaded.
protected  long computeCurrentAge()
          Compute this cached entry's current age.
 void delete()
          Delete this cached entry from the cache.
 int getContentLength()
          Get this cached entry content length.
 MimeType getContentType()
          Get the content type associated to that cache entry.
 long getDate()
          Get the HTTP date of the reply used to created that entry.
 java.lang.String getETag()
          Get the entity tag associated with that cached entry.
 ArrayDictionary getExtraHeaders()
          Get the extra headers stored for that resource.
 java.io.File getFile()
          Get the file in which that resource's content is saved.
 int getGeneration()
          Get the generation of that cached entry.
 HttpEntityTag getHETag()
          Get the entity tag associated with that cached entry
 long getInitialAge()
          Get this cached entry initial age.
 java.io.InputStream getInputStream()
          Get the cached data for that cached entry.
 long getLastModified()
          Get the last modified time of that entry.
 long getMaxAge()
          Get this resource's max age.
 ResourceStore getResourceStore()
          Get the store associated with this resource.
 long getResponseTime()
          Get the time of the response used to cached that entry.
 boolean getRevalidate()
          Should this cache entry always be revalidated ?
 int getStatus()
          Get the HTTP status of that cached entry.
 Reply handleRangeRequest(Request request, HttpRange r)
           
 void initialize(CacheFilter filter, Request request, Reply reply)
           
 void initialize(java.lang.Object[] values)
           
 boolean isShared()
           
 boolean isValid(Request request)
          Is the currently cached version usable to answer the given request ?
 void notifyTeeFailure()
           
 void notifyTeeSuccess(int size)
           
protected  boolean notifyUploading(int upoldsize, int upnewsize)
          Is this resource already being loaded from the net.
 Reply perform(Request request)
          This cached entry has been checked valid, perform given request.
 void setContentLength(int length)
          Set the content length of that cached entry.
 void setContentType(MimeType type)
          Set this cached entry content-type.
 void setDate(long date)
          Set the HTTP date of the reply used to create or update that entry.
protected  void setDefaultMaxAge()
          Compute a reasonable default value for the max-age of this resource.
 void setETag(java.lang.String etag)
          Associate an entity tag with that cached enrty.
 void setExtraHeaders(ArrayDictionary a)
          Set a new set of extra headers for that resource.
 void setFile(java.io.File file)
          Set the file to which that resource's content should be saved into.
 void setGeneration(int generation)
          Set the generation number of that resource.
 void setInitialAge(long initage)
          Set this resource's initial age.
 void setLastModified(long lastmodified)
          Set the last modified time of that cached entry.
 void setMaxAge(long maxage)
          Set this cached entry max age.
 void setReplyHeaders(Reply reply)
          Dump all our knowledge about this cached resource into the reply.
 void setRequestValidation(Request request)
          Update an original request for a revalidation.
 void setResourceHeaders(Request request, Reply reply)
          Cache all the headers that can be cached after the given transaction.
 void setResponseTime(long responsetime)
          Set this cached entry response time.
 void setRevalidate(boolean onoff)
          Set the revalidation flag for that cached entry.
 void setStatus(int status)
          Set the reply status for that entry.
 void setValue(int idx, java.lang.Object value)
           
 java.io.InputStream tryActiveCacheContent(java.io.InputStream in)
          Try using an active stream to cache the content.
 void updateAges(Request request, Reply reply)
          Update this entry's age according to the given HTTP transaction.
 Reply validate(Request request, Reply reply)
          Given an HTTP transaction, validate that cached entry.
protected  void waitUpload()
          Wait for the upload to finish, if needed.
 
Methods inherited from class org.w3c.tools.resources.Resource
acceptUnload, checkMultipleLock, collectFrames, getClone, getContext, getFrame, getFrames, getHelpURL, getHelpURL, getIdentifier, getParent, getResourceReference, getServer, getSpace, getSpaceEntry, getStoreEntry, getURLPath, getValue, getValue, isInitialized, markModified, notifyUnload, registerFrame, setContext, setContext, setValue, unregisterFrame, updateAttributes
 
Methods inherited from class org.w3c.tools.resources.AttributeHolder
definesAttribute, definesAttribute, getAttributes, getBoolean, getChar, getClone, getClone, getDouble, getFloat, getInt, getLong, getString, getValue, initialize, lookupAttribute, pickle, print, setBoolean, setChar, setDouble, setFloat, setInt, setLong, setString, setValue, unpickle, unpickle, unpickle, unpickleInstance, unpickleInstance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COND_FAILED

public static final int COND_FAILED
Condition check return code - Condition existed but failed.

COND_OK

public static final int COND_OK
Condition check return code - Condition existed and succeeded.

DEFAULT_MAXAGE

protected static long DEFAULT_MAXAGE
The default maxage, set to less then 24 hours, as specified in HTTP/1.1

ATTR_FILE

protected static int ATTR_FILE
Attribute index - The path of the file that has the resource content.

ATTR_CONTENT_TYPE

protected static int ATTR_CONTENT_TYPE
Attribute index - The resource content type.

ATTR_CONTENT_LENGTH

protected static int ATTR_CONTENT_LENGTH
Attribute index - The resource content length.

ATTR_MAXAGE

protected static int ATTR_MAXAGE
Attribute index - The resource's max age.

ATTR_INITAGE

protected static int ATTR_INITAGE
Attribute index - The initial age of this resource.

ATTR_RESPONSE_TIME

protected static int ATTR_RESPONSE_TIME
Attribute index - The response time of the cached response.

ATTR_LAST_MODIFIED

protected static int ATTR_LAST_MODIFIED
Attribute index - The last modified date of the cached content

ATTR_REVALIDATE

protected static int ATTR_REVALIDATE
Attribute index - Does this cached resource should revalidate.

ATTR_CACHE_FILTER

protected static int ATTR_CACHE_FILTER
Attribute index - The cache filter that manages us.

ATTR_DATE

protected static int ATTR_DATE
Attribute index - The date at which the reply was emited.

ATTR_STATUS

protected static int ATTR_STATUS
Attribute index - The reply status.

ATTR_ETAG

protected static int ATTR_ETAG
Attribute index - The entity tag (if any) associated with the resource.

ATTR_GENERATION

protected static int ATTR_GENERATION
Attribute index - This cached resource generation.

ATTR_EXTRA_HEADERS

protected static int ATTR_EXTRA_HEADERS
Attribute index - The extension headers attribute.

ATTR_RESOURCE_STORE

protected static int ATTR_RESOURCE_STORE
Attribute index - The resource store associated with this resource.
Constructor Detail

CachedResource

public CachedResource()
Method Detail

getResourceStore

public ResourceStore getResourceStore()
Get the store associated with this resource.
Returns:
The associated store or null. Not all resources have a store associated with them (eg the one that whose creation is cheap, etc).

setValue

public void setValue(int idx,
                     java.lang.Object value)
Overrides:
setValue in class org.w3c.tools.resources.Resource

isShared

public boolean isShared()

setDefaultMaxAge

protected void setDefaultMaxAge()
Compute a reasonable default value for the max-age of this resource.

This method computes and sets the default max age for this resource, it should be called as a last resort only.


getFile

public java.io.File getFile()
Get the file in which that resource's content is saved.
Returns:
A File instance.

setFile

public void setFile(java.io.File file)
Set the file to which that resource's content should be saved into.
Parameters:
file - The file to use.

getContentType

public MimeType getContentType()
Get the content type associated to that cache entry.
Returns:
A MimeType instance, or null if undefined.

setContentType

public void setContentType(MimeType type)
Set this cached entry content-type.
Parameters:
type - The new content type for that entry.

getContentLength

public int getContentLength()
Get this cached entry content length.
Returns:
An integer, giving the content length, or -1 if undefined.

setContentLength

public void setContentLength(int length)
Set the content length of that cached entry.
Parameters:
length - The new content length of that entry.

getMaxAge

public long getMaxAge()
Get this resource's max age.
Returns:
A long number of milliseconds for which that entry will remain valid, or -1 if undefined.

setMaxAge

public void setMaxAge(long maxage)
Set this cached entry max age.
Parameters:
maxage - A number of milliseconds during which the entry will remain valid, or -1 to undefine previous setting.

getInitialAge

public long getInitialAge()
Get this cached entry initial age.
Returns:
A long number of milliseconds giving the initial age relative to Java epoch, or -1 if undefined.

setInitialAge

public void setInitialAge(long initage)
Set this resource's initial age.
Parameters:
initage - The initial age as a number of milliseconds since Java epoch, or -1 to undefine previous setting.

getResponseTime

public long getResponseTime()
Get the time of the response used to cached that entry.
Returns:
A long number of milliseconds since Java epoch, or -1 if undefined.

setResponseTime

public void setResponseTime(long responsetime)
Set this cached entry response time.
Parameters:
responsetime - A long number of milliseconds indicating the response time relative to Java epoch, or -1 to undefined previous setting.

getLastModified

public long getLastModified()
Get the last modified time of that entry. This usually corresponds to the Last-Modified HTTP header.
Returns:
A long number of milliseconds since java epoch giving the last modified date of the cached entry, or -1 if undefined.
Overrides:
getLastModified in class org.w3c.tools.resources.Resource

setLastModified

public void setLastModified(long lastmodified)
Set the last modified time of that cached entry.
Parameters:
lastmodified - The last modification date as a number of milliseconds since Java epoch, or -1 to undefine previous setting.

getRevalidate

public boolean getRevalidate()
Should this cache entry always be revalidated ?
Returns:
A boolean, true if revalidation is always needed before serving it, false otherwise.

setRevalidate

public void setRevalidate(boolean onoff)
Set the revalidation flag for that cached entry.
Parameters:
A - boolean.

getDate

public long getDate()
Get the HTTP date of the reply used to created that entry.
Returns:
The HTTP date encoded as the number of milliseconds relative to Java epoch, or -1 if undefined.

setDate

public void setDate(long date)
Set the HTTP date of the reply used to create or update that entry.
Parameters:
date - The HTTP date encoded as a number of milliseconds since Java epoch, or -1 to undefine previous setting.

getStatus

public int getStatus()
Get the HTTP status of that cached entry.
Returns:
An integer HTTP status code, or -1 if undefined.

setStatus

public void setStatus(int status)
Set the reply status for that entry.
Parameters:
status - The HTTP status code of that entry, or -1 to undefine the previous setting.

getETag

public java.lang.String getETag()
Get the entity tag associated with that cached entry.
Returns:
The String encoded entity tag, or null if undefined.

getHETag

public HttpEntityTag getHETag()
Get the entity tag associated with that cached entry
Returns:
the entity tag or null if undefined

setETag

public void setETag(java.lang.String etag)
Associate an entity tag with that cached enrty.
Parameters:
etag - The entity tag of the entry, or null to reset the value.

getGeneration

public int getGeneration()
Get the generation of that cached entry.
Returns:
An integer giving the generation number of that resource.

setGeneration

public void setGeneration(int generation)
Set the generation number of that resource.
Parameters:
generation - The new generation number of that resource.

getExtraHeaders

public ArrayDictionary getExtraHeaders()
Get the extra headers stored for that resource.
Returns:
An ArrayDictionary with the extra headers, or null if undefined.

setExtraHeaders

public void setExtraHeaders(ArrayDictionary a)
Set a new set of extra headers for that resource.
Parameters:
headers - The new set of headers.

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Get the cached data for that cached entry.
Returns:
A non-buffered output stream.

notifyTeeFailure

public void notifyTeeFailure()
Specified by:
notifyTeeFailure in interface TeeMonitor

notifyTeeSuccess

public void notifyTeeSuccess(int size)
Specified by:
notifyTeeSuccess in interface TeeMonitor

tryActiveCacheContent

public java.io.InputStream tryActiveCacheContent(java.io.InputStream in)
                                          throws java.io.IOException
Try using an active stream to cache the content. Byte size usage is taken care of only at the end of the download to make sure we get the right sizes (might different from the advertized ones).
Returns:
An InputStream instance if active caching was possible, null otherwise.

cacheContent

public java.io.InputStream cacheContent(java.io.InputStream in)
                                 throws java.io.IOException
Cache this content, then return the corresponding stream. This method is called as a fallback to tryActiveCacheContent. At the end of the method, the cache filter is notified of byte size usage.

setResourceHeaders

public void setResourceHeaders(Request request,
                               Reply reply)
Cache all the headers that can be cached after the given transaction. Most reply headers are cached as attributes, other ones are left over to a slow, dictionary.
Parameters:
request - The request that triggered the reply.
reply - The origin server's reply.

setReplyHeaders

public void setReplyHeaders(Reply reply)
Dump all our knowledge about this cached resource into the reply.
Parameters:
reply - The reply we are building.

setRequestValidation

public void setRequestValidation(Request request)
Update an original request for a revalidation. This method updates the original request to use as much conditionals as possible.
Parameters:
request - The request to update.

cleanUpload

protected void cleanUpload()
Mark that resource as now being loaded.

notifyUploading

protected final boolean notifyUploading(int upoldsize,
                                        int upnewsize)
Is this resource already being loaded from the net. If not, mark that resource has currently being uploaded. When that method returns true, the caller MUST make sure the cleanUpload method gets called latter on.
Parameters:
upoldsize - The old size of cache entry (if available)
upnewsize - The expected size of the entry.
Returns:
A boolean, true if that resource is being loaded, false otherwise.

waitUpload

protected final void waitUpload()
Wait for the upload to finish, if needed.

validate

public Reply validate(Request request,
                      Reply reply)
               throws java.io.IOException
Given an HTTP transaction, validate that cached entry.
Parameters:
request - The request that we used to validate.
reply - The origin's server reply.
Returns:
A reply instance suitable to emit back to the client.

checkIfModifiedSince

public int checkIfModifiedSince(Request request)
Try to validate an If-Modified-Since request.
Parameters:
request - The request to validate.
Returns:
An integer, COND_FAILED, if the condition was checked, but failed; COND_OK of condition was checked and succeeded, 0 otherwise.

checkIfUnmodifiedSince

public int checkIfUnmodifiedSince(Request request)
Try to validate an If-Unmodified-Since request.
Parameters:
request - The request to validate.
Returns:
An integer, COND_FAILED, if the condition was checked, but failed; COND_OK of condition was checked and succeeded, 0 otherwise.

checkIfMatch

public int checkIfMatch(Request request)
Try to validate an If-Match request.
Parameters:
request - The request to validate.
Returns:
An integer, COND_FAILED, if the condition was checked, but failed; COND_OK of condition was checked and succeeded, 0 otherwise.

checkIfNoneMatch

public int checkIfNoneMatch(Request request)
Try to validate an If-None-Match request.
Parameters:
request - The request to validate.
Returns:
An integer, COND_FAILED, if the condition was checked, but failed; COND_OK of condition was checked and succeeded, 0 otherwise.

isValid

public boolean isValid(Request request)
Is the currently cached version usable to answer the given request ?
Returns:
A boolean, true if we are able to generate a valid answer to this request by the perform method, false otherwise (the resource needs to be refreshed).

handleRangeRequest

public Reply handleRangeRequest(Request request,
                                HttpRange r)

perform

public Reply perform(Request request)
              throws HttpException
This cached entry has been checked valid, perform given request.
Parameters:
request - The request to perform.
Returns:
An Reply instance.
Throws:
HttpException - If something went wrong.

delete

public void delete()
Delete this cached entry from the cache.
Overrides:
delete in class org.w3c.tools.resources.Resource

computeCurrentAge

protected long computeCurrentAge()
Compute this cached entry's current age.
Returns:
The age, as a number of milliseconds.

updateAges

public void updateAges(Request request,
                       Reply reply)
Update this entry's age according to the given HTTP transaction.
Parameters:
request - Our original request.
reply - The corresponding reply.

initialize

public void initialize(CacheFilter filter,
                       Request request,
                       Reply reply)
                throws java.io.IOException

initialize

public void initialize(java.lang.Object[] values)
Overrides:
initialize in class org.w3c.tools.resources.Resource