Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. //compile ("com.jcraft:jsch:0.1.54")
  2. //compile("org.apache.sshd:sshd-core:0.6.0")
  3.  
  4. import org.apache.sshd.SshServer;
  5. import org.apache.sshd.server.command.ScpCommandFactory;
  6. import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
  7. import org.apache.sshd.server.sftp.SftpSubsystem;
  8. import org.apache.sshd.server.shell.ProcessShellFactory;
  9. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  10.  
  11. import java.io.IOException;
  12. import java.security.Security;
  13. import java.util.Collections;
  14.  
  15. public class SFTPServer {
  16. private SshServer sshd;
  17.  
  18. public SFTPServer(int port) {
  19. Security.addProvider(new BouncyCastleProvider());
  20. this.sshd = SshServer.setUpDefaultServer();
  21. this.sshd.setPort(port);
  22. this.sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("./build/hostkey.ser"));
  23. this.sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystem.Factory()));
  24. this.sshd.setCommandFactory(new ScpCommandFactory());
  25. this.sshd.setShellFactory(new ProcessShellFactory());
  26. this.sshd.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
  27. }
  28.  
  29. public void start(){
  30. try {
  31. sshd.start();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36.  
  37. public void stop(){
  38. try {
  39. sshd.stop();
  40. } catch (InterruptedException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44. }
  45.  
  46. import com.jcraft.jsch.ChannelSftp;
  47. import com.jcraft.jsch.JSch;
  48. import com.jcraft.jsch.JSchException;
  49. import com.jcraft.jsch.Session;
  50. import com.jcraft.jsch.SftpException;
  51.  
  52. import java.io.File;
  53. import java.io.FileInputStream;
  54. import java.io.FileNotFoundException;
  55. import java.util.Properties;
  56.  
  57. public class SFTPClient {
  58. private static final String SFTP_DIR = "/files/";
  59. private static final int DEFAULT_PORT = 22;
  60. private static int PORT = 22;
  61.  
  62. private Session session = null;
  63. private ChannelSftp channelSftp = null;
  64.  
  65. private String hostname;
  66. private String username;
  67. private String password;
  68.  
  69. protected SFTPClient(String hostname, String username, String password){
  70. this.hostname = hostname;
  71. this.username = username;
  72. this.password = password;
  73. }
  74.  
  75. public SFTPClient(int port){
  76. this("localhost", "test", "test");
  77. PORT = port;
  78. }
  79.  
  80. protected void connect() throws JSchException {
  81. JSch jsch = new JSch();
  82. session = jsch.getSession(username,hostname, PORT);
  83. session.setPassword(password);
  84.  
  85. Properties config = new Properties();
  86. config.put("StrictHostKeyChecking", "no");
  87.  
  88. session.setConfig(config);
  89. session.connect();
  90.  
  91. channelSftp = (ChannelSftp) session.openChannel("sftp");
  92. channelSftp.connect();
  93. }
  94.  
  95. protected void disconnect(){
  96. channelSftp.disconnect();
  97. session.disconnect();
  98. }
  99.  
  100. protected void upload(File file) throws SftpException, FileNotFoundException {
  101. if (PORT == DEFAULT_PORT){
  102. channelSftp.cd(SFTP_DIR);
  103. }
  104.  
  105. channelSftp.put(new FileInputStream(file), file.getName(), ChannelSftp.OVERWRITE);
  106. }
  107.  
  108. }
  109.  
  110. public class VibesClientIntegrationTest {
  111. @Rule
  112. public TemporaryFolder folder = new TemporaryFolder();
  113.  
  114. FakeFtpServer fakeFtpServer;
  115. private static final String HOME_DIR = "/";
  116. private static final String HOSTNAME = "localhost";
  117. private static final String USERNAME = "username";
  118. private static final String PASSWORD = "password";
  119. private static int PORT;
  120.  
  121. @Before
  122. public void setUp() throws Exception {
  123. fakeFtpServer = new FakeFtpServer();
  124. fakeFtpServer.setServerControlPort(0);
  125. UnixFakeFileSystem fileSystem = new UnixFakeFileSystem();
  126. fileSystem.add(new DirectoryEntry(HOME_DIR));
  127. fakeFtpServer.setFileSystem(fileSystem);
  128. fakeFtpServer.addUserAccount(new UserAccount(USERNAME, PASSWORD, HOME_DIR));
  129. fakeFtpServer.start();
  130. PORT = fakeFtpServer.getServerControlPort();
  131. }
  132.  
  133. @After
  134. public void tearDown() throws Exception {
  135. fakeFtpServer.stop();
  136. }
  137.  
  138. @Test
  139. public void shouldSendFileToFTP() throws Exception {
  140. VibesCredential credential = new VibesCredential(HOSTNAME,USERNAME,PASSWORD,PORT);
  141. VibesClient vibesClient = new VibesClient(credential);
  142. File file = folder.newFile("testFile");
  143.  
  144. vibesClient.connect();
  145. vibesClient.sendFile(file);
  146. vibesClient.disconnect();
  147.  
  148. FTPClient ftpClient = new FTPClient();
  149. ftpClient.connect(HOSTNAME, PORT);
  150. ftpClient.login(USERNAME, PASSWORD);
  151. boolean isSent = ftpClient.retrieveFile(file.getName(), new ByteArrayOutputStream());
  152. ftpClient.disconnect();
  153.  
  154. assertThat(isSent).isTrue();
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement