Search in sources :

Example 1 with NoSuchFileSftpException

use of com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException in project agileway by fangjinuo.

the class Ssh2SftpSession method open.

@Override
public SftpFile open(String filepath, int openMode, FileAttrs attrs) throws SftpException {
    try {
        SFTPv3FileHandle handle = null;
        if (!Sftps.exists(this, filepath)) {
            if (attrs == null) {
                attrs = new FileAttrs();
            }
            FileMode fileMode = attrs.getFileMode();
            if (fileMode == null) {
                fileMode = FileMode.createFileMode(FileType.REGULAR, 0644);
                attrs.setFileMode(fileMode);
            }
            if (OpenMode.isCreatable(openMode)) {
                handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
            } else {
                throw new NoSuchFileSftpException(StringTemplates.formatWithPlaceholder("no such file: {}", filepath));
            }
        } else {
            if (OpenMode.isTruncated(openMode)) {
                handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
            }
        }
        if (handle != null) {
            sftpClient.closeFile(handle);
            handle = null;
        }
        if (OpenMode.isWritable(openMode)) {
            handle = sftpClient.openFileRW(filepath);
        } else {
            handle = sftpClient.openFileRO(filepath);
        }
        Ssh2SftpFile sftpFile = new Ssh2SftpFile(this, filepath);
        sftpFile.setFileHandle(handle);
        return sftpFile;
    } catch (Throwable ex) {
        throw Ssh2Sftps.wrapSftpException(ex);
    }
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SFTPv3FileHandle(ch.ethz.ssh2.SFTPv3FileHandle)

Example 2 with NoSuchFileSftpException

use of com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException in project agileway by fangjinuo.

the class Ssh2SftpSession method open.

@Override
public SftpFile open(String filepath, int openMode, FileAttrs attrs) throws SftpException {
    try {
        SFTPv3FileHandle handle = null;
        if (!Sftps.exists(this, filepath)) {
            if (attrs == null) {
                attrs = new FileAttrs();
            }
            FileMode fileMode = attrs.getFileMode();
            if (fileMode == null) {
                fileMode = FileMode.createFileMode(FileType.REGULAR, 0644);
                attrs.setFileMode(fileMode);
            }
            if (OpenMode.isCreatable(openMode)) {
                handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
            } else {
                throw new NoSuchFileSftpException(StringTemplates.formatWithPlaceholder("no such file: {}", filepath));
            }
        } else {
            if (OpenMode.isTruncated(openMode)) {
                handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
            }
        }
        if (handle != null) {
            sftpClient.closeFile(handle);
            handle = null;
        }
        if (OpenMode.isWritable(openMode)) {
            handle = sftpClient.openFileRW(filepath);
        } else {
            handle = sftpClient.openFileRO(filepath);
        }
        Ssh2SftpFile sftpFile = new Ssh2SftpFile(this, filepath);
        sftpFile.setFileHandle(handle);
        return sftpFile;
    } catch (Throwable ex) {
        throw new SftpException(ex);
    }
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SFTPv3FileHandle(com.trilead.ssh2.SFTPv3FileHandle) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException)

Example 3 with NoSuchFileSftpException

use of com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException in project agileway by fangjinuo.

the class JschSftps method wrapSftpException.

public static SftpException wrapSftpException(com.jcraft.jsch.SftpException ex) {
    ResponseStatusCode statusCode = Enums.ofCode(ResponseStatusCode.class, ex.id);
    SftpException exception = null;
    if (statusCode == ResponseStatusCode.NO_SUCH_FILE) {
        exception = new NoSuchFileSftpException(ex);
    } else {
        exception = new SftpException(ex);
    }
    exception.setStatusCode(statusCode);
    return exception;
}
Also used : NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) ResponseStatusCode(com.jn.agileway.ssh.client.sftp.ResponseStatusCode) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)

Example 4 with NoSuchFileSftpException

use of com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException in project agileway by fangjinuo.

the class Ssh2Sftps method wrapSftpException.

public static SftpException wrapSftpException(Throwable ex) {
    if (ex instanceof SFTPException) {
        ResponseStatusCode statusCode = Enums.ofCode(ResponseStatusCode.class, ((SFTPException) ex).getServerErrorCode());
        SftpException exception = null;
        if (statusCode == ResponseStatusCode.NO_SUCH_FILE) {
            exception = new NoSuchFileSftpException(ex);
        } else {
            exception = new SftpException(ex);
        }
        exception.setStatusCode(statusCode);
        return exception;
    } else {
        return new SftpException(ex);
    }
}
Also used : NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) ResponseStatusCode(com.jn.agileway.ssh.client.sftp.ResponseStatusCode) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SFTPException(ch.ethz.ssh2.SFTPException)

Example 5 with NoSuchFileSftpException

use of com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException in project agileway by fangjinuo.

the class SshjSftps method wrapSftpException.

public static SftpException wrapSftpException(Throwable ex) {
    if (ex instanceof SFTPException) {
        ResponseStatusCode statusCode = Enums.ofName(ResponseStatusCode.class, ((SFTPException) ex).getStatusCode().name());
        SftpException exception = null;
        if (statusCode == ResponseStatusCode.NO_SUCH_FILE) {
            exception = new NoSuchFileSftpException(ex);
        } else {
            exception = new SftpException(ex);
        }
        exception.setStatusCode(statusCode);
        return exception;
    } else {
        return new SftpException(ex);
    }
}
Also used : NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) ResponseStatusCode(com.jn.agileway.ssh.client.sftp.ResponseStatusCode) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)

Aggregations

NoSuchFileSftpException (com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)6 SftpException (com.jn.agileway.ssh.client.sftp.exception.SftpException)5 ResponseStatusCode (com.jn.agileway.ssh.client.sftp.ResponseStatusCode)4 FileAttrs (com.jn.agileway.ssh.client.sftp.attrs.FileAttrs)2 FileMode (com.jn.agileway.ssh.client.sftp.attrs.FileMode)2 SFTPException (ch.ethz.ssh2.SFTPException)1 SFTPv3FileHandle (ch.ethz.ssh2.SFTPv3FileHandle)1 SFTPException (com.trilead.ssh2.SFTPException)1 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)1