1 4 5 9 10 package org.openlaszlo.servlets.responders; 11 12 import java.io.*; 13 import java.util.*; 14 import java.net.URLEncoder ; 15 import java.util.Enumeration ; 16 import java.util.Properties ; 17 import javax.servlet.ServletConfig ; 18 import javax.servlet.ServletException ; 19 import javax.servlet.ServletOutputStream ; 20 import javax.servlet.http.HttpSession ; 21 import javax.servlet.http.HttpServletRequest ; 22 import javax.servlet.http.HttpServletResponse ; 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 33 private static Map mResponderMap = new HashMap(); 34 35 public static Responder getResponder(String lzt) { 36 String 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 mTemplatePathname; 52 53 protected TemplateResponder(String templatePathname) { 54 this.mTemplatePathname = templatePathname; 55 } 56 57 60 protected void respondImpl(String fileName, HttpServletRequest req, 61 HttpServletResponse res) 62 throws IOException 63 { 64 mLogger.info("Responding with HTML wrapper for " + fileName); 65 res.setContentType("text/html"); 66 ServletOutputStream out = res.getOutputStream(); 67 try { 68 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 92 private void writeCanvas (ServletOutputStream out, HttpServletRequest req, 93 Canvas canvas, String fileName) 94 throws IOException 95 { 96 String xmlString = canvas.getXML(ResponderAPP_CONSOLE.getRequestXML(req, fileName)); 97 Properties properties = new Properties (); 98 TransformUtils.applyTransform(mTemplatePathname, properties, 99 xmlString, out); 100 } 101 102 105 private String getQueryArgs(HttpServletRequest req) { 106 StringBuffer query = new StringBuffer (); 107 Enumeration e = req.getParameterNames(); 108 while (e.hasMoreElements()) { 109 String name = (String )e.nextElement(); 110 String 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 |