001: /******************************************************************************
002: * ResponderOBJECT.java
003: * ****************************************************************************/package org.openlaszlo.servlets.responders;
004:
005: import java.io.*;
006: import java.net.URL;
007: import java.util.Hashtable;
008: import java.util.Properties;
009: import javax.servlet.ServletConfig;
010: import javax.servlet.ServletException;
011: import javax.servlet.ServletOutputStream;
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014: import org.openlaszlo.media.MimeType;
015: import org.openlaszlo.utils.FileUtils;
016: import org.openlaszlo.utils.LZHttpUtils;
017: import org.openlaszlo.utils.StringUtils;
018: import org.openlaszlo.server.LPS;
019: import org.openlaszlo.compiler.CompilationError;
020:
021: import org.openlaszlo.utils.ChainedException;
022:
023: import org.apache.log4j.Logger;
024:
025: public final class ResponderOBJECT extends ResponderCompile {
026: private static Logger mLogger = Logger
027: .getLogger(ResponderOBJECT.class);
028:
029: private Object mKrankEncodingLock = new Object();
030:
031: public void init(String reqName, ServletConfig config,
032: Properties prop) throws ServletException, IOException {
033: super .init(reqName, config, prop);
034: }
035:
036: /**
037: * @param fileName Full pathname to file from request.
038: */
039: protected void respondImpl(String fileName, HttpServletRequest req,
040: HttpServletResponse res) {
041: ServletOutputStream output = null;
042: InputStream input = null;
043:
044: // Compile the file and send it out
045: try {
046: mLogger.info(
047: /* (non-Javadoc)
048: * @i18n.test
049: * @org-mes="Requesting object for " + p[0]
050: */
051: org.openlaszlo.i18n.LaszloMessages.getMessage(
052: ResponderSWF.class.getName(), "051018-60",
053: new Object[] { fileName }));
054:
055: output = res.getOutputStream();
056: Properties props = initCMgrProperties(req);
057: String encoding = props
058: .getProperty(LZHttpUtils.CONTENT_ENCODING);
059:
060: String runtime = req.getParameter("lzr");
061: if (runtime == null) {
062: runtime = LPS.getRuntimeDefault();
063: }
064:
065: input = mCompMgr.getObjectStream(fileName, props);
066:
067: long total = input.available();
068: // Set length header before writing content. WebSphere
069: // requires this.
070: // Ok to cast to int because SWF file must be a 32bit file
071: res.setContentLength((int) total);
072: if (runtime.startsWith("swf")) {
073: res.setContentType(MimeType.JS);
074: } else {
075: res.setContentType(MimeType.JS);
076: }
077:
078: if (encoding != null) {
079: res.setHeader(LZHttpUtils.CONTENT_ENCODING, encoding);
080: }
081:
082: try {
083: total = 0;
084: total = FileUtils.sendToStream(input, output);
085: } catch (FileUtils.StreamWritingException e) {
086: // This should be the client hanging up on us.
087: mLogger.warn(
088: /* (non-Javadoc)
089: * @i18n.test
090: * @org-mes="StreamWritingException while sending SWF: " + p[0]
091: */
092: org.openlaszlo.i18n.LaszloMessages.getMessage(
093: ResponderSWF.class.getName(), "051018-130",
094: new Object[] { e.getMessage() }));
095: } catch (IOException e) {
096: mLogger.error(
097: /* (non-Javadoc)
098: * @i18n.test
099: * @org-mes="IO exception while sending SWF: "
100: */
101: org.openlaszlo.i18n.LaszloMessages.getMessage(
102: ResponderSWF.class.getName(), "051018-139"), e);
103: }
104: mLogger.info(
105: /* (non-Javadoc)
106: * @i18n.test
107: * @org-mes="Sent SWF, " + p[0] + " bytes"
108: */
109: org.openlaszlo.i18n.LaszloMessages.getMessage(
110: ResponderSWF.class.getName(), "051018-148",
111: new Object[] { new Long(total) }));
112:
113: } catch (Exception e) {
114: mLogger.error("Exception: ", e);
115: StringWriter s = new StringWriter();
116: PrintWriter p = new PrintWriter(s);
117: e.printStackTrace(p);
118: respondWithMessageSWF(res, s.toString());
119: } finally {
120: FileUtils.close(input);
121: FileUtils.close(output);
122: }
123: }
124:
125: public int getMimeType(HttpServletRequest req) {
126: String runtime = req.getParameter("lzr");
127: if (runtime != null && runtime.startsWith("swf")) {
128: return MIME_TYPE_SWF;
129: } else {
130: return MIME_TYPE_HTML;
131: }
132: }
133:
134: public int getMimeType() {
135: return MIME_TYPE_HTML;
136: }
137:
138: protected void handleCompilationError(CompilationError e,
139: HttpServletRequest req, HttpServletResponse res)
140: throws IOException {
141: String runtime = req.getParameter("lzr");
142: if (runtime != null && runtime.startsWith("swf")) {
143: respondWithMessageSWF(res, e.getMessage());
144: } else {
145: respondWithErrorHTML(res, e.getMessage());
146: }
147:
148: }
149: }
|