org.jboss.seam.web
public abstract class ConditionalAbstractResource extends AbstractResource
public class MyResource extends ConditionalAbstractResource { public void getResource(final HttpServletRequest request, final HttpServletResponse response) { String resourceVersion = ... // Calculate current state as string or byte[] resourceVersion = ... // Calculate current state as bytes String resourcePath = ... // Get the relative (to servlet) path of the requested resource if ( !sendConditional(request, response, createdEntityTag(resourceVersion, false), getLastModifiedTimestamp(resourcePath) ) { // Send the regular resource representation with 200 OK etc. } } }Note that the getLastModifiedTimestamp() method is only supplied for convenience; it may not return what you expect as the "last modification timestamp" of the given resource. In many cases you'd rather calculate that timestamp yourself.
Modifier and Type | Field and Description |
---|---|
static String |
HEADER_ETAG |
static String |
HEADER_IF_MODIFIED_SINCE |
static String |
HEADER_IF_NONE_MATCH |
static String |
HEADER_LAST_MODIFIED |
Constructor and Description |
---|
ConditionalAbstractResource() |
Modifier and Type | Method and Description |
---|---|
protected String |
createEntityTag(byte[] hashSource,
boolean weak)
Generates a (globally) unique identifier of the current state of the resource.
|
protected String |
createEntityTag(String hashSource,
boolean weak)
Generates a (globally) unique identifier of the current state of the resource.
|
protected Long |
getLastModifiedTimestamp(String resourcePath)
Tries to get last modification timestamp of the resource by obtaining
a URLConnection to the file in the filesystem or JAR.
|
protected String |
hash(byte[] bytes,
String algorithm) |
protected String |
hash(String text,
String charset,
String algorithm) |
protected boolean |
isModifiedSinceConditionValid(Long modifiedSinceHeader,
Long lastModified) |
protected boolean |
isNoneMatchConditionValid(String noneMatchHeader,
String entityTag) |
boolean |
sendConditional(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
String entityTag,
Long lastModified)
Validates the request headers If-Modified-Since and If-None-Match to determine
if a 304 NOT MODIFIED response can be send.
|
getResource, getResourcePath, getServletContext, isCompressedMimeType, isGzipEnabled, selectOutputStream, setServletContext
public static final String HEADER_LAST_MODIFIED
public static final String HEADER_IF_MODIFIED_SINCE
public static final String HEADER_ETAG
public static final String HEADER_IF_NONE_MATCH
public boolean sendConditional(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, String entityTag, Long lastModified) throws IOException
request
- The usual HttpServletRequest for header retrieval.response
- The usual HttpServletResponse for header manipulation.entityTag
- An entity tag (weak or strong, in doublequotes), typically produced by hashing the content
of the resource representation. If null, no entity tag will be send and if
validation is requested by the client, no match for a NOT MODIFIED response will be possible.lastModified
- The timestamp in number of milliseconds since unix epoch when the resource was
last modified. If null, no last modification timestamp will be send and if
validation is requested by the client, no match for a NOT MODIFIED response will be possible.IOException
- If setting the response status failed.protected boolean isNoneMatchConditionValid(String noneMatchHeader, String entityTag)
protected boolean isModifiedSinceConditionValid(Long modifiedSinceHeader, Long lastModified)
protected Long getLastModifiedTimestamp(String resourcePath)
resourcePath
- The relative (to the servlet) resource path.protected String createEntityTag(String hashSource, boolean weak)
hashSource
- The string source for hashing or the already hashed (strong or weak) entity tag.weak
- Set to true if you want a weak entity tag.protected String createEntityTag(byte[] hashSource, boolean weak)
hashSource
- The string source for hashing.weak
- Set to true if you want a weak entity tag.Copyright © 2015 Seam Framework. All Rights Reserved.