1 19 package org.openharmonise.ftp.client; 20 21 import java.io.IOException ; 22 import java.net.InetAddress ; 23 import java.net.URI ; 24 import java.net.UnknownHostException ; 25 import java.util.List ; 26 27 import org.openharmonise.commons.xml.namespace.NamespaceType; 28 import org.openharmonise.vfs.*; 29 import org.openharmonise.vfs.authentication.*; 30 import org.openharmonise.vfs.metadata.*; 31 import org.openharmonise.vfs.search.*; 32 import org.openharmonise.vfs.status.*; 33 34 import com.enterprisedt.net.ftp.FTPClient; 35 import com.enterprisedt.net.ftp.FTPException; 36 37 43 public class FTPFileSystem extends org.openharmonise.vfs.AbstractVirtualFileSystem { 44 45 private VirtualFileCache m_cache = null; 46 47 private FTPClient m_conn = null; 48 49 private VirtualFileSystemView m_vfView = null; 50 51 54 public FTPFileSystem(URI uri) { 55 super(uri); 56 this.setup(); 57 } 58 59 62 public FTPFileSystem(URI uri, AuthInfo authInfo) { 63 super(uri, authInfo); 64 this.setup(); 65 } 66 67 70 public FTPFileSystem(URI uri, AbstractAuthenticationStore authStore) { 71 super(uri, authStore); 72 this.setup(); 73 } 74 75 private void setup() { 76 InetAddress addr; 77 this.m_sInitialPath = this.m_sInitialPath.replace('\\', '/'); 78 79 try { 80 addr = InetAddress.getByName(this.getURI().getHost()); 81 m_conn = new FTPClient(addr, this.getURI().getPort()); 82 } catch (UnknownHostException e) { 83 e.printStackTrace(); 84 } catch (IOException e) { 85 e.printStackTrace(); 86 } catch (FTPException e) { 87 e.printStackTrace(); 88 } 89 this.m_cache = new VirtualFileCache(); 90 } 91 92 95 public List getOptions() { 96 return null; 97 } 98 99 102 public ResourceStatusWrapper getVirtualFile(String sFullPath) { 103 VirtualFile vfFile = null; 104 105 String sRealPath = this.getInitialPath() + sFullPath; 106 107 if( this.m_cache.hasFile(sFullPath) ) { 108 vfFile = this.m_cache.getFile(sFullPath); 109 } else { 110 try { 111 vfFile = new VirtualFile(sFullPath); 112 vfFile.setVFS(this); 113 String [] sListing = this.m_conn.dir(sRealPath, true); 114 this.populateVirtualFiles(sListing,sFullPath, vfFile); 115 } catch (IOException e) { 116 e.printStackTrace(); 117 } catch (FTPException e) { 118 if( e.getMessage().trim().equals("Please login with USER and PASS.")) { 119 AuthInfo auth = this.getAuthentication(); 120 try { 121 this.m_conn.login(auth.getUsername(), auth.getPassword()); 122 } catch (IOException e1) { 123 e1.printStackTrace(); 124 } catch (FTPException e1) { 125 e1.printStackTrace(); 126 } 127 vfFile = this.getVirtualFile(sFullPath).getResource(); 128 } else { 129 e.printStackTrace(); 130 } 131 } 132 } 133 134 if(vfFile==null) { 135 this.fireErrorEvent("FileNotFound", "The file " + sFullPath + " cannot be found."); 136 } 137 138 return new ResourceStatusWrapper(vfFile, new VFSStatus()); 139 } 140 141 private void populateVirtualFiles(String [] sListing, String sPath, VirtualFile vfFile) { 142 143 List aListing = this.getListParser(sListing).parse(sListing, sPath); 144 145 146 int nCount=1; 147 for(int i=0; i<aListing.size(); i++) { 148 if( nCount==1) { 149 this.populateVirtualFile( (FTPListItem)aListing.get(i), vfFile ); 150 } else if( nCount==2) { 151 } else { 153 VirtualFile vfChild = null; 154 String sTempPath = sPath + "/" + ((FTPListItem)aListing.get(i)).getFileName(); 155 if( this.m_cache.hasFile(sTempPath) ) { 156 vfChild = this.m_cache.getFile(sTempPath); 157 } else { 158 vfChild = new VirtualFile(sTempPath); 159 vfChild.setVFS(this); 160 } 161 this.populateVirtualFile((FTPListItem)aListing.get(i), vfChild); 162 vfFile.addChild( vfChild.getFullPath() ); 163 } 164 165 nCount++; 166 } 167 if( nCount>2 ) { 168 this.setFileChildrenPopulated(vfFile, true); 169 this.setFileMetadataPopulated(vfFile, true); 170 } 171 172 } 173 174 private void populateVirtualFile(FTPListItem item, VirtualFile vfFile) { 175 vfFile.setIsDirectory( item.isDirectory() ); 176 177 PropertyInstance prop = new PropertyInstance(NamespaceType.OHRM.getURI(), "filesize"); 178 vfFile.addProperty(prop); 179 180 prop = new PropertyInstance(NamespaceType.OHRM.getURI(), "modificationdate"); 181 vfFile.addProperty(prop); 182 } 183 184 private FTPListParseable getListParser(String [] sListing) { 185 FTPListParseable listParser = new FTPUNIXListParser(); 186 return listParser; 187 } 188 189 192 public ResourceStatusWrapper addVirtualFile(String sPath, VirtualFile content) { 193 VirtualFile vfFile = null; 194 195 String sFullPath = sPath + "/" + content.getFileName(); 196 197 sPath = this.getInitialPath() + sPath + "/" + content.getFileName(); 198 199 200 201 if( this.m_cache.hasFile(sFullPath) ) { 202 } else { 204 try { 205 this.m_conn.put(content.getContent(), sPath); 206 } catch (IOException e) { 207 this.fireErrorEvent("FileNotAdded", "The file " + sFullPath + " cannot be added here, this might be because a file already exists here with the same name."); 208 e.printStackTrace(); 209 } catch (FTPException e) { 210 this.fireErrorEvent("FileNotAdded", "The file " + sFullPath + " cannot be added here, this might be because a file already exists here with the same name."); 211 e.printStackTrace(); 212 } 213 } 214 215 if(vfFile==null) { 216 this.fireErrorEvent("FileNotAdded", "The file " + sFullPath + " cannot be added here, this might be because a file already exists here with the same name."); 217 } 218 219 return new ResourceStatusWrapper(vfFile, new VFSStatus()); 220 } 221 222 225 public StatusData moveVirtualFile(String sFromFullPath, String sToFullPath) { 226 VFSStatus retnStatus = new VFSStatus(); 227 228 boolean bError = false; 229 230 String sRealFromPath = this.getInitialPath() + sFromFullPath; 231 String sRealToPath = this.getInitialPath() + sToFullPath; 232 233 VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource(); 234 if( !vfFromFile.isDirectory() ) { 235 byte[] content = vfFromFile.getContent(); 236 try { 237 this.m_conn.put(content, sRealToPath); 238 } catch (IOException e) { 239 this.fireErrorEvent("FileNotMoved", "The file " + sFromFullPath + " cannot be moved here, this might be because a file already exists here with the same name."); 240 bError = true; 241 e.printStackTrace(); 242 } catch (FTPException e) { 243 this.fireErrorEvent("FileNotMoved", "The file " + sFromFullPath + " cannot be moved here, this might be because a file already exists here with the same name."); 244 bError = true; 245 e.printStackTrace(); 246 } 247 248 if( !bError ) { 249 try { 250 this.m_conn.delete(sRealFromPath); 251 } catch (IOException e1) { 252 this.fireErrorEvent("FileNotMoved", "The original file " + sFromFullPath + " cannot be removed."); 253 bError = true; 254 e1.printStackTrace(); 255 } catch (FTPException e1) { 256 this.fireErrorEvent("FileNotMoved", "The original file " + sFromFullPath + " cannot be removed."); 257 bError = true; 258 e1.printStackTrace(); 259 } 260 } else { 261 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 262 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 263 } 264 } 265 266 return retnStatus; 267 } 268 269 272 public StatusData copyVirtualFile(String sFromFullPath, String sToFullPath) { 273 VFSStatus retnStatus = new VFSStatus(); 274 275 boolean bError = false; 276 277 String sRealFromPath = this.getInitialPath() + sFromFullPath; 278 String sRealToPath = this.getInitialPath() + sToFullPath; 279 280 VirtualFile vfFromFile = this.getVirtualFile(sFromFullPath).getResource(); 281 if( !vfFromFile.isDirectory() ) { 282 byte[] content = vfFromFile.getContent(); 283 try { 284 this.m_conn.put(content, sRealToPath); 285 } catch (IOException e) { 286 this.fireErrorEvent("FileNotCopied", "The file " + sFromFullPath + " cannot be copied here, this might be because a file already exists here with the same name."); 287 bError = true; 288 e.printStackTrace(); 289 } catch (FTPException e) { 290 this.fireErrorEvent("FileNotCopied", "The file " + sFromFullPath + " cannot be copied here, this might be because a file already exists here with the same name."); 291 bError = true; 292 e.printStackTrace(); 293 } 294 } 295 296 if(bError) { 297 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 298 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 299 } 300 301 return retnStatus; 302 } 303 304 307 public StatusData deleteVirtualFile(String sFullPath) { 308 VFSStatus retnStatus = new VFSStatus(); 309 310 boolean bError = false; 311 312 String sRealPath = this.getInitialPath() + sFullPath; 313 314 try { 315 this.m_conn.delete(sRealPath); 316 } catch (IOException e) { 317 this.fireErrorEvent("FileNotDeleted", "The file " + sFullPath + " cannot be deleted."); 318 bError = true; 319 e.printStackTrace(); 320 } catch (FTPException e) { 321 this.fireErrorEvent("FileNotDeleted", "The file " + sFullPath + " cannot be deleted."); 322 bError = true; 323 e.printStackTrace(); 324 } 325 326 if(bError) { 327 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 328 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 329 } 330 331 return retnStatus; 332 } 333 334 337 public StatusData lockVirtualFile(String sFullPath) { 338 VFSStatus retnStatus = new VFSStatus(); 339 340 String sRealPath = this.getInitialPath() + sFullPath; 341 this.fireErrorEvent("FileNotLocked", "This server does not support the locking of files."); 342 return retnStatus; 343 } 344 345 348 public StatusData unlockVirtualFile(String sFullPath) { 349 VFSStatus retnStatus = new VFSStatus(); 350 351 String sRealPath = this.getInitialPath() + sFullPath; 352 this.fireErrorEvent("FileNotUnlocked", "This server does not support the locking of files."); 353 return retnStatus; 354 } 355 356 359 public StatusData createVirtualDirectory(String sFullPath) { 360 VFSStatus retnStatus = new VFSStatus(); 361 362 boolean bError = false; 363 364 String sRealPath = this.getInitialPath() + sFullPath; 365 366 try { 367 this.m_conn.mkdir(sRealPath); 368 } catch (IOException e) { 369 this.fireErrorEvent("DirectoryNotCreated", "The directory " + sFullPath + " cannot be created."); 370 bError = true; 371 e.printStackTrace(); 372 } catch (FTPException e) { 373 this.fireErrorEvent("DirectoryNotCreated", "The directory " + sFullPath + " cannot be created."); 374 bError = true; 375 e.printStackTrace(); 376 } 377 378 if(bError) { 379 retnStatus.setStatusLevel(StatusData.LEVEL_ERROR); 380 retnStatus.setStatusCode(StatusData.STATUS_INVALID_REQUEST); 381 } 382 383 return retnStatus; 384 } 385 386 389 public StatusData createShortcut(String sFullPath, String sToFullPath) { 390 VFSStatus retnStatus = new VFSStatus(); 391 392 String sRealPath = this.getInitialPath() + sFullPath; 393 String sRealToPath = this.getInitialPath() + sToFullPath; 394 this.fireErrorEvent("ShortcutNotCreted", "This server does not support shortcuts."); 395 return retnStatus; 396 } 397 398 401 public ResourceListStatusWrapper search(Query query) { 402 return null; 403 } 404 405 408 public VirtualFileSystemView getVirtualFileSystemView() { 409 if( this.m_vfView==null ) { 410 this.m_vfView = new FTPFileSystemView(); 411 } 412 413 return this.m_vfView; 414 } 415 416 419 public byte[] getVirtualFileContent(String sFullPath) { 420 String sRealPath = this.getInitialPath() + sFullPath; 421 422 byte[] content = null; 423 424 try { 425 content = this.m_conn.get(sRealPath); 426 } catch (IOException e) { 427 this.fireErrorEvent("ContentNotRetrieved", "The content for " + sFullPath + " could not be retrieved."); 428 e.printStackTrace(); 429 } catch (FTPException e) { 430 this.fireErrorEvent("ContentNotRetrieved", "The content for " + sFullPath + " could not be retrieved."); 431 e.printStackTrace(); 432 } 433 434 return content; 435 } 436 437 440 protected void fullyPopulateFileMetadata(VirtualFile vfFile) { 441 } 443 444 447 protected void fullyPopulateFileChildren(VirtualFile vfFile) { 448 450 } 451 452 455 public StatusData synchroniseFile(VirtualFile vfFile) { 456 VFSStatus retnStatus = new VFSStatus(); 457 return retnStatus; 458 } 459 460 463 public StatusData synchroniseAllFiles() { 464 VFSStatus retnStatus = new VFSStatus(); 465 return retnStatus; 466 } 467 468 471 public boolean exists(String sFullPath) { 472 boolean bExists = true; 473 474 return bExists; 475 } 476 477 480 protected void refreshChildren(VirtualFile vfFile) { 481 } 482 483 486 public StatusData orderVirtualFileChildren(List aPaths, VirtualFile vfDir) { 487 VFSStatus retnStatus = new VFSStatus(); 488 return retnStatus; 489 } 490 491 494 public ValueInstance getNewValueInstance(PropertyInstance propInst) { 495 return null; 496 } 497 498 501 public boolean rejectAllChanges() { 502 return false; 503 } 504 505 508 protected void fullyPopulateFileAllowedMethods(VirtualFile vfFile) { 509 } 510 511 514 public String currentUserResourcePath(AuthInfo authInfo) { 515 return null; 516 } 517 518 521 public List getChangedVirtualFiles() { 522 return null; 523 } 524 525 528 public VirtualFile getPropertyVirtualFile(String sPropPath) { 529 return null; 530 } 531 532 } 533
| Popular Tags
|