001package lucee.runtime.net.ftp; 002 003import java.io.IOException; 004import java.io.InputStream; 005import java.io.OutputStream; 006import java.net.InetAddress; 007import java.net.SocketException; 008 009import lucee.commons.lang.ExceptionUtil; 010import lucee.commons.lang.StringUtil; 011 012import org.apache.commons.net.ftp.FTP; 013import org.apache.commons.net.ftp.FTPClient; 014import org.apache.commons.net.ftp.FTPFile; 015import org.apache.commons.net.ftp.FTPReply; 016 017public class FTPClientImpl extends AFTPClient { 018 019 private FTPClient client; 020 private InetAddress host; 021 private int port; 022 private String username; 023 private String password; 024 private boolean stopOnError; 025 026 public FTPClientImpl(FTPClient client) { 027 this.client=client; 028 } 029 030 FTPClientImpl() { 031 this.client=new FTPClient(); 032 } 033 034 035 036 @Override 037 public void init(InetAddress host, int port, String username, String password, String fingerprint, boolean stopOnError) throws SocketException, IOException { 038 this.host=host; 039 this.port=port; 040 this.username=username; 041 this.password=password; 042 this.stopOnError=stopOnError; 043 } 044 045 046 @Override 047 public void connect() throws SocketException, IOException { 048 client.connect(host, port); 049 if(!StringUtil.isEmpty(username))client.login(username,password); 050 } 051 052 @Override 053 public boolean rename(String from, String to) throws IOException { 054 return client.rename(from, to); 055 } 056 057 @Override 058 public int getReplyCode() { 059 return client.getReplyCode(); 060 } 061 062 @Override 063 public String getReplyString() { 064 return client.getReplyString(); 065 } 066 067 @Override 068 public boolean changeWorkingDirectory(String pathname) throws IOException { 069 return client.changeWorkingDirectory(pathname); 070 } 071 072 @Override 073 public boolean makeDirectory(String pathname) throws IOException { 074 return client.makeDirectory(pathname); 075 } 076 077 @Override 078 public FTPFile[] listFiles(String pathname) throws IOException { 079 return client.listFiles(pathname); 080 } 081 082 @Override 083 public boolean removeDirectory(String pathname) throws IOException { 084 return client.removeDirectory(pathname); 085 } 086 087 @Override 088 public boolean setFileType(int fileType) throws IOException { 089 return client.setFileType(toFTPClientFileType(fileType)); 090 } 091 092 private int toFTPClientFileType(int fileType) { 093 if(fileType==FILE_TYPE_BINARY)return FTP.BINARY_FILE_TYPE; 094 return FTP.ASCII_FILE_TYPE; 095 } 096 097 @Override 098 public boolean retrieveFile(String remote, OutputStream local) throws IOException { 099 return client.retrieveFile(remote, local); 100 } 101 102 @Override 103 public boolean storeFile(String remote, InputStream local) throws IOException { 104 return client.storeFile(remote, local); 105 } 106 107 @Override 108 public boolean deleteFile(String pathname) throws IOException { 109 return client.deleteFile(pathname); 110 } 111 112 @Override 113 public String printWorkingDirectory() throws IOException { 114 return client.printWorkingDirectory(); 115 } 116 117 @Override 118 public String getPrefix() { 119 return "ftp"; 120 } 121 122 @Override 123 public InetAddress getRemoteAddress() { 124 return client.getRemoteAddress(); 125 } 126 127 @Override 128 public boolean isConnected() { 129 return client.isConnected(); 130 } 131 132 @Override 133 public int quit() throws IOException { 134 return client.quit(); 135 } 136 137 @Override 138 public void disconnect() throws IOException { 139 client.disconnect(); 140 } 141 142 @Override 143 public int getDataConnectionMode() { 144 return client.getDataConnectionMode(); 145 } 146 147 @Override 148 public void enterLocalPassiveMode() { 149 client.enterLocalPassiveMode(); 150 } 151 152 @Override 153 public void enterLocalActiveMode() { 154 client.enterLocalActiveMode(); 155 } 156 157 @Override 158 public boolean isPositiveCompletion() { 159 return FTPReply.isPositiveCompletion(client.getReplyCode()); 160 } 161 162 @Override 163 public boolean directoryExists(String pathname) throws IOException { 164 String pwd=null; 165 try { 166 pwd = client.printWorkingDirectory(); 167 return client.changeWorkingDirectory(pathname); 168 } 169 finally { 170 if(pwd!=null)client.changeWorkingDirectory(pwd); 171 } 172 } 173 174 @Override 175 public void setTimeout(int timeout) { 176 client.setDataTimeout(timeout); 177 try { 178 client.setSoTimeout(timeout); 179 } catch (Throwable t) { 180 ExceptionUtil.rethrowIfNecessary(t); 181 } 182 } 183 184}