enable resource caching

This commit is contained in:
Keith Donald
2007-11-18 14:38:44 +00:00
parent b30f8f250a
commit 560a786c09
3 changed files with 22 additions and 9 deletions

View File

@@ -21,7 +21,7 @@ import org.springframework.webflow.execution.RequestContext;
*
* @author Jeremy Grelle
*/
public class ResolveAndRenderResourceAction implements Action {
public class RenderResourceAction implements Action {
private static final String IF_MODIFIED_SINCE_HEADER = "If-Modified-Since";
@@ -29,6 +29,10 @@ public class ResolveAndRenderResourceAction implements Action {
private static final String HTTP_LAST_MODIFIED_HEADER = "Last-Modified";
private static final String HTTP_EXPIRES_HEADER = "Expires";
private static final String HTTP_CACHE_CONTROL_HEADER = "Cache-Control";
private Map<String, String> defaultMimeTypes = new HashMap<String, String>();
{
defaultMimeTypes.put(".css", "text/css");
@@ -77,10 +81,8 @@ public class ResolveAndRenderResourceAction implements Action {
response.setContentType(mimeType);
response.setHeader(HTTP_CONTENT_LENGTH_HEADER, Long.toString(resourceConn.getContentLength()));
response.setDateHeader(HTTP_LAST_MODIFIED_HEADER, lastModified);
// TODO - Should probably be setting cache and expires headers as well
configureCaching(response, 31556926);
InputStream in = resourceConn.getInputStream();
OutputStream out = response.getOutputStream();
@@ -96,4 +98,15 @@ public class ResolveAndRenderResourceAction implements Action {
}
return new Event(this, "success");
}
/**
* Set HTTP headers to allow caching for the given number of seconds.
* @param seconds number of seconds into the future that the response should be cacheable for
*/
private void configureCaching(HttpServletResponse response, int seconds) {
// HTTP 1.0 header
response.setDateHeader(HTTP_EXPIRES_HEADER, System.currentTimeMillis() + seconds * 1000L);
// HTTP 1.1 header
response.setHeader(HTTP_CACHE_CONTROL_HEADER, "max-age=" + seconds);
}
}

View File

@@ -10,7 +10,7 @@ import org.springframework.webflow.engine.builder.support.AbstractFlowBuilder;
*/
public class ResourcesFlowBuilder extends AbstractFlowBuilder {
public void buildStates() throws FlowBuilderException {
EndState endState = new EndState(getFlow(), "renderResource");
endState.setFinalResponseAction(new ResolveAndRenderResourceAction());
EndState renderResource = new EndState(getFlow(), "renderResource");
renderResource.setFinalResponseAction(new RenderResourceAction());
}
}