/******************************************************************************
 * ResponderSWF.java
 * ****************************************************************************/

/* J_LZ_COPYRIGHT_BEGIN *******************************************************
* Copyright 2001-2004 Laszlo Systems, Inc.  All Rights Reserved.              *
* Use is subject to license terms.                                            *
* J_LZ_COPYRIGHT_END *********************************************************/

package org.openlaszlo.servlets.responders;

import java.io.*;
import java.net.URL;
import java.util.Hashtable;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openlaszlo.media.MimeType;
import org.openlaszlo.utils.FileUtils;
import org.openlaszlo.utils.LZHttpUtils;
import org.openlaszlo.utils.StringUtils;
import org.openlaszlo.compiler.CompilationError;
import org.apache.log4j.Logger;

public final class ResponderSWF extends ResponderCompile
{
    private static Logger mLogger = Logger.getLogger(ResponderSWF.class);

    private Object mKrankEncodingLock = new Object();

    public void init(String reqName, ServletConfig config, Properties prop)
        throws ServletException, IOException
    {
        super.init(reqName, config, prop);
    }


    /**
     * @param fileName Full pathname to file from request.
     */
    protected void respondImpl(String fileName, HttpServletRequest req, 
                               HttpServletResponse res)
    {
        ServletOutputStream output = null;
        InputStream input = null;

        // Is this a request for an optimized file?
        boolean opt = fileName.endsWith(".lzo");

        // Compile the file and send it out
        try {
            mLogger.info(
/* (non-Javadoc)
 * @i18n.test
 * @org-mes="Requesting object for " + p[0]
 */
                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                                ResponderSWF.class.getName(),"051018-60", new Object[] {fileName})
);

            output = res.getOutputStream();
            Properties props = initCMgrProperties(req);
            String encoding = props.getProperty(LZHttpUtils.CONTENT_ENCODING);

            if (opt) {
                String objName = fileName;
                File obj = new File(objName);
                objName += ".gz";
                File gz = new File(objName); 
                // TODO: [2004-03-12 bloch] When we move to 1.4, we could use
                // per-file locking (java.io.FileLock) to avoid the global lock here.
                synchronized (mKrankEncodingLock) {
                    if (encoding != null && encoding.equals("gzip")) {
                        // Make sure gz is uptodate with obj
                        if (!gz.exists() || gz.lastModified() < obj.lastModified()) {
                            mLogger.info(
/* (non-Javadoc)
 * @i18n.test
 * @org-mes="Encoding into " + p[0]
 */
                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                                ResponderSWF.class.getName(),"051018-84", new Object[] {objName})
);
                            FileUtils.encode(obj, gz, "gzip");
                        }
                        input = new FileInputStream(objName);
                    } else {
                        // Simply make sure obj exists
                        if (!obj.exists()) {
                            mLogger.info(
/* (non-Javadoc)
 * @i18n.test
 * @org-mes="Decoding into " + p[0]
 */
                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                                ResponderSWF.class.getName(),"051018-98", new Object[] {objName})
);
                            FileUtils.decode(gz, obj, "gzip");
                        }
                        input = new FileInputStream(fileName);
                    }
                }
            } else {
                input = mCompMgr.getObjectStream(fileName, props);
            }

            long total = input.available();
            // Set length header before writing content.  WebSphere
            // requires this.
            // Ok to cast to int because SWF file must be a 32bit file
            res.setContentLength((int)total);
            res.setContentType(MimeType.SWF);
            if (encoding != null) {
                res.setHeader(LZHttpUtils.CONTENT_ENCODING, encoding);
            }

            try {
                total = 0;
                total = FileUtils.sendToStream(input, output);
            } catch (FileUtils.StreamWritingException e) {
                // This should be the client hanging up on us.
                mLogger.warn(
/* (non-Javadoc)
 * @i18n.test
 * @org-mes="StreamWritingException while sending SWF: " + p[0]
 */
                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                                ResponderSWF.class.getName(),"051018-130", new Object[] {e.getMessage()})
);
            } catch (IOException e) {
                mLogger.error(
/* (non-Javadoc)
 * @i18n.test
 * @org-mes="IO exception while sending SWF: "
 */
                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                                ResponderSWF.class.getName(),"051018-139")
, e);
            } 
            mLogger.info(
/* (non-Javadoc)
 * @i18n.test
 * @org-mes="Sent SWF, " + p[0] + " bytes"
 */
                        org.openlaszlo.i18n.LaszloMessages.getMessage(
                                ResponderSWF.class.getName(),"051018-148", new Object[] {new Long(total)})
);

        } catch (Exception e) {
            mLogger.error("Exception: ", e);
            StringWriter s = new StringWriter();
            PrintWriter p = new PrintWriter(s);
            e.printStackTrace(p);
            respondWithMessageSWF (res, s.toString());
        } finally {
            FileUtils.close(input);
            FileUtils.close(output);
        }
    }

    public int getMimeType()
    {
        return MIME_TYPE_SWF;
    }

    protected void handleCompilationError(CompilationError e,
                                          HttpServletRequest req,
                                          HttpServletResponse res)
        throws IOException
    {
        respondWithMessageSWF(res, e.getMessage());
    }
}

@@ -276,17 +276,17 @@
 2001-200
-4
+7
  Laszlo 
@@ -1748,110 +1748,8 @@
 l;%0A%0A
-        // Is this a request for an optimized file?%0A        boolean opt = fileName.endsWith(%22.lzo%22);%0A%0A
     
@@ -2265,1701 +2265,8 @@
 );%0A%0A
-            if (opt) %7B%0A                String objName = fileName;%0A                File obj = new File(objName);%0A                objName += %22.gz%22;%0A                File gz = new File(objName); %0A                // TODO: %5B2004-03-12 bloch%5D When we move to 1.4, we could use%0A                // per-file locking (java.io.FileLock) to avoid the global lock here.%0A                synchronized (mKrankEncodingLock) %7B%0A                    if (encoding != null && encoding.equals(%22gzip%22)) %7B%0A                        // Make sure gz is uptodate with obj%0A                        if (!gz.exists() %7C%7C gz.lastModified() %3C obj.lastModified()) %7B%0A                            mLogger.info(%0A/* (non-Javadoc)%0A * @i18n.test%0A * @org-mes=%22Encoding into %22 + p%5B0%5D%0A */%0A                        org.openlaszlo.i18n.LaszloMessages.getMessage(%0A                                ResponderSWF.class.getName(),%22051018-84%22, new Object%5B%5D %7BobjName%7D)%0A);%0A                            FileUtils.encode(obj, gz, %22gzip%22);%0A                        %7D%0A                        input = new FileInputStream(objName);%0A                    %7D else %7B%0A                        // Simply make sure obj exists%0A                        if (!obj.exists()) %7B%0A                            mLogger.info(%0A/* (non-Javadoc)%0A * @i18n.test%0A * @org-mes=%22Decoding into %22 + p%5B0%5D%0A */%0A                        org.openlaszlo.i18n.LaszloMessages.getMessage(%0A                                ResponderSWF.class.getName(),%22051018-98%22, new Object%5B%5D %7BobjName%7D)%0A);%0A                            FileUtils.decode(gz, obj, %22gzip%22);%0A                        %7D%0A                        input = new FileInputStream(fileName);%0A                    %7D%0A                %7D%0A            %7D else %7B%0A    
     
@@ -2323,30 +2323,16 @@
  props);
-%0A            %7D
 %0A%0A      

Change 20071127-hqm-4 by hqm@DADDY_THNKPAD67 on 2007-11-27 10:44:20 EST in /cygdrive/c/users/hqm/openlaszlo/trunk/WEB-INF/lps/server/src for http://svn.openlaszlo.org/openlaszlo/trunk/WEB-INF/lps/server/src