01: /******************************************************************************
02: * ResponderJSCRIPT.java
03: * ****************************************************************************/package org.openlaszlo.servlets.responders;
04:
05: import java.io.*;
06: import java.net.URL;
07: import java.util.Hashtable;
08: import java.util.Properties;
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.media.MimeType;
15: import org.openlaszlo.utils.FileUtils;
16: import org.openlaszlo.utils.LZHttpUtils;
17: import org.openlaszlo.utils.StringUtils;
18: import org.openlaszlo.compiler.CompilationError;
19: import org.apache.log4j.Logger;
20:
21: public final class ResponderJSCRIPT extends ResponderCompile {
22: private static Logger mLogger = Logger
23: .getLogger(ResponderJSCRIPT.class);
24:
25: private Object mKrankEncodingLock = new Object();
26:
27: public void init(String reqName, ServletConfig config,
28: Properties prop) throws ServletException, IOException {
29: super .init(reqName, config, prop);
30: }
31:
32: /**
33: * @param fileName Full pathname to file from request.
34: */
35: protected void respondImpl(String fileName, HttpServletRequest req,
36: HttpServletResponse res) {
37: ServletOutputStream output = null;
38: InputStream input = null;
39:
40: // Compile the file to script and send it out
41: try {
42: mLogger.info("Requesting javascript for " + fileName);
43:
44: output = res.getOutputStream();
45: Properties props = initCMgrProperties(req);
46: String encoding = props
47: .getProperty(LZHttpUtils.CONTENT_ENCODING);
48:
49: input = mCompMgr.getScriptStream(fileName, props);
50:
51: long total = input.available();
52: // Set length header before writing content. WebSphere
53: // requires this.
54: // Ok to cast to int because SWF file must be a 32bit file
55: res.setContentLength((int) total);
56: res.setContentType(MimeType.JS);
57: if (encoding != null) {
58: res.setHeader(LZHttpUtils.CONTENT_ENCODING, encoding);
59: }
60:
61: try {
62: total = 0;
63: total = FileUtils.sendToStream(input, output);
64: } catch (FileUtils.StreamWritingException e) {
65: // This should be the client hanging up on us.
66: mLogger
67: .warn("StreamWritingException while sending javascript: "
68: + e.getMessage());
69: } catch (IOException e) {
70: mLogger.error(
71: "IO exception while sending javascript: ", e);
72: }
73: mLogger.info("Sent javascript, " + total + " bytes");
74:
75: } catch (Exception e) {
76: mLogger.error("Exception: ", e);
77: StringWriter s = new StringWriter();
78: PrintWriter p = new PrintWriter(s);
79: e.printStackTrace(p);
80: respondWithErrorHTML(res, s.toString());
81: } finally {
82: FileUtils.close(input);
83: FileUtils.close(output);
84: }
85: }
86:
87: public int getMimeType() {
88: return MIME_TYPE_HTML;
89: }
90:
91: protected void handleCompilationError(CompilationError e,
92: HttpServletRequest req, HttpServletResponse res)
93: throws IOException {
94: respondWithErrorHTML(res, e.getMessage());
95: }
96: }
|