diff --git a/sftp-connector-demo/src/com/axonivy/connector/sftp/demo/Constants.java b/sftp-connector-demo/src/com/axonivy/connector/sftp/demo/Constants.java index 5f9d079..96ea47a 100644 --- a/sftp-connector-demo/src/com/axonivy/connector/sftp/demo/Constants.java +++ b/sftp-connector-demo/src/com/axonivy/connector/sftp/demo/Constants.java @@ -1,6 +1,6 @@ package com.axonivy.connector.sftp.demo; public class Constants { - public static final String DUMMY = "dummy"; + public static final String TEST_SFTP_SERVER_NAME = "dummy"; } diff --git a/sftp-connector-demo/src_hd/com/axonivy/connector/sftp/demo/SftpClientDemo/SftpClientDemoData.ivyClass b/sftp-connector-demo/src_hd/com/axonivy/connector/sftp/demo/SftpClientDemo/SftpClientDemoData.ivyClass index 84c9ef1..6015ef3 100644 --- a/sftp-connector-demo/src_hd/com/axonivy/connector/sftp/demo/SftpClientDemo/SftpClientDemoData.ivyClass +++ b/sftp-connector-demo/src_hd/com/axonivy/connector/sftp/demo/SftpClientDemo/SftpClientDemoData.ivyClass @@ -1,7 +1,7 @@ SftpClientDemoData #class com.axonivy.connector.sftp.demo.SftpClientDemo #namespace -sftpName String #field -sftpName PERSISTENT #fieldModifier +sftpServerName String #field +sftpServerName PERSISTENT #fieldModifier clientHost String #field clientHost PERSISTENT #fieldModifier clientPort Number #field diff --git a/sftp-connector-demo/src_hd/com/axonivy/connector/sftp/demo/SftpClientDemo/SftpClientDemoProcess.p.json b/sftp-connector-demo/src_hd/com/axonivy/connector/sftp/demo/SftpClientDemo/SftpClientDemoProcess.p.json index 9c63e5e..9eb1297 100644 --- a/sftp-connector-demo/src_hd/com/axonivy/connector/sftp/demo/SftpClientDemo/SftpClientDemoProcess.p.json +++ b/sftp-connector-demo/src_hd/com/axonivy/connector/sftp/demo/SftpClientDemo/SftpClientDemoProcess.p.json @@ -34,10 +34,10 @@ "import com.axonivy.connector.sftp.service.SftpClientService;", "import com.axonivy.connector.sftp.demo.Constants;", "", - "in.sftpName = new String(Constants.DUMMY);", - "in.clientHost = SftpClientService.getClientHost(in.sftpName);", - "in.clientPort = Integer.parseInt(SftpClientService.getPort(in.sftpName));", - "in.clientUsername = SftpClientService.getUsername(in.sftpName);" + "in.sftpServerName = Constants.TEST_SFTP_SERVER_NAME;", + "in.clientHost = SftpClientService.getClientHost(in.sftpServerName);", + "in.clientPort = Integer.parseInt(SftpClientService.getPort(in.sftpServerName));", + "in.clientUsername = SftpClientService.getUsername(in.sftpServerName);" ] } }, @@ -84,8 +84,7 @@ "output" : { "map" : { "out" : "in", - "out.isFileUploaded" : "result.isSuccess", - "out.sftpName" : "in.sftpName" + "out.isFileUploaded" : "result.isSuccess" } }, "call" : { @@ -95,7 +94,7 @@ { "name" : "fileName", "type" : "String" } ], "map" : { - "param.sftpName" : "in.sftpName", + "param.sftpName" : "in.sftpServerName", "param.fileToBeUploaded" : "in.uploadedFile.getInputStream()", "param.fileName" : "in.uploadedFile.getFileName()" } @@ -130,7 +129,7 @@ { "name" : "remoteFileName", "type" : "String" } ], "map" : { - "param.sftpName" : "in.sftpName", + "param.sftpName" : "in.sftpServerName", "param.remoteFileName" : "in.fileToDownload.name" } } @@ -212,7 +211,7 @@ { "name" : "remoteDirectory", "type" : "String" } ], "map" : { - "param.sftpName" : "in.sftpName", + "param.sftpName" : "in.sftpServerName", "param.remoteDirectory" : "\".\"" } } diff --git a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessTest.java b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessTest.java index ad58ef5..82b2b27 100644 --- a/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessTest.java +++ b/sftp-connector-test/src_test/com/axonivy/connector/sftp/test/SftpProcessTest.java @@ -41,7 +41,7 @@ public class SftpProcessTest { private static final BpmProcess TEST_UPLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpUploadFile"); private static final BpmProcess TEST_DOWNLOAD_FILE_PROCESS = BpmProcess.path("Sftp/SftpDownloadFile"); - private static final String TEST_SFTP_NAME = "dummy"; + private static final String TEST_SFTP_SERVER_NAME = "dummy"; private static final String TEST_FILE_NAME = "market_market_connector_sftp.pdf"; private static final long TEST_FILE_SIZE = 207569L; @@ -53,7 +53,7 @@ public void callOpenConnection(BpmClient bpmClient) { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME) // Callable sub process input arguments + .execute(TEST_SFTP_SERVER_NAME) // Callable sub process input arguments .subResult(); SftpClientService sftpClient = result.param("sftpClient", SftpClientService.class); @@ -70,7 +70,7 @@ public void callUploadFile(BpmClient bpmClient) { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME, fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments + .execute(TEST_SFTP_SERVER_NAME, fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments .subResult(); Boolean isSuccess = result.param("isSuccess", Boolean.class); @@ -91,7 +91,7 @@ public void callUploadIvyFile(BpmClient bpmClient) throws IOException { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME, ivyFile) // Callable sub process input arguments + .execute(TEST_SFTP_SERVER_NAME, ivyFile) // Callable sub process input arguments .subResult(); Boolean isSuccess = result.param("isSuccess", Boolean.class); @@ -105,7 +105,7 @@ public void callListAllFiles(BpmClient bpmClient) { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME, ".") // Callable sub process input arguments + .execute(TEST_SFTP_SERVER_NAME, ".") // Callable sub process input arguments .subResult(); List listFiles = result.param("listFiles", List.class); assertThat(listFiles.size()).isGreaterThanOrEqualTo(1); @@ -119,7 +119,7 @@ public void callDownloadFile(BpmClient bpmClient) { SubProcessCallResult result = bpmClient.start() .subProcess(startable) - .execute(TEST_SFTP_NAME, TEST_FILE_NAME) // Callable sub process input arguments + .execute(TEST_SFTP_SERVER_NAME, TEST_FILE_NAME) // Callable sub process input arguments .subResult(); java.io.File downloadedFile = result.param("toFile", java.io.File.class); assertThat(downloadedFile.length()).isEqualTo(TEST_FILE_SIZE); diff --git a/sftp-connector/config/variables.yaml b/sftp-connector/config/variables.yaml index eeb2d96..39bc1d0 100644 --- a/sftp-connector/config/variables.yaml +++ b/sftp-connector/config/variables.yaml @@ -11,7 +11,8 @@ Variables: # The username to the SFTP server username: 'usr' - # Auth type to the SFPT server: password OR ssh + # Auth type to the SFPT server + # [enum: password, ssh] auth: 'password' # The password to the SFTP server diff --git a/sftp-connector/src/com/axonivy/connector/sftp/enums/AuthMethod.java b/sftp-connector/src/com/axonivy/connector/sftp/enums/AuthMethod.java new file mode 100644 index 0000000..2dde9c9 --- /dev/null +++ b/sftp-connector/src/com/axonivy/connector/sftp/enums/AuthMethod.java @@ -0,0 +1,10 @@ +package com.axonivy.connector.sftp.enums; + +/** + * Enumeration types of authentication method used in SFTP client + */ +public enum AuthMethod { + PASSWORD, + SSH, + +} diff --git a/sftp-connector/src/com/axonivy/connector/sftp/service/SftpClientService.java b/sftp-connector/src/com/axonivy/connector/sftp/service/SftpClientService.java index e69d123..fe98451 100644 --- a/sftp-connector/src/com/axonivy/connector/sftp/service/SftpClientService.java +++ b/sftp-connector/src/com/axonivy/connector/sftp/service/SftpClientService.java @@ -8,7 +8,6 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import java.util.Properties; import org.apache.commons.lang3.StringUtils; import ch.ivyteam.log.Logger; @@ -19,9 +18,9 @@ import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; - import ch.ivyteam.ivy.environment.Ivy; +import static com.axonivy.connector.sftp.enums.AuthMethod.PASSWORD; /** * Service class for file transfer to/from the SFTP server. The service class is * used to decouple the SFTP implementation. @@ -41,7 +40,6 @@ public class SftpClientService implements AutoCloseable { private static final String AUTH_VAR = "auth"; private static final String PASSWORD_VAR = "password"; private static final String USERNAME_VAR = "username"; - private static final String PASSWORD = "password"; /** * A Session represents a connection to an SSH server. @@ -79,7 +77,7 @@ public SftpClientService(String sftpName) throws IOException { JSch jsch = new JSch(); session = jsch.getSession(username, host, port); - if (StringUtils.isEmpty(auth) || PASSWORD.equalsIgnoreCase(auth)) { + if (StringUtils.isEmpty(auth) || PASSWORD.name().equalsIgnoreCase(auth)) { session.setPassword(password); } else { session.setConfig("PreferredAuthentications", "publickey");