01: package org.ztemplates.flex.impl;
02:
03: import java.io.BufferedInputStream;
04: import java.io.BufferedOutputStream;
05: import java.io.IOException;
06: import java.io.InputStream;
07: import java.io.OutputStream;
08:
09: import javax.servlet.http.HttpServletResponse;
10:
11: import org.apache.log4j.Logger;
12: import org.ztemplates.actions.ZMatch;
13: import org.ztemplates.web.ZTemplates;
14:
15: @ZMatch("/flexloader*{resourcePath}")
16: public class ZFlexLoaderAction {
17: protected static Logger log = Logger
18: .getLogger(ZFlexLoaderAction.class);
19:
20: private String resourcePath;
21:
22: private static final String PREFIX = "";
23:
24: private static final int CACHESIZE = 2048;
25:
26: public void after() throws Exception {
27: String resource = PREFIX + resourcePath;
28:
29: InputStream in = getClass().getResourceAsStream(resource);
30: if (in == null) {
31: throw new IOException("resource not found: " + resource);
32: }
33: in = new BufferedInputStream(in, CACHESIZE);
34: try {
35: HttpServletResponse resp = ZTemplates.getServletService()
36: .getResponse();
37: resp.setHeader("Cache-Control", "max-age=360000");
38: if (resourcePath.endsWith(".js")) {
39: resp.setHeader("Content-Type",
40: "application/x-javascript");
41: } else if (resourcePath.endsWith(".css")) {
42: resp.setHeader("Content-Type", "text/css");
43: }
44: resp.setDateHeader("Expires",
45: System.currentTimeMillis() + 360000);
46:
47: OutputStream out = new BufferedOutputStream(resp
48: .getOutputStream(), CACHESIZE);
49: try {
50: byte[] buff = new byte[CACHESIZE];
51: int c = in.read(buff);
52: while (c > 0) {
53: out.write(buff, 0, c);
54: c = in.read(buff);
55: }
56: } finally {
57: out.flush();
58: }
59: } finally {
60: in.close();
61: }
62: }
63:
64: public String getResourcePath() {
65: return resourcePath;
66: }
67:
68: public void setResourcePath(String resource) {
69: this.resourcePath = resource;
70: }
71: }
|