org.weborganic.bastille.cache.util
Class StaticResource

java.lang.Object
  extended by org.weborganic.bastille.cache.util.StaticResource
All Implemented Interfaces:
Serializable, CachedResource

public final class StaticResource
extends Object
implements Serializable, CachedResource

A Serializable representation of a cached resource designed for static file based content.

The etag is based on the last modified date.

If the resource is considered to be gzippable, the body content is stored compressed, otherwise it is sent raw.

See Also:
Serialized Form

Constructor Summary
StaticResource(int status, String contentType, byte[] body, long modified, String cacheControl, long expires)
          Creates a PageInfo object representing the "page".
 
Method Summary
 void copyHeadersTo(javax.servlet.http.HttpServletResponse res, boolean gzipped)
          Copy the headers to the HTTP servlet response.
 byte[] getBody(boolean gzipped)
          Returns the body content as bytes.
 String getContentType()
           
 String getETag(boolean gzipped)
          Returns the etag for this resource.
 byte[] getGzippedBody()
          Returns the gzip content if stored as such.
 List<HttpHeader<? extends Serializable>> getHeaders(boolean gzipped)
           
 long getLastModified()
          Returns the last modified date for this resource.
 int getStatusCode()
           
 byte[] getUngzippedBody()
          Returns the ungzipped content.
 boolean hasContent()
           
 boolean hasGzippedBody()
           
 boolean isOK()
          Returns true if the response is OK (200).
static long toLastModified(String etag)
          Returns the last modified date form the etag.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StaticResource

public StaticResource(int status,
                      String contentType,
                      byte[] body,
                      long modified,
                      String cacheControl,
                      long expires)
               throws IOException
Creates a PageInfo object representing the "page".

Parameters:
status - The HTTP status code of the response
contentType - The content type
body - The body in bytes to store
modified - The last modified date of the resource.
cacheControl - The cache control header for this static resource
expires - When this resource expires
Throws:
IOException - If the content was already gzipped
Method Detail

getContentType

public String getContentType()
Specified by:
getContentType in interface CachedResource
Returns:
the content type of the response.

getStatusCode

public int getStatusCode()
Specified by:
getStatusCode in interface CachedResource
Returns:
the HTTP status code of the response.

getGzippedBody

public byte[] getGzippedBody()
Returns the gzip content if stored as such.

Returns:
the gzipped version of the body if the content is stores gzipped or null

getUngzippedBody

public byte[] getUngzippedBody()
                        throws IOException
Returns the ungzipped content.

If the content is stored gzipped, this method will unzip the content on demand.

Returns:
the ungzipped version of the body.
Throws:
IOException - if thrown whil ungzippind the content.

getBody

public byte[] getBody(boolean gzipped)
               throws IOException
Description copied from interface: CachedResource
Returns the body content as bytes.

If the content is gzippable, this method will unzip the content on demand.

If the content is not gzippable, this method will trown a

Specified by:
getBody in interface CachedResource
Parameters:
gzipped - true to request the gzipped content; false to get the raw content.
Returns:
the ungzipped version of the body.
Throws:
IOException - if thrown while ungzipping the content.

hasContent

public boolean hasContent()
Specified by:
hasContent in interface CachedResource
Returns:
true if the body is not null and would not result in a 0-length content.

hasGzippedBody

public boolean hasGzippedBody()
Specified by:
hasGzippedBody in interface CachedResource
Returns:
true if there is a non null gzipped body

isOK

public boolean isOK()
Returns true if the response is OK (200).

Specified by:
isOK in interface CachedResource
Returns:
true if the status code is 200; false for any other code.

getLastModified

public long getLastModified()
Returns the last modified date for this resource.

Specified by:
getLastModified in interface CachedResource
Returns:
the last modified date if there is an "Last-Modified" header defined; -1 otherwise.

getETag

public String getETag(boolean gzipped)
Description copied from interface: CachedResource
Returns the etag for this resource.

Specified by:
getETag in interface CachedResource
Parameters:
gzipped - true to get the etag for a gzipped content; false to get the etag for raw content.
Returns:
the etag if there is an "Etag" header defined; null otherwise.

getHeaders

public List<HttpHeader<? extends Serializable>> getHeaders(boolean gzipped)
Specified by:
getHeaders in interface CachedResource
Parameters:
gzipped - true if the content is sent gzipped; false otherwise.
Returns:
All of the headers set on the page.

copyHeadersTo

public void copyHeadersTo(javax.servlet.http.HttpServletResponse res,
                          boolean gzipped)
Description copied from interface: CachedResource
Copy the headers to the HTTP servlet response.

Specified by:
copyHeadersTo in interface CachedResource
Parameters:
res - The HTTP servlet response where the headers should be copied.
gzipped - true if the content is sent gzipped; false otherwise.

toLastModified

public static long toLastModified(String etag)
Returns the last modified date form the etag.

Parameters:
etag - the etag used
Returns:
the last modified date (in seconds)