public C createConnection()

in commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpClientFactory.java [123:261]


        public C createConnection(final String hostname, final int port, char[] username, char[] password,
                final String workingDirectory, final FileSystemOptions fileSystemOptions) throws FileSystemException {
            // Determine the username and password to use
            if (username == null) {
                username = ANON_CHAR_ARRAY;
            }

            if (password == null) {
                password = ANON_CHAR_ARRAY;
            }

            try {
                final C client = createClient(fileSystemOptions);

                if (log.isDebugEnabled()) {
                    final Writer writer = new StringWriter(1024) {
                        @Override
                        public void flush() {
                            final StringBuffer buffer = getBuffer();
                            String message = buffer.toString();
                            final String prefix = "PASS ";
                            if (message.toUpperCase().startsWith(prefix) && message.length() > prefix.length()) {
                                message = prefix + "***";
                            }
                            log.debug(message);
                            buffer.setLength(0);
                        }
                    };
                    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(writer)));
                }

                configureClient(fileSystemOptions, client);

                final FTPFileEntryParserFactory myFactory = builder.getEntryParserFactory(fileSystemOptions);
                if (myFactory != null) {
                    client.setParserFactory(myFactory);
                }

                final Boolean remoteVerification = builder.getRemoteVerification(fileSystemOptions);
                if (remoteVerification != null) {
                    client.setRemoteVerificationEnabled(remoteVerification.booleanValue());
                }

                try {
                    final Duration connectTimeout = builder.getConnectTimeoutDuration(fileSystemOptions);
                    if (connectTimeout != null) {
                        client.setDefaultTimeout(DurationUtils.toMillisInt(connectTimeout));
                    }

                    final String controlEncoding = builder.getControlEncoding(fileSystemOptions);
                    if (controlEncoding != null) {
                        client.setControlEncoding(controlEncoding);
                    }

                    final Boolean autodetectUTF8 = builder.getAutodetectUtf8(fileSystemOptions);
                    if (autodetectUTF8 != null) {
                        client.setAutodetectUTF8(autodetectUTF8);
                    }

                    final Proxy proxy = builder.getProxy(fileSystemOptions);
                    if (proxy != null) {
                        client.setProxy(proxy);
                    }

                    client.connect(hostname, port);

                    final int reply = client.getReplyCode();
                    if (!FTPReply.isPositiveCompletion(reply)) {
                        throw new FileSystemException("vfs.provider.ftp/connect-rejected.error", hostname);
                    }

                    // Login
                    if (!client.login(UserAuthenticatorUtils.toString(username),
                        UserAuthenticatorUtils.toString(password))) {
                        throw new FileSystemException("vfs.provider.ftp/login.error", hostname,
                            UserAuthenticatorUtils.toString(username));
                    }

                    FtpFileType fileType = builder.getFileType(fileSystemOptions);
                    if (fileType == null) {
                        fileType = FtpFileType.BINARY;
                    }
                    // Set binary mode
                    if (!client.setFileType(fileType.getValue())) {
                        throw new FileSystemException("vfs.provider.ftp/set-file-type.error", fileType);
                    }

                    // Set dataTimeout value
                    final Duration dataTimeout = builder.getDataTimeoutDuration(fileSystemOptions);
                    if (dataTimeout != null) {
                        client.setDataTimeout(DurationUtils.toMillisInt(dataTimeout));
                    }

                    final Duration socketTimeout = builder.getSoTimeoutDuration(fileSystemOptions);
                    if (socketTimeout != null) {
                        client.setSoTimeout(DurationUtils.toMillisInt(socketTimeout));
                    }

                    final Duration controlKeepAliveTimeout = builder.getControlKeepAliveTimeout(fileSystemOptions);
                    if (controlKeepAliveTimeout != null) {
                        // yes, in seconds.
                        client.setControlKeepAliveTimeout(controlKeepAliveTimeout.getSeconds());
                    }

                    final Duration controlKeepAliveReplyTimeout = builder
                        .getControlKeepAliveReplyTimeout(fileSystemOptions);
                    if (controlKeepAliveReplyTimeout != null) {
                        client.setControlKeepAliveReplyTimeout((int) controlKeepAliveReplyTimeout.toMillis());
                    }

                    final Boolean userDirIsRoot = builder.getUserDirIsRoot(fileSystemOptions);
                    if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue())
                        && !client.changeWorkingDirectory(workingDirectory)) {
                        throw new FileSystemException("vfs.provider.ftp/change-work-directory.error", workingDirectory);
                    }

                    final Boolean passiveMode = builder.getPassiveMode(fileSystemOptions);
                    if (passiveMode != null && passiveMode.booleanValue()) {
                        client.enterLocalPassiveMode();
                    }

                    final Range<Integer> activePortRange = builder.getActivePortRange(fileSystemOptions);
                    if (activePortRange != null) {
                        client.setActivePortRange(activePortRange.getMinimum(), activePortRange.getMaximum());
                    }

                    setupOpenConnection(client, fileSystemOptions);
                } 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", exc, hostname);
            }
        }