KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************************
2  * ResponderHTML.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.URLEncoder JavaDoc;
14 import java.util.Enumeration 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.HttpSession JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import org.openlaszlo.compiler.Canvas;
23 import org.openlaszlo.compiler.CompilationError;
24 import org.openlaszlo.utils.*;
25 import org.apache.log4j.Logger;
26
27 public final class ResponderHTML extends ResponderCompile
28 {
29     private static Logger mLogger = Logger.getLogger(ResponderHTML.class);
30
31     /**
32      * @param fileName Full pathname to file from request.
33      */

34     protected void respondImpl(String JavaDoc fileName, HttpServletRequest JavaDoc req,
35                                HttpServletResponse JavaDoc res)
36         throws IOException
37     {
38         mLogger.info("Responding with HTML wrapper for " + fileName);
39         res.setContentType ("text/html");
40         ServletOutputStream JavaDoc out = res.getOutputStream();
41         try {
42             // Get the canvas first, so that if this fails and we
43
// write the compilation error, nothing has been written
44
// to out yet.
45
if (fileName.endsWith(".lzo")) {
46                 fileName = fileName.substring(0, fileName.length() - 1) + "x";
47             }
48             Canvas canvas = getCanvas(fileName, req);
49             /* This method doesn't call writeHeader and writeFooter, since
50              * the stylesheet handles the whole HTML generation. */

51             //writeHeader(out, canvas);
52
writeCanvas(out, req, canvas, fileName);
53             //writeFooter(out);
54
} catch (CompilationError e) {
55             out.print("<pre>\n"+e.toHTML()+"\n<pre>");
56         } finally {
57             FileUtils.close(out);
58         }
59     }
60
61     /**
62      * Writes the canvas html. The canvas is the area in which the
63      * Laszlo application is rendered.
64      * @param out <tt>ServletOutputStream</tt> to write on
65      * @param req request to retrieve scheme, server name, server port and
66      * requested url
67      * @param canvas the canvas for the given request
68      * @param fileName file for the app
69      */

70     private void writeCanvas (ServletOutputStream JavaDoc out, HttpServletRequest JavaDoc req,
71                               Canvas canvas, String JavaDoc fileName)
72         throws IOException
73     {
74         String JavaDoc styleSheetPathname =
75             org.openlaszlo.server.LPS.getTemplateDirectory() +
76             File.separator + "html-response.xslt";
77         String JavaDoc xmlString = canvas.getXML(ResponderAPP_CONSOLE.getRequestXML(req, fileName));
78         Properties JavaDoc properties = new Properties JavaDoc();
79         TransformUtils.applyTransform(styleSheetPathname, properties, xmlString, out);
80     }
81     
82     /**
83      * Return all query args except for "lzt"
84      */

85     private String JavaDoc getQueryArgs(HttpServletRequest JavaDoc req) {
86         StringBuffer JavaDoc query = new StringBuffer JavaDoc();
87         Enumeration JavaDoc e = req.getParameterNames();
88         while (e.hasMoreElements()) {
89             String JavaDoc name = (String JavaDoc)e.nextElement();
90             String JavaDoc val = req.getParameter(name);
91             if (!name.equals("lzt")) {
92                 query.append("&"+name+"="+URLEncoder.encode(val));
93             }
94         }
95         return query.toString();
96     }
97
98     public int getMimeType()
99     {
100         return MIME_TYPE_HTML;
101     }
102 }
103
Popular Tags