KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > file > FileServlet


1 package com.quadcap.http.servlets.file;
2
3 /* Copyright 1998 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.File JavaDoc;
42 import java.io.FileInputStream JavaDoc;
43 import java.io.FileNotFoundException JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.OutputStream JavaDoc;
46
47 import java.util.Enumeration JavaDoc;
48 import java.util.Hashtable JavaDoc;
49
50 import java.net.MalformedURLException JavaDoc;
51 import java.net.URL JavaDoc;
52
53 import javax.servlet.ServletConfig JavaDoc;
54 import javax.servlet.ServletContext JavaDoc;
55 import javax.servlet.ServletException JavaDoc;
56
57 import javax.servlet.http.HttpServlet JavaDoc;
58 import javax.servlet.http.HttpServletRequest JavaDoc;
59 import javax.servlet.http.HttpServletResponse JavaDoc;
60
61 import com.quadcap.util.Debug;
62
63 /**
64  * This class implements a generic file-serving servlet
65  * <p>
66  *
67  * XXX This implementation is not done; it's been checked in as a work in
68  * progress.
69  * <p>
70  *
71  * @author Stan Bailes
72  */

73
74 public class FileServlet extends HttpServlet JavaDoc {
75     /** A table of mime types, indexed by file extension */
76     Hashtable JavaDoc mimeTypes = new Hashtable JavaDoc();
77     ServletContext JavaDoc context;
78
79     FileCache files;
80
81     /**
82      * Initialize this servlet
83      *
84      * @param config the servlet configuration object, containing my
85      * initialization parameters
86      * @exception ServletException if I can't initialize for some reason.
87      */

88     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
89         super.init(config);
90         context = config.getServletContext();
91     String JavaDoc s = config.getInitParameter("cacheSize");
92     if (s == null) s = "128";
93     files = new FileCache(this, Integer.parseInt(s));
94     }
95     
96     /**
97      * Handle an incoming request.
98      *
99      * @param req HttpServletRequest that encapsulates the request to
100      * the servlet
101      * @param res HttpServletResponse that encapsulates the response
102      * from the servlet
103      *
104      * @exception IOException if detected when handling the request
105      * @exception ServletException if the request could not be handled
106      */

107     protected void service(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
108         throws ServletException JavaDoc, IOException JavaDoc
109     {
110         try {
111             HttpFile file = getFileForRequest(req);
112         if (file == null) {
113         res.sendError(HttpServletResponse.SC_NOT_FOUND,
114                               "Not Found");
115         } else {
116                 try {
117                     file.service(req, res);
118                 } catch (FileNotFoundException JavaDoc ex) {
119                     res.sendError(HttpServletResponse.SC_NOT_FOUND,
120                                   "Not Found");
121                 }
122                 
123         }
124         } catch (FileNotFoundException JavaDoc f3) {
125             res.sendError(HttpServletResponse.SC_NOT_FOUND,
126                           "Not Found");
127         } catch (Exception JavaDoc e) {
128             Debug.print(e);
129             res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
130                            e.toString());
131         }
132     }
133
134     /**
135      * Return a file object which is suitable for processing the specified
136      * request.
137      *
138      * @param req the http request to service.
139      * @return the file, or null if the file doesn't exist.
140      *
141      * @exception Exception may be thrown.
142      */

143     public HttpFile getFileForRequest(HttpServletRequest JavaDoc req)
144         throws Exception JavaDoc
145     {
146         HttpFile f = null;
147         String JavaDoc path = req.getServletPath();
148         String JavaDoc p = req.getPathInfo();
149         if (p != null) path = path + p;
150         String JavaDoc real = context.getRealPath(path);
151         if (real != null) {
152             f = (HttpFile)files.get(real); // Get the file from the cache
153
if (f != null) {
154                 f.checkModified();
155             }
156         }
157     return f;
158     }
159
160     final URL JavaDoc getURL(String JavaDoc path) throws MalformedURLException JavaDoc {
161         return context.getResource(path);
162     }
163
164     final String JavaDoc getContentType(String JavaDoc path) {
165         return context.getMimeType(path);
166     }
167
168 }
169
Popular Tags