001: /******************************************************************************
002: * TemplateResponder.java
003: * ****************************************************************************/package org.openlaszlo.servlets.responders;
004:
005: import java.io.*;
006: import java.util.*;
007: import java.net.URLEncoder;
008: import java.util.Enumeration;
009: import java.util.Properties;
010: import javax.servlet.ServletConfig;
011: import javax.servlet.ServletException;
012: import javax.servlet.ServletOutputStream;
013: import javax.servlet.http.HttpSession;
014: import javax.servlet.http.HttpServletRequest;
015: import javax.servlet.http.HttpServletResponse;
016: import org.openlaszlo.compiler.Canvas;
017: import org.openlaszlo.compiler.CompilationError;
018: import org.openlaszlo.utils.*;
019: import org.apache.log4j.Logger;
020:
021: public final class TemplateResponder extends ResponderCompile {
022: private static Logger mLogger = Logger
023: .getLogger(TemplateResponder.class);
024:
025: /** {lzt -> Responder} */
026: private static Map mResponderMap = new HashMap();
027:
028: public static Responder getResponder(String lzt) {
029: String pathname = org.openlaszlo.server.LPS
030: .getTemplateDirectory()
031: + File.separator
032: + lzt
033: + org.openlaszlo.i18n.LaszloMessages.getMessage(
034: TemplateResponder.class.getName(),
035: "xsltfilename");
036:
037: /* for i18n get filename from properties file.
038: original : String pathname = org.openlaszlo.server.LPS.getTemplateDirectory() +
039: File.separator + lzt + "-response.xslt";
040: */
041:
042: synchronized (mResponderMap) {
043: Responder responder = (Responder) mResponderMap
044: .get(pathname);
045: if (responder == null) {
046: if (new File(pathname).exists())
047: responder = new TemplateResponder(pathname);
048: mLogger.debug("getResponder(" + lzt + ") -> "
049: + responder);
050: mResponderMap.put(pathname, responder);
051: }
052: return responder;
053: }
054: }
055:
056: protected final String mTemplatePathname;
057:
058: protected TemplateResponder(String templatePathname) {
059: this .mTemplatePathname = templatePathname;
060: }
061:
062: /**
063: * @param fileName Full pathname to file from request.
064: */
065: protected void respondImpl(String fileName, HttpServletRequest req,
066: HttpServletResponse res) throws IOException {
067: mLogger.info(
068: /* (non-Javadoc)
069: * @i18n.test
070: * @org-mes="Responding with HTML wrapper for " + p[0]
071: */
072: org.openlaszlo.i18n.LaszloMessages.getMessage(
073: TemplateResponder.class.getName(), "051018-69",
074: new Object[] { fileName }));
075: if ("svg".equals(req.getParameter("lzr"))
076: && "svg".equals(req.getParameter("lzt"))) {
077: res.setContentType("image/svg+xml");
078: } else {
079: res.setContentType("text/html");
080: }
081: ServletOutputStream out = res.getOutputStream();
082: try {
083: // Get the canvas first, so that if this fails and we
084: // write the compilation error, nothing has been written
085: // to out yet.
086: if (fileName.endsWith(".lzo")) {
087: fileName = fileName.substring(0, fileName.length() - 1)
088: + "x";
089: }
090: Canvas canvas = getCanvas(fileName, req);
091: writeCanvas(out, req, canvas, fileName);
092: } catch (CompilationError e) {
093: ResponderAPP_CONSOLE.respondCompilationError(e, req, res);
094: } finally {
095: FileUtils.close(out);
096: }
097: }
098:
099: /**
100: * Writes the canvas html. The canvas is the area in which the
101: * Laszlo application is rendered.
102: * @param out <tt>ServletOutputStream</tt> to write on
103: * @param req request to retrieve scheme, server name, server port and
104: * requested url
105: * @param canvas the canvas for the given request
106: * @param fileName file for the app
107: */
108: private void writeCanvas(ServletOutputStream out,
109: HttpServletRequest req, Canvas canvas, String fileName)
110: throws IOException {
111: String xmlString = canvas.getXML(ResponderAPP_CONSOLE
112: .getRequestXML(req, fileName));
113: Properties properties = new Properties();
114: TransformUtils.applyTransform(mTemplatePathname, properties,
115: xmlString, out);
116: }
117:
118: /**
119: * Return all query args except for "lzt"
120: */
121: private String getQueryArgs(HttpServletRequest req) {
122: StringBuffer query = new StringBuffer();
123: Enumeration e = req.getParameterNames();
124: while (e.hasMoreElements()) {
125: String name = (String) e.nextElement();
126: String val = req.getParameter(name);
127: if (!name.equals("lzt")) {
128: query.append("&" + name + "=" + URLEncoder.encode(val));
129: }
130: }
131: return query.toString();
132: }
133:
134: public int getMimeType() {
135: return MIME_TYPE_HTML;
136: }
137: }
|