From f8a1d7c7d1075858a29325e71589c94d02fcad9e Mon Sep 17 00:00:00 2001 From: Scott Leberknight <174812+sleberknight@users.noreply.github.com> Date: Wed, 11 Nov 2020 17:52:06 -0500 Subject: [PATCH] Misc code re-org in SftpConnector and SftpTransfers Move private helper methods closer to their last use in SftpConnector and SftpTransfers --- .../org/kiwiproject/jsch/SftpConnector.java | 48 +++++++++---------- .../org/kiwiproject/jsch/SftpTransfers.java | 48 +++++++++---------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/main/java/org/kiwiproject/jsch/SftpConnector.java b/src/main/java/org/kiwiproject/jsch/SftpConnector.java index 3fcf58ac..c766ada0 100644 --- a/src/main/java/org/kiwiproject/jsch/SftpConnector.java +++ b/src/main/java/org/kiwiproject/jsch/SftpConnector.java @@ -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); } @@ -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"); - } - } } diff --git a/src/main/java/org/kiwiproject/jsch/SftpTransfers.java b/src/main/java/org/kiwiproject/jsch/SftpTransfers.java index 88d6bdec..790de9e0 100644 --- a/src/main/java/org/kiwiproject/jsch/SftpTransfers.java +++ b/src/main/java/org/kiwiproject/jsch/SftpTransfers.java @@ -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 @@ -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}. @@ -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); - } - } }