Search in sources :

Example 91 with SftpException

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

the class ScpFromMessage method setLastModified.

private void setLastModified(final File localFile) throws JSchException {
    SftpATTRS fileAttributes = null;
    final ChannelSftp channel = openSftpChannel();
    channel.connect();
    try {
        fileAttributes = channel.lstat(remoteDir(remoteFile) + localFile.getName());
    } catch (final SftpException e) {
        throw new JSchException("failed to stat remote file", e);
    }
    FileUtils.getFileUtils().setFileLastModified(localFile, ((long) fileAttributes.getMTime()) * 1000);
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 92 with SftpException

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

the class ScpFromMessageBySftp method execute.

/**
 * Carry out the transfer.
 * @throws IOException on i/o errors
 * @throws JSchException on errors detected by scp
 */
@Override
public void execute() throws IOException, JSchException {
    final ChannelSftp channel = openSftpChannel();
    try {
        channel.connect();
        try {
            final SftpATTRS attrs = channel.stat(remoteFile);
            if (attrs.isDir() && !remoteFile.endsWith("/")) {
                remoteFile = remoteFile + "/";
            }
        } catch (final SftpException ee) {
        // Ignored
        }
        getDir(channel, remoteFile, localFile);
    } catch (final SftpException e) {
        throw new JSchException("Could not get '" + remoteFile + "' to '" + localFile + "' - " + e.toString(), e);
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
    log("done\n");
}
Also used : JSchException(com.jcraft.jsch.JSchException) ChannelSftp(com.jcraft.jsch.ChannelSftp) SftpATTRS(com.jcraft.jsch.SftpATTRS) SftpException(com.jcraft.jsch.SftpException)

Example 93 with SftpException

use of com.jcraft.jsch.SftpException in project DataX by alibaba.

the class SftpHelper method getListFiles.

@Override
public HashSet<String> getListFiles(String directoryPath, int parentLevel, int maxTraversalLevel) {
    if (parentLevel < maxTraversalLevel) {
        // 父级目录,以'/'结尾
        String parentPath = null;
        int pathLen = directoryPath.length();
        if (directoryPath.contains("*") || directoryPath.contains("?")) {
            // *和?的限制
            // path是正则表达式
            String subPath = UnstructuredStorageReaderUtil.getRegexPathParentPath(directoryPath);
            if (isDirExist(subPath)) {
                parentPath = subPath;
            } else {
                String message = String.format("不能进入目录:[%s]," + "请确认您的配置项path:[%s]存在,且配置的用户有权限进入", subPath, directoryPath);
                LOG.error(message);
                throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
            }
        } else if (isDirExist(directoryPath)) {
            // path是目录
            if (directoryPath.charAt(pathLen - 1) == IOUtils.DIR_SEPARATOR) {
                parentPath = directoryPath;
            } else {
                parentPath = directoryPath + IOUtils.DIR_SEPARATOR;
            }
        } else if (isSymbolicLink(directoryPath)) {
            // path是链接文件
            String message = String.format("文件:[%s]是链接文件,当前不支持链接文件的读取", directoryPath);
            LOG.error(message);
            throw DataXException.asDataXException(FtpReaderErrorCode.LINK_FILE, message);
        } else if (isFileExist(directoryPath)) {
            // path指向具体文件
            sourceFiles.add(directoryPath);
            return sourceFiles;
        } else {
            String message = String.format("请确认您的配置项path:[%s]存在,且配置的用户有权限读取", directoryPath);
            LOG.error(message);
            throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
        }
        try {
            Vector vector = channelSftp.ls(directoryPath);
            for (int i = 0; i < vector.size(); i++) {
                LsEntry le = (LsEntry) vector.get(i);
                String strName = le.getFilename();
                String filePath = parentPath + strName;
                if (isDirExist(filePath)) {
                    // 是子目录
                    if (!(strName.equals(".") || strName.equals(".."))) {
                        // 递归处理
                        getListFiles(filePath, parentLevel + 1, maxTraversalLevel);
                    }
                } else if (isSymbolicLink(filePath)) {
                    // 是链接文件
                    String message = String.format("文件:[%s]是链接文件,当前不支持链接文件的读取", filePath);
                    LOG.error(message);
                    throw DataXException.asDataXException(FtpReaderErrorCode.LINK_FILE, message);
                } else if (isFileExist(filePath)) {
                    // 是文件
                    sourceFiles.add(filePath);
                } else {
                    String message = String.format("请确认path:[%s]存在,且配置的用户有权限读取", filePath);
                    LOG.error(message);
                    throw DataXException.asDataXException(FtpReaderErrorCode.FILE_NOT_EXISTS, message);
                }
            }
        // end for vector
        } catch (SftpException e) {
            String message = String.format("获取path:[%s] 下文件列表时发生I/O异常,请确认与ftp服务器的连接正常", directoryPath);
            LOG.error(message);
            throw DataXException.asDataXException(FtpReaderErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
        }
        return sourceFiles;
    } else {
        // 超出最大递归层数
        String message = String.format("获取path:[%s] 下文件列表时超出最大层数,请确认路径[%s]下不存在软连接文件", directoryPath, directoryPath);
        LOG.error(message);
        throw DataXException.asDataXException(FtpReaderErrorCode.OUT_MAX_DIRECTORY_LEVEL, message);
    }
}
Also used : SftpException(com.jcraft.jsch.SftpException) Vector(java.util.Vector) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry)

Example 94 with SftpException

use of com.jcraft.jsch.SftpException in project DataX by alibaba.

the class SftpHelperImpl method getOutputStream.

@Override
public OutputStream getOutputStream(String filePath) {
    try {
        this.printWorkingDirectory();
        String parentDir = filePath.substring(0, StringUtils.lastIndexOf(filePath, IOUtils.DIR_SEPARATOR));
        this.channelSftp.cd(parentDir);
        this.printWorkingDirectory();
        OutputStream writeOutputStream = this.channelSftp.put(filePath, ChannelSftp.APPEND);
        String message = String.format("打开FTP文件[%s]获取写出流时出错,请确认文件%s有权限创建,有权限写出等", filePath, filePath);
        if (null == writeOutputStream) {
            throw DataXException.asDataXException(FtpWriterErrorCode.OPEN_FILE_ERROR, message);
        }
        return writeOutputStream;
    } catch (SftpException e) {
        String message = String.format("写出文件[%s] 时出错,请确认文件%s有权限写出, errorMessage:%s", filePath, filePath, e.getMessage());
        LOG.error(message);
        throw DataXException.asDataXException(FtpWriterErrorCode.OPEN_FILE_ERROR, message);
    }
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SftpException(com.jcraft.jsch.SftpException)

Example 95 with SftpException

use of com.jcraft.jsch.SftpException in project DataX by alibaba.

the class SftpHelperImpl method getAllFilesInDir.

@Override
public Set<String> getAllFilesInDir(String dir, String prefixFileName) {
    Set<String> allFilesWithPointedPrefix = new HashSet<String>();
    try {
        this.printWorkingDirectory();
        @SuppressWarnings("rawtypes") Vector allFiles = this.channelSftp.ls(dir);
        LOG.debug(String.format("ls: %s", JSON.toJSONString(allFiles, SerializerFeature.UseSingleQuotes)));
        for (int i = 0; i < allFiles.size(); i++) {
            LsEntry le = (LsEntry) allFiles.get(i);
            String strName = le.getFilename();
            if (strName.startsWith(prefixFileName)) {
                allFilesWithPointedPrefix.add(strName);
            }
        }
    } catch (SftpException e) {
        String message = String.format("获取path:[%s] 下文件列表时发生I/O异常,请确认与ftp服务器的连接正常,拥有目录ls权限, errorMessage:%s", dir, e.getMessage());
        LOG.error(message);
        throw DataXException.asDataXException(FtpWriterErrorCode.COMMAND_FTP_IO_EXCEPTION, message, e);
    }
    return allFilesWithPointedPrefix;
}
Also used : SftpException(com.jcraft.jsch.SftpException) Vector(java.util.Vector) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) HashSet(java.util.HashSet)

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