Skip to content

Instantly share code, notes, and snippets.

@davengeo
Last active November 12, 2015 18:07
Show Gist options
  • Save davengeo/e13da8c604a3a062b3b7 to your computer and use it in GitHub Desktop.
Save davengeo/e13da8c604a3a062b3b7 to your computer and use it in GitHub Desktop.
SFTP transmission of content using apache VFS2 and OpenSSH private key
@Component
public class FileTransferScpImpl {
private final String userName;
private final String hostName;
private final FileSystemOptions opts;
private final StandardFileSystemManager manager;
private final String privateKeyPath;
private final int timeout;
/**
* @param userName the user name in the remote host, davengeo
* @param hostName the remote host, myhost.com
* @param privateKeyPath the private key in openssh format, C:/Users/XXXXXX/.ssh/id_rsa.pem
*/
@Autowired
public FileTransferScpImpl(@Value("${sftp.username}") String userName,
@Value("${sftp.hostname}") String hostName,
@Value("${sftp.privateKey}") String privateKeyPath,
@Value("S{sftp.timeout}") int timeout) {
this.userName = userName;
this.hostName = hostName;
this.privateKeyPath = privateKeyPath;
this.timeout = timeout;
opts = new FileSystemOptions();
manager = new StandardFileSystemManager();
}
@PostConstruct
public void init() throws FileSystemException {
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
File[] identities = { new File(privateKeyPath) };
SftpFileSystemConfigBuilder.getInstance().setIdentities(opts, identities);
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, timeout);
manager.init();
}
public Observable<String> getStream(final String filename) {
return Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
try {
subscriber.onNext(getContentOf(filename);
subscriber.onCompleted();
} catch (IOException e) {
subscriber.onError(e);
}
}
});
}
public String getContentOf(String filename) throws IOException {
String sftpUri = formatUri(filename);
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
return IOUtils.toString(remoteFile.getContent().getInputStream());
}
public void sendDataTo(String filename, String content) throws IOException {
String sftpUri = formatUri(filename);
writeInLocal(filename, content);
LocalFile localFile = getLocalFile(filename);
FileObject remoteFile = manager.resolveFile(sftpUri, opts);
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
closeAndDelete(localFile);
}
private String formatUri(String filename) {
return String.format("sftp://%s@%s/%s", userName, hostName, filename);
}
private LocalFile getLocalFile(String filename) throws FileSystemException {
File file = new File(filename);
return (LocalFile) manager.resolveFile(file.getAbsolutePath());
}
private void writeInLocal(String filename, String content) throws IOException {
FileWriter fileWriter = new FileWriter(filename);
fileWriter.write(content);
fileWriter.close();
}
private void closeAndDelete(LocalFile localFile) throws FileSystemException {
localFile.close();
localFile.delete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment