1 4 5 9 10 package org.openlaszlo.servlets.responders; 11 12 import java.io.*; 13 import java.net.URL ; 14 import java.util.Hashtable ; 15 import java.util.Properties ; 16 import javax.servlet.ServletConfig ; 17 import javax.servlet.ServletException ; 18 import javax.servlet.ServletOutputStream ; 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 21 import org.openlaszlo.media.MimeType; 22 import org.openlaszlo.utils.FileUtils; 23 import org.openlaszlo.utils.LZHttpUtils; 24 import org.openlaszlo.utils.StringUtils; 25 import org.openlaszlo.compiler.CompilationError; 26 import org.apache.log4j.Logger; 27 28 public final class ResponderSWF extends ResponderCompile 29 { 30 private static Logger mLogger = Logger.getLogger(ResponderSWF.class); 31 32 private Object mKrankEncodingLock = new Object (); 33 34 public void init(String reqName, ServletConfig config, Properties prop) 35 throws ServletException , IOException 36 { 37 super.init(reqName, config, prop); 38 } 39 40 41 44 protected void respondImpl(String fileName, HttpServletRequest req, 45 HttpServletResponse res) 46 { 47 ServletOutputStream output = null; 48 InputStream input = null; 49 50 boolean opt = fileName.endsWith(".lzo"); 52 53 try { 55 mLogger.info("Requesting object for " + fileName); 56 57 output = res.getOutputStream(); 58 Properties props = initCMgrProperties(req); 59 String encoding = props.getProperty(LZHttpUtils.CONTENT_ENCODING); 60 61 if (opt) { 62 String objName = fileName; 63 File obj = new File(objName); 64 objName += ".gz"; 65 File gz = new File(objName); 66 synchronized (mKrankEncodingLock) { 69 if (encoding != null && encoding.equals("gzip")) { 70 if (!gz.exists() || gz.lastModified() < obj.lastModified()) { 72 mLogger.info("Encoding into " + objName); 73 FileUtils.encode(obj, gz, "gzip"); 74 } 75 input = new FileInputStream(objName); 76 } else { 77 if (!obj.exists()) { 79 mLogger.info("Decoding into " + objName); 80 FileUtils.decode(gz, obj, "gzip"); 81 } 82 input = new FileInputStream(fileName); 83 } 84 } 85 } else { 86 input = mCompMgr.getObjectStream(fileName, props); 87 } 88 89 long total = input.available(); 90 res.setContentLength((int)total); 94 res.setContentType(MimeType.SWF); 95 if (encoding != null) { 96 res.setHeader(LZHttpUtils.CONTENT_ENCODING, encoding); 97 } 98 99 try { 100 total = 0; 101 total = FileUtils.sendToStream(input, output); 102 } catch (FileUtils.StreamWritingException e) { 103 mLogger.warn("StreamWritingException while sending SWF: " + e.getMessage()); 105 } catch (IOException e) { 106 mLogger.error("IO exception while sending SWF: ", e); 107 } 108 mLogger.info("Sent SWF, " + total + " bytes"); 109 110 } catch (Exception e) { 111 mLogger.error("Exception: ", e); 112 StringWriter s = new StringWriter(); 113 PrintWriter p = new PrintWriter(s); 114 e.printStackTrace(p); 115 respondWithMessageSWF (res, s.toString()); 116 } finally { 117 FileUtils.close(input); 118 FileUtils.close(output); 119 } 120 } 121 122 public int getMimeType() 123 { 124 return MIME_TYPE_SWF; 125 } 126 127 protected void handleCompilationError(CompilationError e, 128 HttpServletRequest req, 129 HttpServletResponse res) 130 throws IOException 131 { 132 respondWithMessageSWF(res, e.getMessage()); 133 } 134 } 135 | Popular Tags |