Quadcap Embeddable Server

com/quadcap/http/servlets/file/FileServlet.java

Go to the documentation of this file.
00001 package com.quadcap.http.servlets.file; 00002 00003 /* Copyright 1998 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.File; 00042 import java.io.FileInputStream; 00043 import java.io.FileNotFoundException; 00044 import java.io.IOException; 00045 import java.io.OutputStream; 00046 00047 import java.util.Enumeration; 00048 import java.util.Hashtable; 00049 00050 import java.net.MalformedURLException; 00051 import java.net.URL; 00052 00053 import javax.servlet.ServletConfig; 00054 import javax.servlet.ServletContext; 00055 import javax.servlet.ServletException; 00056 00057 import javax.servlet.http.HttpServlet; 00058 import javax.servlet.http.HttpServletRequest; 00059 import javax.servlet.http.HttpServletResponse; 00060 00061 import com.quadcap.util.Debug; 00062 00063 /** 00064 * This class implements a generic file-serving servlet 00065 * <p> 00066 * 00067 * XXX This implementation is not done; it's been checked in as a work in 00068 * progress. 00069 * <p> 00070 * 00071 * @author Stan Bailes 00072 */ 00073 00074 public class FileServlet extends HttpServlet { 00075 /** A table of mime types, indexed by file extension */ 00076 Hashtable mimeTypes = new Hashtable(); 00077 ServletContext context; 00078 00079 FileCache files; 00080 00081 /** 00082 * Initialize this servlet 00083 * 00084 * @param config the servlet configuration object, containing my 00085 * initialization parameters 00086 * @exception ServletException if I can't initialize for some reason. 00087 */ 00088 public void init(ServletConfig config) throws ServletException { 00089 super.init(config); 00090 context = config.getServletContext(); 00091 String s = config.getInitParameter("cacheSize"); 00092 if (s == null) s = "128"; 00093 files = new FileCache(this, Integer.parseInt(s)); 00094 } 00095 00096 /** 00097 * Handle an incoming request. 00098 * 00099 * @param req HttpServletRequest that encapsulates the request to 00100 * the servlet 00101 * @param res HttpServletResponse that encapsulates the response 00102 * from the servlet 00103 * 00104 * @exception IOException if detected when handling the request 00105 * @exception ServletException if the request could not be handled 00106 */ 00107 protected void service(HttpServletRequest req, HttpServletResponse res) 00108 throws ServletException, IOException 00109 { 00110 try { 00111 HttpFile file = getFileForRequest(req); 00112 if (file == null) { 00113 res.sendError(HttpServletResponse.SC_NOT_FOUND, 00114 "Not Found"); 00115 } else { 00116 try { 00117 file.service(req, res); 00118 } catch (FileNotFoundException ex) { 00119 res.sendError(HttpServletResponse.SC_NOT_FOUND, 00120 "Not Found"); 00121 } 00122 00123 } 00124 } catch (FileNotFoundException f3) { 00125 res.sendError(HttpServletResponse.SC_NOT_FOUND, 00126 "Not Found"); 00127 } catch (Exception e) { 00128 Debug.print(e); 00129 res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 00130 e.toString()); 00131 } 00132 } 00133 00134 /** 00135 * Return a file object which is suitable for processing the specified 00136 * request. 00137 * 00138 * @param req the http request to service. 00139 * @return the file, or null if the file doesn't exist. 00140 * 00141 * @exception Exception may be thrown. 00142 */ 00143 public HttpFile getFileForRequest(HttpServletRequest req) 00144 throws Exception 00145 { 00146 HttpFile f = null; 00147 String path = req.getServletPath(); 00148 String p = req.getPathInfo(); 00149 if (p != null) path = path + p; 00150 String real = context.getRealPath(path); 00151 if (real != null) { 00152 f = (HttpFile)files.get(real); // Get the file from the cache 00153 if (f != null) { 00154 f.checkModified(); 00155 } 00156 } 00157 return f; 00158 } 00159 00160 final URL getURL(String path) throws MalformedURLException { 00161 return context.getResource(path); 00162 } 00163 00164 final String getContentType(String path) { 00165 return context.getMimeType(path); 00166 } 00167 00168 }