KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************************
2  * ResponderSOURCE.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.Properties JavaDoc;
15 import javax.servlet.ServletConfig JavaDoc;
16 import javax.servlet.ServletException JavaDoc;
17 import javax.servlet.ServletOutputStream JavaDoc;
18 import javax.servlet.http.HttpServletRequest JavaDoc;
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20 import org.openlaszlo.utils.FileUtils;
21 import org.openlaszlo.utils.StringUtils;
22 import org.openlaszlo.xml.internal.XMLUtils;
23 import org.apache.log4j.Logger;
24
25 public final class ResponderSOURCE extends ResponderCompile
26 {
27     private static Logger mLogger = Logger.getLogger(ResponderSOURCE.class);
28
29     /**
30      * Overridden method from ReponseCompile.
31      *
32      * @param req unused
33      */

34     protected long getLastModified(String JavaDoc fileName, HttpServletRequest JavaDoc req)
35     {
36         // We don't care about other dependencies since all we show is the
37
// top-level LZX file.
38
return new File(fileName).lastModified();
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         throws IOException
47     {
48         mLogger.info("Responding with SOURCE for " + fileName);
49         if (fileName.endsWith(".lzo")) {
50             fileName = StringUtils.setCharAt(fileName, fileName.length() - 1, 'x');
51         }
52     
53         ServletOutputStream JavaDoc out = res.getOutputStream();
54
55         try {
56             res.setContentType ("text/html");
57             writeHeader(out, null);
58             writeText(out, FileUtils.readFileString(new File(fileName)));
59             writeFooter(out);
60         } finally {
61             FileUtils.close(out);
62         }
63     }
64
65     /**
66      * Writes the Text entry
67      * @param out A PrintWriter
68      * @param text A <tt>String</tt> containing the .lzx text
69      * @param message Message(s) generated during the compilation process
70      */

71     private void writeText(ServletOutputStream JavaDoc out, String JavaDoc text)
72         throws IOException {
73         out.println ("<pre class=\"code\">");
74         // TODO: [2003-04-03 bloch] should rename SWFHTML to HTML
75
// since that's a better description.
76
out.println (XMLUtils.escapeXmlForSWFHTML(text));
77         out.println ("</pre>");
78     }
79
80
81     public int getMimeType()
82     {
83         return MIME_TYPE_HTML;
84     }
85 }
86
Popular Tags