@@ -332,28 +332,7 @@ private Directory getDirectory(String path) throws Exception {
332332 dir = new Directory (path );
333333 }
334334 else {
335- path = path .replace ("\\ " , "/" );
336-
337-
338- //Check if path starts with baseDir. Trim as needed.
339- String a = baseDir .toString ().replace ("\\ " , "/" );
340- String b = path ;
341- if (!b .endsWith ("/" )) b += "/" ;
342- if (b .startsWith (a )){
343- path = path .substring (a .length ());
344- }
345-
346-
347- String [] arr = path .split ("/" );
348- path = baseDir .toString ();
349-
350- for (String str : arr ){
351- str = str .trim ();
352- if (str .equals ("." ) || str .equals (".." )){
353- throw new Exception ("Illegal path" );
354- }
355- path += str + "/" ;
356- }
335+ path = getPath (path );
357336 dir = new Directory (path );
358337 }
359338 }
@@ -402,7 +381,7 @@ private List getList(Directory dir, Object filter, boolean recursiveSearch){
402381 * Headers for the ServiceResponse are set with file metadata (e.g. content
403382 * type, file size, and date). Caller can add additional response headers
404383 * such as the content disposition to make the file "downloadable". See
405- * setContentDisposition() for more information.
384+ * ServiceResponse. setContentDisposition() for more information.
406385 */
407386 public ServiceResponse getFile (ServiceRequest request ) throws Exception {
408387
@@ -415,6 +394,7 @@ public ServiceResponse getFile(ServiceRequest request) throws Exception {
415394
416395 try {
417396 ServiceResponse response = new ServiceResponse (file .getInputStream ());
397+ response .set ("name" , file .getName ());
418398 //response.setContentDisposition(file.getName());
419399 response .setContentLength (file .getSize ());
420400 response .setContentType (file .getContentType ());
@@ -484,19 +464,8 @@ private File getFile(String path){
484464 file = new File (path );
485465 }
486466 else {
487- path = path .replace ("\\ " , "/" );
488- String [] arr = path .split ("/" );
489- path = baseDir .toString ();
490-
491- for (String str : arr ){
492- str = str .trim ();
493- if (str .equals ("" ) || str .equals ("." ) || str .equals (".." )){
494- throw new RuntimeException ("Illegal path" );
495- }
496- path += str + "/" ;
497- }
467+ path = getPath (path );
498468 if (path .endsWith ("/" )) path = path .substring (0 , path .length ()-1 );
499-
500469 file = new File (path );
501470 }
502471 return file ;
@@ -506,6 +475,51 @@ private File getFile(String path){
506475 //**************************************************************************
507476 //** getPath
508477 //**************************************************************************
478+ /** Returns a string representing a fully qualified path to a file or
479+ * directory. Throws a RuntimeException if the relative path contains
480+ * illegal path directives (e.g. ".."). Note that the returned path always
481+ * ends with a "/" character. Trim as needed (e.g. file path).
482+ * @param path Either a relative path or a fully qualified path to a file
483+ * or directory.
484+ */
485+ private String getPath (String path ){
486+ path = path .replace ("\\ " , "/" );
487+
488+
489+ //Check if path starts with baseDir. Trim as needed.
490+ String a = baseDir .toString ().replace ("\\ " , "/" );
491+ String b = path ;
492+ if (!b .endsWith ("/" )) b += "/" ;
493+ if (b .startsWith (a )){
494+ path = path .substring (a .length ());
495+ }
496+
497+
498+ //Remove leading "/" character
499+ if (path .startsWith ("/" )) path = path .substring (1 ).trim ();
500+
501+
502+ String [] arr = (path .length ()>0 ) ? path .split ("/" ) : new String [0 ];
503+ path = baseDir .toString ();
504+
505+ //Append relative path to the baseDir
506+ for (String str : arr ){
507+ str = str .trim ();
508+ if (str .equals ("" ) || str .equals ("." ) || str .equals (".." )){
509+ throw new RuntimeException ("Illegal path" );
510+ }
511+ path += str + "/" ;
512+ }
513+
514+ return path ;
515+ }
516+
517+
518+ //**************************************************************************
519+ //** getPath
520+ //**************************************************************************
521+ /** Returns the "path" parameter in the ServiceRequest
522+ */
509523 private String getPath (ServiceRequest request ){
510524
511525 //TODO: check url path
0 commit comments