client.connect(hostname, port);
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",
new Object[]{hostname, UserAuthenticatorUtils.toString(username)}, null);
}
// Set binary mode
if (!client.setFileType(FTP.BINARY_FILE_TYPE))
{
throw new FileSystemException("vfs.provider.ftp/set-binary.error", hostname);
}
// Set dataTimeout value
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());
}
// Change to root by default
// All file operations a relative to the filesystem-root
// String root = getRoot().getName().getPath();
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 (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);
}
}