Skip to content

Instantly share code, notes, and snippets.

@eduardomoroni
Last active February 1, 2017 18:49
Show Gist options
  • Save eduardomoroni/479db5139efcfef5b71dfe3f3a1bca72 to your computer and use it in GitHub Desktop.
Save eduardomoroni/479db5139efcfef5b71dfe3f3a1bca72 to your computer and use it in GitHub Desktop.
//compile ("com.jcraft:jsch:0.1.54")
//compile("org.apache.sshd:sshd-core:0.6.0")
import org.apache.sshd.SshServer;
import org.apache.sshd.server.command.ScpCommandFactory;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.sftp.SftpSubsystem;
import org.apache.sshd.server.shell.ProcessShellFactory;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.io.IOException;
import java.security.Security;
import java.util.Collections;
public class SFTPServer {
private SshServer sshd;
public SFTPServer(int port) {
Security.addProvider(new BouncyCastleProvider());
this.sshd = SshServer.setUpDefaultServer();
this.sshd.setPort(port);
this.sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("./build/hostkey.ser"));
this.sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystem.Factory()));
this.sshd.setCommandFactory(new ScpCommandFactory());
this.sshd.setShellFactory(new ProcessShellFactory());
this.sshd.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
}
public void start(){
try {
sshd.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop(){
try {
sshd.stop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Properties;
public class SFTPClient {
private static final String SFTP_DIR = "/files/";
private static final int DEFAULT_PORT = 22;
private static int PORT = 22;
private Session session = null;
private ChannelSftp channelSftp = null;
private String hostname;
private String username;
private String password;
protected SFTPClient(String hostname, String username, String password){
this.hostname = hostname;
this.username = username;
this.password = password;
}
public SFTPClient(int port){
this("localhost", "test", "test");
PORT = port;
}
protected void connect() throws JSchException {
JSch jsch = new JSch();
session = jsch.getSession(username,hostname, PORT);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "password");
session.setConfig(config);
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
}
protected void disconnect(){
channelSftp.disconnect();
session.disconnect();
}
protected void upload(File file) throws SftpException, FileNotFoundException {
if (PORT == DEFAULT_PORT){
channelSftp.cd(SFTP_DIR);
}
channelSftp.put(new FileInputStream(file), file.getName(), ChannelSftp.OVERWRITE);
}
}
public class VibesClientIntegrationTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();
FakeFtpServer fakeFtpServer;
private static final String HOME_DIR = "/";
private static final String HOSTNAME = "localhost";
private static final String USERNAME = "username";
private static final String PASSWORD = "password";
private static int PORT;
@Before
public void setUp() throws Exception {
fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setServerControlPort(0);
UnixFakeFileSystem fileSystem = new UnixFakeFileSystem();
fileSystem.add(new DirectoryEntry(HOME_DIR));
fakeFtpServer.setFileSystem(fileSystem);
fakeFtpServer.addUserAccount(new UserAccount(USERNAME, PASSWORD, HOME_DIR));
fakeFtpServer.start();
PORT = fakeFtpServer.getServerControlPort();
}
@After
public void tearDown() throws Exception {
fakeFtpServer.stop();
}
@Test
public void shouldSendFileToFTP() throws Exception {
VibesCredential credential = new VibesCredential(HOSTNAME,USERNAME,PASSWORD,PORT);
VibesClient vibesClient = new VibesClient(credential);
File file = folder.newFile("testFile");
vibesClient.connect();
vibesClient.sendFile(file);
vibesClient.disconnect();
FTPClient ftpClient = new FTPClient();
ftpClient.connect(HOSTNAME, PORT);
ftpClient.login(USERNAME, PASSWORD);
boolean isSent = ftpClient.retrieveFile(file.getName(), new ByteArrayOutputStream());
ftpClient.disconnect();
assertThat(isSent).isTrue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment