@@ -39,11 +39,24 @@ public class FileManager {
3939 //**************************************************************************
4040 //** Constructor
4141 //**************************************************************************
42+ /** Used to create an instance of this class.
43+ * @param web Directory from which to serve files (aka web root folder)
44+ */
4245 public FileManager (javaxt .io .Directory web ){
4346 this .web = web ;
4447 }
4548
4649
50+ //**************************************************************************
51+ //** getDirectory
52+ //**************************************************************************
53+ /** Returns the root directory.
54+ */
55+ public javaxt .io .Directory getDirectory (){
56+ return web ;
57+ }
58+
59+
4760 //**************************************************************************
4861 //** getFileUpdates
4962 //**************************************************************************
@@ -115,10 +128,7 @@ public static interface EventMonitor {
115128 */
116129 public java .io .File getFile (HttpServletRequest request ){
117130
118- //Get path from url, excluding servlet path and leading "/" character
119131 String path = request .getPathInfo ();
120- if (path !=null ) path = path .substring (1 );
121-
122132 return getFile (path );
123133 }
124134
@@ -129,9 +139,11 @@ public java.io.File getFile(HttpServletRequest request){
129139 /** Returns a file that best matches the given path. If the path represents
130140 * a directory, searches for welcome files in the directory (e.g.
131141 * "index.html"). Returns null if a file is not found.
142+ * @param path Relative path to a file or folder, relative to the web root.
132143 */
133144 public java .io .File getFile (String path ){
134145 if (path ==null ) path = "" ;
146+ while (path .startsWith ("/" )) path = path .substring (1 );
135147
136148
137149 //Construct a list of possible file paths
@@ -170,31 +182,29 @@ public java.io.File getFile(String path){
170182 //**************************************************************************
171183 //** sendFile
172184 //**************************************************************************
173- /** Used to send a file to the client.
185+ /** Used to send a file to the client. Determines which file to send via
186+ * the URL path in the HTTP request.
174187 */
175188 public void sendFile (HttpServletRequest request , HttpServletResponse response )
176189 throws IOException {
177190
178- //Get path from url, excluding servlet path and leading "/" character
179- String path = request .getPathInfo ();
180- if (path !=null ) path = path .substring (1 );
181-
182- //Send file
183- sendFile (path , request , response );
191+ java .io .File file = getFile (request );
192+ sendFile (file , request , response );
184193 }
185194
186195
187196 //**************************************************************************
188197 //** sendFile
189198 //**************************************************************************
190- /** Used to send a file to the client.
199+ /** Used to send a file to the client. Determines which file to send via
200+ * the given path.
201+ * @param path Relative path to a file or folder, relative to the web root.
191202 */
192203 public void sendFile (String path , HttpServletRequest request , HttpServletResponse response )
193204 throws IOException {
194205
195206 java .io .File file = getFile (path );
196- if (file !=null ) _sendFile (file , request , response );
197- else response .setStatus (404 );
207+ sendFile (file , request , response );
198208 }
199209
200210
@@ -203,14 +213,10 @@ public void sendFile(String path, HttpServletRequest request, HttpServletRespons
203213 //**************************************************************************
204214 /** Used to send a file to the client.
205215 */
206- public void sendFile (java .io .File file , HttpServletRequest request , HttpServletResponse response )
216+ public void sendFile (javaxt .io .File file , HttpServletRequest request , HttpServletResponse response )
207217 throws IOException {
208- if (file .exists () && file .isFile () && !file .isHidden ()){
209- _sendFile (file , request , response );
210- }
211- else {
212- response .setStatus (404 );
213- }
218+
219+ sendFile (file ==null ? null : file .toFile (), request , response );
214220 }
215221
216222
@@ -219,21 +225,15 @@ public void sendFile(java.io.File file, HttpServletRequest request, HttpServletR
219225 //**************************************************************************
220226 /** Used to send a file to the client.
221227 */
222- public void sendFile (javaxt .io .File file , HttpServletRequest request , HttpServletResponse response )
228+ public void sendFile (java .io .File file , HttpServletRequest request , HttpServletResponse response )
223229 throws IOException {
224- this .sendFile (file .toFile (), request , response );
225- }
226230
231+ //Validate the given file. Send 404 response as needed.
232+ if (file ==null || !file .isFile () || !file .exists () || file .isHidden ()){
233+ response .setStatus (404 );
234+ return ;
235+ }
227236
228- //**************************************************************************
229- //** sendFile
230- //**************************************************************************
231- /** Used to send a file to the client. Does not check if the file exists or
232- * is valid. Only returns 200 and 3XX responses. It is up to the caller to
233- * pass in a valid file and return errors to the client (e.g. 404).
234- */
235- private void _sendFile (java .io .File file , HttpServletRequest request , HttpServletResponse response )
236- throws IOException {
237237
238238 String name = file .getName ();
239239 int idx = name .lastIndexOf ("." );
0 commit comments