Skip to content

Instantly share code, notes, and snippets.

@emoran
Created November 4, 2020 21:29
Show Gist options
  • Save emoran/b287f416cb2c1191368b80d5d587f4d2 to your computer and use it in GitHub Desktop.
Save emoran/b287f416cb2c1191368b80d5d587f4d2 to your computer and use it in GitHub Desktop.
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
public class FTPSClientController extends AbstractMessageTransformer{
@Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
Boolean result=false;
String sftp_host = (String)message.getInvocationProperty("sftp_host");
sftp_host = sftp_host.replaceAll("\\s+","");
String userName = (String)message.getInvocationProperty("sftp_username");
userName = userName.replaceAll("\\s+","");
String password = (String)message.getInvocationProperty("sftp_password");
password = password.replaceAll("\\s+","");
String folder = (String)message.getInvocationProperty("dateFolderVar");
String remoteFileName = (String)message.getInvocationProperty("remoteFileName");
int port = 21;
String status = null;
try{
status = ftpsCreateDirectoryTree(sftp_host, port, userName, password,folder);
status = uploadFile(sftp_host, port, userName, password, message, remoteFileName);
logger.info("FTP Status: "+status);
}
catch(Exception er){
status = er.getMessage();
logger.info("Error occurred: "+er.getMessage());
}
message.setInvocationProperty("ftpUploadStatus", status);
return result;
}
public String ftpsCreateDirectoryTree(String sftp_host,
int port,
String userName,
String password, String dirTree) throws IOException {
boolean dirExists = true;
String status = null;
//tokenize the string and attempt to change into each directory level. If you cannot, then start creating.
String[] directories = dirTree.split("/");
FTPSClient ftpClient = new FTPSClient(false);
// Connect to sftp_host
ftpClient.connect(sftp_host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
// Login
if (ftpClient.login(userName, password)) {
for (String dir : directories ) {
if (!dir.isEmpty() ) {
if (dirExists) {
dirExists = ftpClient.changeWorkingDirectory(dir);
}
if (!dirExists) {
if (!ftpClient.makeDirectory(dir)) {
status = "Unable to create remote directory ".concat(dir).concat(" Error : ").concat(ftpClient.getReplyString());
throw new IOException("Unable to create remote directory '" + dir + "'. error='" + ftpClient.getReplyString()+"'");
}
if (!ftpClient.changeWorkingDirectory(dir)) {
status = "Unable to change into newly created remote directory ".concat(dir).concat(" Error : ").concat(ftpClient.getReplyString());
throw new IOException("Unable to change into newly created remote directory '" + dir + "'. error='" + ftpClient.getReplyString()+"'");
}
}
}
}
status = "Folders Created....";
System.out.println("Folders Created....");
// Logout
ftpClient.logout();
} else {
status = "FTP login failed";
System.out.println("FTP login failed");
}
// Disconnect
ftpClient.disconnect();
} else {
status = "FTP connect to sftp_host failed";
System.out.println("FTP connect to sftp_host failed");
}
return status;
}
public String uploadFile(String sftp_host,
int port,
String userName,
String password,
MuleMessage message,
String remoteFilename) throws Exception {
String status = null;
try {
FTPSClient ftpClient = new FTPSClient(false);
// Connect to sftp_host
ftpClient.connect(sftp_host, port);
int reply = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(reply)) {
// Login
if (ftpClient.login(userName, password)) {
// Set protection buffer size
ftpClient.execPBSZ(0);
// Set data channel protection to private
ftpClient.execPROT("P");
// Enter local passive mode
ftpClient.enterLocalPassiveMode();
// Store file on sftp_host
InputStream stream = new ByteArrayInputStream(message.getPayloadAsBytes());
if (ftpClient.storeFile(remoteFilename, stream)) {
stream.close();
status = "******* File uploaded to FTPS *******";
System.out.println("File uploaded to FTPS");
} else {
status = "Could not store file";
System.out.println("Could not store file");
}
// Logout
ftpClient.logout();
} else {
status = "FTP login failed";
System.out.println("FTP login failed");
}
// Disconnect
ftpClient.disconnect();
} else {
status = "Could not store file";
System.out.println("Could not store file");
}
} catch (IOException ioe) {
status = "FTP client received network error";
System.out.println("FTP client received network error");
}
logger.info("FTP Upload File Status: "+status);
return status;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment