core/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java
/**
     * Creates a new connection to the server.
     *
     * @param hostname          The host name of the server.
     * @param port              The port to connect to.
     * @param username          The name of the user for authentication.
     * @param password          The user's password.
     * @param workingDirectory  The base directory.
     * @param fileSystemOptions The FileSystemOptions.
     * @param proxyServer       Proxy server address
     * @param proxyPort         Proxy server port
     * @param proxyUser         Proxy server username
     * @param proxyPassword     Proxy server password
     * @param defaultTimeout    default Timeout for the connection
     * @return An FTPClient.
     * @throws FileSystemException if an error occurs while connecting.
     */
Creates a new connection to the server.
@param hostname          The host name of the server.
@param port              The port to connect to.
@param username          The name of the user for authentication.
@param password          The user's password.
@param workingDirectory  The base directory.
Creates a new connection to the server . @param hostname The host name of the server . @param port The port to connect to . @param username The name of the user for authentication . @param password The user ' s password . @param workingDirectory The base directory .
public static FTPClient createConnection(String hostname, int port, char[] username, char[] password,
                                             String workingDirectory, FileSystemOptions fileSystemOptions,
                                             String proxyServer, String proxyPort, String proxyUser,
                                             String proxyPassword, String timeout, String retryCount,
                                             Integer defaultTimeout) throws FileSystemException {
        if (username == null)
        {
            username = "anonymous".toCharArray();
        }
        if (password == null)
        {
            password = "anonymous".toCharArray();
        }
        Integer lTimeout = null;
        try{
        	lTimeout = Integer.parseInt(timeout);
        }catch(NumberFormatException nfe){
        	lTimeout = 5000;
        }
        Integer iRetryCount = null;
        try{
        	iRetryCount = Integer.parseInt(retryCount);
        }catch(NumberFormatException nfe){
        	iRetryCount = 5;
        }           
        boolean proxyMode = false;
        try
        {
			final FTPClient client;			
			if (proxyServer != null && proxyPort != null) {
				int iProxyPort;
				try {
					iProxyPort = Integer.parseInt(proxyPort);
				} catch (NumberFormatException nfe) {
					log.warn("Invalid proxy port " + proxyPort + ". Set the port as 8080. (default)");
					iProxyPort = 8080;
				}
				if (proxyUser != null && proxyPassword != null) {
					client = new FTPHTTPClient(proxyServer, iProxyPort, proxyUser, proxyPassword);
					proxyMode = true;
				} else {
					client = new FTPHTTPClient(proxyServer, iProxyPort);
					proxyMode = true;
				}
			} else {
				client = new FTPClient();
			}
            configureClient(fileSystemOptions, client);
            FTPFileEntryParserFactory myFactory =
                FtpFileSystemConfigBuilder.getInstance().getEntryParserFactory(fileSystemOptions);
            if (myFactory != null)
            {
                client.setParserFactory(myFactory);
            }
            try
            {
                if (defaultTimeout != null && (defaultTimeout > 0)) {
                    client.setDefaultTimeout(defaultTimeout);
                }
            	if(proxyMode){
                	client.setConnectTimeout(lTimeout);
                	boolean connect = false;
                	while(!connect && (iRetryCount > 0)){
                		try{
                			client.connect(hostname, port);
                			connect = true;
                		}catch(SocketTimeoutException e){
                			iRetryCount--;
                  		}catch(IOException e){
                			iRetryCount--;                			
                		}
                	}  
            	}else{
            		client.connect(hostname, port);
            	}
                if (defaultTimeout != null && (defaultTimeout > 0)) {
                    client.setSoTimeout(defaultTimeout);
                }
                int reply = client.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply))
                {
                    throw new FileSystemException("vfs.provider.ftp/connect-rejected.error", hostname);
                }
                if (!client.login(
                    UserAuthenticatorUtils.toString(username),
                    UserAuthenticatorUtils.toString(password)))
                {
                    throw new FileSystemException("vfs.provider.ftp/login.error",
                        new Object[]{hostname, UserAuthenticatorUtils.toString(username)}, null);
                }
                Integer fileType = FtpFileSystemConfigBuilder.getInstance().getFileType(fileSystemOptions);
                if (fileType == null) {
                    fileType = FTP.BINARY_FILE_TYPE;
                }
                if (!client.setFileType(fileType)) {
                    throw new FileSystemException("vfs.provider.ftp/set-fileType.error", hostname);
                }
                Integer dataTimeout = FtpFileSystemConfigBuilder.getInstance().getDataTimeout(fileSystemOptions);
                if (dataTimeout != null)
                {
                    client.setDataTimeout(dataTimeout.intValue());
                }
                Integer socketTimeout = FtpFileSystemConfigBuilder.getInstance().getSoTimeout(fileSystemOptions);
                if (socketTimeout != null)
                {
                    client.setSoTimeout(socketTimeout.intValue());
                }
                Boolean userDirIsRoot = FtpFileSystemConfigBuilder.getInstance().getUserDirIsRoot(fileSystemOptions);
                if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue()))
                {
                    if (!client.changeWorkingDirectory(workingDirectory))
                    {
                        throw new FileSystemException("vfs.provider.ftp/change-work-directory.error", workingDirectory);
                    }
                }
                Boolean passiveMode = FtpFileSystemConfigBuilder.getInstance().getPassiveMode(fileSystemOptions);
                if (proxyMode || (passiveMode != null && passiveMode.booleanValue()))
                {
                    client.enterLocalPassiveMode();
                }
                String controlEncoding = FtpFileSystemConfigBuilder.getInstance().getControlEncoding(fileSystemOptions);
                if (controlEncoding != null)
                {
                    client.setControlEncoding(controlEncoding);
                }
            }
            catch (final IOException e)
            {
                if (client.isConnected())
                {
                    client.disconnect();
                }
                throw e;
            }
            return client;
        }
        catch (final Exception exc)
        {
            throw new FileSystemException("vfs.provider.ftp/connect.error", new Object[]{hostname}, exc);
        }
    }
public static FTPClient createConnection ( String hostname , int port , char [ ] username , char [ ] password , String workingDirectory , FileSystemOptions fileSystemOptions , String proxyServer , String proxyPort , String proxyUser , String proxyPassword , String timeout , String retryCount , Integer defaultTimeout ) throws FileSystemException { if ( username == null ) { username = "anonymous" . toCharArray ( ) ; } if ( password == null ) { password = "anonymous" . toCharArray ( ) ; } Integer lTimeout = null ; try { lTimeout = Integer . parseInt ( timeout ) ; } catch ( NumberFormatException nfe ) { lTimeout = 5000 ; } Integer iRetryCount = null ; try { iRetryCount = Integer . parseInt ( retryCount ) ; } catch ( NumberFormatException nfe ) { iRetryCount = 5 ; } boolean proxyMode = false ; try { final FTPClient client ; if ( proxyServer != null && proxyPort != null ) { int iProxyPort ; try { iProxyPort = Integer . parseInt ( proxyPort ) ; } catch ( NumberFormatException nfe ) { log . warn ( "Invalid proxy port " + proxyPort + ". Set the port as 8080. (default)" ) ; iProxyPort = 8080 ; } if ( proxyUser != null && proxyPassword != null ) { client = new FTPHTTPClient ( proxyServer , iProxyPort , proxyUser , proxyPassword ) ; proxyMode = true ; } else { client = new FTPHTTPClient ( proxyServer , iProxyPort ) ; proxyMode = true ; } } else { client = new FTPClient ( ) ; } configureClient ( fileSystemOptions , client ) ; FTPFileEntryParserFactory myFactory = FtpFileSystemConfigBuilder . getInstance ( ) . getEntryParserFactory ( fileSystemOptions ) ; if ( myFactory != null ) { client . setParserFactory ( myFactory ) ; } try { if ( defaultTimeout != null && ( defaultTimeout > 0 ) ) { client . setDefaultTimeout ( defaultTimeout ) ; } if ( proxyMode ) { client . setConnectTimeout ( lTimeout ) ; boolean connect = false ; while ( ! connect && ( iRetryCount > 0 ) ) { try { client . connect ( hostname , port ) ; connect = true ; } catch ( SocketTimeoutException e ) { iRetryCount -- ; } catch ( IOException e ) { iRetryCount -- ; } } } else { client . connect ( hostname , port ) ; } if ( defaultTimeout != null && ( defaultTimeout > 0 ) ) { client . setSoTimeout ( defaultTimeout ) ; } int reply = client . getReplyCode ( ) ; if ( ! FTPReply . isPositiveCompletion ( reply ) ) { throw new FileSystemException ( "vfs.provider.ftp/connect-rejected.error" , hostname ) ; } if ( ! client . login ( UserAuthenticatorUtils . toString ( username ) , UserAuthenticatorUtils . toString ( password ) ) ) { throw new FileSystemException ( "vfs.provider.ftp/login.error" , new Object [ ] { hostname , UserAuthenticatorUtils . toString ( username ) } , null ) ; } Integer fileType = FtpFileSystemConfigBuilder . getInstance ( ) . getFileType ( fileSystemOptions ) ; if ( fileType == null ) { fileType = FTP . BINARY_FILE_TYPE ; } if ( ! client . setFileType ( fileType ) ) { throw new FileSystemException ( "vfs.provider.ftp/set-fileType.error" , hostname ) ; } Integer dataTimeout = FtpFileSystemConfigBuilder . getInstance ( ) . getDataTimeout ( fileSystemOptions ) ; if ( dataTimeout != null ) { client . setDataTimeout ( dataTimeout . intValue ( ) ) ; } Integer socketTimeout = FtpFileSystemConfigBuilder . getInstance ( ) . getSoTimeout ( fileSystemOptions ) ; if ( socketTimeout != null ) { client . setSoTimeout ( socketTimeout . intValue ( ) ) ; } Boolean userDirIsRoot = FtpFileSystemConfigBuilder . getInstance ( ) . getUserDirIsRoot ( fileSystemOptions ) ; if ( workingDirectory != null && ( userDirIsRoot == null || ! userDirIsRoot . booleanValue ( ) ) ) { if ( ! client . changeWorkingDirectory ( workingDirectory ) ) { throw new FileSystemException ( "vfs.provider.ftp/change-work-directory.error" , workingDirectory ) ; } } Boolean passiveMode = FtpFileSystemConfigBuilder . getInstance ( ) . getPassiveMode ( fileSystemOptions ) ; if ( proxyMode || ( passiveMode != null && passiveMode . booleanValue ( ) ) ) { client . enterLocalPassiveMode ( ) ; } String controlEncoding = FtpFileSystemConfigBuilder . getInstance ( ) . getControlEncoding ( fileSystemOptions ) ; if ( controlEncoding != null ) { client . setControlEncoding ( controlEncoding ) ; } } catch ( final IOException e ) { if ( client . isConnected ( ) ) { client . disconnect ( ) ; } throw e ; } return client ; } catch ( final Exception exc ) { throw new FileSystemException ( "vfs.provider.ftp/connect.error" , new Object [ ] { hostname } , exc ) ; } }
Creates a new connection to the server.
Creates a new connection to the server .
// Determine the username and password to use //Implement the logic to support FTP over HTTP Proxy //Need to enforce this since simethimes if we don't do this thread will hang forever // Login // Set binary mode // Set dataTimeout value // Change to root by default // All file operations a relative to the filesystem-root // String root = getRoot().getName().getPath(); //proxy mode only supported with passiveMode