Search in sources :

Example 1 with SCPException

use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.

the class SCPClientImpl method put.

public void put(String local, String remote, String mode) throws SCPException {
    if (connection == null) {
        throw new SCPException("Connection is not established!");
    }
    if (scpClient == null) {
        throw new SCPException("It is not authenticated!");
    }
    File localFile = null;
    try {
        localFile = new File(local);
        if (homeDir != null && !localFile.isAbsolute()) {
            localFile = new File(homeDir, local);
        }
        if (!localFile.exists()) {
            throw new SCPException("File not exists! path=" + local);
        }
        File remoteFile = new File(remote);
        if (localFile.getName().equals(remoteFile.getName())) {
            if (mode == null) {
                scpClient.put(localFile.getPath(), remoteFile.getParentFile() == null ? "." : remoteFile.getParentFile().getPath());
            } else {
                scpClient.put(localFile.getPath(), remoteFile.getParentFile() == null ? "." : remoteFile.getParentFile().getPath(), mode);
            }
        } else {
            FileInputStream fis = null;
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                fis = new FileInputStream(localFile);
                int length = 0;
                byte[] buf = new byte[1024];
                while ((length = fis.read(buf)) != -1) {
                    baos.write(buf, 0, length);
                }
            } catch (IOException e) {
                throw new SCPException("It failed to read path=" + localFile, e);
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                    }
                }
            }
            if (mode == null) {
                scpClient.put(baos.toByteArray(), remoteFile.getName(), remoteFile.getParentFile() == null ? "." : remoteFile.getParentFile().getPath());
            } else {
                scpClient.put(baos.toByteArray(), remoteFile.getName(), remoteFile.getParentFile() == null ? "." : remoteFile.getParentFile().getPath(), mode);
            }
        }
    } catch (IOException e) {
        throw new SCPException("It failed to put! file=" + localFile, e);
    }
}
Also used : SCPException(jp.ossc.nimbus.service.scp.SCPException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) File(java.io.File) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) FileInputStream(java.io.FileInputStream)

Example 2 with SCPException

use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.

the class SCPClientImpl method mput.

public void mput(String local, String remoteDir, String mode) throws SCPException {
    if (connection == null) {
        throw new SCPException("Connection is not established!");
    }
    if (scpClient == null) {
        throw new SCPException("It is not authenticated!");
    }
    RecurciveSearchFile rsf = new RecurciveSearchFile(".");
    File[] localFiles = rsf.listAllTreeFiles(local);
    try {
        for (int i = 0; i < localFiles.length; i++) {
            if (mode == null) {
                scpClient.put(localFiles[i].getPath(), remoteDir);
            } else {
                scpClient.put(localFiles[i].getPath(), remoteDir, mode);
            }
        }
    } catch (IOException e) {
        throw new SCPException("It failed to mput! local=" + local, e);
    }
}
Also used : RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) SCPException(jp.ossc.nimbus.service.scp.SCPException) IOException(java.io.IOException) File(java.io.File) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile)

Example 3 with SCPException

use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.

the class SCPClientImpl method get.

public File get(String remote) throws SCPException {
    if (connection == null) {
        throw new SCPException("Connection is not established!");
    }
    if (scpClient == null) {
        throw new SCPException("It is not authenticated!");
    }
    File localFile = null;
    try {
        File file = new File(remote);
        String name = file.getName();
        localFile = homeDir == null ? new File(name) : new File(homeDir, name);
        scpClient.get(remote, homeDir == null ? "." : homeDir.getPath());
    } catch (IOException e) {
        throw new SCPException("It failed to get! file=" + remote, e);
    }
    return localFile;
}
Also used : SCPException(jp.ossc.nimbus.service.scp.SCPException) IOException(java.io.IOException) File(java.io.File) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile)

Example 4 with SCPException

use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.

the class SCPClientImpl method mget.

public File[] mget(String remote, String localDir) throws SCPException {
    if (remote == null || remote.length() == 0) {
        throw new SCPException("Path is null.");
    }
    if (localDir == null) {
        localDir = ".";
    }
    File localDirFile = new File(localDir);
    if (homeDir != null && !localDirFile.isAbsolute()) {
        localDirFile = new File(homeDir, localDir);
    }
    List localFiles = new ArrayList();
    Session session = null;
    final String cmd = "scp -f " + remote;
    File localFile = null;
    try {
        session = connection.openSession();
        session.execCommand(cmd);
        byte[] buf = new byte[1024];
        OutputStream os = new BufferedOutputStream(session.getStdin(), 512);
        InputStream is = new BufferedInputStream(session.getStdout(), 40000);
        os.write(0);
        os.flush();
        while (true) {
            int c = checkAck(is);
            if (c == -1) {
                if (localFiles.size() == 0) {
                    throw new IOException("Remote SCP terminated unexpectedly.");
                }
                break;
            }
            if (c != 'C') {
                break;
            }
            is.read(buf, 0, 5);
            long fileSize = 0L;
            while (true) {
                if (is.read(buf, 0, 1) < 0) {
                    throw new SCPException("Unexpected EOF.");
                }
                if (buf[0] == ' ') {
                    break;
                }
                fileSize = fileSize * 10L + (long) (buf[0] - '0');
            }
            String fileName = null;
            for (int i = 0; ; i++) {
                is.read(buf, i, 1);
                if (buf[i] == (byte) 0x0a) {
                    fileName = new String(buf, 0, i);
                    break;
                }
            }
            buf[0] = 0;
            os.write(buf, 0, 1);
            os.flush();
            localFile = new File(localDirFile, fileName);
            localFiles.add(localFile);
            FileOutputStream fos = new FileOutputStream(localFile);
            try {
                int readLen = 0;
                while (true) {
                    if (buf.length < fileSize) {
                        readLen = buf.length;
                    } else {
                        readLen = (int) fileSize;
                    }
                    readLen = is.read(buf, 0, readLen);
                    if (readLen < 0) {
                        throw new SCPException("Unexpected EOF.");
                    }
                    fos.write(buf, 0, readLen);
                    fileSize -= readLen;
                    if (fileSize == 0L) {
                        break;
                    }
                }
            } finally {
                fos.close();
                fos = null;
            }
            checkAck(is);
            localFile = null;
            buf[0] = 0;
            os.write(buf, 0, 1);
            os.flush();
        }
    } catch (IOException e) {
        throw new SCPException("It failed to mget! from=" + remote + ", to=" + (localFile == null ? localDirFile : localFile), e);
    } finally {
        if (session != null) {
            session.close();
        }
    }
    return (File[]) localFiles.toArray(new File[localFiles.size()]);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SCPException(jp.ossc.nimbus.service.scp.SCPException) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) RecurciveSearchFile(jp.ossc.nimbus.io.RecurciveSearchFile) BufferedOutputStream(java.io.BufferedOutputStream) Session(ch.ethz.ssh2.Session)

Example 5 with SCPException

use of jp.ossc.nimbus.service.scp.SCPException in project nimbus by nimbus-org.

the class SCPClientImpl method connect.

public void connect(String user, String host, int port, String password) throws SCPException {
    if (connection != null) {
        throw new SCPException("It is already connected!");
    }
    connection = new Connection(host, port);
    try {
        if (isTcpNoDelay != null) {
            connection.setTCPNoDelay(isTcpNoDelay.booleanValue());
        }
        if (serverHostKeyAlgorithms != null) {
            connection.setServerHostKeyAlgorithms(serverHostKeyAlgorithms);
        }
        connection.connect(null, connectionTimeout, keyExchangeTimeout);
        if (!connection.authenticateWithPassword(user, password)) {
            throw new SCPException("It failed to authenticate!");
        }
        scpClient = connection.createSCPClient();
    } catch (IOException e) {
        scpClient = null;
        connection.close();
        connection = null;
        throw new SCPException("It failed to connect!", e);
    }
}
Also used : SCPException(jp.ossc.nimbus.service.scp.SCPException) Connection(ch.ethz.ssh2.Connection) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)6 SCPException (jp.ossc.nimbus.service.scp.SCPException)6 File (java.io.File)4 RecurciveSearchFile (jp.ossc.nimbus.io.RecurciveSearchFile)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 Connection (ch.ethz.ssh2.Connection)2 Session (ch.ethz.ssh2.Session)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1