001: package com.quadcap.http.servlets.file;
002:
003: /* Copyright 1998 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.File;
042: import java.io.FileInputStream;
043: import java.io.FileNotFoundException;
044: import java.io.IOException;
045: import java.io.OutputStream;
046:
047: import java.util.Enumeration;
048: import java.util.Hashtable;
049:
050: import java.net.MalformedURLException;
051: import java.net.URL;
052:
053: import javax.servlet.ServletConfig;
054: import javax.servlet.ServletContext;
055: import javax.servlet.ServletException;
056:
057: import javax.servlet.http.HttpServlet;
058: import javax.servlet.http.HttpServletRequest;
059: import javax.servlet.http.HttpServletResponse;
060:
061: import com.quadcap.util.Debug;
062:
063: /**
064: * This class implements a generic file-serving servlet
065: * <p>
066: *
067: * XXX This implementation is not done; it's been checked in as a work in
068: * progress.
069: * <p>
070: *
071: * @author Stan Bailes
072: */
073:
074: public class FileServlet extends HttpServlet {
075: /** A table of mime types, indexed by file extension */
076: Hashtable mimeTypes = new Hashtable();
077: ServletContext context;
078:
079: FileCache files;
080:
081: /**
082: * Initialize this servlet
083: *
084: * @param config the servlet configuration object, containing my
085: * initialization parameters
086: * @exception ServletException if I can't initialize for some reason.
087: */
088: public void init(ServletConfig config) throws ServletException {
089: super .init(config);
090: context = config.getServletContext();
091: String s = config.getInitParameter("cacheSize");
092: if (s == null)
093: s = "128";
094: files = new FileCache(this , Integer.parseInt(s));
095: }
096:
097: /**
098: * Handle an incoming request.
099: *
100: * @param req HttpServletRequest that encapsulates the request to
101: * the servlet
102: * @param res HttpServletResponse that encapsulates the response
103: * from the servlet
104: *
105: * @exception IOException if detected when handling the request
106: * @exception ServletException if the request could not be handled
107: */
108: protected void service(HttpServletRequest req,
109: HttpServletResponse res) throws ServletException,
110: IOException {
111: try {
112: HttpFile file = getFileForRequest(req);
113: if (file == null) {
114: res.sendError(HttpServletResponse.SC_NOT_FOUND,
115: "Not Found");
116: } else {
117: try {
118: file.service(req, res);
119: } catch (FileNotFoundException ex) {
120: res.sendError(HttpServletResponse.SC_NOT_FOUND,
121: "Not Found");
122: }
123:
124: }
125: } catch (FileNotFoundException f3) {
126: res
127: .sendError(HttpServletResponse.SC_NOT_FOUND,
128: "Not Found");
129: } catch (Exception e) {
130: Debug.print(e);
131: res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
132: e.toString());
133: }
134: }
135:
136: /**
137: * Return a file object which is suitable for processing the specified
138: * request.
139: *
140: * @param req the http request to service.
141: * @return the file, or null if the file doesn't exist.
142: *
143: * @exception Exception may be thrown.
144: */
145: public HttpFile getFileForRequest(HttpServletRequest req)
146: throws Exception {
147: HttpFile f = null;
148: String path = req.getServletPath();
149: String p = req.getPathInfo();
150: if (p != null)
151: path = path + p;
152: String real = context.getRealPath(path);
153: if (real != null) {
154: f = (HttpFile) files.get(real); // Get the file from the cache
155: if (f != null) {
156: f.checkModified();
157: }
158: }
159: return f;
160: }
161:
162: final URL getURL(String path) throws MalformedURLException {
163: return context.getResource(path);
164: }
165:
166: final String getContentType(String path) {
167: return context.getMimeType(path);
168: }
169:
170: }
|