}
try
{
final FTPSClient client;
if (FtpsFileSystemConfigBuilder.getInstance().getFtpsType(fileSystemOptions).equals("explicit"))
{
client = new FTPSClient();
}
else if (FtpsFileSystemConfigBuilder.getInstance().getFtpsType(fileSystemOptions).equals("implicit"))
{
client = new FTPSClient(true);
}
else
{
throw new FileSystemException(
"Invalid FTPS type of " + FtpsFileSystemConfigBuilder.getInstance().getFtpsType(
fileSystemOptions) + " specified. Must be 'implicit' or 'explicit'");
}
String key = FtpsFileSystemConfigBuilder.getInstance().getEntryParser(fileSystemOptions);
if (key != null)
{
FTPClientConfig config = new FTPClientConfig(key);
String serverLanguageCode = FtpsFileSystemConfigBuilder.getInstance().getServerLanguageCode(
fileSystemOptions);
if (serverLanguageCode != null)
{
config.setServerLanguageCode(serverLanguageCode);
}
String defaultDateFormat = FtpsFileSystemConfigBuilder.getInstance().getDefaultDateFormat(
fileSystemOptions);
if (defaultDateFormat != null)
{
config.setDefaultDateFormatStr(defaultDateFormat);
}
String recentDateFormat = FtpsFileSystemConfigBuilder.getInstance().getRecentDateFormat(
fileSystemOptions);
if (recentDateFormat != null)
{
config.setRecentDateFormatStr(recentDateFormat);
}
String serverTimeZoneId = FtpsFileSystemConfigBuilder.getInstance().getServerTimeZoneId(
fileSystemOptions);
if (serverTimeZoneId != null)
{
config.setServerTimeZoneId(serverTimeZoneId);
}
String[] shortMonthNames = FtpsFileSystemConfigBuilder.getInstance().getShortMonthNames(
fileSystemOptions);
if (shortMonthNames != null)
{
StringBuilder shortMonthNamesStr = new StringBuilder(40);
for (int i = 0; i < shortMonthNames.length; i++)
{
if (shortMonthNamesStr.length() > 0)
{
shortMonthNamesStr.append("|");
}
shortMonthNamesStr.append(shortMonthNames[i]);
}
config.setShortMonthNames(shortMonthNamesStr.toString());
}
client.configure(config);
}
FTPFileEntryParserFactory myFactory = FtpsFileSystemConfigBuilder.getInstance().getEntryParserFactory(
fileSystemOptions);
if (myFactory != null)
{
client.setParserFactory(myFactory);
}
try
{
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 = FtpsFileSystemConfigBuilder.getInstance().getDataTimeout(fileSystemOptions);
if (dataTimeout != null)
{
client.setDataTimeout(dataTimeout.intValue());
}
// Change to root by default
// All file operations a relative to the filesystem-root
// String root = getRoot().getName().getPath();
Boolean userDirIsRoot = FtpsFileSystemConfigBuilder.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 = FtpsFileSystemConfigBuilder.getInstance().getPassiveMode(fileSystemOptions);
if (passiveMode != null && passiveMode.booleanValue())
{
client.enterLocalPassiveMode();
}
}
catch (final IOException e)
{
if (client.isConnected())
{
client.disconnect();
}
throw e;
}
return client;