extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/servlet/Download.java

Code
Comments
Other
Rev Date Author Line
1107 03 Jun 09 nicklas 1 /*
1107 03 Jun 09 nicklas 2   $Id$
1107 03 Jun 09 nicklas 3
1107 03 Jun 09 nicklas 4   Copyright (C) 2007 Nicklas Nordborg
1107 03 Jun 09 nicklas 5
1107 03 Jun 09 nicklas 6   This file is part of BASE - BioArray Software Environment.
1107 03 Jun 09 nicklas 7   Available at http://base.thep.lu.se/
1107 03 Jun 09 nicklas 8
1107 03 Jun 09 nicklas 9   BASE is free software; you can redistribute it and/or
1107 03 Jun 09 nicklas 10   modify it under the terms of the GNU General Public License
1107 03 Jun 09 nicklas 11   as published by the Free Software Foundation; either version 2
1107 03 Jun 09 nicklas 12   of the License, or (at your option) any later version.
1107 03 Jun 09 nicklas 13
1107 03 Jun 09 nicklas 14   BASE is distributed in the hope that it will be useful,
1107 03 Jun 09 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
1107 03 Jun 09 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1107 03 Jun 09 nicklas 17   GNU General Public License for more details.
1107 03 Jun 09 nicklas 18
1107 03 Jun 09 nicklas 19   You should have received a copy of the GNU General Public License
1107 03 Jun 09 nicklas 20   along with this program; if not, write to the Free Software
1107 03 Jun 09 nicklas 21   Foundation, Inc., 59 Temple Place - Suite 330,
1107 03 Jun 09 nicklas 22   Boston, MA  02111-1307, USA.
1107 03 Jun 09 nicklas 23 */
1107 03 Jun 09 nicklas 24 package net.sf.basedb.genepattern.servlet;
1107 03 Jun 09 nicklas 25
1107 03 Jun 09 nicklas 26 import javax.servlet.ServletException;
1107 03 Jun 09 nicklas 27 import javax.servlet.http.HttpServlet;
1107 03 Jun 09 nicklas 28 import javax.servlet.http.HttpServletRequest;
1107 03 Jun 09 nicklas 29 import javax.servlet.http.HttpServletResponse;
1107 03 Jun 09 nicklas 30
1107 03 Jun 09 nicklas 31 import net.sf.basedb.core.Application;
1107 03 Jun 09 nicklas 32 import net.sf.basedb.genepattern.file.FileTransferGateway;
1107 03 Jun 09 nicklas 33 import net.sf.basedb.util.FileUtil;
1107 03 Jun 09 nicklas 34 import net.sf.basedb.util.StaticCache;
1107 03 Jun 09 nicklas 35
1107 03 Jun 09 nicklas 36 import java.io.IOException;
1107 03 Jun 09 nicklas 37 import java.io.InputStream;
1107 03 Jun 09 nicklas 38
1107 03 Jun 09 nicklas 39 /**
1107 03 Jun 09 nicklas 40   A servlet for sending files to a GenePattern server.
1107 03 Jun 09 nicklas 41   Input data files are registered with a {@link FileTransferGateway} which 
1107 03 Jun 09 nicklas 42   generates the URL and {@link Parameter} that is given to GenePattern.
1107 03 Jun 09 nicklas 43   <p>
1107 03 Jun 09 nicklas 44   The files themselves are copied and stored in the static cache
1107 03 Jun 09 nicklas 45   ({@link Application#getStaticCache()}. The files should be removed
1107 03 Jun 09 nicklas 46   from the static cache after the job has been completed by calling
1107 03 Jun 09 nicklas 47   {@link FileTransferGateway#cleanUp()}.
1107 03 Jun 09 nicklas 48
1107 03 Jun 09 nicklas 49   @author Nicklas
1107 03 Jun 09 nicklas 50   @version 1.0
1107 03 Jun 09 nicklas 51   @base.modified $Date$
1107 03 Jun 09 nicklas 52 */
1107 03 Jun 09 nicklas 53 public final class Download
1107 03 Jun 09 nicklas 54   extends HttpServlet
1107 03 Jun 09 nicklas 55 {
1107 03 Jun 09 nicklas 56
1107 03 Jun 09 nicklas 57   private static final long serialVersionUID = 848237452492077399L;
1107 03 Jun 09 nicklas 58
1107 03 Jun 09 nicklas 59   public Download()
1107 03 Jun 09 nicklas 60   {}
1107 03 Jun 09 nicklas 61   
1107 03 Jun 09 nicklas 62   @Override
1107 03 Jun 09 nicklas 63   public void doGet(HttpServletRequest request, HttpServletResponse response)
1107 03 Jun 09 nicklas 64     throws IOException, ServletException
1107 03 Jun 09 nicklas 65   {
1107 03 Jun 09 nicklas 66     // Get the requested file path and make sure it doesn't contain /../
1107 03 Jun 09 nicklas 67     String path = request.getPathInfo();
1107 03 Jun 09 nicklas 68     if (path.contains("/../")) 
1107 03 Jun 09 nicklas 69     {
1107 03 Jun 09 nicklas 70       response.sendError(HttpServletResponse.SC_BAD_REQUEST);
1107 03 Jun 09 nicklas 71       return;
1107 03 Jun 09 nicklas 72     }
1107 03 Jun 09 nicklas 73     StaticCache cache = Application.getStaticCache();
1107 03 Jun 09 nicklas 74     String cacheKey = FileTransferGateway.cacheRoot + path;
1107 03 Jun 09 nicklas 75     InputStream data = cache.read(cacheKey, 1000);
1107 03 Jun 09 nicklas 76     try
1107 03 Jun 09 nicklas 77     {
1107 03 Jun 09 nicklas 78       if (data == null)
1107 03 Jun 09 nicklas 79       {
1107 03 Jun 09 nicklas 80         response.sendError(HttpServletResponse.SC_NOT_FOUND);
1107 03 Jun 09 nicklas 81         return;
1107 03 Jun 09 nicklas 82       }
1107 03 Jun 09 nicklas 83       FileUtil.copy(data, response.getOutputStream());
1107 03 Jun 09 nicklas 84     }
1107 03 Jun 09 nicklas 85     finally
1107 03 Jun 09 nicklas 86     {
1107 03 Jun 09 nicklas 87       FileUtil.close(data);
1107 03 Jun 09 nicklas 88     }
1107 03 Jun 09 nicklas 89   }
1107 03 Jun 09 nicklas 90
1107 03 Jun 09 nicklas 91   @Override
1107 03 Jun 09 nicklas 92   public void doPost(HttpServletRequest request, HttpServletResponse response)
1107 03 Jun 09 nicklas 93     throws IOException, ServletException
1107 03 Jun 09 nicklas 94   {
1107 03 Jun 09 nicklas 95     doGet(request, response);
1107 03 Jun 09 nicklas 96   }
1107 03 Jun 09 nicklas 97 }