Search in sources :

Example 56 with SftpException

use of com.jcraft.jsch.SftpException in project incubator-gobblin by apache.

the class SftpFsHelper method getFileSize.

@Override
public long getFileSize(String filePath) throws FileBasedHelperException {
    try {
        ChannelSftp channelSftp = getSftpChannel();
        long fileSize = channelSftp.lstat(filePath).getSize();
        channelSftp.disconnect();
        return fileSize;
    } catch (SftpException e) {
        throw new FileBasedHelperException(String.format("Failed to get size for file at path %s due to error %s", filePath, e.getMessage()), e);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) FileBasedHelperException(org.apache.gobblin.source.extractor.filebased.FileBasedHelperException) SftpException(com.jcraft.jsch.SftpException)

Example 57 with SftpException

use of com.jcraft.jsch.SftpException in project incubator-gobblin by apache.

the class SftpFsHelper method getExecChannel.

/**
 * Create a new sftp channel to execute commands.
 *
 * @param command to execute on the remote machine
 * @return a new execution channel
 * @throws SftpException if a channel could not be opened
 */
public ChannelExec getExecChannel(String command) throws SftpException {
    ChannelExec channelExec;
    try {
        channelExec = (ChannelExec) this.session.openChannel("exec");
        channelExec.setCommand(command);
        channelExec.connect();
        return channelExec;
    } catch (JSchException e) {
        throw new SftpException(0, "Cannot open a channel to SFTP server", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) SftpException(com.jcraft.jsch.SftpException) ChannelExec(com.jcraft.jsch.ChannelExec)

Example 58 with SftpException

use of com.jcraft.jsch.SftpException in project incubator-gobblin by apache.

the class SftpFsHelper method getSftpChannel.

/**
 * Create new channel every time a command needs to be executed. This is required to support execution of multiple
 * commands in parallel. All created channels are cleaned up when the session is closed.
 *
 * @return a new {@link ChannelSftp}
 * @throws SftpException
 */
public ChannelSftp getSftpChannel() throws SftpException {
    try {
        ChannelSftp channelSftp = (ChannelSftp) this.session.openChannel("sftp");
        // In millsec
        int connTimeout = state.getPropAsInt(SFTP_CONNECTION_TIMEOUT_KEY, DEFAULT_SFTP_CONNECTION_TIMEOUT);
        channelSftp.connect(connTimeout);
        return channelSftp;
    } catch (JSchException e) {
        throw new SftpException(0, "Cannot open a channel to SFTP server", e);
    }
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException)

Example 59 with SftpException

use of com.jcraft.jsch.SftpException in project incubator-gobblin by apache.

the class SftpLightWeightFileSystem method mkdirs.

@Override
public boolean mkdirs(Path path, FsPermission permission) throws IOException {
    ChannelSftp channel = null;
    try {
        channel = this.fsHelper.getSftpChannel();
        channel.mkdir(HadoopUtils.toUriPath(path));
        channel.chmod(permission.toShort(), HadoopUtils.toUriPath(path));
    } catch (SftpException e) {
        throw new IOException(e);
    } finally {
        safeDisconnect(channel);
    }
    return true;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException)

Example 60 with SftpException

use of com.jcraft.jsch.SftpException in project hadoop by apache.

the class SFTPFileSystem method listStatus.

/**
   * Convenience method, so that we don't open a new connection when using this
   * method from within another method. Otherwise every API invocation incurs
   * the overhead of opening/closing a TCP connection.
   */
@SuppressWarnings("unchecked")
private FileStatus[] listStatus(ChannelSftp client, Path file) throws IOException {
    Path workDir;
    try {
        workDir = new Path(client.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, file);
    FileStatus fileStat = getFileStatus(client, absolute);
    if (!fileStat.isDirectory()) {
        return new FileStatus[] { fileStat };
    }
    Vector<LsEntry> sftpFiles;
    try {
        sftpFiles = (Vector<LsEntry>) client.ls(absolute.toUri().getPath());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    ArrayList<FileStatus> fileStats = new ArrayList<FileStatus>();
    for (int i = 0; i < sftpFiles.size(); i++) {
        LsEntry entry = sftpFiles.get(i);
        String fname = entry.getFilename();
        // skip current and parent directory, ie. "." and ".."
        if (!".".equalsIgnoreCase(fname) && !"..".equalsIgnoreCase(fname)) {
            fileStats.add(getFileStatus(client, entry, absolute));
        }
    }
    return fileStats.toArray(new FileStatus[fileStats.size()]);
}
Also used : Path(org.apache.hadoop.fs.Path) FileStatus(org.apache.hadoop.fs.FileStatus) SftpException(com.jcraft.jsch.SftpException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Aggregations

SftpException (com.jcraft.jsch.SftpException)113 ChannelSftp (com.jcraft.jsch.ChannelSftp)67 IOException (java.io.IOException)49 JSchException (com.jcraft.jsch.JSchException)28 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)24 File (java.io.File)24 SftpATTRS (com.jcraft.jsch.SftpATTRS)16 Path (org.apache.hadoop.fs.Path)15 Session (com.jcraft.jsch.Session)13 OutputStream (java.io.OutputStream)11 JSch (com.jcraft.jsch.JSch)10 FileNotFoundException (java.io.FileNotFoundException)10 InputStream (java.io.InputStream)10 Channel (com.jcraft.jsch.Channel)9 GenericFileOperationFailedException (org.apache.camel.component.file.GenericFileOperationFailedException)9 FileStatus (org.apache.hadoop.fs.FileStatus)9 ArrayList (java.util.ArrayList)8 Vector (java.util.Vector)8 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 Test (org.junit.Test)8