KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > responders > ResponderSWF


1 /******************************************************************************
2  * ResponderSWF.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.servlets.responders;
11
12 import java.io.*;
13 import java.net.URL JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Properties JavaDoc;
16 import javax.servlet.ServletConfig JavaDoc;
17 import javax.servlet.ServletException JavaDoc;
18 import javax.servlet.ServletOutputStream JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21 import org.openlaszlo.media.MimeType;
22 import org.openlaszlo.utils.FileUtils;
23 import org.openlaszlo.utils.LZHttpUtils;
24 import org.openlaszlo.utils.StringUtils;
25 import org.openlaszlo.compiler.CompilationError;
26 import org.apache.log4j.Logger;
27
28 public final class ResponderSWF extends ResponderCompile
29 {
30     private static Logger mLogger = Logger.getLogger(ResponderSWF.class);
31
32     private Object JavaDoc mKrankEncodingLock = new Object JavaDoc();
33
34     public void init(String JavaDoc reqName, ServletConfig JavaDoc config, Properties JavaDoc prop)
35         throws ServletException JavaDoc, IOException
36     {
37         super.init(reqName, config, prop);
38     }
39
40
41     /**
42      * @param fileName Full pathname to file from request.
43      */

44     protected void respondImpl(String JavaDoc fileName, HttpServletRequest JavaDoc req,
45                                HttpServletResponse JavaDoc res)
46     {
47         ServletOutputStream JavaDoc output = null;
48         InputStream input = null;
49
50         // Is this a request for an optimized file?
51
boolean opt = fileName.endsWith(".lzo");
52
53         // Compile the file and send it out
54
try {
55             mLogger.info("Requesting object for " + fileName);
56
57             output = res.getOutputStream();
58             Properties JavaDoc props = initCMgrProperties(req);
59             String JavaDoc encoding = props.getProperty(LZHttpUtils.CONTENT_ENCODING);
60
61             if (opt) {
62                 String JavaDoc objName = fileName;
63                 File obj = new File(objName);
64                 objName += ".gz";
65                 File gz = new File(objName);
66                 // TODO: [2004-03-12 bloch] When we move to 1.4, we could use
67
// per-file locking (java.io.FileLock) to avoid the global lock here.
68
synchronized (mKrankEncodingLock) {
69                     if (encoding != null && encoding.equals("gzip")) {
70                         // Make sure gz is uptodate with obj
71
if (!gz.exists() || gz.lastModified() < obj.lastModified()) {
72                             mLogger.info("Encoding into " + objName);
73                             FileUtils.encode(obj, gz, "gzip");
74                         }
75                         input = new FileInputStream(objName);
76                     } else {
77                         // Simply make sure obj exists
78
if (!obj.exists()) {
79                             mLogger.info("Decoding into " + objName);
80                             FileUtils.decode(gz, obj, "gzip");
81                         }
82                         input = new FileInputStream(fileName);
83                     }
84                 }
85             } else {
86                 input = mCompMgr.getObjectStream(fileName, props);
87             }
88
89             long total = input.available();
90             // Set length header before writing content. WebSphere
91
// requires this.
92
// Ok to cast to int because SWF file must be a 32bit file
93
res.setContentLength((int)total);
94             res.setContentType(MimeType.SWF);
95             if (encoding != null) {
96                 res.setHeader(LZHttpUtils.CONTENT_ENCODING, encoding);
97             }
98
99             try {
100                 total = 0;
101                 total = FileUtils.sendToStream(input, output);
102             } catch (FileUtils.StreamWritingException e) {
103                 // This should be the client hanging up on us.
104
mLogger.warn("StreamWritingException while sending SWF: " + e.getMessage());
105             } catch (IOException e) {
106                 mLogger.error("IO exception while sending SWF: ", e);
107             }
108             mLogger.info("Sent SWF, " + total + " bytes");
109
110         } catch (Exception JavaDoc e) {
111             mLogger.error("Exception: ", e);
112             StringWriter s = new StringWriter();
113             PrintWriter p = new PrintWriter(s);
114             e.printStackTrace(p);
115             respondWithMessageSWF (res, s.toString());
116         } finally {
117             FileUtils.close(input);
118             FileUtils.close(output);
119         }
120     }
121
122     public int getMimeType()
123     {
124         return MIME_TYPE_SWF;
125     }
126
127     protected void handleCompilationError(CompilationError e,
128                                           HttpServletRequest JavaDoc req,
129                                           HttpServletResponse JavaDoc res)
130         throws IOException
131     {
132         respondWithMessageSWF(res, e.getMessage());
133     }
134 }
135
Popular Tags