001 package railo.runtime.net.ftp; 002 003 import java.io.IOException; 004 import java.net.InetAddress; 005 006 import org.apache.commons.net.ftp.FTP; 007 import org.apache.commons.net.ftp.FTPClient; 008 009 import railo.runtime.net.proxy.Proxy; 010 011 012 /** 013 * Wrap a Client and a Connection 014 */ 015 public final class FTPWrap { 016 017 private FTPConnection conn; 018 private FTPClient client; 019 private InetAddress address; 020 private long lastAccess=0; 021 022 /** 023 * @return the lastAccess 024 */ 025 public long getLastAccess() { 026 return lastAccess; 027 } 028 029 030 031 /** 032 * @param lastAccess the lastAccess to set 033 */ 034 public void setLastAccess(long lastAccess) { 035 this.lastAccess = lastAccess; 036 } 037 038 039 040 /** 041 * 042 * @param connection 043 * @throws IOException 044 */ 045 public FTPWrap(FTPConnection connection) throws IOException { 046 this.conn=connection; 047 this.address = InetAddress.getByName(connection.getServer()); 048 connect(); 049 } 050 051 052 053 /** 054 * @return Returns the connection. 055 */ 056 public FTPConnection getConnection() { 057 return conn; 058 } 059 060 /** 061 * @return Returns the client. 062 */ 063 public FTPClient getClient() { 064 return client; 065 } 066 067 /** 068 * @throws IOException 069 * 070 */ 071 public void reConnect() throws IOException { 072 try { 073 if(client!=null && client.isConnected())client.disconnect(); 074 } 075 catch(IOException ioe) {} 076 connect(); 077 } 078 079 public void reConnect(short transferMode) throws IOException { 080 if(transferMode!=conn.getTransferMode()) 081 ((FTPConnectionImpl)conn).setTransferMode(transferMode); 082 reConnect(); 083 } 084 085 /** 086 * connects the client 087 * @throws IOException 088 */ 089 private void connect() throws IOException { 090 091 client=new FTPClient(); 092 093 setConnectionSettings(client,conn); 094 095 // transfer mode 096 if(conn.getTransferMode()==FTPConstant.TRANSFER_MODE_ASCCI) getClient().setFileType(FTP.ASCII_FILE_TYPE); 097 else if(conn.getTransferMode()==FTPConstant.TRANSFER_MODE_BINARY) getClient().setFileType(FTP.BINARY_FILE_TYPE); 098 099 100 101 // Connect 102 try { 103 Proxy.start( 104 conn.getProxyServer(), 105 conn.getProxyPort(), 106 conn.getProxyUser(), 107 conn.getProxyPassword() 108 ); 109 client.connect(address,conn.getPort()); 110 client.login(conn.getUsername(),conn.getPassword()); 111 } 112 finally { 113 Proxy.end(); 114 } 115 } 116 117 118 119 static void setConnectionSettings(FTPClient client, FTPConnection conn) { 120 if(client==null) return; 121 122 // timeout 123 client.setDataTimeout(conn.getTimeout()*1000); 124 try { 125 client.setSoTimeout(conn.getTimeout()*1000); 126 } catch (Throwable t) {} 127 128 // passive/active Mode 129 int mode = client.getDataConnectionMode(); 130 if(conn.isPassive()) { 131 if(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE!=mode) 132 client.enterLocalPassiveMode(); 133 } 134 else { 135 if(FTPClient.ACTIVE_LOCAL_DATA_CONNECTION_MODE!=mode) 136 client.enterLocalActiveMode(); 137 } 138 } 139 }