Skip to content

Commit

Permalink
adapt multi sftp connection
Browse files Browse the repository at this point in the history
  • Loading branch information
anh-bolt committed Aug 15, 2024
1 parent abd059a commit 0eb0bc7
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.axonivy.connector.sftp.demo;

public class Constants {
public static final String DUMMY = "dummy";

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<p:outputLabel for="fileUpload" value="File to Upload"/>
<p:fileUpload id="fileUpload" mode="advanced" skinSimple="true"
auto="true" update="@form" process="@form"
fileUploadListener="#{logic.handleFileUpload}" />
listener="#{logic.handleFileUpload}" />
</h:panelGrid>
</h:form>
</p:tab>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
SftpClientDemoData #class
com.axonivy.connector.sftp.demo.SftpClientDemo #namespace
sftpName String #field
sftpName PERSISTENT #fieldModifier
clientHost String #field
clientHost PERSISTENT #fieldModifier
clientPort Number #field
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@
"config" : {
"output" : {
"code" : [
"String prefix = \"com_axonivy_connector_sftp_server_\";",
"in.clientHost = ivy.var.variable(prefix+\"host\").value();",
"in.clientPort = Integer.parseInt(ivy.var.variable(prefix+\"port\").value());",
"in.clientUsername = ivy.var.variable(prefix+\"username\").value();"
"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);"
]
}
},
Expand Down Expand Up @@ -77,19 +80,22 @@
"type" : "SubProcessCall",
"name" : "Sftp/SftpUploadFile",
"config" : {
"processCall" : "Sftp/SftpUploadFile:uploadFile(java.io.InputStream,String)",
"processCall" : "Sftp/SftpUploadFile:uploadFile(String,java.io.InputStream,String)",
"output" : {
"map" : {
"out" : "in",
"out.isFileUploaded" : "result.isSuccess"
"out.isFileUploaded" : "result.isSuccess",
"out.sftpName" : "in.sftpName"
}
},
"call" : {
"params" : [
{ "name" : "sftpName", "type" : "String" },
{ "name" : "fileToBeUploaded", "type" : "java.io.InputStream" },
{ "name" : "fileName", "type" : "String" }
],
"map" : {
"param.sftpName" : "in.sftpName",
"param.fileToBeUploaded" : "in.uploadedFile.getInputStream()",
"param.fileName" : "in.uploadedFile.getFileName()"
}
Expand All @@ -111,7 +117,7 @@
"type" : "SubProcessCall",
"name" : "Sftp/SftpDownloadFile",
"config" : {
"processCall" : "Sftp/SftpDownloadFile:downloadFile(String)",
"processCall" : "Sftp/SftpDownloadFile:downloadFile(String,String)",
"output" : {
"map" : {
"out" : "in",
Expand All @@ -120,9 +126,11 @@
},
"call" : {
"params" : [
{ "name" : "sftpName", "type" : "String" },
{ "name" : "remoteFileName", "type" : "String" }
],
"map" : {
"param.sftpName" : "in.sftpName",
"param.remoteFileName" : "in.fileToDownload.name"
}
}
Expand Down Expand Up @@ -191,7 +199,7 @@
"type" : "SubProcessCall",
"name" : "call list All Files",
"config" : {
"processCall" : "Sftp/SftpDownloadFile:listAllFiles(String)",
"processCall" : "Sftp/SftpDownloadFile:listAllFiles(String,String)",
"output" : {
"map" : {
"out" : "in",
Expand All @@ -200,9 +208,11 @@
},
"call" : {
"params" : [
{ "name" : "sftpName", "type" : "String" },
{ "name" : "remoteDirectory", "type" : "String" }
],
"map" : {
"param.sftpName" : "in.sftpName",
"param.remoteDirectory" : "\".\""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,32 @@ public class SftpProcessSSHTest {
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_NAME_VAR = TEST_SFTP_NAME + ".";
private static final String TEST_FILE_NAME = "market_market_connector_sftp.pdf";
private static final long TEST_FILE_SIZE = 207569L;


private static final String PREFIX = "com.axonivy.connector.sftp.server.";

@BeforeAll
public static void init() throws Exception {
String prefix = "com_axonivy_connector_sftp_server_";
Ivy.var().set(prefix+"auth", "ssh");
Ivy.var().set(prefix+"password", "");

Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"auth", "ssh");
Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"password", "");

String keyString = Files.readString(Paths.get(SftpProcessSSHTest.class.getResource("sftptest").toURI()));
Ivy.var().set(prefix+"secret_sshkey", keyString);
Ivy.var().set(prefix+"secret_sshpassphrase", "123456");
Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"secret.sshkey", keyString);
Ivy.var().set(PREFIX+TEST_SFTP_NAME_VAR+"secret.sshpassphrase", "123456");
}

@Test
@Order(1)
public void callOpenConnection(BpmClient bpmClient) throws Exception {
BpmElement startable = TEST_HELPER_PROCESS.elementName("openConnection()");
BpmElement startable = TEST_HELPER_PROCESS.elementName("openConnection(String)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute() // Callable sub process input arguments
.execute(TEST_SFTP_NAME) // Callable sub process input arguments
.subResult();

SftpClientService sftpClient = result.param("sftpClient", SftpClientService.class);
Expand All @@ -82,11 +85,11 @@ public void callOpenConnection(BpmClient bpmClient) throws Exception {
public void callUploadFile(BpmClient bpmClient) {
InputStream fileToBeUploaded = getClass().getResourceAsStream(TEST_FILE_NAME);

BpmElement startable = TEST_UPLOAD_FILE_PROCESS.elementName("uploadFile(InputStream,String)");
BpmElement startable = TEST_UPLOAD_FILE_PROCESS.elementName("uploadFile(String,InputStream,String)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments
.execute(TEST_SFTP_NAME,fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments
.subResult();

Boolean isSuccess = result.param("isSuccess", Boolean.class);
Expand All @@ -103,11 +106,11 @@ public void callUploadIvyFile(BpmClient bpmClient) throws IOException {
File ivyFile = new File(TEST_FILE_NAME, true);
FileUtils.moveFile(javaFile, ivyFile.getJavaFile());

BpmElement startable = TEST_UPLOAD_FILE_PROCESS.elementName("uploadFile(File)");
BpmElement startable = TEST_UPLOAD_FILE_PROCESS.elementName("uploadFile(String,File)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(ivyFile) // Callable sub process input arguments
.execute(TEST_SFTP_NAME, ivyFile) // Callable sub process input arguments
.subResult();

Boolean isSuccess = result.param("isSuccess", Boolean.class);
Expand All @@ -117,11 +120,11 @@ public void callUploadIvyFile(BpmClient bpmClient) throws IOException {
@Test
@Order(4)
public void callListAllFiles(BpmClient bpmClient) {
BpmElement startable = TEST_DOWNLOAD_FILE_PROCESS.elementName("listAllFiles(String)");
BpmElement startable = TEST_DOWNLOAD_FILE_PROCESS.elementName("listAllFiles(String,String)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(".") // Callable sub process input arguments
.execute(TEST_SFTP_NAME, ".") // Callable sub process input arguments
.subResult();
List<FileData> listFiles = result.param("listFiles", List.class);
assertThat(listFiles.size()).isGreaterThanOrEqualTo(1);
Expand All @@ -131,11 +134,11 @@ public void callListAllFiles(BpmClient bpmClient) {
@Test
@Order(5)
public void callDownloadFile(BpmClient bpmClient) {
BpmElement startable = TEST_DOWNLOAD_FILE_PROCESS.elementName("downloadFile(String)");
BpmElement startable = TEST_DOWNLOAD_FILE_PROCESS.elementName("downloadFile(String,String)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(TEST_FILE_NAME) // Callable sub process input arguments
.execute(TEST_SFTP_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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ 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_FILE_NAME = "market_market_connector_sftp.pdf";
private static final long TEST_FILE_SIZE = 207569L;


@Test
@Order(1)
public void callOpenConnection(BpmClient bpmClient) {
BpmElement startable = TEST_HELPER_PROCESS.elementName("openConnection()");
BpmElement startable = TEST_HELPER_PROCESS.elementName("openConnection(String)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute() // Callable sub process input arguments
.execute(TEST_SFTP_NAME) // Callable sub process input arguments
.subResult();

SftpClientService sftpClient = result.param("sftpClient", SftpClientService.class);
Expand All @@ -67,11 +68,11 @@ public void callOpenConnection(BpmClient bpmClient) {
public void callUploadFile(BpmClient bpmClient) {
InputStream fileToBeUploaded = getClass().getResourceAsStream(TEST_FILE_NAME);

BpmElement startable = TEST_UPLOAD_FILE_PROCESS.elementName("uploadFile(InputStream,String)");
BpmElement startable = TEST_UPLOAD_FILE_PROCESS.elementName("uploadFile(String,InputStream,String)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments
.execute(TEST_SFTP_NAME, fileToBeUploaded, TEST_FILE_NAME) // Callable sub process input arguments
.subResult();

Boolean isSuccess = result.param("isSuccess", Boolean.class);
Expand All @@ -88,11 +89,11 @@ public void callUploadIvyFile(BpmClient bpmClient) throws IOException {
File ivyFile = new File(TEST_FILE_NAME, true);
FileUtils.moveFile(javaFile, ivyFile.getJavaFile());

BpmElement startable = TEST_UPLOAD_FILE_PROCESS.elementName("uploadFile(File)");
BpmElement startable = TEST_UPLOAD_FILE_PROCESS.elementName("uploadFile(String,File)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(ivyFile) // Callable sub process input arguments
.execute(TEST_SFTP_NAME, ivyFile) // Callable sub process input arguments
.subResult();

Boolean isSuccess = result.param("isSuccess", Boolean.class);
Expand All @@ -102,11 +103,11 @@ public void callUploadIvyFile(BpmClient bpmClient) throws IOException {
@Test
@Order(4)
public void callListAllFiles(BpmClient bpmClient) {
BpmElement startable = TEST_DOWNLOAD_FILE_PROCESS.elementName("listAllFiles(String)");
BpmElement startable = TEST_DOWNLOAD_FILE_PROCESS.elementName("listAllFiles(String,String)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(".") // Callable sub process input arguments
.execute(TEST_SFTP_NAME, ".") // Callable sub process input arguments
.subResult();
List<FileData> listFiles = result.param("listFiles", List.class);
assertThat(listFiles.size()).isGreaterThanOrEqualTo(1);
Expand All @@ -116,11 +117,11 @@ public void callListAllFiles(BpmClient bpmClient) {
@Test
@Order(5)
public void callDownloadFile(BpmClient bpmClient) {
BpmElement startable = TEST_DOWNLOAD_FILE_PROCESS.elementName("downloadFile(String)");
BpmElement startable = TEST_DOWNLOAD_FILE_PROCESS.elementName("downloadFile(String,String)");

SubProcessCallResult result = bpmClient.start()
.subProcess(startable)
.execute(TEST_FILE_NAME) // Callable sub process input arguments
.execute(TEST_SFTP_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);
Expand Down
44 changes: 23 additions & 21 deletions sftp-connector/config/variables.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
Variables:

com.axonivy.connector.sftp.server:
# The host name to the SFTP server
host: 'localhost'

# The port number to the SFTP server
port: 22

# The username to the SFTP server
username: 'usr'

# Auth type to the SFPT server: password OR ssh
auth: 'ssh'

# The password to the SFTP server
# [password]
password: ''

# The ssh key string to SFTP server
# [secret private key]
secret.sshkey: ''
# The ssh key passphrase
secret.sshpassphrase: ''
dummy:
# The host name to the SFTP server
host: 'localhost'

# The port number to the SFTP server
port: 22

# The username to the SFTP server
username: 'usr'

# Auth type to the SFPT server: password OR ssh
auth: 'password'

# The password to the SFTP server
# [password]
password: pwd

# The ssh key string to SFTP server
# [secret private key]
secret.sshkey: ''

# The ssh key passphrase
secret.sshpassphrase: ''
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ toFile File #field
remoteDirectory String #field
listFiles java.util.List<com.axonivy.connector.sftp.service.SftpClientService.FileData> #field
sftpClient com.axonivy.connector.sftp.service.SftpClientService #field
sftpName String #field
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
SftpHelperData #class
com.axonivy.connector.sftp #namespace
sftpName String #field
sftpClient com.axonivy.connector.sftp.service.SftpClientService #field
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ fileName String #field
sftpClient com.axonivy.connector.sftp.service.SftpClientService #field
isSuccess Boolean #field
ivyFile File #field
sftpName String #field
Loading

0 comments on commit 0eb0bc7

Please sign in to comment.