1 /* 2 * $HeadURL: $ 3 * $Date: $ 4 * $Revision: $ 5 * $Author: $ 6 * 7 * Copyright (c) 2005 MindTree Consulting Ltd. 8 * 9 * This file is part of Insight. 10 * 11 * Insight is free software: you can redistribute it and/or modify it under the 12 * terms of the GNU General Public License as published by the Free Software 13 * Foundation, either version 3 of the License, or (at your option) any later 14 * version. 15 * 16 * Insight is distributed in the hope that it will be useful, but 17 * WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 19 * Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License along with 22 * Insight. If not, see <http://www.gnu.org/licenses/>. 23 */ 24 25 package com.mindtree.techworks.insight.download.sftpbrowse; 26 27 import java.io.File; 28 import java.io.FileFilter; 29 import java.io.FilenameFilter; 30 import java.io.IOException; 31 import java.net.MalformedURLException; 32 import java.net.PasswordAuthentication; 33 import java.net.URI; 34 import java.net.URISyntaxException; 35 import java.net.URL; 36 37 import javax.swing.filechooser.FileSystemView; 38 39 import com.sshtools.j2ssh.sftp.SftpFile; 40 41 42 /** 43 * This class is a wrapper for <code>SFTPFile</code>. This class is to be used 44 * by {@link javax.swing.JFileChooser JFileChooser}which requires an instance 45 * of <code>File</code> to it. 46 * <p> 47 * Most operations on the file wrapped by this class are <b>not allowed </b>. 48 * <code>SFTPFile</code> is meant to contain information about files on a 49 * remote server, and <code>SFTPFileFile</code> is also meant for the same. It 50 * merely provides a <code>File</code> like interface to <code>SFTPFile</code>. 51 * </p> 52 * <p> 53 * For any operations on the file, use the information about the file from an 54 * instance of this class and use 55 * {@link com.sshtools.j2ssh.SftpClient SftpClient} or any wrapper around 56 * it. You might use any other SFTPClient also. 57 * </p> 58 * <p> 59 * The {@link SFTPFileFile#getHost() getHostURL}, {@link #getPort() getPort} and 60 * {@link SFTPFileFile#getPasswordAuthentication() getPasswordAuthentication} 61 * methods return the host and authentication information to connect to the 62 * host. 63 * </p> 64 * 65 * @see com.sshtools.j2ssh.sftp.SftpFile SftpFile 66 * @see java.io.File File 67 * 68 * @author Bindul Bhowmik 69 * @version $Revision: 27 $ $Date: 2007-12-16 04:58:03 -0700 (Sun, 16 Dec 2007) $ 70 * 71 */ 72 public class SFTPFileFile extends File { 73 74 /** 75 * Used for object serialization 76 */ 77 private static final long serialVersionUID = 1566302834356038498L; 78 79 // 80 // Instance variables 81 // 82 83 /** 84 * The sftpFile instance backing the wrapper 85 */ 86 private SftpFile sftpFile; 87 88 /** 89 * The file system view which created this. 90 */ 91 private FileSystemView fileSystemView; 92 93 /** 94 * The host connected to. 95 */ 96 private String host; 97 98 /** 99 * The port connected to. 100 */ 101 private int port = 22; 102 103 /** 104 * User credentials for the remote host. 105 */ 106 private PasswordAuthentication passwordAuthentication; 107 108 // 109 // Constructors 110 // 111 112 /** 113 * Constructs a new FTPFileFile object. 114 * 115 * @param sftpFile The FTPFile instance to wrap 116 * @param fileSystemView The file system view which created this instance. 117 */ 118 public SFTPFileFile (SftpFile sftpFile, FileSystemView fileSystemView) { 119 120 super(sftpFile.getAbsolutePath()); 121 this.sftpFile = sftpFile; 122 this.fileSystemView = fileSystemView; 123 if (null != fileSystemView 124 && fileSystemView instanceof SFTPRemoteFileSystemView) { 125 SFTPRemoteFileSystemView sftpFileSystemView = (SFTPRemoteFileSystemView) fileSystemView; 126 this.host = sftpFileSystemView.getHost(); 127 this.port = sftpFileSystemView.getPort(); 128 this.passwordAuthentication = sftpFileSystemView 129 .getPasswordAuthentication(); 130 } 131 } 132 133 // 134 // Accessors 135 // 136 137 /** 138 * @return Returns the passwordAuthentication. 139 */ 140 public PasswordAuthentication getPasswordAuthentication () { 141 142 return passwordAuthentication; 143 } 144 145 /** 146 * @return Returns the hostURL. 147 */ 148 public String getHost () { 149 150 return host; 151 } 152 153 /** 154 * @return Returns the port connected to 155 */ 156 public int getPort() { 157 return port; 158 } 159 160 // 161 // Overridden methods from java.io.File 162 // 163 164 /** 165 * @return Returns the sftpFile. 166 */ 167 public SftpFile getSftpFile () { 168 169 return this.sftpFile; 170 } 171 172 /** 173 * @see java.io.File#isAbsolute() 174 */ 175 public boolean isAbsolute () { 176 177 return !this.sftpFile.isLink(); 178 } 179 180 /** 181 * @see java.io.File#isDirectory() 182 */ 183 public boolean isDirectory () { 184 185 return this.sftpFile.isDirectory(); 186 } 187 188 /** 189 * @see java.io.File#isFile() 190 */ 191 public boolean isFile () { 192 193 return this.sftpFile.isFile(); 194 } 195 196 /** 197 * @see java.io.File#lastModified() 198 */ 199 public long lastModified () { 200 201 return sftpFile.getAttributes().getModifiedTime().longValue(); 202 } 203 204 205 /** 206 * Always returns true. Read access is always allowed. 207 * 208 * @see java.io.File#canRead() 209 */ 210 public boolean canRead () { 211 212 return true; 213 } 214 215 /** 216 * Always returns false. For more information, see note 217 * {@link SFTPFileFile above}. 218 * 219 * @see java.io.File#canWrite() 220 */ 221 public boolean canWrite () { 222 223 return false; 224 } 225 226 /** 227 * @see java.io.File#compareTo(java.io.File) 228 */ 229 public int compareTo (File pathname) { 230 231 return this.getPath().compareToIgnoreCase(pathname.getPath()); 232 } 233 234 /** 235 * @see java.lang.Comparable#compareTo(java.lang.Object) 236 */ 237 public int compareTo (Object o) { 238 239 return compareTo((File) o); 240 } 241 242 /** 243 * Operation not supported. 244 * 245 * @throws IOException Since this operation is not supported. 246 * @see java.io.File#createNewFile() 247 */ 248 public boolean createNewFile () throws IOException { 249 250 throw new IOException("READ-ONLY view"); 251 } 252 253 /** 254 * Operation not supported, always returns false 255 * 256 * @throws UnsupportedOperationException Since this operation is not 257 * supported. 258 * @see java.io.File#delete() 259 */ 260 public boolean delete () { 261 262 return false; 263 } 264 265 /** 266 * Operation not supported. Nothing is returned. 267 * 268 * @see java.io.File#deleteOnExit() 269 */ 270 public void deleteOnExit () { 271 272 // No Operation 273 } 274 275 /** 276 * @see java.lang.Object#equals(java.lang.Object) 277 */ 278 public boolean equals (Object obj) { 279 280 if ((obj != null) && (obj instanceof File)) { 281 return compareTo((File) obj) == 0; 282 } 283 return false; 284 } 285 286 /** 287 * Returns <code>true</code> always. Any file for which an FTPFile 288 * instance can be got is assumed to exist. 289 * 290 * @see java.io.File#exists() 291 */ 292 public boolean exists () { 293 294 return true; 295 } 296 297 /** 298 * Returns the same instance! 299 * 300 * @see java.io.File#getAbsoluteFile() 301 */ 302 public File getAbsoluteFile () { 303 304 return this; 305 } 306 307 /** 308 * Returns the path! 309 * 310 * @see java.io.File#getAbsolutePath() 311 */ 312 public String getAbsolutePath () { 313 314 return this.getPath(); 315 } 316 317 /** 318 * Returns the instance as the canonical file 319 * 320 * @see java.io.File#getCanonicalFile() 321 */ 322 public File getCanonicalFile () throws IOException { 323 324 return this; 325 } 326 327 /** 328 * Always returns the unix style path from the root. 329 * 330 * @see java.io.File#getCanonicalPath() 331 */ 332 public String getCanonicalPath () throws IOException { 333 334 return this.getPath(); 335 } 336 337 /** 338 * Returns the name of the file 339 * 340 * @see java.io.File#getName() 341 */ 342 public String getName () { 343 344 return this.sftpFile.getFilename(); 345 } 346 347 /** 348 * Returns the path of the parent, if a file system view is present, or 349 * <code>null</code>. 350 * 351 * @see java.io.File#getParent() 352 */ 353 public String getParent () { 354 355 if (null != fileSystemView) { 356 return fileSystemView.getParentDirectory(this).getPath(); 357 } else { 358 return null; 359 } 360 } 361 362 /** 363 * Returns the parent directory, if a file system view is present, or 364 * <code>null</code>. 365 * 366 * @see java.io.File#getParentFile() 367 */ 368 public File getParentFile () { 369 370 if (null != fileSystemView) { 371 return fileSystemView.getParentDirectory(this); 372 } else { 373 return null; 374 } 375 } 376 377 /** 378 * Returns the path in the unix style. 379 * 380 * @see java.io.File#getPath() 381 */ 382 public String getPath () { 383 return this.sftpFile.getAbsolutePath(); 384 } 385 386 /** 387 * Returns false. No files are hidden! 388 * 389 * @see java.io.File#isHidden() 390 */ 391 public boolean isHidden () { 392 393 return false; 394 } 395 396 /** 397 * Returns the size of the FTP file instance 398 * 399 * @see java.io.File#length() 400 */ 401 public long length () { 402 403 return this.sftpFile.getAttributes().getSize().longValue(); 404 } 405 406 /** 407 * Operation is not supported. Use the 408 * {@link SFTPRemoteFileSystemView SFTPRemoteFileSystemView}for this. 409 * 410 * @see java.io.File#list() 411 * @return An empty String array 412 */ 413 public String [] list () { 414 415 return new String [0]; 416 } 417 418 /** 419 * Operation is not supported. Use the 420 * {@link SFTPRemoteFileSystemView SFTPRemoteFileSystemView}for this. 421 * 422 * @see java.io.File#list(java.io.FilenameFilter) 423 * @return An empty String array 424 */ 425 public String [] list (FilenameFilter filter) { 426 427 return new String [0]; 428 } 429 430 /** 431 * Operation is not supported. Use the 432 * {@link SFTPRemoteFileSystemView SFTPRemoteFileSystemView}for this. 433 * 434 * @see java.io.File#listFiles() 435 * @return An empty File array 436 */ 437 public File [] listFiles () { 438 439 return new SFTPFileFile [0]; 440 } 441 442 /** 443 * Operation is not supported. Use the 444 * {@link SFTPRemoteFileSystemView SFTPRemoteFileSystemView}for this. 445 * 446 * @see java.io.File#listFiles(java.io.FileFilter) 447 * @return An empty File array 448 */ 449 public File [] listFiles (FileFilter filter) { 450 451 return new SFTPFileFile [0]; 452 } 453 454 /** 455 * Operation is not supported. Use the 456 * {@link SFTPRemoteFileSystemView SFTPRemoteFileSystemView}for this. 457 * 458 * @see java.io.File#listFiles(java.io.FilenameFilter) 459 * @return An empty File array 460 */ 461 public File [] listFiles (FilenameFilter filter) { 462 463 return new SFTPFileFile [0]; 464 } 465 466 /** 467 * The operation is not supported, always returns <code>false</code> 468 * 469 * @see java.io.File#mkdir() 470 */ 471 public boolean mkdir () { 472 473 return false; 474 } 475 476 /** 477 * The operation is not supported, always returns <code>false</code> 478 * 479 * @see java.io.File#mkdirs() 480 */ 481 public boolean mkdirs () { 482 483 return false; 484 } 485 486 /** 487 * The operation is not supported, always returns <code>false</code> 488 * 489 * @see java.io.File#renameTo(java.io.File) 490 */ 491 public boolean renameTo (File dest) { 492 493 return false; 494 } 495 496 /** 497 * The operation is not supported, always returns <code>false</code> 498 * 499 * @see java.io.File#setLastModified(long) 500 */ 501 public boolean setLastModified (long time) { 502 503 return false; 504 } 505 506 /** 507 * The operation is not supported, always returns <code>false</code> 508 * 509 * @see java.io.File#setReadOnly() 510 */ 511 public boolean setReadOnly () { 512 513 return false; 514 } 515 516 /** 517 * @see java.io.File#toURI() 518 */ 519 public URI toURI () { 520 521 try { 522 String p = slashify(this.getPath(), this.isDirectory()); 523 if (p.startsWith("//")) 524 p = "//" + p; 525 return new URI("file", null, p, null); 526 } catch (URISyntaxException x) { 527 throw new Error(x); // Can't happen 528 } 529 } 530 531 /** 532 * @see java.io.File#toURL() 533 */ 534 public URL toURL () throws MalformedURLException { 535 536 return new URL("file", "", slashify(this.getPath(), this.isDirectory())); 537 } 538 539 /** 540 * Creates a slash representation of the file 541 * 542 * @param path The path to slashify 543 * @param isDirectory Is it a directory 544 * @return The slashified path 545 */ 546 private String slashify (String path, boolean isDirectory) { 547 548 String p = path; 549 if (!p.startsWith("/")) 550 p = "/" + p; 551 if (!p.endsWith("/") && isDirectory) 552 p = p + "/"; 553 return p; 554 } 555 }