Search in sources :

Example 96 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project structr by structr.

the class SSHFilesTest method test06DeleteDirectory.

@Test
public void test06DeleteDirectory() {
    final ChannelSftp sftp = setupSftpClient("ftpuser1", "ftpuserpw1");
    try {
        // create some dirs
        sftp.mkdir("/files/test1");
        sftp.mkdir("/files/test2");
        sftp.mkdir("/files/test2/nested1");
        sftp.mkdir("/files/test2/nested1/nested2");
        // delete one dir
        sftp.rmdir("/files/test2/nested1/nested2");
        // byebye
        sftp.disconnect();
    } catch (SftpException ex) {
        fail("Unexpected exception: " + ex.getMessage());
    }
    try (final Tx tx = app.tx()) {
        assertEquals("Folder test1 should exist", "test1", app.nodeQuery(Folder.class).andName("test1").getFirst().getName());
        assertEquals("Folder test2 should exist", "test2", app.nodeQuery(Folder.class).andName("test2").getFirst().getName());
        assertEquals("Folder nested1 should exist", "nested1", app.nodeQuery(Folder.class).andName("nested1").getFirst().getName());
        assertNull("Folder nested2 should have been deleted", app.nodeQuery(Folder.class).andName("nested2").getFirst());
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception: " + fex.getMessage());
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) SftpException(com.jcraft.jsch.SftpException) Folder(org.structr.web.entity.Folder) SSHTest(org.structr.web.files.SSHTest) Test(org.junit.Test)

Example 97 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project structr by structr.

the class SSHFilesTest method test03MoveFile.

@Test
public void test03MoveFile() {
    ChannelSftp sftp = setupSftpClient("ftpuser1", "ftpuserpw1");
    final String testContent1 = "Test Content öäü";
    final String name1 = "file1.txt";
    final String name2 = "fileöäüß.txt";
    String date = null;
    try {
        sftp.mkdir("/files/dir1");
        sftp.mkdir("/files/dir2");
        try (final OutputStream os = sftp.put("/files/dir1/" + name1)) {
            IOUtils.write(testContent1, os);
            os.flush();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
        sftp.rename("/files/dir1/" + name1, "/files/dir2/" + name2);
        date = getDateStringDependingOnCurrentDayOfMonth();
    } catch (SftpException ex) {
        logger.warn("", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
    try {
        final Vector<LsEntry> entries = sftp.ls("/files/dir2");
        // listing contains "." and ".." => 3 entries
        assertEquals("Invalid result size for directory", 3, entries.size());
        final LsEntry file = entries.get(2);
        // check attributes
        assertEquals("Invalid test file name", name2, file.getFilename());
        assertEquals("Invalid permissions on test file", "-rw-------", file.getAttrs().getPermissionsString());
        assertEquals("Invalid flags on test file", 13, file.getAttrs().getFlags());
        assertEquals("Invalid test file size", 19, file.getAttrs().getSize());
        assertEquals("Invalid string representation of test file", "-rw-------   1 ftpuser1 ftpuser1       19 " + date + " " + name2, file.getLongname());
        sftp.disconnect();
    } catch (SftpException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) OutputStream(java.io.OutputStream) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SSHTest(org.structr.web.files.SSHTest) Test(org.junit.Test)

Example 98 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project structr by structr.

the class SSHFilesTest method test04OverwriteFile.

@Test
public void test04OverwriteFile() {
    ChannelSftp sftp = setupSftpClient("ftpuser1", "ftpuserpw1");
    final String testContent1 = "Initial Content";
    final String testContent2 = "Overwritten Content";
    final String name = "file1.txt";
    try {
        final Vector files = sftp.ls("/files");
        assertNotNull(files);
        // listing contains "." and ".." => 3 entries
        assertEquals(2, files.size());
        try (final OutputStream os = sftp.put("/files/" + name)) {
            IOUtils.write(testContent1, os);
            os.flush();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
        try (final OutputStream os = sftp.put("/files/" + name)) {
            IOUtils.write(testContent2, os);
            os.flush();
        } catch (IOException ioex) {
            ioex.printStackTrace();
        }
    } catch (SftpException ex) {
        logger.warn("", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
    try (final Tx tx = app.tx()) {
        final List<File> files = app.nodeQuery(File.class).getAsList();
        assertEquals("Invalid number of test files", 1, files.size());
        final File file1 = files.get(0);
        assertEquals("Invalid test file name", name, file1.getName());
        try {
            assertEquals("Invalid test file content", testContent2, IOUtils.toString(file1.getInputStream()));
        } catch (IOException ioex) {
            fail("Unexpected exception: " + ioex.getMessage());
        }
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception: " + fex.getMessage());
    }
    try {
        final Vector<LsEntry> entries = sftp.ls("/files");
        // listing contains "." and ".." => 3 entries
        assertEquals("Invalid result size for directory", 3, entries.size());
        final LsEntry file1 = entries.get(2);
        // check names
        assertEquals("Invalid test file name", name, file1.getFilename());
        // check permissions
        assertEquals("Invalid permissions on test file", "-rw-------", file1.getAttrs().getPermissionsString());
        // check flags (?)
        assertEquals("Invalid flags on test file", 13, file1.getAttrs().getFlags());
        // check size
        assertEquals("Invalid test file size", 19, file1.getAttrs().getSize());
        final String date = getDateStringDependingOnCurrentDayOfMonth();
        // check string representation
        assertEquals("Invalid string representation of test file", "-rw-------   1 ftpuser1 ftpuser1       19 " + date + " " + name, file1.getLongname());
        sftp.disconnect();
    } catch (SftpException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) OutputStream(java.io.OutputStream) SftpException(com.jcraft.jsch.SftpException) IOException(java.io.IOException) Vector(java.util.Vector) File(org.structr.web.entity.File) LsEntry(com.jcraft.jsch.ChannelSftp.LsEntry) SSHTest(org.structr.web.files.SSHTest) Test(org.junit.Test)

Example 99 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project azure-tools-for-java by Microsoft.

the class SparkSubmitHelper method sftpFileToEmulator.

public String sftpFileToEmulator(String localFile, String folderPath, IClusterDetail clusterDetail) throws IOException, HDIException, JSchException, SftpException {
    EmulatorClusterDetail emulatorClusterDetail = (EmulatorClusterDetail) clusterDetail;
    final File file = new File(localFile);
    try (FileInputStream fileInputStream = new FileInputStream(file)) {
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
            String sshEndpoint = emulatorClusterDetail.getSSHEndpoint();
            URL url = new URL(sshEndpoint);
            String host = url.getHost();
            int port = url.getPort();
            JSch jsch = new JSch();
            Session session = jsch.getSession(emulatorClusterDetail.getHttpUserName(), host, port);
            session.setPassword(emulatorClusterDetail.getHttpPassword());
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            String[] folders = folderPath.split("/");
            for (String folder : folders) {
                if (folder.length() > 0) {
                    try {
                        channel.cd(folder);
                    } catch (SftpException e) {
                        channel.mkdir(folder);
                        channel.cd(folder);
                    }
                }
            }
            channel.put(bufferedInputStream, file.getName());
            channel.disconnect();
            session.disconnect();
            return file.getName();
        }
    }
}
Also used : EmulatorClusterDetail(com.microsoft.azure.hdinsight.sdk.cluster.EmulatorClusterDetail) SftpException(com.jcraft.jsch.SftpException) JSch(com.jcraft.jsch.JSch) FileInputStream(java.io.FileInputStream) URL(java.net.URL) ChannelSftp(com.jcraft.jsch.ChannelSftp) BufferedInputStream(java.io.BufferedInputStream) File(java.io.File) Session(com.jcraft.jsch.Session)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)99 SftpException (com.jcraft.jsch.SftpException)61 IOException (java.io.IOException)36 JSchException (com.jcraft.jsch.JSchException)28 Session (com.jcraft.jsch.Session)25 JSch (com.jcraft.jsch.JSch)20 Channel (com.jcraft.jsch.Channel)17 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)17 File (java.io.File)16 Test (org.junit.Test)14 InputStream (java.io.InputStream)12 SftpATTRS (com.jcraft.jsch.SftpATTRS)11 FileNotFoundException (java.io.FileNotFoundException)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 FileInputStream (java.io.FileInputStream)7 OutputStream (java.io.OutputStream)7 Path (org.apache.hadoop.fs.Path)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7