KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > ResourceServlet


1 package org.roller.presentation;
2
3 import java.io.*;
4 import java.util.Date JavaDoc;
5
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import org.roller.model.RollerFactory;
12
13
14 /**
15  * Resources servlet. Acts as a gateway to files uploaded by users.
16  *
17  * Since we keep uploaded resources in a location outside of the webapp
18  * context we need a way to serve them up. This servlet assumes that
19  * resources are stored on a filesystem in the "uploads.dir" directory.
20  *
21  * @author Allen Gilliland
22  *
23  * @web.servlet name="ResourcesServlet"
24  * @web.servlet-mapping url-pattern="/resources/*"
25  */

26 public class ResourceServlet extends HttpServlet
27 {
28     private static Log mLogger =
29             LogFactory.getFactory().getInstance(ResourceServlet.class);
30     
31     private String JavaDoc upload_dir = null;
32     private ServletContext context = null;
33     
34     
35     /** Initializes the servlet.*/
36     public void init(ServletConfig config) throws ServletException {
37         super.init(config);
38         
39         this.context = config.getServletContext();
40
41         try {
42             this.upload_dir = RollerFactory.getRoller().getFileManager().getUploadDir();
43             mLogger.debug("upload dir is ["+this.upload_dir+"]");
44         } catch(Exception JavaDoc e) { mLogger.warn(e); }
45
46     }
47     
48     /** Destroys the servlet.
49      */

50     public void destroy() {
51         
52     }
53     
54     
55     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
56      * @param request servlet request
57      * @param response servlet response
58      */

59     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
60     throws ServletException, IOException {
61         
62         String JavaDoc context = request.getContextPath();
63         String JavaDoc servlet = request.getServletPath();
64         String JavaDoc reqURI = request.getRequestURI();
65         
66         // calculate the path of the requested resource
67
// we expect ... /<context>/<servlet>/path/to/resource
68
String JavaDoc reqResource = reqURI.substring(servlet.length() + context.length());
69         
70         // now we can formulate the *real* path to the resource on the filesystem
71
String JavaDoc resource_path = this.upload_dir + reqResource;
72         File resource = new File(resource_path);
73
74         mLogger.debug("Resource requested ["+reqURI+"]");
75         mLogger.debug("Real path is ["+resource.getAbsolutePath()+"]");
76         
77         // do a quick check to make sure the resource exits, otherwise 404
78
if(!resource.exists() || !resource.canRead()) {
79             response.sendError(HttpServletResponse.SC_NOT_FOUND);
80             return;
81         }
82         
83         // does the client already have this file? if so, then 304
84
Date JavaDoc ifModDate = new Date JavaDoc(request.getDateHeader("If-Modified-Since"));
85         Date JavaDoc lastMod = new Date JavaDoc(resource.lastModified());
86         if(lastMod.compareTo(ifModDate) <= 0) {
87             mLogger.debug("Resource unmodified ... sending 304");
88             response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
89             return;
90         }
91         
92         // looks like we'll be serving up the file ... lets set some headers
93
// set last-modified date so we can do if-modified-since checks
94
// set the content type based on whatever is in our web.xml mime defs
95
response.addDateHeader("Last-Modified", (new Date JavaDoc()).getTime());
96         response.setContentType(this.context.getMimeType(resource.getAbsolutePath()));
97         
98         // ok, lets serve up the file
99
byte[] buf = new byte[8192];
100         int length = 0;
101         OutputStream out = response.getOutputStream();
102         InputStream resource_file = new FileInputStream(resource);
103         while((length = resource_file.read(buf)) > 0)
104             out.write(buf, 0, length);
105     }
106     
107     
108     /** Handles the HTTP <code>GET</code> method.
109      * @param request servlet request
110      * @param response servlet response
111      */

112     protected void doGet(HttpServletRequest request, HttpServletResponse response)
113     throws ServletException, IOException {
114         processRequest(request, response);
115     }
116     
117     /** Handles the HTTP <code>POST</code> method.
118      * @param request servlet request
119      * @param response servlet response
120      */

121     protected void doPost(HttpServletRequest request, HttpServletResponse response)
122     throws ServletException, IOException {
123         processRequest(request, response);
124     }
125     
126     /** Returns a short description of the servlet.
127      */

128     public String JavaDoc getServletInfo() {
129         return "ResourceServlet ... serving you since 2005 ;)";
130     }
131     
132 }
133
Popular Tags