Changeset 3964


Ignore:
Timestamp:
Nov 14, 2007, 9:45:36 AM (14 years ago)
Author:
Nicklas Nordborg
Message:

References #492: Web services

Redesigned file download to work with streams instead of java.io.File.

Location:
trunk/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/test/TestWebservices.java

    r3952 r3964  
    3939import net.sf.basedb.info.QueryOptions;
    4040import net.sf.basedb.info.RawBioAssayInfo;
     41import net.sf.basedb.util.FileUtil;
    4142import net.sf.basedb.ws.client.ArrayDesignClient;
    4243import net.sf.basedb.ws.client.BioAssaySetClient;
     
    4748
    4849import java.io.File;
     50import java.io.FileOutputStream;
     51import java.io.InputStream;
     52import java.io.OutputStream;
    4953import java.util.ArrayList;
    5054
     
    216220        for (String format : exportFormats)
    217221        {
    218           File receivedFile = null;
     222          File receivedFile = new File(format);
     223          OutputStream out = new FileOutputStream(receivedFile);
    219224          System.out.print("--Download BioAssaySet in file format " + format + "...");
    220           receivedFile = basClient.downloadBioAssaySet(
    221               bioAssaySetId, format, format, rep.toArray(reporterFields), spot.toArray(spotFields), mergeReporters);
    222           if (receivedFile == null)
     225          InputStream in = basClient.downloadBioAssaySet(
     226              bioAssaySetId, format, rep.toArray(reporterFields), spot.toArray(spotFields), mergeReporters);
     227          if (in == null)
    223228          {
    224             write ("FAILED");
     229            write ("FAILED InputStream==null");
    225230            ok = false;
    226231          }
    227           else 
     232          else
    228233          {
    229             write("OK");
     234            FileUtil.copy(in, out);
     235            in.close();
     236            out.close();
     237            write("OK; size=" + receivedFile.length() + " bytes");
    230238            receivedFile.delete();
    231239          }
     
    285293        {
    286294          System.out.print("--Download of array design file....");
    287           File receivedFile = arrayDesignClient.downloadArrayDesignByType(arrayDesignId, fileTypes[0], fileTypes[0].getName());
    288           if (receivedFile != null)
     295          File receivedFile = new File(fileTypes[0].getName());
     296          OutputStream out = new FileOutputStream(receivedFile);
     297          InputStream in = arrayDesignClient.downloadArrayDesignByType(arrayDesignId, fileTypes[0].getExternalId());
     298          if (in == null)
    289299          {
     300            write ("FAILED InputStream==null");
     301            ok = false;
     302          }
     303          else
     304          {
     305            FileUtil.copy(in, out);
     306            in.close();
     307            out.close();
     308            write("OK; size=" + receivedFile.length() + " bytes");
    290309            receivedFile.delete();
    291             write("OK");
    292310          }
    293           else throw new Exception("Download file FAILED");
    294         }
    295         else
    296         {
    297           File receivedFile = arrayDesignClient.downloadArrayDesignByType(arrayDesignId, null, "nullFile");
    298           if (receivedFile != null) throw new Exception("Received a file though the array design doesn't have any downloadable data");
    299311        }
    300312      }
     
    362374      {
    363375        System.out.print("--Download of raw data file for RawBioAssay....");
    364         File receivedFile = rawClient.downloadRawDataByType(rawBioAssayId, fileTypes[0], fileTypes[0].getName());
    365         if (receivedFile != null)
    366         {
    367           write ("OK");
     376        File receivedFile = new File(fileTypes[0].getName());
     377        OutputStream out = new FileOutputStream(receivedFile);
     378        InputStream in = rawClient.downloadRawDataByType(rawBioAssayId, fileTypes[0].getExternalId());
     379        if (in == null)
     380        {
     381          write ("FAILED InputStream==null");
     382          ok = false;
     383        }
     384        else
     385        {
     386          FileUtil.copy(in, out);
     387          in.close();
     388          out.close();
     389          write("OK; size=" + receivedFile.length() + " bytes");
    368390          receivedFile.delete();
    369391        }
    370         else write("FAILED");
    371392      }
    372393      write ("--Test of RawBioAssayClient OK");
  • trunk/src/webservices/client/java/net/sf/basedb/ws/client/AbstractRPCClient.java

    r3920 r3964  
    2424package net.sf.basedb.ws.client;
    2525
    26 import java.io.File;
    27 import java.io.FileOutputStream;
    2826import java.io.IOException;
    2927import java.io.InputStream;
     
    7573  }
    7674 
    77   protected File invokeFileBlocking(String operation, String fileName, Object... args)
     75  protected InputStream invokeFileBlocking(String operation, Object... args)
    7876    throws AxisFault, IOException
    7977  {
    80     File receivedFile = new File(fileName);
    8178    OMElement response = getService().invokeBlocking(getOperation(operation), args);
    8279    OMElement fileElement = response.getFirstElement();
     
    8683    node.setBinary(true);
    8784    DataHandler dataHandler = (DataHandler) node.getDataHandler();   
    88     InputStream in = dataHandler.getInputStream();
    89     FileOutputStream out = new FileOutputStream(receivedFile);
    90    
    91     byte[] buffer = new byte[512];
    92     int bytes = 0;
    93     long totalBytes = 0;
    94     while (bytes != -1)
    95     {
    96       bytes = in.read(buffer, 0, buffer.length);
    97       if (bytes > 0)
    98       {
    99         totalBytes += bytes;
    100         out.write(buffer, 0, bytes);
    101       }
    102     }
    103     out.close();
    104     return receivedFile;
     85    return dataHandler.getInputStream();
    10586  }
    10687 
  • trunk/src/webservices/client/java/net/sf/basedb/ws/client/ArrayDesignClient.java

    r3952 r3964  
    2727import net.sf.basedb.info.QueryOptions;
    2828
    29 import java.io.File;
    3029import java.io.IOException;
     30import java.io.InputStream;
    3131
    3232import org.apache.axis2.AxisFault;
     
    6262    if it is stored as a file in BASE.
    6363      @param arrayDesignId The array design's id.
    64       @param typeInfo Information about which data file type the file is.
    65       @param filePath Path to save the file in.
    66       @return A java.io.File, or NULL if there is no file to download.
     64      @param dataFileType The external ID of the data file type to download
     65      @return An InputStream to read the file data from, or null
     66        if no file of the specified type exists
    6767      @throws IOException If writing to the file fails.
    68    */
    69   public File downloadArrayDesignByType(int arrayDesignId,
    70       DataFileTypeInfo typeInfo, String filePath)
     68  */
     69  public InputStream downloadArrayDesignByType(int arrayDesignId, String dataFileType)
    7170    throws IOException
    7271  {
    73     File receivedFile = null;
    7472    RPCServiceClient service = getService();
    7573    Options options = service.getOptions();   
    7674    options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    7775    service.setOptions(options);
    78     receivedFile = invokeFileBlocking
     76    return invokeFileBlocking
    7977      (
    8078        "downloadArrayDesignByType",
    81         filePath,
    8279        session.getId(),
    8380        arrayDesignId,
    84         typeInfo
    85       );     
    86     return receivedFile;
     81        dataFileType
     82      );
    8783  }
    8884 
  • trunk/src/webservices/client/java/net/sf/basedb/ws/client/BioAssaySetClient.java

    r3876 r3964  
    2424package net.sf.basedb.ws.client;
    2525
    26 import java.io.File;
    2726import java.io.IOException;
     27import java.io.InputStream;
    2828
    2929import org.apache.axis2.AxisFault;
     
    7070    then sends it as a file.
    7171      @param bioAssaySetId Id of the bioAssay set to export
    72       @param filePath Abstract file path to give to the received file.
    7372      @param format Export format to use when exporting the bioassay set
    7473      @param reporterFields Reporter fields to include in the export
    7574      @param spotFields Spot fields to include in the export
    7675      @param mergeReporters If the reporters should be merged or not.
    77       @return A file at the specified path
     76      @return An InputStream to read the file data from
    7877      @throws AxisFault If communication with services fails.
    7978      @throws IOException If accessing the file files.
    8079   */
    81   public File downloadBioAssaySet(int bioAssaySetId, String filePath, String format,
     80  public InputStream downloadBioAssaySet(int bioAssaySetId, String format,
    8281      String[] reporterFields, String[] spotFields, boolean mergeReporters)
    8382    throws AxisFault, IOException
    8483  {
    85     File receivedFile = null;
    8684    RPCServiceClient service = getService();
    8785    Options options = service.getOptions();   
    8886    options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    8987    service.setOptions(options);
    90     receivedFile = invokeFileBlocking
     88    return invokeFileBlocking
    9189      (
    92         "downloadBioAssaySet",
    93         filePath,
     90        "downloadBioAssaySet",
    9491        session.getId(),
    9592        bioAssaySetId,
     
    9996        mergeReporters
    10097      );     
    101     return receivedFile;
    10298  }
    10399}
  • trunk/src/webservices/client/java/net/sf/basedb/ws/client/RawBioAssayClient.java

    r3952 r3964  
    2828import net.sf.basedb.info.QueryOptions;
    2929
    30 import java.io.File;
    3130import java.io.IOException;
     31import java.io.InputStream;
    3232
    3333import org.apache.axis2.AxisFault;
     
    4444 
    4545 */
    46 public class RawBioAssayClient extends AbstractRPCClient
     46public class RawBioAssayClient
     47  extends AbstractRPCClient
    4748{
    4849
     
    8182    in BASE.
    8283      @param rawBioAssayId Id of rawbioassay to get raw data from
    83       @param info Information about the file type to get.
    84       @param filePath Filepath to save the file to.
    85       @return A java.io.File with raw data
    86         or NULL if the raw data could not be downloaded as a file.
     84      @param dataFileType The external ID of the data file type to download
     85      @return An InputStream to read the file data from, or null
     86        if no file of the specified type exists
    8787      @throws AxisFault If communication with webservices fails.
    8888      @throws IOException If writing to the target file fails.
    8989   */
    90   public File downloadRawDataByType(int rawBioAssayId, DataFileTypeInfo info, String filePath)
     90  public InputStream downloadRawDataByType(int rawBioAssayId, String dataFileType)
    9191    throws AxisFault, IOException
    9292  {
    93     File receivedFile = null;
    9493    RPCServiceClient service = getService();
    9594    Options options = service.getOptions();   
    9695    options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
    9796    service.setOptions(options);
    98     receivedFile = invokeFileBlocking
     97    return invokeFileBlocking
    9998      (
    10099        "downloadRawDataByType",
    101         filePath,
    102100        session.getId(),
    103101        rawBioAssayId,
    104         info
     102        dataFileType
    105103      );
    106     return receivedFile;
    107104  }
    108105 
  • trunk/src/webservices/server/net/sf/basedb/ws/server/ArrayDesignService.java

    r3956 r3964  
    4848  @version 2.5
    4949 */
    50 public class ArrayDesignService extends AbstractRPCService
     50public class ArrayDesignService
     51  extends AbstractRPCService
    5152{
    5253  /**
     
    6061      @param ID Used to identify an active session with.
    6162      @param arrayDesignId Id of the array design the file is associated with.
    62       @param typeInfo Information about the kind of DataFileType the file is.
     63      @param dataFileType IThe external ID of the file type.
    6364      @return OMElement with the file attached,
    64         or NULL if something went wrong or no file was found.
     65        or NULL if no file was found.
    6566   */
    6667  public OMElement downloadArrayDesignByType(String ID,
    67         int arrayDesignId, DataFileTypeInfo typeInfo)
     68        int arrayDesignId, String dataFileType)
    6869  {
    69     if (ID == null || typeInfo == null) return null;
    70    
    7170    SessionControl sc = getSessionControl(ID);
    7271    DbControl dc = sc.newDbControl();
    73    
     72    OMElement toDownload = null;
    7473    try
    7574    {
    7675      ArrayDesign design = ArrayDesign.getById(dc, arrayDesignId);
    77       DataFileType type = DataFileType.getByExternalId(dc, typeInfo.getExternalId());
     76      DataFileType type = DataFileType.getByExternalId(dc, dataFileType);
    7877      if (design.hasFileSet())
    7978      {
    8079        FileSet fileSet = design.getFileSet();
    81         FileSetMember member = fileSet.getMember(type);
    82         File file = member.getFile();
    83         BaseFileDataSource source = new BaseFileDataSource(member.getFile());
    84         return attachFile(source, file.getName());
     80        if (fileSet.hasMember(type))
     81        {
     82          FileSetMember member = fileSet.getMember(type);
     83          File file = member.getFile();
     84          BaseFileDataSource source = new BaseFileDataSource(member.getFile());
     85          toDownload = attachFile(source, file.getName());
     86        }
    8587      }
    86       else return null;
    8788    }
    8889    finally
     
    9091      if (dc != null) dc.close();
    9192    }   
     93    return toDownload;
    9294  }
    9395 
  • trunk/src/webservices/server/net/sf/basedb/ws/server/RawBioAssayService.java

    r3956 r3964  
    8787      @param ID Used to identify an active session with.
    8888      @param rawBioAssayId Id of the rawbio assay the file belongs to.
    89       @param typeInfo Information about the kind of DataFileType the file is.
     89      @param dataFileType The external ID of the file type.
    9090      @return OMElement with the file attached,
    91         or NULL if something went wrong or no file was found.
    92    */
     91        or NULL if no file was found.
     92      @see DataFileType
     93  */
    9394  public OMElement downloadRawDataByType(String ID,
    94       int rawBioAssayId, DataFileTypeInfo typeInfo)
     95      int rawBioAssayId, String dataFileType)
    9596  {
    96     if (ID == null || typeInfo == null) return null;
    9797    SessionControl sc = getSessionControl(ID);
    9898    DbControl dc = sc.newDbControl();
    99    
     99    OMElement toDownload = null;
    100100    try
    101101    {
    102102      RawBioAssay rawBioAssay = RawBioAssay.getById(dc, rawBioAssayId);
    103       DataFileType type = DataFileType.getByExternalId(dc, typeInfo.getExternalId());
     103      DataFileType type = DataFileType.getByExternalId(dc, dataFileType);
    104104      if (rawBioAssay.hasFileSet())
    105105      {
    106         FileSet fileSet = rawBioAssay.getFileSet();
    107         FileSetMember member = fileSet.getMember(type);
    108         File file = member.getFile();
    109         BaseFileDataSource source = new BaseFileDataSource(file);
    110         return attachFile(source, file.getName());
     106        FileSet fileSet = rawBioAssay.getFileSet();
     107        if (fileSet.hasMember(type))
     108        {
     109          FileSetMember member = fileSet.getMember(type);
     110          File file = member.getFile();
     111          BaseFileDataSource source = new BaseFileDataSource(member.getFile());
     112          toDownload = attachFile(source, file.getName());
     113        }
    111114      }
    112       else return null;
    113115    }
    114116    finally
    115117    {
    116118      if (dc != null) dc.close();
    117     }
     119    }   
     120    return toDownload;
    118121  }
    119122 
Note: See TracChangeset for help on using the changeset viewer.