Search in sources :

Example 16 with SFTPException

use of com.trilead.ssh2.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)

Example 17 with SFTPException

use of com.trilead.ssh2.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(com.trilead.ssh2.SFTPException)

Example 18 with SFTPException

use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.

the class SFTPv3Client method read.

/**
 * Read bytes from a file. No more than 32768 bytes may be read at once.
 * Be aware that the semantics of read() are different than for Java streams.
 * <p>
 * <ul>
 * <li>The server will read as many bytes as it can from the file (up to <code>len</code>),
 * and return them.</li>
 * <li>If EOF is encountered before reading any data, <code>-1</code> is returned.</li>
 * <li>If an error occurs, an exception is thrown.</li>
 * <li>For normal disk files, it is guaranteed that the server will return the specified
 * number of bytes, or up to end of file. For, e.g., device files this may return
 * fewer bytes than requested.</li>
 * </ul>
 *
 * @param handle a SFTPv3FileHandle handle
 * @param fileOffset offset (in bytes) in the file
 * @param dst the destination byte array
 * @param dstoff offset in the destination byte array
 * @param len how many bytes to read, 0 &lt; len &lt;= 32768 bytes
 * @return the number of bytes that could be read, may be less than requested if
 *         the end of the file is reached, -1 is returned in case of <code>EOF</code>
 * @throws IOException
 */
public int read(SFTPv3FileHandle handle, long fileOffset, byte[] dst, int dstoff, int len) throws IOException {
    checkHandleValidAndOpen(handle);
    if ((len > 32768) || (len <= 0))
        throw new IllegalArgumentException("invalid len argument");
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(handle.fileHandle, 0, handle.fileHandle.length);
    tw.writeUINT64(fileOffset);
    tw.writeUINT32(len);
    if (debug != null) {
        debug.println("Sending SSH_FXP_READ...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_READ, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_DATA) {
        if (debug != null) {
            debug.println("Got SSH_FXP_DATA...");
            debug.flush();
        }
        int readLen = tr.readUINT32();
        if ((readLen < 0) || (readLen > len))
            throw new IOException("The server sent an invalid length field.");
        tr.readBytes(dst, dstoff, readLen);
        return readLen;
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    if (errorCode == ErrorCodes.SSH_FX_EOF) {
        if (debug != null) {
            debug.println("Got SSH_FX_EOF.");
            debug.flush();
        }
        return -1;
    }
    String errorMessage = tr.readString();
    throw new SFTPException(errorMessage, errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) IOException(java.io.IOException) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 19 with SFTPException

use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.

the class SFTPv3Client method statBoth.

private SFTPv3FileAttributes statBoth(String path, int statMethod) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_STAT/SSH_FXP_LSTAT...");
        debug.flush();
    }
    sendMessage(statMethod, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_ATTRS) {
        return readAttrs(tr);
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) IOException(java.io.IOException) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 20 with SFTPException

use of com.trilead.ssh2.SFTPException in project sshlib by connectbot.

the class SFTPv3Client method readLink.

/**
 * Read the target of a symbolic link.
 *
 * @param path See the {@link SFTPv3Client comment} for the class for more details.
 * @return The target of the link.
 * @throws IOException
 */
public String readLink(String path) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_READLINK...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_READLINK, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_NAME) {
        int count = tr.readUINT32();
        if (count != 1)
            throw new IOException("The server sent an invalid SSH_FXP_NAME packet.");
        return tr.readString(charsetName);
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) IOException(java.io.IOException) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Aggregations

TypesReader (com.trilead.ssh2.packets.TypesReader)20 TypesWriter (com.trilead.ssh2.packets.TypesWriter)18 IOException (java.io.IOException)11 NoSuchFileSftpException (com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)3 SftpException (com.jn.agileway.ssh.client.sftp.exception.SftpException)3 SFTPException (com.trilead.ssh2.SFTPException)3 ResponseStatusCode (com.jn.agileway.ssh.client.sftp.ResponseStatusCode)2 Connection (com.trilead.ssh2.Connection)2 SFTPv3Client (com.trilead.ssh2.SFTPv3Client)2 Vector (java.util.Vector)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 SFTPv3FileAttributes (com.trilead.ssh2.SFTPv3FileAttributes)1 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)1 FileNotFoundException (java.io.FileNotFoundException)1