KickJava   Java API By Example, From Geeks To Geeks.

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


1 /******************************************************************************
2  * TemplateResponder.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.util.*;
14 import java.net.URLEncoder JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Properties JavaDoc;
17 import javax.servlet.ServletConfig JavaDoc;
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.ServletOutputStream JavaDoc;
20 import javax.servlet.http.HttpSession JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
23 import org.openlaszlo.compiler.Canvas;
24 import org.openlaszlo.compiler.CompilationError;
25 import org.openlaszlo.utils.*;
26 import org.apache.log4j.Logger;
27
28 public final class TemplateResponder extends ResponderCompile
29 {
30     private static Logger mLogger = Logger.getLogger(TemplateResponder.class);
31
32     /** {lzt -> Responder} */
33     private static Map mResponderMap = new HashMap();
34
35     public static Responder getResponder(String JavaDoc lzt) {
36         String JavaDoc pathname =
37             org.openlaszlo.server.LPS.getTemplateDirectory() +
38             File.separator + lzt + "-response.xslt";
39         synchronized (mResponderMap) {
40             Responder responder = (Responder) mResponderMap.get(pathname);
41             if (responder == null) {
42                 if (new File(pathname).exists())
43                     responder = new TemplateResponder(pathname);
44                 mLogger.debug("getResponder(" + lzt + ") -> " + responder);
45                 mResponderMap.put(pathname, responder);
46             }
47             return responder;
48         }
49     }
50     
51     protected final String JavaDoc mTemplatePathname;
52     
53     protected TemplateResponder(String JavaDoc templatePathname) {
54         this.mTemplatePathname = templatePathname;
55     }
56     
57     /**
58      * @param fileName Full pathname to file from request.
59      */

60     protected void respondImpl(String JavaDoc fileName, HttpServletRequest JavaDoc req,
61                                HttpServletResponse JavaDoc res)
62         throws IOException
63     {
64         mLogger.info("Responding with HTML wrapper for " + fileName);
65         res.setContentType("text/html");
66         ServletOutputStream JavaDoc out = res.getOutputStream();
67         try {
68             // Get the canvas first, so that if this fails and we
69
// write the compilation error, nothing has been written
70
// to out yet.
71
if (fileName.endsWith(".lzo")) {
72                 fileName = fileName.substring(0, fileName.length() - 1) + "x";
73             }
74             Canvas canvas = getCanvas(fileName, req);
75             writeCanvas(out, req, canvas, fileName);
76         } catch (CompilationError e) {
77             ResponderAPP_CONSOLE.respondCompilationError(e, req, res);
78         } finally {
79             FileUtils.close(out);
80         }
81     }
82
83     /**
84      * Writes the canvas html. The canvas is the area in which the
85      * Laszlo application is rendered.
86      * @param out <tt>ServletOutputStream</tt> to write on
87      * @param req request to retrieve scheme, server name, server port and
88      * requested url
89      * @param canvas the canvas for the given request
90      * @param fileName file for the app
91      */

92     private void writeCanvas (ServletOutputStream JavaDoc out, HttpServletRequest JavaDoc req,
93                               Canvas canvas, String JavaDoc fileName)
94         throws IOException
95     {
96         String JavaDoc xmlString = canvas.getXML(ResponderAPP_CONSOLE.getRequestXML(req, fileName));
97         Properties JavaDoc properties = new Properties JavaDoc();
98         TransformUtils.applyTransform(mTemplatePathname, properties,
99                                       xmlString, out);
100     }
101     
102     /**
103      * Return all query args except for "lzt"
104      */

105     private String JavaDoc getQueryArgs(HttpServletRequest JavaDoc req) {
106         StringBuffer JavaDoc query = new StringBuffer JavaDoc();
107         Enumeration JavaDoc e = req.getParameterNames();
108         while (e.hasMoreElements()) {
109             String JavaDoc name = (String JavaDoc)e.nextElement();
110             String JavaDoc val = req.getParameter(name);
111             if (!name.equals("lzt")) {
112                 query.append("&"+name+"="+URLEncoder.encode(val));
113             }
114         }
115         return query.toString();
116     }
117
118     public int getMimeType()
119     {
120         return MIME_TYPE_HTML;
121     }
122 }
123
Popular Tags