Ignore:
Timestamp:
Jun 4, 2009, 11:35:59 AM (14 years ago)
Author:
Nicklas Nordborg
Message:

References #222: Utility for transfering file between BASE and a GenePattern? server

Added support for downloading files. The files are downloaded to a temporary working directory and exposed as InputStream/OutputStream objects since that suits the BASE API better. I first tried to use JobResult.getURLForFileName() but that issues a request outside the web services session and only results in downloading a HTML page asking for a login/password.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.genepattern/trunk/src/net/sf/basedb/genepattern/file/FileTransferGateway.java

    r1107 r1109  
    11package net.sf.basedb.genepattern.file;
    22
     3import java.io.BufferedInputStream;
    34import java.io.File;
    45import java.io.FileFilter;
     6import java.io.FileInputStream;
    57import java.io.IOException;
    68import java.io.InputStream;
     
    1719import net.sf.basedb.util.formatter.DateFormatter;
    1820
     21import org.genepattern.webservice.JobResult;
    1922import org.genepattern.webservice.Parameter;
    2023
    2124/**
    22   Handles file transfers to a GenePattern server. One getaway instance can
     25  Handles file transfers to and from a GenePattern server. One getaway instance can
    2326  handle file for one GenePattern job. The files that are needed for the
    2427  job should be added to the gateway with {@link #addFile(String, FileProxy)}
    2528  which returns a {@link Parameter} object that can be used when invoking
    2629  GenePattern.
     30  <p>
     31  Once the job has finished the result files can be downloaded with
     32  {@link #downloadResultFile(JobResult, String, OutputStream)} or
     33  {@link #downloadResultFile(JobResult, String)}.
     34  <p>
     35  After the gateway has been used it is recommended to call {@link #cleanUp()}
     36  to make sure that any temporary files are deleted.
    2737   
    2838  @author nicklas
     
    4050  private final String id;
    4151  private final String baseUrl;
    42   private final Set<String> files;
     52  private final Set<String> uploadFiles;
     53  private final Set<File> downloadFiles;
     54  private final File workDir;
    4355  private final Set<String> directories;
    4456  private final String subDir;
     
    5668    this.id = "gp-" + System.identityHashCode(this);
    5769    this.subDir = new DateFormatter("yyyy-MM-dd").format(new Date());
    58     this.files = new HashSet<String>();
     70    this.uploadFiles = new HashSet<String>();
    5971    this.directories = new HashSet<String>();
     72    this.downloadFiles = new HashSet<File>();
     73    this.workDir = new File(new File(System.getProperty("java.io.tmpdir")), getId());
    6074    directories.add(cacheRoot);
    6175    directories.add(subDir);
     
    97111  {
    98112    String filename = file.getFileName();
    99     if (files.contains(filename))
     113    if (uploadFiles.contains(filename))
    100114    {
    101115      throw new ItemAlreadyExistsException("File already exists: " + filename);
     
    134148      FileUtil.close(fromProxy);
    135149    }
    136     files.add(filename);
     150    uploadFiles.add(filename);
    137151    return new Parameter(gpParameterName, baseUrl + "/Download/" + downloadPath + ".servlet");
    138152  }
    139153 
    140154  /**
    141     Clean up and remove all files that has been added to this gateway.
     155    Download a GenePattern result file and make it available as a InputStream.
     156    Downloaded file are cached and subseqent calls to this method requesting
     157    the same filename will use the cached file.
     158   
     159    @param result The job result information
     160    @param filename A filename that is part of the job result
     161    @return An input stream that reads from the downloaded file or null if the
     162      file doesn't exists as part of the job result
     163    @throws IOException
     164  */
     165  public InputStream downloadResultFile(JobResult result, String filename)
     166    throws IOException
     167  {
     168    File workDir = getWorkingDirectory();
     169    File tmpFile = new File(workDir, filename);
     170    if (!tmpFile.exists())
     171    {
     172      tmpFile = result.downloadFile(filename, workDir.getAbsolutePath(), true);
     173      if (tmpFile != null) downloadFiles.add(tmpFile);
     174    }
     175    InputStream in = null;
     176    if (tmpFile != null)
     177    {
     178      in = new BufferedInputStream(new FileInputStream(tmpFile));
     179    }
     180    return in;
     181  }
     182 
     183  /**
     184    Download a GenePattern result file and copy it to the given ouput stream.
     185    Downloaded file are cached and subseqent calls to this method requesting
     186    the same filename will use the cached file.
     187   
     188    @param result The job result information
     189    @param filename A filename that is part of the job result
     190    @param to The output stream to write to
     191    @throws IOException
     192  */
     193  public long downloadResultFile(JobResult result, String filename, OutputStream to)
     194    throws IOException
     195  {
     196    return FileUtil.copy(downloadResultFile(result, filename), to);
     197  }
     198 
     199  private File getWorkingDirectory()
     200  {
     201    workDir.mkdirs();
     202    return workDir;
     203  }
     204 
     205  /**
     206    Clean up and remove all files that has been added or downloaded
     207    to this gateway.
    142208  */
    143209  public void cleanUp()
    144210  {
    145     if (files.size() > 0)
     211    if (uploadFiles.size() > 0)
    146212    {
    147213      StaticCache cache = Application.getStaticCache();
     
    153219          {
    154220            String name = file.getName();
    155             boolean accept = file.isDirectory() ?directories.contains(name) :
    156               id.equals(file.getParentFile().getName()) && files.contains(name);
     221            boolean accept = file.isDirectory() ? directories.contains(name) :
     222              id.equals(file.getParentFile().getName()) && uploadFiles.contains(name);
    157223            return accept;
    158224          }
    159225        }
    160226      );
    161       files.clear();
     227      uploadFiles.clear();
     228    }
     229    if (downloadFiles.size() > 0)
     230    {
     231      for (File f : downloadFiles)
     232      {
     233        f.delete();
     234      }
     235      workDir.delete();
     236      downloadFiles.clear();
    162237    }
    163238  }
Note: See TracChangeset for help on using the changeset viewer.