001: /******************************************************************************
002: * ResponderSWF.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.compiler.CompilationError;
019: import org.apache.log4j.Logger;
020:
021: public final class ResponderSWF extends ResponderCompile {
022: private static Logger mLogger = Logger
023: .getLogger(ResponderSWF.class);
024:
025: private Object mKrankEncodingLock = new Object();
026:
027: public void init(String reqName, ServletConfig config,
028: Properties prop) throws ServletException, IOException {
029: super .init(reqName, config, prop);
030: }
031:
032: /**
033: * @param fileName Full pathname to file from request.
034: */
035: protected void respondImpl(String fileName, HttpServletRequest req,
036: HttpServletResponse res) {
037: ServletOutputStream output = null;
038: InputStream input = null;
039:
040: // Compile the file and send it out
041: try {
042: mLogger.info(
043: /* (non-Javadoc)
044: * @i18n.test
045: * @org-mes="Requesting object for " + p[0]
046: */
047: org.openlaszlo.i18n.LaszloMessages.getMessage(
048: ResponderSWF.class.getName(), "051018-60",
049: new Object[] { fileName }));
050:
051: output = res.getOutputStream();
052: Properties props = initCMgrProperties(req);
053: String encoding = props
054: .getProperty(LZHttpUtils.CONTENT_ENCODING);
055:
056: input = mCompMgr.getObjectStream(fileName, props);
057:
058: long total = input.available();
059: // Set length header before writing content. WebSphere
060: // requires this.
061: // Ok to cast to int because SWF file must be a 32bit file
062: res.setContentLength((int) total);
063: res.setContentType(MimeType.SWF);
064: if (encoding != null) {
065: res.setHeader(LZHttpUtils.CONTENT_ENCODING, encoding);
066: }
067:
068: try {
069: total = 0;
070: total = FileUtils.sendToStream(input, output);
071: } catch (FileUtils.StreamWritingException e) {
072: // This should be the client hanging up on us.
073: mLogger.warn(
074: /* (non-Javadoc)
075: * @i18n.test
076: * @org-mes="StreamWritingException while sending SWF: " + p[0]
077: */
078: org.openlaszlo.i18n.LaszloMessages.getMessage(
079: ResponderSWF.class.getName(), "051018-130",
080: new Object[] { e.getMessage() }));
081: } catch (IOException e) {
082: mLogger.error(
083: /* (non-Javadoc)
084: * @i18n.test
085: * @org-mes="IO exception while sending SWF: "
086: */
087: org.openlaszlo.i18n.LaszloMessages.getMessage(
088: ResponderSWF.class.getName(), "051018-139"), e);
089: }
090: mLogger.info(
091: /* (non-Javadoc)
092: * @i18n.test
093: * @org-mes="Sent SWF, " + p[0] + " bytes"
094: */
095: org.openlaszlo.i18n.LaszloMessages.getMessage(
096: ResponderSWF.class.getName(), "051018-148",
097: new Object[] { new Long(total) }));
098:
099: } catch (Exception e) {
100: mLogger.error("Exception: ", e);
101: StringWriter s = new StringWriter();
102: PrintWriter p = new PrintWriter(s);
103: e.printStackTrace(p);
104: respondWithMessageSWF(res, s.toString());
105: } finally {
106: FileUtils.close(input);
107: FileUtils.close(output);
108: }
109: }
110:
111: public int getMimeType() {
112: return MIME_TYPE_SWF;
113: }
114:
115: protected void handleCompilationError(CompilationError e,
116: HttpServletRequest req, HttpServletResponse res)
117: throws IOException {
118: respondWithMessageSWF(res, e.getMessage());
119: }
120: }
|