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

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

Introduction

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

Prototype

public FTPClient() 

Source Link

Document

Default FTPClient constructor.

Usage

From source file:org.apache.hadoop.fs.ftp.FTPFileSystem.java

/**
 * Connect to the FTP server using configuration parameters *
 * //from www .j av  a 2  s  .c  o  m
 * @return An FTPClient instance
 * @throws IOException
 */
private FTPClient connect() throws IOException {
    FTPClient client = null;
    Configuration conf = getConf();
    String host = conf.get("fs.ftp.host");
    int port = conf.getInt("fs.ftp.host.port", FTP.DEFAULT_PORT);
    String user = conf.get("fs.ftp.user." + host);
    String password = conf.get("fs.ftp.password." + host);
    client = new FTPClient();
    client.connect(host, port);
    int reply = client.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        throw new IOException("Server - " + host + " refused connection on port - " + port);
    } else if (client.login(user, password)) {
        client.setFileTransferMode(FTP.BLOCK_TRANSFER_MODE);
        client.setFileType(FTP.BINARY_FILE_TYPE);
        client.setBufferSize(DEFAULT_BUFFER_SIZE);
    } else {
        throw new IOException("Login failed on server - " + host + ", port - " + port);
    }

    return client;
}

From source file:org.apache.hive.hplsql.Ftp.java

/**
 * Open and initialize FTP/*from www .j a  v a2 s  .c  o  m*/
 */
FTPClient openConnection(HplsqlParser.Copy_from_ftp_stmtContext ctx) {
    FTPClient ftp = new FTPClient();
    Timer timer = new Timer();
    timer.start();
    try {
        ftp.connect(host);
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        if (!ftp.login(user, pwd)) {
            if (ftp.isConnected()) {
                ftp.disconnect();
            }
            exec.signal(Signal.Type.SQLEXCEPTION, "Cannot login to FTP server: " + host);
            return null;
        }
        timer.stop();
        if (info) {
            info(ctx, "Connected to ftp: " + host + " (" + timer.format() + ")");
        }
    } catch (IOException e) {
        exec.signal(e);
    }
    return ftp;
}

From source file:org.apache.jmeter.protocol.ftp.sampler.FTPSampler.java

@Override
public SampleResult sample(Entry e) {
    SampleResult res = new SampleResult();
    res.setSuccessful(false); // Assume failure
    String remote = getRemoteFilename();
    String local = getLocalFilename();
    boolean binaryTransfer = isBinaryMode();
    res.setSampleLabel(getName());//from   w ww .  jav a 2 s.c  o  m
    final String label = getLabel();
    res.setSamplerData(label);
    try {
        res.setURL(new URL(label));
    } catch (MalformedURLException e1) {
        log.warn("Cannot set URL: " + e1.getLocalizedMessage());
    }
    InputStream input = null;
    OutputStream output = null;

    res.sampleStart();
    FTPClient ftp = new FTPClient();
    try {
        savedClient = ftp;
        final int port = getPortAsInt();
        if (port > 0) {
            ftp.connect(getServer(), port);
        } else {
            ftp.connect(getServer());
        }
        res.latencyEnd();
        int reply = ftp.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            if (ftp.login(getUsername(), getPassword())) {
                if (binaryTransfer) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                ftp.enterLocalPassiveMode();// should probably come from the setup dialog
                boolean ftpOK = false;
                if (isUpload()) {
                    String contents = getLocalFileContents();
                    if (contents.length() > 0) {
                        byte[] bytes = contents.getBytes(); // TODO - charset?
                        input = new ByteArrayInputStream(bytes);
                        res.setBytes(bytes.length);
                    } else {
                        File infile = new File(local);
                        res.setBytes((int) infile.length());
                        input = new BufferedInputStream(new FileInputStream(infile));
                    }
                    ftpOK = ftp.storeFile(remote, input);
                } else {
                    final boolean saveResponse = isSaveResponse();
                    ByteArrayOutputStream baos = null; // No need to close this
                    OutputStream target = null; // No need to close this
                    if (saveResponse) {
                        baos = new ByteArrayOutputStream();
                        target = baos;
                    }
                    if (local.length() > 0) {
                        output = new FileOutputStream(local);
                        if (target == null) {
                            target = output;
                        } else {
                            target = new TeeOutputStream(output, baos);
                        }
                    }
                    if (target == null) {
                        target = new NullOutputStream();
                    }
                    input = ftp.retrieveFileStream(remote);
                    if (input == null) {// Could not access file or other error
                        res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                        res.setResponseMessage(ftp.getReplyString());
                    } else {
                        long bytes = IOUtils.copy(input, target);
                        ftpOK = bytes > 0;
                        if (saveResponse && baos != null) {
                            res.setResponseData(baos.toByteArray());
                            if (!binaryTransfer) {
                                res.setDataType(SampleResult.TEXT);
                            }
                        } else {
                            res.setBytes((int) bytes);
                        }
                    }
                }

                if (ftpOK) {
                    res.setResponseCodeOK();
                    res.setResponseMessageOK();
                    res.setSuccessful(true);
                } else {
                    res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                    res.setResponseMessage(ftp.getReplyString());
                }
            } else {
                res.setResponseCode(Integer.toString(ftp.getReplyCode()));
                res.setResponseMessage(ftp.getReplyString());
            }
        } else {
            res.setResponseCode("501"); // TODO
            res.setResponseMessage("Could not connect");
            //res.setResponseCode(Integer.toString(ftp.getReplyCode()));
            res.setResponseMessage(ftp.getReplyString());
        }
    } catch (IOException ex) {
        res.setResponseCode("000"); // TODO
        res.setResponseMessage(ex.toString());
    } finally {
        savedClient = null;
        if (ftp.isConnected()) {
            try {
                ftp.logout();
            } catch (IOException ignored) {
            }
            try {
                ftp.disconnect();
            } catch (IOException ignored) {
            }
        }
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);
    }

    res.sampleEnd();
    return res;
}

From source file:org.apache.maven.wagon.providers.ftp.FtpWagon.java

protected void openConnectionInternal() throws ConnectionException, AuthenticationException {
    AuthenticationInfo authInfo = getAuthenticationInfo();

    if (authInfo == null) {
        throw new IllegalArgumentException("Authentication Credentials cannot be null for FTP protocol");
    }/*  w ww .j  a va2  s  . c  om*/

    if (authInfo.getUserName() == null) {
        authInfo.setUserName(System.getProperty("user.name"));
    }

    String username = authInfo.getUserName();

    String password = authInfo.getPassword();

    if (username == null) {
        throw new AuthenticationException("Username not specified for repository " + getRepository().getId());
    }
    if (password == null) {
        throw new AuthenticationException("Password not specified for repository " + getRepository().getId());
    }

    String host = getRepository().getHost();

    ftp = new FTPClient();
    ftp.setDefaultTimeout(getTimeout());
    ftp.setDataTimeout(getTimeout());
    ftp.setControlEncoding(getControlEncoding());

    ftp.addProtocolCommandListener(new PrintCommandListener(this));

    try {
        if (getRepository().getPort() != WagonConstants.UNKNOWN_PORT) {
            ftp.connect(host, getRepository().getPort());
        } else {
            ftp.connect(host);
        }

        // After connection attempt, you should check the reply code to
        // verify
        // success.
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();

            throw new AuthenticationException("FTP server refused connection.");
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                fireSessionError(e);

                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }

        throw new AuthenticationException("Could not connect to server.", e);
    }

    try {
        if (!ftp.login(username, password)) {
            throw new AuthenticationException("Cannot login to remote system");
        }

        fireSessionDebug("Remote system is " + ftp.getSystemName());

        // Set to binary mode.
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.setListHiddenFiles(true);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        if (isPassiveMode()) {
            ftp.enterLocalPassiveMode();
        }
    } catch (IOException e) {
        throw new ConnectionException("Cannot login to remote system", e);
    }
}

From source file:org.apache.maven.wagon.providers.ftp.FtpWagonInclRmdir.java

protected void openConnectionInternal() throws ConnectionException, AuthenticationException {
    logger.info("Open ftp connection ...");
    this.remoteRemovedDirectories = new LinkedList<String>();

    AuthenticationInfo authInfo = getAuthenticationInfo();

    if (authInfo == null) {
        throw new IllegalArgumentException("Authentication Credentials cannot be null for FTP protocol");
    }//from  w  ww.java  2 s .c  o  m

    if (authInfo.getUserName() == null) {
        authInfo.setUserName(System.getProperty("user.name"));
    }

    String username = authInfo.getUserName();

    String password = authInfo.getPassword();

    if (username == null) {
        throw new AuthenticationException("Username not specified for repository " + getRepository().getId());
    }
    if (password == null) {
        throw new AuthenticationException("Password not specified for repository " + getRepository().getId());
    }

    String host = getRepository().getHost();

    ftp = new FTPClient();
    ftp.setDefaultTimeout(getTimeout());
    ftp.setDataTimeout(getTimeout());
    ftp.setControlEncoding(getControlEncoding());

    ftp.addProtocolCommandListener(new PrintCommandListener(this));

    try {
        if (getRepository().getPort() != WagonConstants.UNKNOWN_PORT) {
            ftp.connect(host, getRepository().getPort());
        } else {
            ftp.connect(host);
        }

        // After connection attempt, you should check the reply code to
        // verify
        // success.
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();

            throw new AuthenticationException("FTP server refused connection.");
        }
    } catch (IOException e) {
        if (ftp.isConnected()) {
            try {
                fireSessionError(e);

                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }

        throw new AuthenticationException("Could not connect to server.", e);
    }

    try {
        if (!ftp.login(username, password)) {
            throw new AuthenticationException("Cannot login to remote system");
        }

        fireSessionDebug("Remote system is " + ftp.getSystemName());

        // Set to binary mode.
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.setListHiddenFiles(true);

        // Use passive mode as default because most of us are
        // behind firewalls these days.
        if (isPassiveMode()) {
            ftp.enterLocalPassiveMode();
        }
    } catch (IOException e) {
        throw new ConnectionException("Cannot login to remote system", e);
    }
}

From source file:org.apache.nifi.processors.standard.util.FTPTransfer.java

private FTPClient getClient(final FlowFile flowFile) throws IOException {
    if (client != null) {
        String desthost = ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue();
        if (remoteHostName.equals(desthost)) {
            // destination matches so we can keep our current session
            resetWorkingDirectory();/* w  ww .  j  a  va2s. c  o  m*/
            return client;
        } else {
            // this flowFile is going to a different destination, reset session
            close();
        }
    }

    final Proxy.Type proxyType = Proxy.Type.valueOf(ctx.getProperty(PROXY_TYPE).getValue());
    final String proxyHost = ctx.getProperty(PROXY_HOST).getValue();
    final Integer proxyPort = ctx.getProperty(PROXY_PORT).asInteger();
    FTPClient client;
    if (proxyType == Proxy.Type.HTTP) {
        client = new FTPHTTPClient(proxyHost, proxyPort, ctx.getProperty(HTTP_PROXY_USERNAME).getValue(),
                ctx.getProperty(HTTP_PROXY_PASSWORD).getValue());
    } else {
        client = new FTPClient();
        if (proxyType == Proxy.Type.SOCKS) {
            client.setSocketFactory(new SocksProxySocketFactory(
                    new Proxy(proxyType, new InetSocketAddress(proxyHost, proxyPort))));
        }
    }
    this.client = client;
    client.setDataTimeout(ctx.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    client.setDefaultTimeout(
            ctx.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    client.setRemoteVerificationEnabled(false);

    final String remoteHostname = ctx.getProperty(HOSTNAME).evaluateAttributeExpressions(flowFile).getValue();
    this.remoteHostName = remoteHostname;
    InetAddress inetAddress = null;
    try {
        inetAddress = InetAddress.getByAddress(remoteHostname, null);
    } catch (final UnknownHostException uhe) {
    }

    if (inetAddress == null) {
        inetAddress = InetAddress.getByName(remoteHostname);
    }

    client.connect(inetAddress, ctx.getProperty(PORT).evaluateAttributeExpressions(flowFile).asInteger());
    this.closed = false;
    client.setDataTimeout(ctx.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    client.setSoTimeout(ctx.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());

    final String username = ctx.getProperty(USERNAME).evaluateAttributeExpressions(flowFile).getValue();
    final String password = ctx.getProperty(PASSWORD).evaluateAttributeExpressions(flowFile).getValue();
    final boolean loggedIn = client.login(username, password);
    if (!loggedIn) {
        throw new IOException("Could not login for user '" + username + "'");
    }

    final String connectionMode = ctx.getProperty(CONNECTION_MODE).getValue();
    if (connectionMode.equalsIgnoreCase(CONNECTION_MODE_ACTIVE)) {
        client.enterLocalActiveMode();
    } else {
        client.enterLocalPassiveMode();
    }

    final String transferMode = ctx.getProperty(TRANSFER_MODE).getValue();
    final int fileType = (transferMode.equalsIgnoreCase(TRANSFER_MODE_ASCII)) ? FTPClient.ASCII_FILE_TYPE
            : FTPClient.BINARY_FILE_TYPE;
    if (!client.setFileType(fileType)) {
        throw new IOException("Unable to set transfer mode to type " + transferMode);
    }

    this.homeDirectory = client.printWorkingDirectory();
    return client;
}

From source file:org.apache.nifi.processors.standard.util.FTPUtils.java

/**
 * Creates a new FTPClient connected to an FTP server. The following properties must exist:
 * <ul>Required Properties:/* ww w. j  a va  2s.  co  m*/
 * <li>remote.host - The hostname or IP address of the FTP server to connect to</li>
 * <li>remote.user - The username of the account to authenticate with</li>
 * <li>remote.password = The password for the username to authenticate with</li>
 * </ul>
 * <ul>Optional Properties:
 * <li>remote.port - The port on the FTP server to connect to. Defaults to FTP default.</li>
 * <li>transfer.mode - The type of transfer for this connection ('ascii', 'binary'). Defaults to 'binary'</li>
 * <li>connection.mode - The type of FTP connection to make ('active_local', 'passive_local'). Defaults to 'active_local'. In active_local the server initiates 'data connections' to the client
 * where in passive_local the client initiates 'data connections' to the server.</li>
 * <li>network.data.timeout - Default is 0. Sets the timeout in milliseconds for waiting to establish a new 'data connection' (not a control connection) when in ACTIVE_LOCAL mode. Also, this
 * establishes the amount of time to wait on read calls on the data connection in either mode. A value of zero means do not timeout. Users should probably set a value here unless using very
 * reliable communications links or else risk indefinite hangs that require a restart.</li>
 * <li>network.socket.timeout - Default is 0. Sets the timeout in milliseconds to use when creating a new control channel socket and also a timeout to set when reading from a control socket. A
 * value of zero means do not timeout. Users should probably set a value here unless using very reliable communications links or else risk indefinite hangs that require a restart.</li>
 * </ul>
 *
 * @param conf conf
 * @param monitor if provided will be used to monitor FTP commands processed but may be null
 * @return FTPClient connected to FTP server as configured
 * @throws NullPointerException if either argument is null
 * @throws IllegalArgumentException if a required property is missing
 * @throws NumberFormatException if any argument that must be an int cannot be converted to int
 * @throws IOException if some problem occurs connecting to FTP server
 */
public static FTPClient connect(final FTPConfiguration conf, final ProtocolCommandListener monitor)
        throws IOException {
    if (null == conf) {
        throw new NullPointerException();
    }

    final String portVal = conf.port;
    final int portNum = (null == portVal) ? -1 : Integer.parseInt(portVal);
    final String connectionModeVal = conf.connectionMode;
    final String connectionMode = (null == connectionModeVal) ? ACTIVE_LOCAL_CONNECTION_MODE
            : connectionModeVal;
    final String transferModeVal = conf.transferMode;
    final String transferMode = (null == transferModeVal) ? BINARY_TRANSFER_MODE : transferModeVal;
    final String networkDataTimeoutVal = conf.dataTimeout;
    final int networkDataTimeout = (null == networkDataTimeoutVal) ? 0
            : Integer.parseInt(networkDataTimeoutVal);
    final String networkSocketTimeoutVal = conf.connectionTimeout;
    final int networkSocketTimeout = (null == networkSocketTimeoutVal) ? 0
            : Integer.parseInt(networkSocketTimeoutVal);

    final FTPClient client = new FTPClient();
    if (networkDataTimeout > 0) {
        client.setDataTimeout(networkDataTimeout);
    }
    if (networkSocketTimeout > 0) {
        client.setDefaultTimeout(networkSocketTimeout);
    }
    client.setRemoteVerificationEnabled(false);
    if (null != monitor) {
        client.addProtocolCommandListener(monitor);
    }
    InetAddress inetAddress = null;
    try {
        inetAddress = InetAddress.getByAddress(conf.remoteHostname, null);
    } catch (final UnknownHostException uhe) {
    }
    if (inetAddress == null) {
        inetAddress = InetAddress.getByName(conf.remoteHostname);
    }

    if (portNum < 0) {
        client.connect(inetAddress);
    } else {
        client.connect(inetAddress, portNum);
    }
    if (networkDataTimeout > 0) {
        client.setDataTimeout(networkDataTimeout);
    }
    if (networkSocketTimeout > 0) {
        client.setSoTimeout(networkSocketTimeout);
    }
    final boolean loggedIn = client.login(conf.username, conf.password);
    if (!loggedIn) {
        throw new IOException("Could not login for user '" + conf.username + "'");
    }
    if (connectionMode.equals(ACTIVE_LOCAL_CONNECTION_MODE)) {
        client.enterLocalActiveMode();
    } else if (connectionMode.equals(PASSIVE_LOCAL_CONNECTION_MODE)) {
        client.enterLocalPassiveMode();
    }
    boolean transferModeSet = false;
    if (transferMode.equals(ASCII_TRANSFER_MODE)) {
        transferModeSet = client.setFileType(FTPClient.ASCII_FILE_TYPE);
    } else {
        transferModeSet = client.setFileType(FTPClient.BINARY_FILE_TYPE);
    }
    if (!transferModeSet) {
        throw new IOException("Unable to set transfer mode to type " + transferMode);
    }

    return client;
}

From source file:org.apache.ofbiz.common.FtpServices.java

public static Map<String, Object> putFile(DispatchContext dctx, Map<String, ?> context) {
    Locale locale = (Locale) context.get("locale");
    Debug.logInfo("[putFile] starting...", module);
    InputStream localFile = null;
    try {/*from w  w w .ja  v a 2 s  . c o m*/
        localFile = new FileInputStream((String) context.get("localFilename"));
    } catch (IOException ioe) {
        Debug.logError(ioe, "[putFile] Problem opening local file", module);
        return ServiceUtil
                .returnError(UtilProperties.getMessage(resource, "CommonFtpFileCannotBeOpen", locale));
    }
    List<String> errorList = new LinkedList<String>();
    FTPClient ftp = new FTPClient();
    try {
        Integer defaultTimeout = (Integer) context.get("defaultTimeout");
        if (UtilValidate.isNotEmpty(defaultTimeout)) {
            Debug.logInfo("[putFile] set default timeout to: " + defaultTimeout.intValue() + " milliseconds",
                    module);
            ftp.setDefaultTimeout(defaultTimeout.intValue());
        }
        Debug.logInfo("[putFile] connecting to: " + (String) context.get("hostname"), module);
        ftp.connect((String) context.get("hostname"));
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            Debug.logInfo("[putFile] Server refused connection", module);
            errorList.add(UtilProperties.getMessage(resource, "CommonFtpConnectionRefused", locale));
        } else {
            String username = (String) context.get("username");
            String password = (String) context.get("password");
            Debug.logInfo("[putFile] logging in: username=" + username + ", password=" + password, module);
            if (!ftp.login(username, password)) {
                Debug.logInfo("[putFile] login failed", module);
                errorList.add(UtilProperties.getMessage(resource, "CommonFtpLoginFailure",
                        UtilMisc.toMap("username", username, "password", password), locale));
            } else {
                Boolean binaryTransfer = (Boolean) context.get("binaryTransfer");
                boolean binary = (binaryTransfer == null) ? false : binaryTransfer.booleanValue();
                if (binary) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                Boolean passiveMode = (Boolean) context.get("passiveMode");
                boolean passive = (passiveMode == null) ? true : passiveMode.booleanValue();
                if (passive) {
                    ftp.enterLocalPassiveMode();
                }
                Debug.logInfo("[putFile] storing local file remotely as: " + context.get("remoteFilename"),
                        module);
                if (!ftp.storeFile((String) context.get("remoteFilename"), localFile)) {
                    Debug.logInfo("[putFile] store was unsuccessful", module);
                    errorList.add(UtilProperties.getMessage(resource, "CommonFtpFileNotSentSuccesfully",
                            UtilMisc.toMap("replyString", ftp.getReplyString()), locale));
                } else {
                    Debug.logInfo("[putFile] store was successful", module);
                    List<String> siteCommands = checkList(context.get("siteCommands"), String.class);
                    if (siteCommands != null) {
                        for (String command : siteCommands) {
                            Debug.logInfo("[putFile] sending SITE command: " + command, module);
                            if (!ftp.sendSiteCommand(command)) {
                                errorList.add(UtilProperties.getMessage(resource, "CommonFtpSiteCommandFailed",
                                        UtilMisc.toMap("command", command, "replyString", ftp.getReplyString()),
                                        locale));
                            }
                        }
                    }
                }
            }
            ftp.logout();
        }
    } catch (IOException ioe) {
        Debug.logWarning(ioe, "[putFile] caught exception: " + ioe.getMessage(), module);
        errorList.add(UtilProperties.getMessage(resource, "CommonFtpProblemWithTransfer",
                UtilMisc.toMap("errorString", ioe.getMessage()), locale));
    } finally {
        try {
            if (ftp.isConnected()) {
                ftp.disconnect();
            }
        } catch (Exception e) {
            Debug.logWarning(e, "[putFile] Problem with FTP disconnect: ", module);
        }
        try {
            localFile.close();
        } catch (Exception e) {
            Debug.logWarning(e, "[putFile] Problem closing local file: ", module);
        }
    }
    if (errorList.size() > 0) {
        Debug.logError("[putFile] The following error(s) (" + errorList.size() + ") occurred: " + errorList,
                module);
        return ServiceUtil.returnError(errorList);
    }
    Debug.logInfo("[putFile] finished successfully", module);
    return ServiceUtil.returnSuccess();
}

From source file:org.apache.ofbiz.common.FtpServices.java

public static Map<String, Object> getFile(DispatchContext dctx, Map<String, ?> context) {
    Locale locale = (Locale) context.get("locale");
    String localFilename = (String) context.get("localFilename");
    OutputStream localFile = null;
    try {/*  w  w w . j  a v a 2  s  . c om*/
        localFile = new FileOutputStream(localFilename);
    } catch (IOException ioe) {
        Debug.logError(ioe, "[getFile] Problem opening local file", module);
        return ServiceUtil
                .returnError(UtilProperties.getMessage(resource, "CommonFtpFileCannotBeOpen", locale));
    }
    List<String> errorList = new LinkedList<String>();
    FTPClient ftp = new FTPClient();
    try {
        Integer defaultTimeout = (Integer) context.get("defaultTimeout");
        if (UtilValidate.isNotEmpty(defaultTimeout)) {
            Debug.logInfo("[getFile] Set default timeout to: " + defaultTimeout.intValue() + " milliseconds",
                    module);
            ftp.setDefaultTimeout(defaultTimeout.intValue());
        }
        ftp.connect((String) context.get("hostname"));
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            errorList.add(UtilProperties.getMessage(resource, "CommonFtpConnectionRefused", locale));
        } else {
            String username = (String) context.get("username");
            String password = (String) context.get("password");
            if (!ftp.login(username, password)) {
                errorList.add(UtilProperties.getMessage(resource, "CommonFtpLoginFailure",
                        UtilMisc.toMap("username", username, "password", password), locale));
            } else {
                Boolean binaryTransfer = (Boolean) context.get("binaryTransfer");
                boolean binary = (binaryTransfer == null) ? false : binaryTransfer.booleanValue();
                if (binary) {
                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                }
                Boolean passiveMode = (Boolean) context.get("passiveMode");
                boolean passive = (passiveMode == null) ? false : passiveMode.booleanValue();
                if (passive) {
                    ftp.enterLocalPassiveMode();
                }
                if (!ftp.retrieveFile((String) context.get("remoteFilename"), localFile)) {
                    errorList.add(UtilProperties.getMessage(resource, "CommonFtpFileNotSentSuccesfully",
                            UtilMisc.toMap("replyString", ftp.getReplyString()), locale));
                }
            }
            ftp.logout();
        }
    } catch (IOException ioe) {
        Debug.logWarning(ioe, "[getFile] caught exception: " + ioe.getMessage(), module);
        errorList.add(UtilProperties.getMessage(resource, "CommonFtpProblemWithTransfer",
                UtilMisc.toMap("errorString", ioe.getMessage()), locale));
    } finally {
        try {
            if (ftp.isConnected()) {
                ftp.disconnect();
            }
        } catch (Exception e) {
            Debug.logWarning(e, "[getFile] Problem with FTP disconnect: ", module);
        }
        try {
            localFile.close();
        } catch (Exception e) {
            Debug.logWarning(e, "[getFile] Problem closing local file: ", module);
        }
    }
    if (errorList.size() > 0) {
        Debug.logError("[getFile] The following error(s) (" + errorList.size() + ") occurred: " + errorList,
                module);
        return ServiceUtil.returnError(errorList);
    }
    return ServiceUtil.returnSuccess();
}

From source file:org.apache.oodt.cas.protocol.ftp.CommonsNetFtpProtocol.java

/**
 * Creates a new FtpClient
 */
public CommonsNetFtpProtocol() {
    ftp = new FTPClient();
}