View Javadoc

1   /*
2    * $Id: FileServer.java,v 1.2 2005/03/21 18:45:55 mch Exp $
3    */
4   
5   package org.astrogrid.webapp;
6   
7   import java.io.File;
8   import java.io.FileInputStream;
9   import java.io.IOException;
10  import java.io.InputStream;
11  import javax.servlet.http.HttpServletRequest;
12  import javax.servlet.http.HttpServletResponse;
13  import org.astrogrid.cfg.ConfigFactory;
14  import org.astrogrid.io.Piper;
15  import org.astrogrid.webapp.DefaultServlet;
16  
17  /***
18   * Simple File Server - serves files on disk with a root given in config
19   * fileserver.root and
20   * relative path given by the parameter 'path'.  NB This can be a major
21   * security problem if the config root is set too high and exposes subdirectories
22   * you don't want - make sure all the files in the root down are suitable for
23   * public access.
24   *
25   * The purpose of this class is to give access to FITS files etc for SIAP.
26   *
27   * @author mch
28   */
29  public class FileServer extends DefaultServlet {
30   
31     public static final String ROOT_KEY = "fileserver.root";
32     
33     public void doGet(HttpServletRequest request, HttpServletResponse response)  throws IOException {
34        
35        String relPath = request.getParameter("path");
36        if ((relPath==null) || (relPath.trim().length()==0)) {
37           //do nothing, return nothing
38           return;
39        }
40  
41        String root = ConfigFactory.getCommonConfig().getString(ROOT_KEY);
42  
43        File source = new File(root+relPath);
44        InputStream in = new FileInputStream(source);
45        Piper.bufferedPipe(in, response.getOutputStream());
46     }
47  }
48