KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > responders > ResponderLIB


1 /******************************************************************************
2  * ResponderLIB.java
3  *****************************************************************************/

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

9
10 package org.openlaszlo.servlets.responders;
11
12 import java.io.*;
13 import java.util.Properties JavaDoc;
14 import javax.servlet.ServletConfig JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18 import org.openlaszlo.utils.LZHttpUtils;
19 import javax.servlet.ServletOutputStream JavaDoc;
20 import org.openlaszlo.cache.MediaCache;
21 import org.openlaszlo.cache.RequestCache;
22 import org.openlaszlo.server.LPS;
23 import org.openlaszlo.utils.FileUtils;
24 import org.openlaszlo.media.MimeType;
25
26 import org.apache.log4j.Logger;
27
28 /**
29  * Respond to library requests.
30  * Currently does not use compilation manager, just looks for and returns
31  * the requested swf file specified by the 'libpath' query arg.
32  */

33
34 public final class ResponderLIB extends Responder
35 {
36     private static Logger mLogger = Logger.getLogger(ResponderLIB.class);
37
38     public int getMimeType()
39     {
40         return MIME_TYPE_SWF;
41     }
42
43     public void init(String JavaDoc reqName, ServletConfig JavaDoc config, Properties JavaDoc prop)
44         throws ServletException JavaDoc, IOException
45     {
46         super.init(reqName, config, prop);
47     }
48
49     /**
50      * Delivers a 'snippet' compiled library file.
51      *
52      * Gets the filename of the snippet we are loading from the
53      * request, where it is specified in the "libpath" query
54      * arg. 'libpath' is relative to the app base filename. Thus, an
55      * app at /intl2/test/snippets/main.lzx, which has &ltimport
56      * HREF="lib/foo.lzx"> will send a request with
57      * lzt=lib&libpath=lib/foo.lzx
58      */

59     protected void respondImpl(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
60         throws IOException
61     {
62
63         try {
64             String JavaDoc patharg = req.getParameter("libpath");
65             if (patharg == null) {
66                 throw new IOException("could not find 'libpath' query arg in lzt=lib request");
67             }
68
69             /* getContextPath() = /intl2
70                getPathInfo() = null
71                getPathTranslated() = null
72                getRequestURI() = /intl2/test/snippets/main.lzx
73                getServletPath() = /test/snippets/main.lzx
74             */

75
76
77             ServletOutputStream JavaDoc out = res.getOutputStream();
78             PrintWriter p = new PrintWriter(out);
79
80             // canonicalize the separator char
81
String JavaDoc path = (new File(patharg)).getPath();
82             String JavaDoc appbasedir = (new File(req.getServletPath())).getParent();
83             String JavaDoc libpath;
84             // Check if we are merging with an absolute or relative lib path.
85
// Why doesn't Java have fs:merge-pathnames?
86
if (path.charAt(0) == File.separatorChar) {
87                 libpath = path;
88             } else {
89                 libpath = (new File(appbasedir, path)).getPath();
90             }
91             String JavaDoc filename = LZHttpUtils.getRealPath(mContext, libpath);
92             mLogger.info("Responding with LIB for " + filename);
93             res.setContentType(MimeType.SWF);
94             InputStream ins = null;
95             try {
96                 // open the file and return it.
97
ins = new BufferedInputStream(new FileInputStream(filename));
98                 FileUtils.send(ins, out);
99             } finally {
100                 FileUtils.close(out);
101                 FileUtils.close(ins);
102             }
103         } catch (java.io.FileNotFoundException JavaDoc e) {
104             throw new IOException(e.getMessage());
105         }
106     }
107 }
108     
109
Popular Tags