1 16 package org.apache.commons.vfs.provider.ftp; 17 18 import java.io.IOException ; 19 20 import org.apache.commons.net.SocketFactory; 21 22 import org.apache.commons.net.ftp.FTP; 23 import org.apache.commons.net.ftp.FTPClient; 24 import org.apache.commons.net.ftp.FTPReply; 25 import org.apache.commons.net.ftp.parser.FTPFileEntryParserFactory; 26 import org.apache.commons.vfs.FileSystemException; 27 import org.apache.commons.vfs.FileSystemOptions; 28 29 34 public class FtpClientFactory 35 { 36 private FtpClientFactory() 37 { 38 } 39 40 43 public static FTPClient createConnection(String hostname, int port, String username, String password, String workingDirectory, FileSystemOptions fileSystemOptions) throws FileSystemException 44 { 45 if (username == null) 47 { 48 username = "anonymous"; 49 } 50 51 if (password == null) 52 { 53 password = "anonymous"; 54 } 55 56 try 57 { 58 final FTPClient client = new FTPClient(); 59 60 FTPFileEntryParserFactory myFactory = FtpFileSystemConfigBuilder.getInstance().getEntryParserFactory(fileSystemOptions); 61 if (myFactory != null) 62 { 63 client.setParserFactory(myFactory); 64 } 65 66 try 67 { 68 if(System.getProperty("org.apache.commons.net.socketFactory", null)!=null) { 69 70 try { 71 SocketFactory socketFactory = (SocketFactory) Class.forName(System.getProperty("org.apache.commons.net.socketFactory")).newInstance(); 72 client.setSocketFactory(socketFactory); 73 } catch(Throwable t) { 74 throw new FileSystemException(t); 75 } 76 } 77 client.connect(hostname, port); 78 79 int reply = client.getReplyCode(); 80 if (!FTPReply.isPositiveCompletion(reply)) 81 { 82 throw new FileSystemException("vfs.provider.ftp/connect-rejected.error", hostname); 83 } 84 85 if (!client.login(username, password)) 87 { 88 throw new FileSystemException("vfs.provider.ftp/login.error", new Object []{hostname, username}, null); 89 } 90 91 if (!client.setFileType(FTP.BINARY_FILE_TYPE)) 93 { 94 throw new FileSystemException("vfs.provider.ftp/set-binary.error", hostname); 95 } 96 97 Integer dataTimeout = FtpFileSystemConfigBuilder.getInstance().getDataTimeout(fileSystemOptions); 99 if (dataTimeout != null) 100 { 101 client.setDataTimeout(dataTimeout.intValue()); 102 } 103 104 108 Boolean userDirIsRoot = FtpFileSystemConfigBuilder.getInstance().getUserDirIsRoot(fileSystemOptions); 109 if (workingDirectory != null && (userDirIsRoot == null || !userDirIsRoot.booleanValue())) 110 { 111 if (!client.changeWorkingDirectory(workingDirectory)) 112 { 113 throw new FileSystemException("vfs.provider.ftp/change-work-directory.error", workingDirectory); 114 } 115 } 116 117 Boolean passiveMode = FtpFileSystemConfigBuilder.getInstance().getPassiveMode(fileSystemOptions); 118 if (passiveMode != null && passiveMode.booleanValue()) 119 { 120 client.enterLocalPassiveMode(); 121 } 122 } 123 catch (final IOException e) 124 { 125 if (client.isConnected()) 126 { 127 client.disconnect(); 128 } 129 throw e; 130 } 131 132 return client; 133 } 134 catch (final Exception exc) 135 { 136 throw new FileSystemException("vfs.provider.ftp/connect.error", new Object []{hostname}, exc); 137 } 138 } 139 } | Popular Tags |