File Transfer Framework - kodePhile/unify-framework GitHub Wiki
File transfer operations are a common feature of enterprise applications. Files generated by or placed into the file system of an application server may need to be copied across a computer network to a remote machine. Conversely, files may be required to be copied over from a remote machine to an application server’s file system. A simple example where a file transfer mechanism is required is an operational requirement where reports generated on an application server must be moved over to a remote archiving machine. Another example is in financial institutions where SWIFT files have to be copied from a remote gateway server to an application server before they can be processed.
There exist numerous protocols for executing a file transfer between multiple machines on a computer network. Some are proprietary and operating system dependent while others are open standards based and supported by many operating systems. The framework abstracts away the complexities of the underlying file transfer protocol by defining a FileTransferServer component interface. This interface represents a file server component with various methods for file transfer and other file related operations targeted at local or remote file systems.
Figure 1.0 File transfer components
With the FileTransferServer interface, we can:
- Get a file list from a folder path on a remote file system.
- Check if a folder exists on a remote file system.
- Check if a file exists on a remote file system.
- Create a folder on a remote file system.
- Create a new file on a remote file system.
- Delete a file on a remote file system.
- Upload a file from the local machine to a specified path on a remote file system.
- Upload all files in a local folder, including subfolders and their files, to a specified path on a remote file system
- Download a file from a specified path on a remote file system to the local machine.
- Download all files in a specified path on a remote file system, including subfolders and their files, to a specified path on the local machine.
There are two basic steps to performing a file transfer operation. The first step is composing a FileTransferSetup data object using a _FileTransferSetup.Builder- instance which you construct by calling the static FileTransferSetup.newBuilder() method. The FileTransferSetup object provides information required by the FileTransferServer to perform a file transfer operation.
This operation allows you to get a list of file and folders that are contained in a remote path. You perform this operation by calling the FileTransferServer getRemoteFileList() method.
Listing 1. Get remote path file list
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose file transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.10.10.40")
.remotePath("/sample/data")
.useAuthenticationId("admin")
.useAuthenticationPassword("admin123")
.build();
// Get remote file list
List<FileInfo> fileInfoList = fileTransferServer.getRemoteFileList(fileTransferSetup);
//Do something with file info listCheck if a remote path exists by calling the file FileTransferServer remoteDirectoryExists() method.
Listing 2: Check if remote path exists
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose with remote path information only
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.10.10.40")
.remotePath("/some/path/to/check")
.useAuthenticationId("admin")
.useAuthenticationPassword("admin123")
.build();
// Check if remote path exists
boolean pathExists = fileTransferServer.remoteDirectoryExists(fileTransferSetup);This operation allows you to check if a particular file exists in a remote path.
Listing 3: Check if remote file exists
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.10.10.40")
.remotePath("/user/manuals")
.useAuthenticationId("admin")
.useAuthenticationPassword("admin123")
.build();
// Check if remote file 'ServerRules.txt' exists
boolean fileExists = fileTransferServer.remoteFileExists(fileTransferSetup,
"ServerRules.txt");This operation creates a file path on a remote file system relative to the user account working directory. It creates parent directories or folders if required.
Listing 4: Create a remote path
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("tcdng.com")
.remotePath("/order/delivery/20181110")
.useAuthenticationId("tom")
.useAuthenticationPassword("harry456")
.build();
// Create remote path
fileTransferServer.createRemoteDirectory(fileTransferSetup);This operation creates a zero-length file in the remote server path. If an existing file with the same name is found, the original file is truncated. The authenticated ID must have an account with file creation privileges on the remote server. An exception is thrown if the file creation fails.
Listing 5: Create a file in remote path
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.100.10.200")
.remotePath("/exchange")
.useAuthenticationId("john")
.useAuthenticationPassword("doe")
.build();
// Create remote file
fileTransferServer.createRemoteFile(fileTransferSetup, "Test.xml");This operation deletes a file in the remote server path. The authenticated ID must have an account with file deletion privileges on the remote server path.
Listing 6: Delete file in remote path
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.100.10.200")
.remotePath("/exchange")
.useAuthenticationId("john")
.useAuthenticationPassword("doe")
.build();
// Delete remote file
fileTransferServer.deleteRemoteFile(fileTransferSetup, "Sundance.png");Use the upload file operations to copy files from the local file system to a remote path.
Listing 7: Upload single file to remote path
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.10.10.40")
.remotePath("/archive/order/reports")
.useAuthenticationId("admin")
.useAuthenticationPassword("admin123")
.localPath("C:\\order\\reports")
.build();
// Upload local file 'OrderSummary.pdf' to remote
// folder as 'OrderSummary_20181111.pdf'
fileTransferServer.uploadFile(fileTransferSetup, "OrderSummary_20181111.pdf",
"OrderSummary.pdf");Listing 8: Upload multiple files to remote path
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.10.10.40")
.remotePath("/gateway/outward")
.useAuthenticationId("admin")
.useAuthenticationPassword("admin123")
.localPath("D:\\exchange\\outward")
.filterByPrefix("CXF_") // Optional file filter
.filterByExtension(".xml") // In this case uploads files that match 'CXF_*.xml'
.build();
// Upload files
fileTransferServer.uploadFiles(fileTransferSetup);Use the download file operations to copy files from a remote path to the local file system.
Listing 9: Download single file from remote path
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.10.10.40")
.remotePath("/swift/gateway/inward")
.useAuthenticationId("admin")
.useAuthenticationPassword("admin123")
.localPath("C:\\transaction\\swift")
.build();
// Download file 'Sample_MT103.txt' from remote path to local
// system as 'Unprocessed_MT103.txt'
fileTransferServer.downloadFile(fileTransferSetup, "Sample_MT103.txt",
"Unprocessed_MT103.txt");Listing 10: Download multiple files from remote path
// Get file transfer server
FileTransferServer fileTransferServer = ...
// Compose transfer information
FileTransferSetup fileTransferSetup = FileTransferSetup.newBuilder()
.remoteHost("192.10.10.40")
.remotePath("/gateway/inward")
.useAuthenticationId("admin")
.useAuthenticationPassword("admin123")
.localPath("E:\\exchange\\inward")
.build(); // No filter. Download all.
// Download files
fileTransferServer.downloadFiles(fileTransferSetup);