aboutsummaryrefslogtreecommitdiffstats
path: root/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java
diff options
context:
space:
mode:
Diffstat (limited to 'datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java')
-rw-r--r--datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java31
1 files changed, 17 insertions, 14 deletions
diff --git a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java
index bdaf6d4c..ec523354 100644
--- a/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java
+++ b/datafile-app-server/src/main/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClient.java
@@ -22,10 +22,8 @@ import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
-
import java.nio.file.Path;
import java.util.Optional;
-
import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -39,11 +37,11 @@ import org.slf4j.LoggerFactory;
public class SftpClient implements FileCollectClient {
private static final Logger logger = LoggerFactory.getLogger(SftpClient.class);
- private static final int FTPS_DEFAULT_PORT = 22;
+ private static final int SFTP_DEFAULT_PORT = 22;
private final FileServerData fileServerData;
- private Session session = null;
- private ChannelSftp sftpChannel = null;
+ protected Session session = null;
+ protected ChannelSftp sftpChannel = null;
public SftpClient(FileServerData fileServerData) {
this.fileServerData = fileServerData;
@@ -57,7 +55,8 @@ public class SftpClient implements FileCollectClient {
sftpChannel.get(remoteFile, localFile.toString());
logger.trace("File {} Download Successfull from xNF", localFile.getFileName());
} catch (SftpException e) {
- boolean retry = e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE && e.id != ChannelSftp.SSH_FX_PERMISSION_DENIED && e.id != ChannelSftp.SSH_FX_OP_UNSUPPORTED;
+ boolean retry = e.id != ChannelSftp.SSH_FX_NO_SUCH_FILE && e.id != ChannelSftp.SSH_FX_PERMISSION_DENIED
+ && e.id != ChannelSftp.SSH_FX_OP_UNSUPPORTED;
throw new DatafileTaskException("Unable to get file from xNF. Data: " + fileServerData, e, retry);
}
@@ -86,28 +85,32 @@ public class SftpClient implements FileCollectClient {
}
} catch (JSchException e) {
boolean retry = !e.getMessage().contains("Auth fail");
- throw new DatafileTaskException("Could not open Sftp client" + e, e, retry);
+ throw new DatafileTaskException("Could not open Sftp client. " + e, e, retry);
}
}
- private static int getPort(Optional<Integer> port) {
- return port.isPresent() ? port.get() : FTPS_DEFAULT_PORT;
+ private int getPort(Optional<Integer> port) {
+ return port.isPresent() ? port.get() : SFTP_DEFAULT_PORT;
}
- private static Session setUpSession(FileServerData fileServerData) throws JSchException {
- JSch jsch = new JSch();
+ private Session setUpSession(FileServerData fileServerData) throws JSchException {
+ JSch jsch = createJsch();
- Session newSession =
- jsch.getSession(fileServerData.userId(), fileServerData.serverAddress(), getPort(fileServerData.port()));
+ Session newSession = jsch.getSession(fileServerData.userId(), fileServerData.serverAddress(),
+ getPort(fileServerData.port()));
newSession.setConfig("StrictHostKeyChecking", "no");
newSession.setPassword(fileServerData.password());
newSession.connect();
return newSession;
}
- private static ChannelSftp getChannel(Session session) throws JSchException {
+ private ChannelSftp getChannel(Session session) throws JSchException {
Channel channel = session.openChannel("sftp");
channel.connect();
return (ChannelSftp) channel;
}
+
+ protected JSch createJsch() {
+ return new JSch();
+ }
}