1 17 package org.alfresco.filesys.ftp; 18 19 import org.alfresco.filesys.server.filesys.*; 20 import org.alfresco.filesys.server.core.*; 21 22 29 public class FTPPath 30 { 31 32 34 private static final String FTP_SEPERATOR = "/"; 35 private static final char FTP_SEPERATOR_CHAR = '/'; 36 37 39 private static final String DIR_SEPERATOR = "\\"; 40 private static final char DIR_SEPERATOR_CHAR = '\\'; 41 42 44 private String m_ftpPath; 45 46 48 private String m_shareName; 49 private String m_sharePath; 50 51 53 private DiskSharedDevice m_shareDev; 54 55 57 private boolean m_dir = true; 58 59 62 public FTPPath() 63 { 64 try 65 { 66 setFTPPath(null); 67 } 68 catch (Exception ex) 69 { 70 } 71 } 72 73 79 public FTPPath(String ftpPath) throws InvalidPathException 80 { 81 setFTPPath(ftpPath); 82 } 83 84 91 public FTPPath(String shrName, String shrPath) throws InvalidPathException 92 { 93 setSharePath(shrName, shrPath); 94 } 95 96 101 public FTPPath(FTPPath ftpPath) 102 { 103 try 104 { 105 setFTPPath(ftpPath.getFTPPath()); 106 m_shareDev = ftpPath.getSharedDevice(); 107 } 108 catch (Exception ex) 109 { 110 } 111 } 112 113 118 public final boolean isRootPath() 119 { 120 return m_ftpPath.compareTo(FTP_SEPERATOR) == 0 ? true : false; 121 } 122 123 128 public final boolean isDirectory() 129 { 130 return m_dir; 131 } 132 133 138 public final boolean hasFTPPath() 139 { 140 return m_ftpPath != null ? true : false; 141 } 142 143 148 public final String getFTPPath() 149 { 150 return m_ftpPath; 151 } 152 153 158 public final boolean hasShareName() 159 { 160 return m_shareName != null ? true : false; 161 } 162 163 168 public final String getShareName() 169 { 170 return m_shareName; 171 } 172 173 178 public final boolean isRootSharePath() 179 { 180 if (m_sharePath == null || m_sharePath.compareTo(DIR_SEPERATOR) == 0) 181 return true; 182 return false; 183 } 184 185 190 public final boolean hasSharePath() 191 { 192 return m_sharePath != null ? true : false; 193 } 194 195 200 public final String getSharePath() 201 { 202 return m_sharePath; 203 } 204 205 210 public final boolean hasSharedDevice() 211 { 212 return m_shareDev != null ? true : false; 213 } 214 215 220 public final DiskSharedDevice getSharedDevice() 221 { 222 return m_shareDev; 223 } 224 225 231 public final void setFTPPath(String path) throws InvalidPathException 232 { 233 234 236 if (path == null || path.length() == 0 || path.compareTo(FTP_SEPERATOR) == 0) 237 { 238 m_ftpPath = FTP_SEPERATOR; 239 m_shareName = null; 240 m_sharePath = null; 241 m_shareDev = null; 242 return; 243 } 244 245 247 if (path.startsWith(FTP_SEPERATOR) == false) 248 throw new InvalidPathException("Invalid FTP path, should start with " + FTP_SEPERATOR); 249 250 252 m_ftpPath = path; 253 254 256 int pos = path.indexOf(FTP_SEPERATOR, 1); 257 if (pos != -1) 258 { 259 m_shareName = path.substring(1, pos); 260 if (path.length() > pos) 261 m_sharePath = path.substring(pos).replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR); 262 else 263 m_sharePath = DIR_SEPERATOR; 264 } 265 else 266 { 267 m_shareName = path.substring(1); 268 m_sharePath = DIR_SEPERATOR; 269 } 270 271 273 if (m_shareDev != null && m_shareName != null && m_shareDev.getName().compareTo(m_shareName) != 0) 274 m_shareDev = null; 275 } 276 277 284 public final void setSharePath(String shr, String path) throws InvalidPathException 285 { 286 287 289 m_shareName = shr; 290 m_sharePath = path != null ? path : DIR_SEPERATOR; 291 292 294 StringBuffer ftpPath = new StringBuffer (); 295 296 ftpPath.append(FTP_SEPERATOR); 297 if (hasShareName()) 298 ftpPath.append(getShareName()); 299 300 if (hasSharePath()) 301 { 302 303 305 String ftp = getSharePath().replace(DIR_SEPERATOR_CHAR, FTP_SEPERATOR_CHAR); 306 ftpPath.append(ftp); 307 } 308 else 309 ftpPath.append(FTP_SEPERATOR); 310 311 313 m_ftpPath = ftpPath.toString(); 314 } 315 316 323 public final boolean setSharedDevice(SharedDeviceList shareList, FTPSrvSession sess) 324 { 325 326 328 m_shareDev = null; 329 330 332 if (hasShareName() == false || shareList == null) 333 return false; 334 335 337 SharedDevice shr = shareList.findShare(getShareName()); 338 339 if (shr != null && shr instanceof DiskSharedDevice) 340 m_shareDev = (DiskSharedDevice) shr; 341 342 344 return m_shareDev != null ? true : false; 345 } 346 347 353 public final boolean setSharedDevice(DiskSharedDevice share) 354 { 355 m_shareDev = share; 356 return true; 357 } 358 359 365 public final String makeFTPPathToFile(String fname) 366 { 367 368 370 StringBuffer path = new StringBuffer (256); 371 path.append(m_ftpPath); 372 if (m_ftpPath.endsWith(FTP_SEPERATOR) == false) 373 path.append(FTP_SEPERATOR); 374 path.append(fname); 375 376 return path.toString(); 377 } 378 379 385 public final String makeSharePathToFile(String fname) 386 { 387 388 390 StringBuilder path = new StringBuilder (256); 391 path.append(m_sharePath); 392 if (m_sharePath.endsWith(DIR_SEPERATOR) == false) 393 path.append(DIR_SEPERATOR); 394 path.append(fname); 395 396 return path.toString(); 397 } 398 399 404 public final void addDirectory(String dir) 405 { 406 407 409 if (dir.length() > 1 && dir.endsWith(FTP_SEPERATOR) || dir.endsWith(DIR_SEPERATOR)) 410 dir = dir.substring(0, dir.length() - 1); 411 412 414 StringBuilder str = new StringBuilder (256); 415 str.append(m_ftpPath); 416 417 if (m_ftpPath.endsWith(FTP_SEPERATOR) == false) 418 str.append(FTP_SEPERATOR); 419 str.append(dir); 420 if (m_ftpPath.endsWith(FTP_SEPERATOR) == false) 421 str.append(FTP_SEPERATOR); 422 423 m_ftpPath = str.toString(); 424 425 427 if (m_ftpPath.indexOf(DIR_SEPERATOR) != -1) 428 m_ftpPath = m_ftpPath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR); 429 430 432 str.setLength(0); 433 str.append(m_sharePath); 434 if (m_sharePath.endsWith(DIR_SEPERATOR) == false) 435 str.append(DIR_SEPERATOR); 436 str.append(dir); 437 438 m_sharePath = str.toString(); 439 440 442 if (m_sharePath.indexOf(FTP_SEPERATOR) != -1) 443 m_sharePath = m_sharePath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR); 444 445 447 setDirectory(true); 448 } 449 450 455 public final void addFile(String file) 456 { 457 458 460 StringBuilder str = new StringBuilder (256); 461 str.append(m_ftpPath); 462 463 if (m_ftpPath.endsWith(FTP_SEPERATOR) == false) 464 str.append(FTP_SEPERATOR); 465 str.append(file); 466 467 m_ftpPath = str.toString(); 468 469 471 if (m_ftpPath.indexOf(DIR_SEPERATOR) != -1) 472 m_ftpPath = m_ftpPath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR); 473 474 476 str.setLength(0); 477 str.append(m_sharePath); 478 if (m_sharePath.endsWith(DIR_SEPERATOR) == false) 479 str.append(DIR_SEPERATOR); 480 str.append(file); 481 482 m_sharePath = str.toString(); 483 484 486 if (m_sharePath.indexOf(FTP_SEPERATOR) != -1) 487 m_sharePath = m_sharePath.replace(FTP_SEPERATOR_CHAR, DIR_SEPERATOR_CHAR); 488 489 491 setDirectory(false); 492 } 493 494 497 public final void removeDirectory() 498 { 499 500 502 if (m_ftpPath != null && m_ftpPath.length() > 1) 503 { 504 505 507 int pos = m_ftpPath.length() - 1; 508 if (m_ftpPath.endsWith(FTP_SEPERATOR)) 509 pos--; 510 511 while (pos > 0 && m_ftpPath.charAt(pos) != FTP_SEPERATOR_CHAR) 512 pos--; 513 514 516 m_ftpPath = m_ftpPath.substring(0, pos); 517 518 520 setDirectory(true); 521 522 524 try 525 { 526 setFTPPath(m_ftpPath); 527 } 528 catch (InvalidPathException ex) 529 { 530 } 531 } 532 } 533 534 539 protected final void setDirectory(boolean dir) 540 { 541 m_dir = dir; 542 } 543 544 550 public final static boolean hasMultipleDirectories(String path) 551 { 552 if (path == null) 553 return false; 554 555 if (path.startsWith(FTP_SEPERATOR)) 556 return true; 557 return false; 558 } 559 560 566 public final static boolean isRelativePath(String path) 567 { 568 if (path == null) 569 return false; 570 return path.startsWith(FTP_SEPERATOR) ? false : true; 571 } 572 573 578 public String toString() 579 { 580 StringBuilder str = new StringBuilder (); 581 582 str.append("["); 583 str.append(getFTPPath()); 584 str.append("="); 585 str.append(getShareName()); 586 str.append(","); 587 str.append(getSharePath()); 588 str.append("]"); 589 590 return str.toString(); 591 } 592 } 593 | Popular Tags |