1 package com.sslexplorer.vfs; 2 3 import java.io.IOException ; 4 import java.net.URI ; 5 import java.util.ArrayList ; 6 import java.util.Date ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.apache.commons.vfs.FileObject; 13 import org.apache.commons.vfs.FileType; 14 import org.apache.commons.vfs.NameScope; 15 16 import com.sslexplorer.policyframework.LaunchSession; 17 import com.sslexplorer.security.PasswordCredentials; 18 import com.sslexplorer.vfs.webdav.DAVAuthenticationRequiredException; 19 import com.sslexplorer.vfs.webdav.DAVException; 20 import com.sslexplorer.vfs.webdav.DAVListener; 21 import com.sslexplorer.vfs.webdav.DAVMultiStatus; 22 import com.sslexplorer.vfs.webdav.DAVStatus; 23 import com.sslexplorer.vfs.webdav.DAVUtilities; 24 import com.sslexplorer.vfs.webdav.methods.GET; 25 26 35 public class FileObjectVFSResource implements VFSResource { 36 final static Log log = LogFactory.getLog(VFSResource.class); 37 38 40 private VFSMount mount = null; 41 private FileObject file = null; 42 43 protected LaunchSession launchSession; 45 protected String relativePath; 46 protected VFSResource parent; 47 protected VFSRepository repository; 48 protected PasswordCredentials requestCredentials; 49 50 59 public FileObjectVFSResource(LaunchSession launchSession, VFSMount mount, VFSResource parent, String relativePath, 60 VFSRepository repository, PasswordCredentials requestCredentials) 61 throws IOException { 62 if (mount == null) 63 throw new NullPointerException ("Null mount"); 64 this.launchSession = launchSession; 65 this.mount = mount; 66 this.parent = parent; 67 this.relativePath = relativePath; 68 this.repository = repository; 69 this.requestCredentials = requestCredentials; 70 71 if (this.parent == null) { 73 if (this.relativePath != null) { 74 String parentPath = DAVUtilities.getParentPath(relativePath); 75 if (parentPath == null) { 76 } else { 77 this.parent = mount.getResource(parentPath, requestCredentials ); 78 } 79 } 80 } 81 } 82 83 88 public int hashCode() { 89 try { 90 return getFile().hashCode(); 91 } catch (IOException e) { 92 return hashCode(); 93 } 94 } 95 96 99 public boolean isMount() { 100 return false; 101 } 102 103 106 public String getWebFolderPath() { 107 return "/fs/" + mount.getMountString() 108 + (mount.getMountString().endsWith("/") || relativePath.startsWith("/") ? "" : "/") 109 + relativePath; 110 } 111 112 113 116 public boolean equals(Object object) { 117 if (object == null) 118 return (false); 119 if (object instanceof FileObjectVFSResource) { 120 FileObjectVFSResource resource = (FileObjectVFSResource) object; 121 try { 122 boolean u = getFile().equals(resource.getFile()); 123 boolean r = this.getMount() == resource.mount; 124 return (u && r); 125 } catch (IOException ioe) { 126 return false; 127 } 128 } else { 129 return (false); 130 } 131 } 132 133 136 public int compareTo(Object object) { 137 FileObjectVFSResource resource = (FileObjectVFSResource) object; 138 try { 139 return (getFile().getURL().toExternalForm().compareTo(resource.getFile().getURL().toExternalForm())); 140 } catch (IOException ioe) { 141 log.warn("Failed to compare two files.", ioe); 142 return -999; 143 } 144 } 145 146 149 public void verifyAccess() throws Exception , DAVAuthenticationRequiredException { 150 this.getFile().exists(); 151 } 152 153 156 public boolean isNull() throws IOException { 157 return !getFile().exists(); 158 } 159 160 163 public boolean isCollection() throws IOException { 164 if (this.isNull()) 165 return false; 166 try { 167 FileObject temp = getFile(); 168 FileType type = temp.getType(); 169 if (type == null) { 170 type = temp.getName().getType(); 171 } 172 return (type.equals(FileType.FOLDER)); 173 } catch (IOException e) { 174 if (log.isDebugEnabled()) { 175 log.warn("Failed to test if resource is a collection.", e); 176 } else { 177 log.warn("Failed to test if resource is a collection : " + e.getMessage()); 178 } 179 return false; 180 } 181 } 182 183 186 public boolean isResource() throws IOException { 187 if (this.isNull()) { 188 return false; 189 } else { 190 return (!this.isCollection()); 191 } 192 } 193 194 197 public FileObject getFile() throws IOException { 198 if (file == null) { 199 FileObject root = getFileObject(""); 200 String stripLeadingSlash = DAVUtilities.stripLeadingSlash(getRelativePath()); 201 file = root.resolveFile(stripLeadingSlash, NameScope.DESCENDENT_OR_SELF); 202 if (file == null) { 203 throw new IOException ("Could not create file object."); 204 } 205 } 206 return file; 207 } 208 209 private FileObject getFileObject(String relativePath) throws DAVAuthenticationRequiredException, IOException { 210 FileObject fileObject = mount.createAuthenticatedVFSFileObject(relativePath, requestCredentials); 211 return fileObject; 212 } 213 214 217 public VFSMount getMount() { 218 return this.mount; 219 } 220 221 224 public String getDisplayName() { 225 try { 226 String name = getFile().getName().getBaseName(); 227 if (this.isCollection()) 228 return (name + "/"); 229 return name; 230 } catch (IOException ioe) { 231 return getBasename(); 232 } 233 } 234 235 238 public String getBasename() { 239 String p = relativePath; 240 int idx = p.length() < 2 ? -1 : (p.lastIndexOf('/', p.endsWith("/") ? p.length() - 2 : p.length() - 1)); 241 if (idx != 1) { 242 p = p.substring(idx + 1); 243 } 244 return p; 245 } 246 247 250 public String getRelativePath() { 251 return relativePath; 252 } 253 254 257 public URI getRelativeURI() { 258 try { 259 return new URI (DAVUtilities.encodePath(getRelativePath())); 260 } catch (Exception e) { 261 log.warn("Failed to get the relativeURI for the resource", e); 262 e.printStackTrace(); 263 return null; 264 } 265 } 266 267 270 public VFSResource getParent() { 271 try { 272 if (parent == null) { 273 String parentPath = DAVUtilities.stripLeadingSlash(DAVUtilities.getParentPath(getFullPath())); 274 if (parentPath == null || parentPath.equals("/")) { 275 return null; 276 } else { 277 return repository.getResource(getLaunchSession(), parentPath, requestCredentials); 281 } 282 } 283 } catch (Throwable throwable) { 284 throwable.printStackTrace(); 285 } 286 return parent; 287 } 288 289 292 public Iterator <VFSResource> getChildren() throws IOException { 293 if (!this.isCollection()) 294 return null; 295 296 this.getFile().close(); 298 299 FileObject children[] = this.getFile().getChildren(); 300 List <VFSResource> resources = new ArrayList <VFSResource>(children.length); 301 302 for (int x = 0; x < children.length; x++) { 303 String fileName = children[x].getName().getBaseName(); 304 if (fileName.startsWith(PREFIX) && fileName.endsWith(SUFFIX)) 305 continue; 306 if (!isCollection() && !isResource()) 307 continue; 308 try { 309 323 324 VFSResource r = getMount().getResource(DAVUtilities.concatenatePaths(relativePath, fileName), requestCredentials); 328 resources.add(r); 329 } catch (Exception e) { 330 334 } 336 } 337 return resources.iterator(); 338 } 339 340 343 public String getContentType() throws IOException { 344 if (this.isNull()) 345 return null; 346 if (this.isCollection()) 347 return GET.COLLECTION_MIME_TYPE; 348 String mime = DAVUtilities.getMimeType(this.getDisplayName()); 349 return mime == null ? "application/octet-stream" : mime; 350 } 351 352 355 public Long getContentLength() throws IOException { 356 if (this.isNull() || this.isCollection()) 357 return null; 358 try { 359 return new Long (getFile().getContent().getSize()); 360 } catch (IOException e) { 361 log.error("Failed to get content length.", e); 362 return null; 363 } 364 } 365 366 369 public Date getLastModified() throws IOException { 370 if (this.isNull()) 371 return null; 372 try { 373 return new Date (getFile().getContent().getLastModifiedTime()); 374 } catch (IOException e) { 375 log.error("Failed to get last modified date of resource.", e); 376 return null; 377 } 378 } 379 380 383 public String getEntityTag() throws IOException { 384 if (this.isNull()) 385 return null; 386 387 String path = this.getRelativePath(); 388 return DAVUtilities.getETAG(path, this.getLastModified()); 389 } 390 391 394 public void delete() throws DAVMultiStatus, IOException { 395 if (this.isNull()) 396 throw new DAVException(404, "Not found", this); 397 398 if (getMount().isReadOnly()) { 399 throw new DAVException(DAVStatus.SC_FORBIDDEN, "You cannot delete this file because the the mount is readonly!"); 400 } 401 402 if (this.isResource()) { 403 try { 404 if (!getFile().delete()) { 405 throw new DAVException(403, "Can't delete resource '" + getRelativePath() + "'", this); 406 } else { 407 this.getMount().getStore().getRepository().notify(this, DAVListener.RESOURCE_REMOVED); 408 } 409 } catch (IOException e) { 410 log.error("Failed to delete resource.", e); 411 throw new DAVException(403, "Can't delete resource. " + e.getMessage(), this); 412 } 413 } else if (this.isMount()) { 414 throw new DAVException(403, "Can't delete resource '" + getRelativePath() 415 + "' as it is the root for the mount point " 416 + this.getMount().getMountString(), this); 417 } else if (this.isCollection()) { 418 419 DAVMultiStatus multistatus = new DAVMultiStatus(); 420 421 Iterator children = this.getChildren(); 422 while (children.hasNext()) 423 try { 424 ((VFSResource) children.next()).delete(); 425 } catch (DAVException exception) { 426 multistatus.merge(exception); 427 } 428 429 if (multistatus.size() > 0) 430 throw multistatus; 431 try { 432 if (!getFile().delete()) { 433 throw new DAVException(403, "Can't delete collection", this); 434 } else { 435 this.getMount().getStore().getRepository().notify(this, DAVListener.COLLECTION_REMOVED); 436 } 437 } catch (IOException e) { 438 log.error("Failed to delete resource.", e); 439 throw new DAVException(403, "Can't delete collection " + getRelativePath() + ". " + e.getMessage(), this); 440 } 441 } 442 } 443 444 447 public void copy(VFSResource dest, boolean overwrite, boolean recursive) throws DAVMultiStatus, IOException { 448 449 454 455 if (this.isNull()) 456 throw new DAVException(404, "Not found", this); 457 458 459 if (!dest.isNull()) { 460 if (!overwrite) { 461 String msg = "Not overwriting existing destination"; 462 throw new DAVException(412, msg, dest); 463 } 464 dest.delete(); 465 } 466 467 468 if (this.isResource()) { 469 VFSInputStream in = this.getInputStream(); 470 VFSOutputStream out = dest.getOutputStream(); 471 byte buffer[] = new byte[4096]; 472 int k = -1; 473 while ((k = in.read(buffer)) != -1) 474 out.write(buffer, 0, k); 475 out.close(); 476 } 477 478 479 if (this.isCollection()) { 480 dest.makeCollection(); 481 if (!recursive) 482 return; 483 484 DAVMultiStatus multistatus = new DAVMultiStatus(); 485 Iterator children = this.getChildren(); 486 while (children.hasNext()) 487 try { 488 FileObjectVFSResource childResource = (FileObjectVFSResource) children.next(); 489 try { 490 FileObject child = ((FileObjectVFSResource) dest).getFile().resolveFile(childResource.getFile() 491 .getName() 492 .getBaseName()); 493 FileObjectVFSResource target = new FileObjectVFSResource(getLaunchSession(), 494 this.getMount(), 495 this, 496 this.getMount().getMountString(), 497 repository, 498 requestCredentials); 499 childResource.copy(target, overwrite, recursive); 500 } catch (IOException e) { 501 throw new DAVException(403, "Could not resolve child.", e); 502 } 503 } catch (DAVException exception) { 504 multistatus.merge(exception); 505 } 506 if (multistatus.size() > 0) 507 throw multistatus; 508 } 509 } 510 511 514 public void move(VFSResource dest, boolean overwrite) throws DAVMultiStatus, IOException { 515 516 521 522 if (this.isNull()) 523 throw new DAVException(404, "Not found", this); 524 525 526 if (getMount().isReadOnly()) { 527 throw new DAVException(DAVStatus.SC_FORBIDDEN, "You cannot move this file because the the mount is readonly!"); 528 } 529 530 531 if (!dest.isNull()) { 532 if (!overwrite) { 533 String msg = "Not overwriting existing destination"; 534 throw new DAVException(412, msg, dest); 535 } 536 dest.delete(); 537 } 538 539 540 if (getFile().canRenameTo(dest.getFile())) { 541 getFile().moveTo(dest.getFile()); 542 return; 543 } 544 545 546 copy(dest, overwrite, true); 547 } 548 549 552 public void makeCollection() throws IOException { 553 VFSResource parent = this.getParent(); 554 if (!this.isNull()) 555 throw new DAVException(405, "Resource exists", this); 556 if (parent.isNull()) 557 throw new DAVException(409, "Parent does not not exist", this); 558 if (!parent.isCollection()) 559 throw new DAVException(403, "Parent not a collection", this); 560 561 562 if (getMount().isReadOnly()) { 563 throw new DAVException(DAVStatus.SC_FORBIDDEN, "You cannot create a folder here because the the mount is readonly!"); 564 } 565 566 try { 567 getFile().createFolder(); 568 this.getMount().getStore().getRepository().notify(this, DAVListener.COLLECTION_CREATED); 569 } catch (IOException e) { 570 throw new DAVException(507, "Can't create collection", this); 571 } 572 } 573 574 577 public VFSInputStream getInputStream() throws IOException { 578 if (this.isNull()) 579 throw new DAVException(404, "Not found", this); 580 if (this.isCollection()) 581 throw new DAVException(403, "Resource is collection", this); 582 return new VFSInputStream(this); 583 } 584 585 588 public VFSOutputStream getOutputStream() throws IOException { 589 if (this.isCollection()) 590 throw new DAVException(409, "Can't write a collection", this); 591 592 if (getMount().isReadOnly()) { 593 throw new DAVException(DAVStatus.SC_FORBIDDEN, "You cannot create a write here because the the mount is readonly!"); 594 } 595 return new VFSOutputStream(this); 596 } 597 598 603 public URI getFullURI() { 604 VFSMount mount = getMount(); 605 URI uri = URI.create(mount == null ? ("/" + getRelativeURI()) 606 : ("/" + DAVUtilities.stripTrailingSlash(DAVUtilities.encodePath(mount.getMountString(), true)) + "/" + DAVUtilities.stripLeadingSlash(DAVUtilities.encodePath(getRelativePath(), 607 true)))); 608 return uri; 609 } 610 611 616 public String getFullPath() { 617 VFSMount mount = getMount(); 618 return mount == null ? ("/" + getRelativeURI()) : ("/" + mount.getMountString() 619 + (mount.getMountString().endsWith("/") || getRelativePath().startsWith("/") ? "" : "/") + getRelativePath()); 620 } 621 622 627 public LaunchSession getLaunchSession() { 628 return launchSession; 629 } 630 } 631 | Popular Tags |