Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc code re-org in SftpConnector and SftpTransfers #430

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions src/main/java/org/kiwiproject/jsch/SftpConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ public void connect() {
}
}

private void setSessionKeyExchangeType(Session session, String keyExchangeType) {
KiwiJSchHelpers.setSessionKeyExchangeType(session, keyExchangeType);
LOG.debug("Set key exchange type [{}] for host {}", keyExchangeType, config.getHost());
}

private void addAuthToSession() throws JSchException {
addAuthToSession(config, jsch, session);
}

@VisibleForTesting
static void addAuthToSession(SftpConfig config, JSch jsch, Session session) throws JSchException {
if (isNotBlank(config.getPrivateKeyFilePath())) {
LOG.debug("Using private key '{}' to connect", config.getPrivateKeyFilePath());
jsch.addIdentity(config.getPrivateKeyFilePath());

} else if (isNotBlank(config.getPassword())) {
LOG.debug("Using password to connect");
session.setPassword(config.getPassword());

} else {
throw new SftpTransfersException("Missing a private key and a password; cannot authenticate to the SFTP server");
}
}

private void disableStrictHostKeyCheckingIfConfigured() {
disableStrictHostKeyCheckingIfConfigured(config, session);
}
Expand Down Expand Up @@ -189,28 +213,4 @@ private void validateSftpIsConnected() {
checkState(nonNull(sftpChannel), SFTP_NOT_CONNECTED);
checkState(sftpChannel.isConnected(), SFTP_NOT_CONNECTED);
}

private void setSessionKeyExchangeType(Session session, String keyExchangeType) {
KiwiJSchHelpers.setSessionKeyExchangeType(session, keyExchangeType);
LOG.debug("Set key exchange type [{}] for host {}", keyExchangeType, config.getHost());
}

private void addAuthToSession() throws JSchException {
addAuthToSession(config, jsch, session);
}

@VisibleForTesting
static void addAuthToSession(SftpConfig config, JSch jsch, Session session) throws JSchException {
if (isNotBlank(config.getPrivateKeyFilePath())) {
LOG.debug("Using private key '{}' to connect", config.getPrivateKeyFilePath());
jsch.addIdentity(config.getPrivateKeyFilePath());

} else if (isNotBlank(config.getPassword())) {
LOG.debug("Using password to connect");
session.setPassword(config.getPassword());

} else {
throw new SftpTransfersException("Missing a private key and a password; cannot authenticate to the SFTP server");
}
}
}
48 changes: 24 additions & 24 deletions src/main/java/org/kiwiproject/jsch/SftpTransfers.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public void putFile(Path remotePath, String filename, InputStream data) {
});
}

/**
* Will change the remote directory or create it if it doesn't exist.
*
* @implNote One way to determine if the directory already exists is to attempt to change to it. If an exception
* is thrown, catch it and attempt to create the directory and then change to it. There doesn't seem to be any
* (obvious) way to check existence in JSch otherwise.
*/
private static void changeOrCreateRemoteDirectory(ChannelSftp channel, Path path) throws SftpException {
try {
changeToRemoteDirectory(channel, path);
} catch (SftpException e) {
LOG.debug("Directory {} did not exist. Will create it", path, e);
channel.mkdir(path.toString());
changeToRemoteDirectory(channel, path);
}
}

/**
* Recursively gets files off of a remote server starting in the given path and stores the files locally in the
* given path and given filename. The local path will be determined through the given {@code BiFunction} supplier
Expand Down Expand Up @@ -146,6 +163,13 @@ public void getAndStoreFile(Path remotePath,
});
}

private static void ensureLocalDirectoryExists(Path path) throws IOException {
if (!Files.exists(path)) {
LOG.debug("Local storage directory {} doesn't exist. Creating.", path);
Files.createDirectories(path);
}
}

/**
* Gets a file off of a remote server with the given path and given filename and returns the contents of the file
* as a {@code String}.
Expand Down Expand Up @@ -221,33 +245,9 @@ public void deleteRemoteFile(Path remotePath, String remoteFilename) {
});
}

/**
* Will change the remote directory or create it if it doesn't exist.
*
* @implNote One way to determine if the directory already exists is to attempt to change to it. If an exception
* is thrown, catch it and attempt to create the directory and then change to it. There doesn't seem to be any
* (obvious) way to check existence in JSch otherwise.
*/
private static void changeOrCreateRemoteDirectory(ChannelSftp channel, Path path) throws SftpException {
try {
changeToRemoteDirectory(channel, path);
} catch (SftpException e) {
LOG.debug("Directory {} did not exist. Will create it", path, e);
channel.mkdir(path.toString());
changeToRemoteDirectory(channel, path);
}
}

private static void changeToRemoteDirectory(ChannelSftp channel, Path path) throws SftpException {
LOG.debug("Attempting to change to {} on the remote host", path);
channel.cd(path.toString());
LOG.debug("Successfully changed directory on the remote host");
}

private static void ensureLocalDirectoryExists(Path path) throws IOException {
if (!Files.exists(path)) {
LOG.debug("Local storage directory {} doesn't exist. Creating.", path);
Files.createDirectories(path);
}
}
}