/plugins/FTP/tags/ftp_0_7_4/ftp/SFtpConnection.java

# · Java · 313 lines · 252 code · 33 blank · 28 comment · 18 complexity · e3ce328f5b05bf3c4dc3d2d5838b3515 MD5 · raw file

  1. /*
  2. * SFtpConnection.java - A connection to an SSH FTP server
  3. * Copyright (C) 2002, 2003 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package ftp;
  20. import com.sshtools.j2ssh.*;
  21. import com.sshtools.j2ssh.authentication.*;
  22. import com.sshtools.j2ssh.configuration.*;
  23. import com.sshtools.j2ssh.connection.*;
  24. import com.sshtools.j2ssh.io.UnsignedInteger32;
  25. import com.sshtools.j2ssh.session.*;
  26. import com.sshtools.j2ssh.sftp.*;
  27. import com.sshtools.j2ssh.transport.*;
  28. import com.sshtools.j2ssh.transport.publickey.*;
  29. import com.sshtools.common.hosts.*;
  30. import java.io.*;
  31. import java.util.*;
  32. import org.gjt.sp.jedit.GUIUtilities;
  33. import org.gjt.sp.jedit.JARClassLoader;
  34. import org.gjt.sp.jedit.jEdit;
  35. import org.gjt.sp.jedit.MiscUtilities;
  36. import org.gjt.sp.jedit.search.RESearchMatcher;
  37. import org.gjt.sp.util.Log;
  38. class SFtpConnection extends ConnectionManager.Connection
  39. {
  40. SFtpConnection(final ConnectionManager.ConnectionInfo info) throws IOException
  41. {
  42. super(info);
  43. client = new SshClient();
  44. client.connect(info.host,info.port,new DialogHostKeyVerification(null));
  45. SshAuthenticationClient auth;
  46. String authType = "";
  47. if (info.privateKey == null) {
  48. auth = new PasswordAuthenticationClient();
  49. authType = "Password";
  50. Log.log(Log.DEBUG,this,"Using password authentication");
  51. ((PasswordAuthenticationClient)auth).setPassword(info.password);
  52. } else {
  53. auth = new PublicKeyAuthenticationClient();
  54. authType = "Public Key";
  55. Log.log(Log.DEBUG,this,"Using public key authentication");
  56. ((PublicKeyAuthenticationClient)auth).setKey(info.privateKey);
  57. }
  58. auth.setUsername(info.user);
  59. int rc = client.authenticate(auth);
  60. String rc_string = "";
  61. switch(rc) {
  62. case AuthenticationProtocolState.CANCELLED:
  63. rc_string = "CANCELLED";
  64. break;
  65. case AuthenticationProtocolState.COMPLETE:
  66. rc_string = "COMPLETE";
  67. break;
  68. case AuthenticationProtocolState.FAILED:
  69. rc_string = "FAILED";
  70. break;
  71. case AuthenticationProtocolState.PARTIAL:
  72. rc_string = "PARTIAL";
  73. break;
  74. case AuthenticationProtocolState.READY:
  75. rc_string = "READY";
  76. break;
  77. default:
  78. rc_string = "UNKNOWN";
  79. }
  80. Log.log(Log.DEBUG,this,"Client.authenticate return code: "+rc_string+" ("+rc+")");
  81. sftp = client.openSftpChannel(null);
  82. home = sftp.getDefaultDirectory();
  83. }
  84. FtpVFS.FtpDirectoryEntry[] listDirectory(String path) throws IOException
  85. {
  86. FtpVFS.FtpDirectoryEntry[] returnValue = null;
  87. SftpFile dir = null;
  88. ArrayList listing = new ArrayList();
  89. try
  90. {
  91. dir = sftp.openDirectory(path);
  92. int count = 0;
  93. do
  94. {
  95. count = sftp.listChildren(dir,listing);
  96. }
  97. while(count != -1);
  98. for(int i = 0; i < listing.size(); i++)
  99. {
  100. SftpFile file = (SftpFile)listing.get(i);
  101. String name = file.getFilename();
  102. if(name.equals(".") || name.equals(".."))
  103. {
  104. listing.remove(i);
  105. i--;
  106. }
  107. else
  108. {
  109. listing.set(i,createDirectoryEntry(
  110. file));
  111. }
  112. }
  113. }
  114. finally
  115. {
  116. if(dir != null)
  117. sftp.closeFile(dir);
  118. }
  119. return (FtpVFS.FtpDirectoryEntry[])listing.toArray(
  120. new FtpVFS.FtpDirectoryEntry[listing.size()]);
  121. }
  122. FtpVFS.FtpDirectoryEntry getDirectoryEntry(String path) throws IOException
  123. {
  124. SftpFile file = null;
  125. FtpVFS.FtpDirectoryEntry returnValue = null;
  126. try
  127. {
  128. file = sftp.openFile(path,SftpSubsystemClient.OPEN_READ);
  129. returnValue = createDirectoryEntry(file);
  130. returnValue.setPath(path);
  131. returnValue.setDeletePath(path);
  132. }
  133. catch(IOException io)
  134. {
  135. }
  136. finally
  137. {
  138. if(file != null)
  139. sftp.closeFile(file);
  140. }
  141. return returnValue;
  142. }
  143. boolean removeFile(String path) throws IOException
  144. {
  145. try
  146. {
  147. sftp.removeFile(path);
  148. return true;
  149. }
  150. catch(SshException e)
  151. {
  152. return false;
  153. }
  154. }
  155. boolean removeDirectory(String path) throws IOException
  156. {
  157. try
  158. {
  159. sftp.removeDirectory(path);
  160. return true;
  161. }
  162. catch(SshException e)
  163. {
  164. return false;
  165. }
  166. }
  167. boolean rename(String from, String to) throws IOException
  168. {
  169. try
  170. {
  171. sftp.renameFile(from,to);
  172. return true;
  173. }
  174. catch(SshException e)
  175. {
  176. return false;
  177. }
  178. }
  179. boolean makeDirectory(String path) throws IOException
  180. {
  181. try
  182. {
  183. sftp.makeDirectory(path);
  184. return true;
  185. }
  186. catch(SshException e)
  187. {
  188. return false;
  189. }
  190. }
  191. InputStream retrieve(String path) throws IOException
  192. {
  193. return new SftpFileInputStream(sftp.openFile(path,
  194. SftpSubsystemClient.OPEN_READ));
  195. }
  196. OutputStream store(String path) throws IOException
  197. {
  198. // ugh...
  199. SftpFile file;
  200. // try
  201. // {
  202. // file = sftp.openFile(path,SftpSubsystemClient.OPEN_WRITE);
  203. // }
  204. // catch(Exception e)
  205. {
  206. file = sftp.openFile(path,SftpSubsystemClient.OPEN_WRITE
  207. | SftpSubsystemClient.OPEN_CREATE
  208. | SftpSubsystemClient.OPEN_TRUNCATE,
  209. DEFAULT_ATTRIBUTES);
  210. }
  211. return new SftpFileOutputStream(file);
  212. }
  213. void chmod(String path, int permissions) throws IOException
  214. {
  215. sftp.changePermissions(path,permissions);
  216. }
  217. boolean checkIfOpen() throws IOException
  218. {
  219. return client.isConnected();
  220. }
  221. public String resolveSymlink(String path, String[] name)
  222. throws IOException
  223. {
  224. SftpFile file = null;
  225. String returnValue;
  226. try
  227. {
  228. file = sftp.openFile(path,SftpSubsystemClient.OPEN_READ);
  229. returnValue = file.getAbsolutePath();
  230. }
  231. catch(IOException io)
  232. {
  233. returnValue = null;
  234. }
  235. finally
  236. {
  237. if(file != null)
  238. sftp.closeFile(file);
  239. }
  240. return returnValue;
  241. }
  242. void logout() throws IOException
  243. {
  244. sftp.close();
  245. client.disconnect();
  246. }
  247. private static FileAttributes DEFAULT_ATTRIBUTES;
  248. static
  249. {
  250. ConfigurationLoader.setContextClassLoader(new JARClassLoader());
  251. DEFAULT_ATTRIBUTES = new FileAttributes();
  252. DEFAULT_ATTRIBUTES.setPermissions(new UnsignedInteger32(600));
  253. }
  254. private SshClient client;
  255. private SftpSubsystemClient sftp;
  256. private FtpVFS.FtpDirectoryEntry createDirectoryEntry(SftpFile file)
  257. {
  258. FileAttributes attrs = file.getAttributes();
  259. long length = (attrs.getSize() == null ? 0L : attrs.getSize().longValue());
  260. int permissions = (attrs.getPermissions() == null
  261. ? 0 : attrs.getPermissions().intValue());
  262. // remove file mode bits from the permissions
  263. permissions &= 0x1ff; // == binary 111111111
  264. String name = file.getFilename();
  265. int type;
  266. if(file.isDirectory())
  267. type = FtpVFS.FtpDirectoryEntry.DIRECTORY;
  268. else if(file.isLink())
  269. type = FtpVFS.FtpDirectoryEntry.LINK;
  270. else
  271. type = FtpVFS.FtpDirectoryEntry.FILE;
  272. // path field filled out by FtpVFS class
  273. // (String name, String path, String deletePath,
  274. // int type, long length, boolean hidden, int permissions)
  275. return new FtpVFS.FtpDirectoryEntry(name,
  276. null,null,type,length,
  277. name.startsWith("."),
  278. permissions,null);
  279. }
  280. }