public void connect()

in ch-commons-rfs/src/main/java/com/cloudhopper/commons/rfs/provider/FtpRemoteFileSystem.java [108:279]


    public void connect() throws FileSystemException {
        // make sure we don't connect twice
        if (ftp != null) {
            throw new FileSystemException("Already connected to FTP(s) server");
        }

        // either create an SSL FTP client or normal one
        if (getProtocol() == Protocol.FTPS) {
            try {
                ftp = new FTPSClient();
            } catch (Exception e) {  //} catch (NoSuchAlgorithmException e) {
                throw new FileSystemException("Unable to create FTPS client: " + e.getMessage(), e);
            }
        } else {
            ftp = new FTPClient();
        }

        //
        // connect to ftp(s) server
        //
        try {
            int reply;

            // either connect to the default port or an overridden one
            if (getURL().getPort() == null) {
                ftp.connect(getURL().getHost());
            } else {
                ftp.connect(getURL().getHost(), getURL().getPort().intValue());
            }

            // After connection attempt, check reply code to verify we're connected
            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                // make sure we're definitely disconnected before we throw exception
                try { ftp.disconnect(); } catch (Exception e) { }
                ftp = null;
                throw new FileSystemException("FTP server refused connection (replyCode=" + reply + ")");
            }

            logger.info("Connected to remote FTP server @ " + getURL().getHost() + " (not authenticated yet)");
            
        } catch (IOException e) {
            if (ftp.isConnected()) {
                try { ftp.disconnect(); } catch (Exception ex) {}
            }
            ftp = null;
            throw new FileSystemException("Unabled to connect to FTP server @ " + getURL().getHost(), e);
        }

        //
        // login either anonymously or with user/pass combo
        //
        try {
            boolean loggedIn = false;
            if (getURL().getUsername() == null) {
                logger.info("Logging in anonymously to FTP server");
                loggedIn = ftp.login( "anonymous", "" );
            } else {
                logger.info("Logging in with username and password to FTP server");
                loggedIn = ftp.login(getURL().getUsername(), (getURL().getPassword() == null ? "" : getURL().getPassword()));
            }

            // did the login work?
            if (!loggedIn) {
                throw new FileSystemException("Login failed with FTP server (reply=" + ftp.getReplyString() + ")");
            }

            //
            // if we're using a secure protocol, encrypt the data channel
            //
            if (getProtocol() == Protocol.FTPS) {
                logger.info("Requesting FTP data channel to also be encrypted with SSL/TLS");
                ((FTPSClient)ftp).execPROT("P");
                // ignore if this actually worked or not -- file just fail to copy
            }

            //
            // make sure we're using binary files
            //
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                throw new FileSystemException("FTP server failed to switch to binary file mode (reply=" + ftp.getReplyString() + ")");
            }


            // should we go into passive or active mode?
            if (mode == Mode.ACTIVE) {
                ftp.enterLocalActiveMode();
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new FileSystemException("FTP server failed to switch to active mode (reply=" + ftp.getReplyString() + ")");
                }
            } else if (mode == Mode.PASSIVE) {
                ftp.enterLocalPassiveMode();
                if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
                    throw new FileSystemException("FTP server failed to switch to passive mode (reply=" + ftp.getReplyString() + ")");
                }
            }

            //
            // handle making or changing directories
            //
            // if the path is not empty
            if (!StringUtil.isEmpty(getURL().getPath())) {
                // recursively iterate thru directory path and attempt to create the path
                StringTokenizer path = new StringTokenizer(getURL().getPath(), "/");

                // create an array list of tokens
                ArrayList<String> pathParts = new ArrayList<String>();
                while (path.hasMoreTokens()) {
                    pathParts.add(path.nextToken());
                }

                // index we'll start searching for
                int i = 0;

                // determine path of directories we're going to take
                if (pathParts.size() > 0 && pathParts.get(i).equals("~")) {
                    // stay in home directory once logged in
                    // just increment what we'll search from
                    i = 1;
                } else {
                    // change to root directory first
                    if (!ftp.changeWorkingDirectory("/")) {
                        throw new FileSystemException("FTP server failed to change to root directory (reply=" + ftp.getReplyString() + ")");
                    }
                }

                for ( ; i < pathParts.size(); i++) {
                    // try to change to this directory
                    String pathPart = pathParts.get(i);
                    boolean changedDir = ftp.changeWorkingDirectory(pathPart);
                    if (!changedDir) {
                        if (!mkdir) {
                            // now try to change to it again
                            if (!ftp.changeWorkingDirectory(pathPart)) {
                                throw new FileSystemException("Unable to change to directory " + getURL().getPath() + " on FTP server: " + pathPart + " does not exist");
                            }
                        } else {
                            // try to create it
                            logger.info("Making new directory on FTP server: " + pathPart);
                            if (!ftp.makeDirectory(pathPart)) {
                                throw new FileSystemException("Unable to make directory '" + pathPart + "' on FTP server");
                            }
                            // now try to change to it again
                            if (!ftp.changeWorkingDirectory(pathPart)) {
                                throw new FileSystemException("Unable to change to new directory '" + pathPart + "' on FTP server");
                            }
                        }
                    }
                }
                
            // just print out our working directory
            } else {
                // staying in whatever directory we were assigned by default
                // for information purposeds, let's try to print out that dir
                String currentDir = ftp.printWorkingDirectory();
                logger.info("Current FTP working directory: " + currentDir);
            }

        } catch (FileSystemException e) {
            // make sure to disconnect, then rethrow error
            try { ftp.disconnect(); } catch (Exception ex) { }
            ftp = null;
            throw e;
        } catch (IOException e) {
            // make sure we're definitely disconnected before we throw exception
            try { ftp.disconnect(); } catch (Exception ex) { }
            ftp = null;
            throw new FileSystemException("Underlying IO exception with FTP server during login and setup process", e);
        }
    }