001: /******************************************************************************
002: * ResponderLIB.java
003: *****************************************************************************/package org.openlaszlo.servlets.responders;
004:
005: import java.io.*;
006: import java.util.Properties;
007: import javax.servlet.ServletConfig;
008: import javax.servlet.ServletException;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011: import org.openlaszlo.utils.LZHttpUtils;
012: import javax.servlet.ServletOutputStream;
013: import org.openlaszlo.cache.MediaCache;
014: import org.openlaszlo.cache.RequestCache;
015: import org.openlaszlo.server.LPS;
016: import org.openlaszlo.utils.FileUtils;
017: import org.openlaszlo.media.MimeType;
018:
019: import org.apache.log4j.Logger;
020:
021: /**
022: * Respond to library requests.
023: * Currently does not use compilation manager, just looks for and returns
024: * the requested swf file specified by the 'libpath' query arg.
025: */
026:
027: public final class ResponderLIB extends Responder {
028: private static Logger mLogger = Logger
029: .getLogger(ResponderLIB.class);
030:
031: public int getMimeType() {
032: return MIME_TYPE_SWF;
033: }
034:
035: public void init(String reqName, ServletConfig config,
036: Properties prop) throws ServletException, IOException {
037: super .init(reqName, config, prop);
038: }
039:
040: /**
041: * Delivers a 'snippet' compiled library file.
042: *
043: * Gets the filename of the snippet we are loading from the
044: * request, where it is specified in the "libpath" query
045: * arg. 'libpath' is relative to the app base filename. Thus, an
046: * app at /intl2/test/snippets/main.lzx, which has <import
047: * href="lib/foo.lzx"> will send a request with
048: * lzt=lib&libpath=lib/foo.lzx
049: */
050: protected void respondImpl(HttpServletRequest req,
051: HttpServletResponse res) throws IOException {
052:
053: try {
054: String patharg = req.getParameter("libpath");
055: if (patharg == null) {
056: throw new IOException(
057: /* (non-Javadoc)
058: * @i18n.test
059: * @org-mes="could not find 'libpath' query arg in lzt=lib request"
060: */
061: org.openlaszlo.i18n.LaszloMessages.getMessage(
062: ResponderLIB.class.getName(), "051018-71"));
063: }
064:
065: /* getContextPath() = /intl2
066: getPathInfo() = null
067: getPathTranslated() = null
068: getRequestURI() = /intl2/test/snippets/main.lzx
069: getServletPath() = /test/snippets/main.lzx
070: */
071:
072: ServletOutputStream out = res.getOutputStream();
073: PrintWriter p = new PrintWriter(out);
074:
075: // canonicalize the separator char
076: String path = (new File(patharg)).getPath();
077: String appbasedir = (new File(req.getServletPath()))
078: .getParent();
079: String libpath;
080: // Check if we are merging with an absolute or relative lib path.
081: // Why doesn't Java have fs:merge-pathnames?
082: if (path.charAt(0) == File.separatorChar) {
083: libpath = path;
084: } else {
085: libpath = (new File(appbasedir, path)).getPath();
086: }
087: String filename = LZHttpUtils
088: .getRealPath(mContext, libpath);
089: mLogger.info(
090: /* (non-Javadoc)
091: * @i18n.test
092: * @org-mes="Responding with LIB for " + p[0]
093: */
094: org.openlaszlo.i18n.LaszloMessages.getMessage(
095: ResponderLIB.class.getName(), "051018-104",
096: new Object[] { filename }));
097: res.setContentType(MimeType.SWF);
098: InputStream ins = null;
099: try {
100: // open the file and return it.
101: ins = new BufferedInputStream(new FileInputStream(
102: filename));
103: FileUtils.send(ins, out);
104: } finally {
105: FileUtils.close(out);
106: FileUtils.close(ins);
107: }
108: } catch (java.io.FileNotFoundException e) {
109: throw new IOException(e.getMessage());
110: }
111: }
112: }
|