use of com.axway.ats.core.filetransfer.model.ftp.FtpResponseListener in project ats-framework by Axway.
the class FtpClient method connect.
/**
* Connect to a remote host using basic authentication
*
* @param hostname
* the host to connect to
* @param userName
* the user name
* @param password
* the password for the provided user name
* @throws FileTransferException
*/
@Override
public void connect(String hostname, String userName, String password) throws FileTransferException {
log.info("Connecting to " + hostname + " on port " + this.port + " using username " + userName + " and password " + password);
// make new FTP object for every new connection
disconnect();
this.ftpConnection = new org.apache.commons.net.ftp.FTPClient();
if (this.listener != null) {
this.listener.setResponses(new ArrayList<String>());
this.ftpConnection.addProtocolCommandListener(((FtpResponseListener) listener));
}
/* if debug mode is true, we log messages from all levels */
if (isDebugMode()) {
this.ftpConnection.addProtocolCommandListener(new FtpListener());
}
try {
this.ftpConnection.setConnectTimeout(this.timeout);
// connect to the host
this.ftpConnection.connect(hostname, this.port);
// login to the host
if (!this.ftpConnection.login(userName, password)) {
throw new Exception("Invallid username and/or password");
}
// enter passive mode
this.ftpConnection.enterLocalPassiveMode();
// set transfer mode
if (this.transferMode == TransferMode.ASCII) {
if (!this.ftpConnection.setFileType(org.apache.commons.net.ftp.FTPClient.ASCII_FILE_TYPE)) {
throw new Exception("Unable to set transfer mode to ASCII");
}
} else {
if (!this.ftpConnection.setFileType(org.apache.commons.net.ftp.FTPClient.BINARY_FILE_TYPE)) {
throw new Exception("Unable to set transfer mode to BINARY");
}
}
} catch (Exception e) {
String errMessage = "Unable to connect to " + hostname + " on port " + this.port + " using username " + userName + " and password " + password;
log.error(errMessage, e);
throw new FileTransferException(e);
}
log.info("Successfully connected to " + hostname + " on port " + this.port + " using username " + userName + " and password " + password);
}
Aggregations