Changeset 2512


Ignore:
Timestamp:
Jun 18, 2014, 10:52:55 AM (9 years ago)
Author:
Nicklas Nordborg
Message:

References #533: Add secondary analysis section to Reggie

Added support for using InputStream/OutputStream? when reading/writing files to remote hosts.

Location:
extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/ssh
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/ssh/CopyFilesToBase.java

    r2417 r2512  
    11package net.sf.basedb.reggie.ssh;
    22
     3import java.io.InputStream;
    34import java.util.Arrays;
    45
     
    78
    89import net.schmizz.sshj.SSHClient;
     10import net.schmizz.sshj.sftp.SFTPClient;
    911import net.sf.basedb.core.AnyToAny;
    1012import net.sf.basedb.core.Application;
     
    99101    DbControl dc = null;
    100102    SSHClient ssh = null;
     103    SFTPClient sftp = null;
    101104    try
    102105    {
     
    116119      try
    117120      {
     121        sftp = ssh.newSFTPClient();
    118122        // Calculate total file size
    119123        long totalSize = 0;
     
    123127          totalSize += ((Number)jsonFile.get("size")).longValue();
    124128        }
    125         System.out.println("item AnyToAny: " + item);
    126129        long copiedSoFar = 0;
    127130        int numCopiedFiles = 0;
     
    171174            file.setRemoved(false);
    172175           
    173             // Actual SCP transfer
    174             from.downloadFile(ssh, rootFolder+"/"+path, new BaseFileDestFile(file));
     176            InputStream in = from.readFile(sftp, rootFolder+"/"+path);
     177            try
     178            {
     179              file.upload(in, false);
     180            }
     181            finally
     182            {
     183              in.close();
     184            }
    175185          }
    176186         
     
    233243    {
    234244      if (dc != null) dc.close();
     245      if (sftp != null) SshUtil.close(sftp);
    235246      if (ssh != null) SshUtil.close(ssh);
    236247    }
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/ssh/SshHost.java

    r2361 r2512  
    33import java.io.ByteArrayOutputStream;
    44import java.io.IOException;
     5import java.io.InputStream;
     6import java.io.OutputStream;
    57import java.security.PublicKey;
    68import java.text.SimpleDateFormat;
    79import java.util.Date;
     10import java.util.EnumSet;
    811import java.util.HashMap;
    912import java.util.Map;
     
    2023import net.schmizz.sshj.connection.channel.direct.Session;
    2124import net.schmizz.sshj.connection.channel.direct.Session.Command;
     25import net.schmizz.sshj.sftp.FileAttributes;
     26import net.schmizz.sshj.sftp.OpenMode;
     27import net.schmizz.sshj.sftp.RemoteFile;
     28import net.schmizz.sshj.sftp.SFTPClient;
    2229import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
    2330import net.schmizz.sshj.xfer.LocalDestFile;
     
    2633import net.sf.basedb.reggie.converter.StringToDateConverter;
    2734import net.sf.basedb.util.FileCopyRunnable;
     35import net.sf.basedb.util.uri.CloseResourceInputStream;
    2836
    2937/**
     
    326334  }
    327335 
     336  public OutputStream writeFile(SFTPClient sftp, String toPath, FileAttributes attributes)
     337  {
     338    if (logger.isDebugEnabled())
     339    {
     340      logger.debug("Writing to " + getAddress() + ":" + getPort() + ":" + toPath);
     341    }
     342    try
     343    {
     344      String toDir = toPath.substring(0, toPath.lastIndexOf("/"));
     345      sftp.mkdirs(toDir);
     346      RemoteFile rf = sftp.open(toPath, EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE), attributes);
     347      return new CloseResourceOutputStream(rf.new RemoteFileOutputStream(), rf);
     348    }
     349    catch (IOException ex)
     350    {
     351      logger.error("File upload failed", ex);
     352      throw new RuntimeException(ex);
     353    }
     354    finally
     355    {}
     356  }
     357 
    328358  public void uploadFile(SSHClient ssh, LocalSourceFile file, String toPath)
    329359  {
     
    357387  }
    358388
     389  /**
     390    Read from a file on the remote server. This method need an
     391    SFTPClient object which can be created from SSHClient.newSFTPClient().
     392
     393    @param sftp A SFTPClient instance connected to the remote host
     394    @param fromPath The path of the remote file
     395    @return An InputStream reading from the remote file
     396  */
     397  public InputStream readFile(SFTPClient sftp, String fromPath)
     398  {
     399    if (logger.isDebugEnabled())
     400    {
     401      logger.debug("Reading file '" + fromPath + "' from " + getAddress() + ":" + getPort());
     402    }
     403    try
     404    {
     405      RemoteFile rf = sftp.open(fromPath, EnumSet.of(OpenMode.READ));
     406      return new CloseResourceInputStream(rf.new RemoteFileInputStream(), rf);
     407    }
     408    catch (IOException ex)
     409    {
     410      logger.error("File download failed", ex);
     411      throw new RuntimeException(ex);
     412    }
     413    finally
     414    {}
     415  }
     416 
     417  /**
     418    Download a remote file to a local destination.
     419    @param ssh A SSHClient instance connected to the remote host
     420    @param fromPath The path of the remote file
     421    @param file The local destination the remote file is downloaded to
     422  */
    359423  public void downloadFile(SSHClient ssh, String fromPath, LocalDestFile file)
    360424  {
Note: See TracChangeset for help on using the changeset viewer.