Search in sources :

Example 1 with SftpException

use of com.jn.agileway.ssh.client.sftp.exception.SftpException 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 2 with SftpException

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

the class JschSftpFile method read.

@Override
public int read(long fileOffset, byte[] buffer, int bufferOffset, int length) throws IOException {
    ChannelSftp channel = ((JschSftpSession) this.session).getChannel();
    InputStream inputStream = null;
    try {
        inputStream = channel.get(this.path, null, fileOffset);
        int readLength = inputStream.read(buffer, bufferOffset, length);
        return readLength;
    } catch (Throwable ex) {
        throw new SftpException(ex);
    } finally {
        IOs.close(inputStream);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException)

Example 3 with SftpException

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

the class JschSftpFile method write.

@Override
public void write(long fileOffset, byte[] data, int offset, int length) throws IOException {
    ChannelSftp channel = ((JschSftpSession) this.session).getChannel();
    if (length < 0) {
        throw new IllegalArgumentException("length < 0");
    }
    if (offset >= data.length) {
        throw new IllegalArgumentException("bufferOffset >= buffer.length");
    }
    if (length == 0) {
        return;
    }
    if (offset + length > data.length) {
        length = data.length - offset;
    }
    byte[] buffer = new byte[length];
    System.arraycopy(data, offset, buffer, 0, length);
    try {
        channel.put(new ByteArrayInputStream(buffer), this.path, fileOffset == 0 ? ChannelSftp.OVERWRITE : ChannelSftp.APPEND);
    } catch (Throwable ex) {
        throw new SftpException(ex);
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) ByteArrayInputStream(java.io.ByteArrayInputStream) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException)

Example 4 with SftpException

use of com.jn.agileway.ssh.client.sftp.exception.SftpException 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 5 with SftpException

use of com.jn.agileway.ssh.client.sftp.exception.SftpException 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)

Aggregations

SftpException (com.jn.agileway.ssh.client.sftp.exception.SftpException)8 NoSuchFileSftpException (com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)6 ResponseStatusCode (com.jn.agileway.ssh.client.sftp.ResponseStatusCode)4 ChannelSftp (com.jcraft.jsch.ChannelSftp)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 SFTPException (ch.ethz.ssh2.SFTPException)1 FileAttrs (com.jn.agileway.ssh.client.sftp.attrs.FileAttrs)1 FileMode (com.jn.agileway.ssh.client.sftp.attrs.FileMode)1 SFTPException (com.trilead.ssh2.SFTPException)1 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)1 InputStream (java.io.InputStream)1