001: /******************************************************************************
002: * ResponderHTML.java
003: * ****************************************************************************/package org.openlaszlo.servlets.responders;
004:
005: import java.io.*;
006: import java.net.URLEncoder;
007: import java.util.Enumeration;
008: import java.util.Properties;
009: import javax.servlet.ServletConfig;
010: import javax.servlet.ServletException;
011: import javax.servlet.ServletOutputStream;
012: import javax.servlet.http.HttpSession;
013: import javax.servlet.http.HttpServletRequest;
014: import javax.servlet.http.HttpServletResponse;
015: import org.openlaszlo.compiler.Canvas;
016: import org.openlaszlo.compiler.CompilationError;
017: import org.openlaszlo.utils.*;
018: import org.apache.log4j.Logger;
019:
020: public final class ResponderHTML extends ResponderCompile {
021: private static Logger mLogger = Logger
022: .getLogger(ResponderHTML.class);
023:
024: /**
025: * @param fileName Full pathname to file from request.
026: */
027: protected void respondImpl(String fileName, HttpServletRequest req,
028: HttpServletResponse res) throws IOException {
029: mLogger.info(
030: /* (non-Javadoc)
031: * @i18n.test
032: * @org-mes="Responding with HTML wrapper for " + p[0]
033: */
034: org.openlaszlo.i18n.LaszloMessages.getMessage(
035: ResponderHTML.class.getName(), "051018-43",
036: new Object[] { fileName }));
037: res.setContentType("text/html");
038: ServletOutputStream out = res.getOutputStream();
039: try {
040: // Get the canvas first, so that if this fails and we
041: // write the compilation error, nothing has been written
042: // to out yet.
043: if (fileName.endsWith(".lzo")) {
044: fileName = fileName.substring(0, fileName.length() - 1)
045: + "x";
046: }
047: Canvas canvas = getCanvas(fileName, req);
048: /* This method doesn't call writeHeader and writeFooter, since
049: * the stylesheet handles the whole HTML generation. */
050: //writeHeader(out, canvas);
051: writeCanvas(out, req, canvas, fileName);
052: //writeFooter(out);
053: } catch (CompilationError e) {
054: out.print("<pre>\n" + e.toHTML() + "\n<pre>");
055: } finally {
056: FileUtils.close(out);
057: }
058: }
059:
060: /**
061: * Writes the canvas html. The canvas is the area in which the
062: * Laszlo application is rendered.
063: * @param out <tt>ServletOutputStream</tt> to write on
064: * @param req request to retrieve scheme, server name, server port and
065: * requested url
066: * @param canvas the canvas for the given request
067: * @param fileName file for the app
068: */
069: private void writeCanvas(ServletOutputStream out,
070: HttpServletRequest req, Canvas canvas, String fileName)
071: throws IOException {
072: String styleSheetPathname = org.openlaszlo.server.LPS
073: .getTemplateDirectory()
074: + File.separator + "html-response.xslt";
075: String xmlString = canvas.getXML(ResponderAPP_CONSOLE
076: .getRequestXML(req, fileName));
077: Properties properties = new Properties();
078: TransformUtils.applyTransform(styleSheetPathname, properties,
079: xmlString, out);
080: }
081:
082: /**
083: * Return all query args except for "lzt"
084: */
085: private String getQueryArgs(HttpServletRequest req) {
086: StringBuffer query = new StringBuffer();
087: Enumeration e = req.getParameterNames();
088: while (e.hasMoreElements()) {
089: String name = (String) e.nextElement();
090: String val = req.getParameter(name);
091: if (!name.equals("lzt")) {
092: query.append("&" + name + "=" + URLEncoder.encode(val));
093: }
094: }
095: return query.toString();
096: }
097:
098: public int getMimeType() {
099: return MIME_TYPE_HTML;
100: }
101: }
|