99//** FileManager
1010//******************************************************************************
1111/**
12- * Used to serve up static files (html, javascript, css, images, etc).
12+ * Used to serve up static files (html, javascript, css, images, etc).
1313 * Ensures static files are cached properly by browsers by appending
14- * version numbers to css and js files. Also updates js and css resources
14+ * version numbers to css and js files. Also updates js and css resources
1515 * referenced inside html files and application-specific xml files. Redirects
1616 * requests to the most recent version of each file. Sends 304 responses as
1717 * required.
@@ -24,28 +24,28 @@ public class FileManager {
2424 private String [] welcomeFiles = new String []{"index.html" , "index.htm" , "default.htm" };
2525 private static final String z = "GMT" ;
2626 private static final TimeZone tz = TimeZone .getTimeZone (z );
27-
28-
27+
28+
2929 //**************************************************************************
3030 //** Constructor
3131 //**************************************************************************
3232 public FileManager (javaxt .io .Directory web ){
3333 this .web = web ;
3434 }
35-
36-
35+
36+
3737 //**************************************************************************
3838 //** sendFile
3939 //**************************************************************************
40- /** Used to send a file to the client.
40+ /** Used to send a file to the client.
4141 */
4242 public void sendFile (HttpServletRequest request , HttpServletResponse response )
4343 throws IOException {
44-
44+
4545 //Get path from url, excluding servlet path and leading "/" character
4646 String path = request .getPathInfo ();
4747 if (path !=null ) path = path .substring (1 );
48-
48+
4949 //Send file
5050 sendFile (path , request , response );
5151 }
@@ -54,38 +54,38 @@ public void sendFile(HttpServletRequest request, HttpServletResponse response)
5454 //**************************************************************************
5555 //** sendFile
5656 //**************************************************************************
57- /** Used to send a file to the client.
57+ /** Used to send a file to the client.
5858 */
5959 public void sendFile (String path , HttpServletRequest request , HttpServletResponse response )
6060 throws IOException {
6161
62-
62+
6363 if (path ==null ) path = "" ;
64-
65-
66-
64+
65+
66+
6767 //Construct a list of possible file paths
6868 java .util .ArrayList <String > files = new java .util .ArrayList <String >();
6969 files .add (web + path );
7070 if (path .length ()>0 && !path .endsWith ("/" )) path +="/" ;
7171 for (String welcomeFile : welcomeFiles ){
7272 files .add (web + path + welcomeFile );
7373 }
74-
75-
76-
74+
75+
76+
7777 //Loop through all the possible file combinations
7878 for (String str : files ){
79-
79+
8080 //Ensure that the path doesn't have any illegal directives
8181 str = str .replace ("\\ " , "/" );
8282 if (str .contains (".." ) || str .contains ("/." ) ||
8383 str .toLowerCase ().contains ("/keystore" )){
8484 continue ;
8585 }
86-
87-
88-
86+
87+
88+
8989 //Send file if it exists
9090 java .io .File file = new java .io .File (str );
9191 if (file .exists () && file .isFile () && !file .isHidden ()){
@@ -94,17 +94,17 @@ public void sendFile(String path, HttpServletRequest request, HttpServletRespons
9494 }
9595
9696 }
97-
98-
97+
98+
9999 //If we're still here, throw an error
100100 response .setStatus (404 );
101101 }
102-
103-
102+
103+
104104 //**************************************************************************
105105 //** sendFile
106106 //**************************************************************************
107- /** Used to send a file to the client.
107+ /** Used to send a file to the client.
108108 */
109109 public void sendFile (java .io .File file , HttpServletRequest request , HttpServletResponse response )
110110 throws IOException {
@@ -115,12 +115,23 @@ public void sendFile(java.io.File file, HttpServletRequest request, HttpServletR
115115 response .setStatus (404 );
116116 }
117117 }
118-
119-
118+
119+
120+ //**************************************************************************
121+ //** sendFile
122+ //**************************************************************************
123+ /** Used to send a file to the client.
124+ */
125+ public void sendFile (javaxt .io .File file , HttpServletRequest request , HttpServletResponse response )
126+ throws IOException {
127+ this .sendFile (file .toFile (), request , response );
128+ }
129+
130+
120131 //**************************************************************************
121132 //** sendFile
122133 //**************************************************************************
123- /** Used to send a file to the client. Does not check if the file exists or
134+ /** Used to send a file to the client. Does not check if the file exists or
124135 * is valid. Only returns 200 and 3XX responses. It is up to the caller to
125136 * pass in a valid file and return errors to the client (e.g. 404).
126137 */
@@ -135,8 +146,8 @@ private void _sendFile(java.io.File file, HttpServletRequest request, HttpServle
135146 if (ext .equals ("js" ) || ext .equals ("css" )){
136147
137148
138- //Add version number to javascript and css files to ensure
139- //proper caching. Otherwise, browsers like Chrome may not
149+ //Add version number to javascript and css files to ensure
150+ //proper caching. Otherwise, browsers like Chrome may not
140151 //return the correct file to the client.
141152 javaxt .utils .URL url = new javaxt .utils .URL (request .getURL ());
142153 long currVersion = new javaxt .utils .Date (file .lastModified ()).toLong ();
@@ -151,17 +162,17 @@ private void _sendFile(java.io.File file, HttpServletRequest request, HttpServle
151162 }
152163 else if (requestedVersion ==currVersion ){
153164 response .setHeader ("Cache-Control" , "public, max-age=31536000, immutable" );
154- }
165+ }
155166
156167 }
157168 else if (ext .equals ("htm" ) || ext .equals ("html" )){
158169
159170
160171 //Convert html to xml. Assumes the html file is xhtml.
161- //Note that the parser will be extremely slow if there is
172+ //Note that the parser will be extremely slow if there is
162173 //a !DOCTYPE declaration.
163174 javaxt .io .File htmlFile = new javaxt .io .File (file );
164- org .w3c .dom .Document xml = htmlFile .getXML ();
175+ org .w3c .dom .Document xml = htmlFile .getXML ();
165176 java .util .TreeSet <Long > dates = new java .util .TreeSet <Long >();
166177 dates .add (file .lastModified ());
167178
@@ -243,9 +254,9 @@ else if (ext.equals("htm") || ext.equals("html")){
243254 else if (ext .equals ("xml" )){
244255
245256
246- //Check whether the xml file is a javaxt-specific file
247- //with CSS and JS includes. If so, add version numbers to
248- //the js and css files sourced in the xml document.
257+ //Check whether the xml file is a javaxt-specific file
258+ //with CSS and JS includes. If so, add version numbers to
259+ //the js and css files sourced in the xml document.
249260 javaxt .io .File xmlFile = new javaxt .io .File (file );
250261 org .w3c .dom .Document xml = xmlFile .getXML ();
251262 String outerNode = javaxt .xml .DOM .getOuterNode (xml ).getNodeName ();
@@ -333,15 +344,15 @@ else if (ext.equals("xml")){
333344 response .write (file , javaxt .io .File .getContentType (file .getName ()), true );
334345
335346 }
336-
337-
347+
348+
338349 //**************************************************************************
339350 //** sendResponse
340351 //**************************************************************************
341352 /** Sends a given string to the client. Transparently handles caching using
342353 * "ETag" and "Last-Modified" headers.
343354 */
344- private void sendResponse (String html , long date , HttpServletRequest request ,
355+ private void sendResponse (String html , long date , HttpServletRequest request ,
345356 HttpServletResponse response ) throws IOException {
346357
347358
@@ -385,13 +396,13 @@ private void sendResponse(String html, long date, HttpServletRequest request,
385396
386397 response .write (html );
387398 }
388-
389-
399+
400+
390401 //**************************************************************************
391402 //** updateNodes
392403 //**************************************************************************
393- /** Adds empty comment blocks to "childless" nodes to prevent self-enclosing
394- * tags.
404+ /** Adds empty comment blocks to "childless" nodes to prevent self-enclosing
405+ * tags.
395406 */
396407 private void updateNodes (org .w3c .dom .NodeList nodes , org .w3c .dom .Document xml ){
397408 for (int i =0 ; i <nodes .getLength (); i ++){
@@ -404,15 +415,15 @@ private void updateNodes(org.w3c.dom.NodeList nodes, org.w3c.dom.Document xml){
404415 updateNode (node , xml );
405416 }
406417 }
407-
418+
408419 }
409420 }
410421
411-
422+
412423 //**************************************************************************
413424 //** updateNode
414425 //**************************************************************************
415- /** Adds an empty comment block to a node to prevent self-enclosing tags.
426+ /** Adds an empty comment block to a node to prevent self-enclosing tags.
416427 */
417428 private void updateNode (org .w3c .dom .Node node , org .w3c .dom .Document xml ){
418429 try {
@@ -422,8 +433,8 @@ private void updateNode(org.w3c.dom.Node node, org.w3c.dom.Document xml){
422433 System .out .println (node .getNodeName ());
423434 }
424435 }
425-
426-
436+
437+
427438 //**************************************************************************
428439 //** getDate
429440 //**************************************************************************
@@ -486,7 +497,7 @@ private static String getDate(Calendar cal){
486497 //return f.format(date);
487498 }
488499
489-
500+
490501 private String getDate (long milliseconds ){
491502 java .util .Calendar cal = java .util .Calendar .getInstance ();
492503 cal .setTimeInMillis (milliseconds );
0 commit comments