Search in sources :

Example 1 with FtpListener

use of com.axway.ats.core.filetransfer.model.ftp.FtpListener in project ats-framework by Axway.

the class FtpsClient method performConnect.

private void performConnect(String hostname, String userName, String password) throws FileTransferException {
    try {
        // make new FTP object for every new connection
        disconnect();
        applyCustomProperties();
        SSLContext sslContext = null;
        try {
            sslContext = createSSLContext();
        } catch (Exception e) {
            throw new Exception("Error while creating SSL context", e);
        }
        // this.ftpsConnection = new FTPSClient( this.protocol, this.implicit);
        this.ftpsConnection = new org.apache.commons.net.ftp.FTPSClient(this.implicit, sslContext);
        if (this.listener != null) {
            this.listener.setResponses(new ArrayList<String>());
            this.ftpsConnection.addProtocolCommandListener(((FtpResponseListener) listener));
        }
        /* if debug mode is true, we log messages from all levels */
        if (isDebugMode()) {
            this.ftpsConnection.addProtocolCommandListener(new FtpListener());
        }
        this.ftpsConnection.setConnectTimeout(this.timeout);
        // connect to the host
        this.ftpsConnection.connect(hostname, this.port);
        // login to the host
        if (!this.ftpsConnection.login(userName, password)) {
            throw new Exception("Invalid username and/or password. ");
        }
        // set transfer mode
        if (this.transferMode == TransferMode.ASCII) {
            if (!this.ftpsConnection.setFileType(org.apache.commons.net.ftp.FTPSClient.ASCII_FILE_TYPE)) {
                throw new Exception("Unable to set transfer mode to ASCII");
            }
        } else {
            if (!this.ftpsConnection.setFileType(org.apache.commons.net.ftp.FTPSClient.BINARY_FILE_TYPE)) {
                throw new Exception("Unable to set transfer mode to BINARY");
            }
        }
        // initial fix - always use passive mode
        // Currently not working: int replyCode = this.ftpsConnection.pasv();
        this.ftpsConnection.enterLocalPassiveMode();
    } 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);
    }
}
Also used : FtpListener(com.axway.ats.core.filetransfer.model.ftp.FtpListener) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) FtpResponseListener(com.axway.ats.core.filetransfer.model.ftp.FtpResponseListener) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Example 2 with FtpListener

use of com.axway.ats.core.filetransfer.model.ftp.FtpListener 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();
    /* 
         * When uploading/downloading file with encoding that the server does not, the server will return 452.
         * So we have to either set the control encoding to UTF-8 (or the one that is desired), or if the needed encoding is UTF-8,
         * set the UTF-8 autodetect to true
        */
    String controlEncoding = CoreLibraryConfigurator.getInstance().getFtpControlEncoding();
    boolean autodetectUTF8 = Boolean.valueOf(CoreLibraryConfigurator.getInstance().getFtpAutodetectUTF8());
    if (!StringUtils.isNullOrEmpty(controlEncoding)) {
        this.ftpConnection.setControlEncoding(controlEncoding);
        if (log.isDebugEnabled()) {
            log.debug("Control encoding is set to " + controlEncoding);
        }
    }
    this.ftpConnection.setAutodetectUTF8(autodetectUTF8);
    if (log.isDebugEnabled()) {
        log.debug("Autodetect for UTF-8 is " + ((autodetectUTF8) ? "enabled" : "disabled"));
    }
    if (!"UTF-8".equalsIgnoreCase(controlEncoding) && autodetectUTF8) {
        log.warn("Autodetecting UTF-8 is enabled, but additionaly, the control encoding is set to '" + controlEncoding + "'. UTF-8 will be used.");
    }
    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);
}
Also used : FtpListener(com.axway.ats.core.filetransfer.model.ftp.FtpListener) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException) FtpResponseListener(com.axway.ats.core.filetransfer.model.ftp.FtpResponseListener) IOException(java.io.IOException) FileTransferException(com.axway.ats.common.filetransfer.FileTransferException)

Aggregations

FileTransferException (com.axway.ats.common.filetransfer.FileTransferException)2 FtpListener (com.axway.ats.core.filetransfer.model.ftp.FtpListener)2 FtpResponseListener (com.axway.ats.core.filetransfer.model.ftp.FtpResponseListener)2 IOException (java.io.IOException)2 CertificateException (java.security.cert.CertificateException)1 SSLContext (javax.net.ssl.SSLContext)1