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());
}
}
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());
}
}
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());
}
}
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();
}
}
}
Aggregations