01: /******************************************************************************
02: * ResponderLFC.java
03: * ****************************************************************************/package org.openlaszlo.servlets.responders;
04:
05: import java.io.*;
06: import java.net.URL;
07: import java.util.Properties;
08: import java.util.Arrays;
09: import javax.servlet.ServletConfig;
10: import javax.servlet.ServletException;
11: import javax.servlet.ServletOutputStream;
12: import javax.servlet.http.HttpServletRequest;
13: import javax.servlet.http.HttpServletResponse;
14: import org.openlaszlo.utils.LZHttpUtils;
15: import org.openlaszlo.compiler.CompilationEnvironment;
16: import org.openlaszlo.utils.FileUtils;
17: import org.openlaszlo.utils.StringUtils;
18: import org.openlaszlo.xml.internal.XMLUtils;
19: import org.openlaszlo.server.LPS;
20: import org.apache.log4j.Logger;
21:
22: /* Sends the requested version of the LFC runtime library.
23: * query args: lzr=dhtml|swf8|...
24: * debug=(Non null value)
25: */
26:
27: public final class ResponderLFC extends Responder {
28: private static Logger mLogger = Logger
29: .getLogger(ResponderLFC.class);
30:
31: private boolean notModified(long lastModified,
32: HttpServletRequest req, HttpServletResponse res)
33: throws IOException {
34: if (lastModified != 0) {
35:
36: String lms = LZHttpUtils.getDateString(lastModified);
37:
38: // Check last-modified and if-modified-since dates
39: String ims = req.getHeader(LZHttpUtils.IF_MODIFIED_SINCE);
40: long ifModifiedSince = LZHttpUtils.getDate(ims);
41:
42: if (ifModifiedSince != -1) {
43: if (lastModified <= ifModifiedSince) {
44: res.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
45: return true;
46: }
47: }
48:
49: res.setHeader(LZHttpUtils.LAST_MODIFIED, lms);
50: }
51:
52: return false;
53: }
54:
55: /**
56: */
57: protected void respondImpl(HttpServletRequest req,
58: HttpServletResponse res) throws IOException {
59: ServletOutputStream out = res.getOutputStream();
60:
61: String lfc = LPS
62: .getLFCname(
63: req.getParameter("lzr"),
64: "true"
65: .equals(req
66: .getParameter(CompilationEnvironment.DEBUG_PROPERTY))
67: || req.getParameter("_canvas_debug") != null,
68: "true"
69: .equals(req
70: .getParameter(CompilationEnvironment.PROFILE_PROPERTY)),
71: "true"
72: .equals(req
73: .getParameter(CompilationEnvironment.BACKTRACE_PROPERTY)));
74: String path = LPS.getLFCDirectory();
75:
76: File lfcfile = new File(path, lfc);
77:
78: long lastModified = lfcfile.lastModified();
79: // Round to the nearest second.
80: lastModified = ((lastModified + 500L) / 1000L) * 1000L;
81: if (notModified(lastModified, req, res)) {
82: return;
83: }
84:
85: try {
86: res.setContentType("application/x-javascript");
87:
88: String content = FileUtils.readFileString(lfcfile);
89: out.println(content);
90: out.flush();
91: } finally {
92: FileUtils.close(out);
93: }
94: }
95:
96: public int getMimeType() {
97: return MIME_TYPE_HTML;
98: }
99: }
|