org.weborganic.bastille.cache.util
Class GenericResource

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

public final class GenericResource
extends Object
implements Serializable, CachedResource

A Serializable representation of a cached resource.

See Also:
Serialized Form

Constructor Summary
GenericResource(int status, String contentType, byte[] body, boolean storeGzipped, Collection<HttpHeader<? extends Serializable>> headers)
          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 hasUngzippedBody()
           
 boolean isOK()
          Returns true if the response is OK (200).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericResource

public GenericResource(int status,
                       String contentType,
                       byte[] body,
                       boolean storeGzipped,
                       Collection<HttpHeader<? extends Serializable>> headers)
                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
storeGzipped - true to store the content as compressed (for text); false otherwise (for image)
headers - The headers for this cached resource.
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.

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.

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.

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.

hasGzippedBody

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

hasUngzippedBody

public boolean hasUngzippedBody()
Returns:
true if there is a non null ungzipped body

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.

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.

copyHeadersTo

public void copyHeadersTo(javax.servlet.http.HttpServletResponse res,
                          boolean gzipped)
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 - Whether the content was sent gzipped