KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > rendering > servlets > ResourceServlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

18
19 package org.apache.roller.ui.rendering.servlets;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.net.URLDecoder JavaDoc;
27 import javax.servlet.ServletConfig JavaDoc;
28 import javax.servlet.ServletContext JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.HttpServlet JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.apache.roller.model.RollerFactory;
36 import org.apache.roller.ui.rendering.util.ModDateHeaderUtil;
37
38
39 /**
40  * Resources servlet. Acts as a gateway to files uploaded by users.
41  *
42  * Since we keep uploaded resources in a location outside of the webapp
43  * context we need a way to serve them up. This servlet assumes that
44  * resources are stored on a filesystem in the "uploads.dir" directory.
45  *
46  * @web.servlet name="ResourcesServlet" load-on-startup="5"
47  * @web.servlet-mapping url-pattern="/roller-ui/rendering/resources/*"
48  */

49 public class ResourceServlet extends HttpServlet JavaDoc {
50
51     private static Log log = LogFactory.getLog(ResourceServlet.class);
52
53     private String JavaDoc upload_dir = null;
54     private ServletContext JavaDoc context = null;
55
56
57     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
58
59         super.init(config);
60
61         log.info("Initializing ResourceServlet");
62
63         this.context = config.getServletContext();
64
65         try {
66             this.upload_dir = RollerFactory.getRoller().getFileManager().getUploadDir();
67             log.debug("upload dir is ["+this.upload_dir+"]");
68         } catch(Exception JavaDoc e) {
69             log.error(e);
70         }
71
72     }
73
74
75     /**
76      * Handles requests for user uploaded resources.
77      */

78     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
79             throws ServletException JavaDoc, IOException JavaDoc {
80
81         String JavaDoc context = request.getContextPath();
82         String JavaDoc servlet = request.getServletPath();
83         String JavaDoc reqURI = request.getRequestURI();
84
85         // URL decoding
86

87         // Fix for ROL-1065: even though a + should mean space in a URL, folks
88
// who upload files with plus signs expect them to work without
89
// escaping. This is essentially what other systems do (e.g. JIRA) to
90
// enable this.
91
reqURI = reqURI.replaceAll("\\+", "%2B");
92
93         // now we really decode the URL
94
reqURI = URLDecoder.decode(reqURI, "UTF-8");
95
96         // calculate the path of the requested resource
97
// we expect ... /<context>/<servlet>/path/to/resource
98
String JavaDoc reqResource = reqURI.substring(servlet.length() + context.length());
99
100         // now we can formulate the *real* path to the resource on the filesystem
101
String JavaDoc resource_path = this.upload_dir + reqResource;
102         File JavaDoc resource = new File JavaDoc(resource_path);
103
104         log.debug("Resource requested ["+reqURI+"]");
105         log.debug("Real path is ["+resource.getAbsolutePath()+"]");
106
107         // do a quick check to make sure the resource exits, otherwise 404
108
if(!resource.exists() || !resource.canRead() || resource.isDirectory()) {
109             response.sendError(HttpServletResponse.SC_NOT_FOUND);
110             return;
111         }
112
113         // make sure someone isn't trying to sneek outside the uploads dir
114
File JavaDoc uploadDir = new File JavaDoc(this.upload_dir);
115         if(!resource.getCanonicalPath().startsWith(uploadDir.getCanonicalPath())) {
116             response.sendError(HttpServletResponse.SC_NOT_FOUND);
117             return;
118         }
119
120         // Respond with 304 Not Modified if it is not modified.
121
if (ModDateHeaderUtil.respondIfNotModified(request,response, resource.lastModified())) {
122             return;
123         }
124
125         // set last-modified date
126
ModDateHeaderUtil.setLastModifiedHeader(response,resource.lastModified());
127
128         // set the content type based on whatever is in our web.xml mime defs
129
response.setContentType(this.context.getMimeType(resource.getAbsolutePath()));
130
131         // ok, lets serve up the file
132
byte[] buf = new byte[8192];
133         int length = 0;
134         OutputStream JavaDoc out = response.getOutputStream();
135         InputStream JavaDoc resource_file = new FileInputStream JavaDoc(resource);
136         while((length = resource_file.read(buf)) > 0)
137             out.write(buf, 0, length);
138
139         // cleanup
140
out.close();
141         resource_file.close();
142     }
143
144 }
145
Popular Tags