Example usage for org.apache.commons.net.ftp FTPClientConfig FTPClientConfig

List of usage examples for org.apache.commons.net.ftp FTPClientConfig FTPClientConfig

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClientConfig FTPClientConfig.

Prototype

public FTPClientConfig(String systemKey) 

Source Link

Document

The main constructor for an FTPClientConfig object

Usage

From source file:org.apache.sqoop.util.MainframeFTPClientUtils.java

public static FTPClient getFTPConnection(Configuration conf) throws IOException {
    FTPClient ftp = null;//  ww  w  .  java  2 s .c  om
    try {
        String username = conf.get(DBConfiguration.USERNAME_PROPERTY);
        String password;
        if (username == null) {
            username = "anonymous";
            password = "";
        } else {
            password = DBConfiguration.getPassword((JobConf) conf);
        }

        String connectString = conf.get(DBConfiguration.URL_PROPERTY);
        String server = connectString;
        int port = 0;
        String[] parts = connectString.split(":");
        if (parts.length == 2) {
            server = parts[0];
            try {
                port = Integer.parseInt(parts[1]);
            } catch (NumberFormatException e) {
                LOG.warn("Invalid port number: " + e.toString());
            }
        }

        if (null != mockFTPClient) {
            ftp = mockFTPClient;
        } else {
            ftp = new FTPClient();
        }

        FTPClientConfig config = new FTPClientConfig(FTPClientConfig.SYST_MVS);
        ftp.configure(config);

        if (conf.getBoolean(JobBase.PROPERTY_VERBOSE, false)) {
            ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
        }
        try {
            if (port > 0) {
                ftp.connect(server, port);
            } else {
                ftp.connect(server);
            }
        } catch (IOException ioexp) {
            throw new IOException("Could not connect to server " + server, ioexp);
        }

        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            throw new IOException("FTP server " + server + " refused connection:" + ftp.getReplyString());
        }
        LOG.info("Connected to " + server + " on " + (port > 0 ? port : ftp.getDefaultPort()));
        if (!ftp.login(username, password)) {
            ftp.logout();
            throw new IOException("Could not login to server " + server + ":" + ftp.getReplyString());
        }
        // set ASCII transfer mode
        ftp.setFileType(FTP.ASCII_FILE_TYPE);
        // Use passive mode as default.
        ftp.enterLocalPassiveMode();
        LOG.info("System type detected: " + ftp.getSystemType());
    } catch (IOException ioe) {
        if (ftp != null && ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
        ftp = null;
        throw ioe;
    }
    return ftp;
}

From source file:org.apache.tools.ant.taskdefs.optional.net.FTPConfigurator.java

/**
 * configures the supplied FTPClient with the various
 * attributes set in the supplied FTP task.
 * @param client the FTPClient to be configured
 * @param task the FTP task whose attributes are used to
 * configure the client/* w w  w  .j a  v a 2  s  . co m*/
 * @return the client as configured.
 */
static FTPClient configure(FTPClient client, FTPTaskConfig task) {
    task.log("custom configuration", Project.MSG_VERBOSE);
    FTPClientConfig config;
    String systemTypeKey = task.getSystemTypeKey();
    if (systemTypeKey != null && !"".equals(systemTypeKey)) {
        config = new FTPClientConfig(systemTypeKey);
        task.log("custom config: system key = " + systemTypeKey, Project.MSG_VERBOSE);
    } else {
        config = new FTPClientConfig();
        task.log("custom config: system key = default (UNIX)", Project.MSG_VERBOSE);
    }

    String defaultDateFormatConfig = task.getDefaultDateFormatConfig();
    if (defaultDateFormatConfig != null) {
        config.setDefaultDateFormatStr(defaultDateFormatConfig);
        task.log("custom config: default date format = " + defaultDateFormatConfig, Project.MSG_VERBOSE);
    }

    String recentDateFormatConfig = task.getRecentDateFormatConfig();
    if (recentDateFormatConfig != null) {
        config.setRecentDateFormatStr(recentDateFormatConfig);
        task.log("custom config: recent date format = " + recentDateFormatConfig, Project.MSG_VERBOSE);
    }

    String serverLanguageCodeConfig = task.getServerLanguageCodeConfig();
    if (serverLanguageCodeConfig != null) {
        if (!"".equals(serverLanguageCodeConfig)
                && !FTPClientConfig.getSupportedLanguageCodes().contains(serverLanguageCodeConfig)) {
            throw new BuildException("unsupported language code" + serverLanguageCodeConfig);
        }
        config.setServerLanguageCode(serverLanguageCodeConfig);
        task.log("custom config: server language code = " + serverLanguageCodeConfig, Project.MSG_VERBOSE);
    }

    String serverTimeZoneConfig = task.getServerTimeZoneConfig();
    if (serverTimeZoneConfig != null) {
        config.setServerTimeZoneId(serverTimeZoneConfig);
        task.log("custom config: server time zone ID = " + serverTimeZoneConfig, Project.MSG_VERBOSE);
    }

    String shortMonthNamesConfig = task.getShortMonthNamesConfig();
    if (shortMonthNamesConfig != null) {
        config.setShortMonthNames(shortMonthNamesConfig);
        task.log("custom config: short month names = " + shortMonthNamesConfig, Project.MSG_VERBOSE);
    }
    client.configure(config);
    return client;
}

From source file:org.codelibs.fess.crawler.client.ftp.FtpClient.java

public synchronized void init() {
    if (ftpAuthenticationHolder != null) {
        return;/*w  w  w  . j  av  a2  s.  co  m*/
    }

    super.init();

    final String systemKey = getInitParameter("ftpConfigSystemKey", FTPClientConfig.SYST_UNIX, String.class);
    ftpClientConfig = new FTPClientConfig(systemKey);

    final String serverLanguageCode = getInitParameter("ftpConfigServerLanguageCode", "en", String.class);
    ftpClientConfig.setServerLanguageCode(serverLanguageCode);

    final String serverTimeZoneId = getInitParameter("ftpConfigServerTimeZoneId", null, String.class);
    if (serverTimeZoneId != null) {
        ftpClientConfig.setServerTimeZoneId(serverTimeZoneId);
    }

    activeExternalHost = getInitParameter("activeExternalHost", null, String.class);
    activeMinPort = getInitParameter("activeMinPort", -1, Integer.class);
    activeMaxPort = getInitParameter("activeMaxPort", -1, Integer.class);
    autodetectEncoding = getInitParameter("autodetectEncoding", true, Boolean.class);
    connectTimeout = getInitParameter("connectTimeout", 0, Integer.class);
    dataTimeout = getInitParameter("dataTimeout", -1, Integer.class);
    controlEncoding = getInitParameter("controlEncoding", Constants.UTF_8, String.class);
    bufferSize = getInitParameter("bufferSize", 0, Integer.class);
    passiveLocalHost = getInitParameter("passiveLocalHost", null, String.class);
    passiveNatWorkaround = getInitParameter("passiveNatWorkaround", true, Boolean.class);
    reportActiveExternalHost = getInitParameter("reportActiveExternalHost", null, String.class);
    useEPSVwithIPv4 = getInitParameter("useEPSVwithIPv4", false, Boolean.class);

    // ftp auth
    final FtpAuthenticationHolder holder = new FtpAuthenticationHolder();
    final FtpAuthentication[] ftpAuthentications = getInitParameter(FTP_AUTHENTICATIONS_PROPERTY,
            new FtpAuthentication[0], FtpAuthentication[].class);
    if (ftpAuthentications != null) {
        for (final FtpAuthentication ftpAuthentication : ftpAuthentications) {
            holder.add(ftpAuthentication);
        }
    }
    ftpAuthenticationHolder = holder;
}

From source file:org.ikasan.connector.ftp.net.FileTransferProtocolClient.java

/**
 * Method that handles the creation and connection for FTP.
 * /*from ww  w . ja v a  2 s.c o m*/
 * @throws ClientConnectionException if connection attempt fails
 */
private void doConnect() throws ClientConnectionException {

    this.ftpClient = new FTPClient();

    String msg = new String("Attempting connection to [" + remoteHostname + "]."); //$NON-NLS-1$ //$NON-NLS-2$
    logger.debug(msg);
    try {
        /*
         * Summer (Nov 26th 2008): Rather than relying on the FTP client to
         * figure out the system it is connecting to (and hence what parsers
         * it should use) we pass this configuration to the client and force
         * it to use it.
         */
        if ((systemKey != null) && (!"".equals(systemKey))) {
            ftpClient.configure(new FTPClientConfig(systemKey));
        }
        // leave local port unspecified
        int localPort = 0;
        this.ftpClient.setDefaultTimeout(this.connectionTimeout);
        // Keep trying to connect, until successful
        for (int i = 0; i < DEFAULT_MAXIMUM_LOCAL_PORT; i++) {
            try {
                logger.debug("Connecting to remote host [" + this.remoteHostname + ":" + this.remotePort
                        + "] from local host [" + this.localHostname + ":" + localPort + "].");
                // Had to update the  ftpClient.connect method as the localhost was not resolving correctly
                //ftpClient.connect(InetAddress.getByName(this.remoteHostname), this.remotePort, InetAddress.getByName(this.localHostname), localPort);
                ftpClient.connect(InetAddress.getByName(this.remoteHostname), this.remotePort);

                int reply = ftpClient.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
                    throw new SocketException("Connection attempt failed with replyCode [" + reply + "]");
                }

                if (active) {
                    this.ftpClient.enterLocalActiveMode();
                } else {
                    this.ftpClient.enterLocalPassiveMode();
                }

                this.ftpClient.setSoTimeout(this.socketTimeout);
                this.ftpClient.setDataTimeout(this.dataTimeout);
                break;
            } catch (BindException be) {
                logger.info("Address is already in use.. will try again.", be);
            }
        }
    } catch (SocketException se) {
        msg = new String(msg + " [Failed]"); //$NON-NLS-1$
        logger.info(msg, se);
        // Clean up after ourselves just in case
        try {
            if (this.ftpClient != null && this.ftpClient.isConnected()) {
                this.ftpClient.disconnect();
            }
        } catch (IOException disconnectException) {
            logger.warn("Could not cleanup after a failed connect, this may leave behind open sockets.",
                    disconnectException);
        }
        throw new ClientConnectionException(msg, se);
    } catch (IOException ie) {
        msg = new String(msg + " [Failed]"); //$NON-NLS-1$
        logger.info(msg, ie);
        // Clean up after ourselves
        try {
            if (this.ftpClient != null && this.ftpClient.isConnected()) {
                this.ftpClient.disconnect();
            }
        } catch (IOException disconnectException) {
            logger.warn("Could not cleanup after a failed connect, this may leave behind open sockets.",
                    disconnectException);
        }
        throw new ClientConnectionException(msg, ie);
    }

    logger.info(
            "Connected to host [" + remoteHostname + "]. " + "Mode [" + (active ? "active" : "passive") + "].");
}

From source file:org.jengine.mbean.FTPHL7Client.java

public void start() throws IOException {
    log = Category.getInstance(getClass() + ":" + ifName);
    log.debug("---start()---");
    try {//from  w w w  .j  a v  a2  s .  c o  m

        initializeQueueConnection();

        FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
        conf.setDefaultDateFormatStr(getDefaultDateFormatStr());
        conf.setRecentDateFormatStr(getRecentDateFormatStr());
        this.configure(conf);

        running = true;
        threadPool.run(this);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.okj.commons.net.FtpUtils.java

/** 
 * FTP // w  w w.ja v  a  2  s .c  o m
 *  
 * @return 
 * @throws Exception 
 */
protected FTPClientConfig getFTPClientConfig() {
    String systemKey = FTPClientConfig.SYST_UNIX;
    //String serverLanguageCode = "zh";
    FTPClientConfig conf = new FTPClientConfig(systemKey);
    //conf.setServerLanguageCode(serverLanguageCode);
    conf.setDefaultDateFormatStr("yyyy-MM-dd");
    return conf;
}