1 /* 2 * Diese Datei ist Teil des GDialog Projektes: 3 * "Gigaset-Erweiterung f�r ProjectX" 4 * 5 * Das GDialog Projekt ist freigegeben unter 6 * der GNU Public Licence (GPL), deren Text sich in 7 * dem Quellen-Verzeichnis befindet. Ist er dort nicht 8 * mehr vorhanden, so kann er unter http://www.gnu.org/licenses/gpl.html 9 * eingesehen werden. 10 */ 11 package de.m740.projectx.gigaset; 12 13 import java.io.IOException; 14 import java.util.ArrayList; 15 import java.util.List; 16 import java.util.StringTokenizer; 17 18 import net.sourceforge.dvb.projectx.common.Common; 19 import net.sourceforge.dvb.projectx.common.Keys; 20 import net.sourceforge.dvb.projectx.xinput.XInputDirectory; 21 import net.sourceforge.dvb.projectx.xinput.XInputFile; 22 import net.sourceforge.dvb.projectx.xinput.ftp.FtpVO; 23 24 import org.apache.commons.net.ftp.FTP; 25 import org.apache.commons.net.ftp.FTPClient; 26 import org.apache.commons.net.ftp.FTPFile; 27 28 /** 29 * GFTPFactory 30 * 31 * @author Arno 32 * @version $Id: GFTPHandler.java,v 1.2 2006/12/18 20:48:04 java Exp $ 33 */ 34 public class GFTPHandler { 35 36 protected boolean debug = false; 37 GFTPHandler()38 public GFTPHandler() { 39 super(); 40 } 41 42 /** 43 * Copied from XInputFileImpl.getUserFTPCommand() 44 * 45 * @throws java.io.IOException 46 */ getUserFTPCommand()47 private String[] getUserFTPCommand() { 48 49 StringTokenizer st = new StringTokenizer(Common.getSettings() 50 .getProperty(Keys.KEY_FtpServer_Commands), "|"); 51 String[] tokens = new String[st.countTokens()]; 52 53 for (int i = 0; st.hasMoreTokens(); i++) 54 tokens[i] = st.nextElement().toString().trim(); 55 56 return tokens; 57 } 58 59 /** 60 * Copied from XInputFileImpl.randomAccessOpen() 61 * 62 * @param inpFtpVO 63 * @return FTPClient 64 */ openClient(FtpVO inpFtpVO)65 public FTPClient openClient(FtpVO inpFtpVO) { 66 FTPClient tmpClient = null; 67 try { 68 boolean ret = false; 69 70 tmpClient = new FTPClient(); 71 tmpClient 72 .connect(inpFtpVO.getServer(), inpFtpVO.getPortasInteger()); // void 73 if (debug) 74 System.out.println("rAO connect " + tmpClient.getReplyString()); 75 76 ret = tmpClient.login(inpFtpVO.getUser(), inpFtpVO.getPassword()); // bool 77 if (debug) 78 System.out.println("rAO login " + ret + " / " 79 + tmpClient.getReplyString()); 80 81 ret = tmpClient.changeWorkingDirectory(inpFtpVO.getDirectory()); // bool 82 if (debug) 83 System.out.println("rAO cwd " + ret + " / " 84 + tmpClient.getReplyString()); 85 86 ret = tmpClient.setFileType(FTP.BINARY_FILE_TYPE); // bool 87 if (debug) 88 System.out.println("rAO binary " + ret + " / " 89 + tmpClient.getReplyString()); 90 91 tmpClient.enterLocalPassiveMode(); // void 92 if (debug) 93 System.out.println("rAO PASV " + tmpClient.getReplyString()); 94 95 String[] commands = getUserFTPCommand(); 96 97 for (int i = 0; i < commands.length; i++) { 98 if (commands[i] != null && commands[i].length() > 0) { 99 tmpClient.sendCommand(commands[i]); // bool 100 if (debug) 101 System.out.println("rAO cmd " 102 + tmpClient.getReplyString()); 103 } 104 } 105 106 } catch (Exception e) { 107 GConsole.setErrorMessage(e); 108 } 109 return tmpClient; 110 } 111 112 /** 113 * Copied from XInputFileImpl.randomAccessClose() 114 * 115 * @param tmpClient 116 */ closeClient(FTPClient tmpClient)117 public void closeClient(FTPClient tmpClient) { 118 try { 119 // no need to abort a transfer explicitly, because we logout here 120 121 boolean ret = false; 122 if (debug) 123 System.out.println("rAC last " + tmpClient.getReplyCode() 124 + " / " + tmpClient.getReplyString()); 125 126 ret = tmpClient.isConnected(); 127 if (debug) 128 System.out.println("rAC isCon " + ret + " / " 129 + tmpClient.getReplyCode() + " / " 130 + tmpClient.getReplyString()); 131 132 // ret = tmpClient.completePendingCommand(); 133 // if (debug) 134 // System.out.println("rAC complete " + ret + " / " 135 // + tmpClient.getReplyCode() + " / " 136 // + tmpClient.getReplyString()); 137 138 ret = tmpClient.logout(); 139 if (debug) 140 System.out.println("rAC logout " + ret + " / " 141 + tmpClient.getReplyCode() + " / " 142 + tmpClient.getReplyString()); 143 144 ret = tmpClient.isConnected(); 145 if (debug) 146 System.out.println("rAC isCon " + ret + " / " 147 + tmpClient.getReplyCode() + " / " 148 + tmpClient.getReplyString()); 149 150 if (ret) { 151 try { 152 tmpClient.disconnect(); 153 if (debug) 154 System.out.println("rAC disc " 155 + tmpClient.getReplyCode() + " / " 156 + tmpClient.getReplyString()); 157 } catch (IOException e) { 158 if (debug) 159 System.out.println("rAC disc-er " + e); 160 } 161 } 162 if (debug) 163 System.out.println("rAC out "); 164 } catch (IOException exc) { 165 GConsole.setErrorMessage(exc); 166 } 167 } 168 169 /** 170 * 171 * @param inpDir 172 * @return FtpVO 173 */ makeVOFrom(XInputDirectory inpDir)174 public FtpVO makeVOFrom(XInputDirectory inpDir) { 175 try { 176 XInputDirectory tmpXDirectory = inpDir; 177 String tmpDirectory = tmpXDirectory.getDirectory(); 178 String tmpServer = tmpXDirectory.getServer(); 179 String tmpUser = tmpXDirectory.getUser(); 180 String tmpPassword = tmpXDirectory.getPassword(); 181 String tmpPort = tmpXDirectory.getPort(); 182 FtpVO tmpVO = new FtpVO(tmpServer, tmpUser, tmpPassword, 183 tmpDirectory, tmpPort, null); 184 return tmpVO; 185 } catch (Exception e) { 186 GConsole.setErrorMessage(e); 187 } 188 return null; 189 } 190 191 /** 192 * 193 * @param inpFile 194 * @return XInputDirectory 195 */ makeDirectoryFrom(XInputFile inpFile, FtpVO inpVO)196 public XInputDirectory makeDirectoryFrom(XInputFile inpFile, FtpVO inpVO) { 197 try { 198 String tmpDirectory = inpFile.getParent() + "/" + inpFile.getName(); 199 String tmpServer = inpVO.getServer(); 200 String tmpUser = inpVO.getUser(); 201 String tmpPassword = inpVO.getPassword(); 202 String tmpPort = inpVO.getPort(); 203 FtpVO tmpVO = new FtpVO(tmpServer, tmpUser, tmpPassword, 204 tmpDirectory, tmpPort, null); 205 XInputDirectory tmpIDirectory = new XInputDirectory(tmpVO); 206 if (tmpIDirectory.test()) { 207 return tmpIDirectory; 208 } 209 } catch (Exception e) { 210 GConsole.setErrorMessage(e); 211 } 212 return null; 213 } 214 215 /** 216 * 217 */ listFiles(FTPClient inpClient, FtpVO inpVO)218 private XInputFile[] listFiles(FTPClient inpClient, FtpVO inpVO) 219 throws IOException { 220 FTPFile[] tmpFTPFiles = null; 221 tmpFTPFiles = inpClient.listFiles(); 222 int anz = (tmpFTPFiles == null) ? 0 : tmpFTPFiles.length; 223 List tmpList = new ArrayList(anz); 224 for (int i = 0; i < anz; i++) { 225 FTPFile tmpFTPFile = tmpFTPFiles[i]; 226 String tmpFN = tmpFTPFile.getName(); 227 if (".".equals(tmpFN) || "..".equals(tmpFN)) { 228 continue; 229 } 230 FtpVO tempFtpVO = (FtpVO) inpVO.clone(); 231 tempFtpVO.setFtpFile(tmpFTPFiles[i]); 232 tmpList.add(new XInputFile(tempFtpVO)); 233 } // for i 234 anz = (tmpList == null) ? 0 : tmpList.size(); 235 XInputFile[] ftpInputFiles = new XInputFile[anz]; 236 for (int i = 0; i < anz; i++) { 237 ftpInputFiles[i] = (XInputFile) tmpList.get(i); 238 } // for i 239 return ftpInputFiles; 240 } 241 242 /** 243 * 244 * @param inpDir 245 * @return 246 */ getFilesAndDirectories(XInputDirectory inpDir)247 public XInputFile[] getFilesAndDirectories(XInputDirectory inpDir) { 248 XInputFile[] tmpXFiles = null; 249 try { 250 FtpVO tmpFtpVO = makeVOFrom(inpDir); 251 FTPClient tmpClient = openClient(tmpFtpVO); 252 tmpXFiles = listFiles(tmpClient, tmpFtpVO); 253 closeClient(tmpClient); 254 } catch (Exception exc) { 255 GConsole.setErrorMessage(exc); 256 } 257 return tmpXFiles; 258 } 259 260 } 261