00001
package com.quadcap.http.servlets.file;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
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
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 public class FileServlet extends HttpServlet {
00075
00076 Hashtable
mimeTypes =
new Hashtable();
00077 ServletContext
context;
00078
00079 FileCache files;
00080
00081
00082
00083
00084
00085
00086
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
00098
00099
00100
00101
00102
00103
00104
00105
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
00136
00137
00138
00139
00140
00141
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);
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 }