1 4 5 9 10 package org.openlaszlo.servlets.responders; 11 12 import java.io.*; 13 import java.net.URLEncoder ; 14 import java.util.*; 15 import javax.servlet.ServletConfig ; 16 import javax.servlet.ServletException ; 17 import javax.servlet.ServletOutputStream ; 18 import javax.servlet.http.HttpSession ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import org.openlaszlo.compiler.Canvas; 22 import org.openlaszlo.compiler.CompilationError; 23 import org.openlaszlo.server.LPS; 24 import org.openlaszlo.utils.*; 25 import org.openlaszlo.xml.internal.XMLUtils; 26 import org.apache.log4j.Logger; 27 28 public final class ResponderAPP_CONSOLE extends ResponderCompile 29 { 30 private static Logger mLogger = Logger.getLogger(ResponderAPP_CONSOLE.class); 31 private static String sStyleSheetPathname = 32 org.openlaszlo.server.LPS.getTemplateDirectory() + 33 File.separator + "app-console.xslt"; 34 35 38 protected void respondImpl(String fileName, HttpServletRequest req, 39 HttpServletResponse res) 40 throws IOException 41 { 42 mLogger.info("Responding with HTML wrapper for " + fileName); 43 res.setContentType("text/html"); 44 ServletOutputStream out = res.getOutputStream(); 45 try { 46 boolean isKranked = false; 51 String orig = fileName; 52 if (fileName.endsWith(".lzo")) { 53 isKranked = true; 54 fileName = fileName.substring(0, fileName.length() - 1) + "x"; 55 } 56 58 Canvas canvas = getCanvas(fileName, req); 59 writeCanvas(out, req, canvas, orig, isKranked); 60 } finally { 61 FileUtils.close(out); 62 } 63 } 64 65 static public String getRequestXML(HttpServletRequest req, String fileName) 66 throws IOException 67 { 68 String lps = req.getContextPath(); 69 String agent = req.getHeader("user-agent"); 70 String os = req.getHeader("ua-os"); 72 String query_args = getQueryArgs(req); 73 String url = req.getRequestURI(); 74 int i = url.lastIndexOf("/"); 75 url = url.substring(i + 1, url.length() ); 76 String unopturl = url.substring(0, url.length() - 4) + ".lzx"; 77 String opturl = url.substring(0, url.length() - 4) + ".lzo"; 78 boolean isPocketPC = (os != null && os.indexOf("POCKET PC") > -1); 79 boolean isWin = (agent != null && agent.toLowerCase().indexOf("win") > -1); 80 81 Properties props = initCMgrProperties(req); 82 83 StringBuffer buffer = new StringBuffer (); 84 buffer.append( 85 "<request " + 86 "lps=\"" + XMLUtils.escapeXml(lps) + "\" " + 87 "url=\"" + XMLUtils.escapeXml(url) + "\" " + 88 "fullpath=\"" + XMLUtils.escapeXml(req.getRequestURI()) + "\" " + 89 "opt-url=\"" + XMLUtils.escapeXml(opturl) + "\" " + 90 "unopt-url=\"" + XMLUtils.escapeXml(unopturl) + "\" " + 91 "query_args=\"" + XMLUtils.escapeXml(query_args) + "\" " + 92 "pocketpc=\"" + isPocketPC + "\" " + 93 "windows=\"" + isWin + "\" "); 94 if (fileName != null) { 95 String optname = fileName.substring(0, fileName.length() - 4) + ".lzo"; 96 buffer.append( 97 "opt-exists=\"" + new File(optname).exists() + "\" "); 98 } 99 buffer.append( 100 ">\n"); 101 102 for (Enumeration e = req.getParameterNames(); e.hasMoreElements(); ) { 103 String name = (String )e.nextElement(); 104 String value = req.getParameter(name); 105 buffer.append("<param name=\"" + XMLUtils.escapeXml(name) + "\" "); 106 buffer.append("value=\"" + XMLUtils.escapeXml(value) + "\"/>\n"); 107 } 108 buffer.append("</request>"); 109 110 String infoXML = ""; 111 if (fileName != null) { 112 boolean isOpt = fileName.endsWith(".lzo"); 113 if (isOpt) { 114 infoXML = getInfoXML(fileName, props); 115 } else { 116 infoXML = getCompilationManager().getInfoXML(fileName, props); 117 } 118 } 119 120 buffer.append(infoXML); 121 122 return buffer.toString(); 123 } 124 125 128 private static String getInfoXML(String fileName, Properties props) 129 throws IOException 130 { 131 String enc = props.getProperty(LZHttpUtils.CONTENT_ENCODING); 132 String zippedSize = null; 133 String unzippedSize = null; 134 String size = null; 135 136 try { 137 unzippedSize = "" + FileUtils.fileSize(new File(fileName)); 138 } catch (FileNotFoundException e) { 139 mLogger.error(e.getMessage()); 140 } 141 142 { 143 String gzName = fileName + ".gz"; 144 File f = new File(gzName); 145 File nongz = new File(fileName); 148 if (nongz.exists()) { 149 if (!f.exists() || f.lastModified() < nongz.lastModified()) { 150 FileUtils.encode(nongz, f, "gzip"); 151 f = new File(gzName); 152 } 153 } 154 try { 155 zippedSize = "" + FileUtils.fileSize(f); 156 } catch (FileNotFoundException e) { 157 mLogger.error(e.getMessage()); 158 } 159 } 160 161 size = ("gzip".equals(enc)) ? zippedSize : unzippedSize; 162 163 return "<info size=\"" + size + 164 "\" zipped-size=\"" + zippedSize + 165 "\" unzipped-size=\"" + unzippedSize + 166 "\" encoding=\"" + enc + 167 "\" />"; 168 } 169 170 protected void handleCompilationError(CompilationError e, 171 HttpServletRequest req, 172 HttpServletResponse res) 173 throws IOException 174 { 175 respondCompilationError(e, req, res); 176 } 177 178 public static void respondCompilationError(CompilationError e, 179 HttpServletRequest req, 180 HttpServletResponse res) 181 throws IOException 182 { 183 res.setContentType("text/html"); 184 ServletOutputStream out = res.getOutputStream(); 185 String xmlString = 186 "<errors>" + 187 getRequestXML(req, null) + 188 e.toXML() + 189 "</errors>"; 190 try { 191 TransformUtils.applyTransform(sStyleSheetPathname, xmlString, out); 192 } catch (Exception ex) { 193 reportTransformException(req, sStyleSheetPathname, ex, out); 194 } 195 } 196 197 205 private void writeCanvas(ServletOutputStream out, HttpServletRequest req, 206 Canvas canvas, String fileName, boolean isKranked) 207 throws IOException 208 { 209 String xmlString = canvas.getXML(getRequestXML(req, fileName)); 210 mLogger.debug(xmlString); 211 Properties properties = new Properties(); 212 if (isKranked) 213 properties.setProperty("isKranked", "true"); 214 try { 215 TransformUtils.applyTransform(sStyleSheetPathname, properties, 216 xmlString, out); 217 } catch (Exception e) { 218 reportTransformException(req, sStyleSheetPathname, e, out); 219 } 220 } 221 222 226 protected static void reportTransformException( 227 HttpServletRequest req, 228 String styleSheetPathname, 229 Exception e, 230 OutputStream out) 231 throws IOException 232 { 233 242 final String XALAN_ERR_MSG = 244 "output format must have a '{http://xml.apache.org/xalan}content-handler' property"; 245 246 Writer writer = new OutputStreamWriter(out); 247 try { 248 String title = "500 Internal Server Error"; 249 writer.write("<html><title>"); 250 writer.write(title); 251 writer.write("</title>"); 252 writer.write("<style type='text/css'>"); 253 writer.write("table th {text-align: right; vertical-align: top; padding-right: 10pt}"); 254 writer.write("</style>"); 255 writer.write("</head><body><h1>"); 256 writer.write(title); 257 writer.write("</h1>"); 258 writer.write("<p>An internal server error occurred. "); 259 if (e.toString().indexOf(XALAN_ERR_MSG) >= 0) { 263 writer.write("If you are running Tomcat, please verify that "); 264 writer.write("that the <code>xalan.jar</code> file from the "); 265 writer.write("LPS distribution has been installed in the "); 266 writer.write("Tomcat <code>/common/endorsed</code> directory. "); 267 writer.write("Otherwise, please check the "); 268 writer.write("<a HREF='http://www.laszlosystems.com/developers/learn/documentation/faq/'>"); 269 writer.write("LPS Developer FAQ</a> for additional "); 270 writer.write("instructions. If that does not work, please "); 271 } else { 272 writer.write("Please "); 273 } 274 writer.write("send a copy of this web page and a "); 275 writer.write("description of your server environment (operating "); 276 writer.write("system, JRE version, servlet container and "); 277 writer.write("version) to "); 278 writer.write("<a HREF='mailto:bugs@laszlosystems.com'>"); 279 writer.write("bugs@laszlosystems.com</a>.</p>"); 280 writer.write("<hr /><table border='0'>"); 281 writer.write("<tr><th>Version</th>"); 286 writer.write("<td>" + LPS.getShortVersion() + "</td></tr>"); 287 writer.write("<tr><th>Build</th>"); 288 writer.write("<td>" + LPS.getBuildDate() + "</td></tr>"); 289 writer.write("<tr><th>Stylesheet</th>"); 290 writer.write("<td>" + new File(styleSheetPathname).getName() + "</td></tr>"); 291 writer.write("<tr><th>Message</th>"); 292 writer.write("<td>" + e.getMessage() + "</td></tr>"); 293 writer.write("</table><pre>"); 294 writer.flush(); 295 PrintStream ps = new PrintStream(out); 296 try { 297 e.printStackTrace(ps); 298 } finally { 299 ps.flush(); 300 } 301 writer.write("</pre></body></html>"); 302 } finally { 303 writer.close(); 304 } 305 } 306 307 310 private static String getQueryArgs(HttpServletRequest req) { 311 StringBuffer query = new StringBuffer (); 312 Enumeration e = req.getParameterNames(); 313 while (e.hasMoreElements()) { 314 String name = (String )e.nextElement(); 315 String val = req.getParameter(name); 316 if (!name.equals("lzt")) { 317 query.append("&"+name+"="+URLEncoder.encode(val)); 318 } 319 } 320 return query.toString(); 321 } 322 323 public int getMimeType() 324 { 325 return MIME_TYPE_HTML; 326 } 327 } 328 | Popular Tags |