1 23 24 package org.apache.webdav.ui; 25 26 import java.io.File ; 27 import java.io.IOException ; 28 import javax.swing.UIManager ; 29 import javax.swing.filechooser.FileSystemView ; 30 import org.apache.commons.httpclient.HttpURL; 31 import org.apache.commons.httpclient.HttpsURL; 32 import org.apache.commons.httpclient.URIException; 33 import org.apache.webdav.lib.WebdavResource; 34 35 38 public class WebdavSystemView extends FileSystemView { 39 40 41 42 private WebdavResource webdavResource = null; 43 private HttpURL rootURL = null; 44 private WebdavFile homedir = null; 45 private String username = null; 46 private String password = null; 47 private String uri = null; 48 private String rootPath = null; 49 50 private static final String newFolderString = 51 UIManager.getString("FileChooser.other.newFolder"); 52 static FileSystemView fsv = null; 53 54 55 public WebdavSystemView(String uri, String rootPath, String username, 56 String password) 57 throws IllegalAccessError , URIException, IOException { 58 try { 59 this.rootURL = this.uriToHttpURL(uri + rootPath); 60 this.uri = uri; 61 this.rootURL.setUserinfo(username, password); 62 this.username = username; 63 this.password = password; 64 this.rootPath = rootPath; 65 66 67 this.connect(); 68 this.disconnect(); 70 71 this.homedir = new WebdavFile(this.rootURL, this.rootURL); 73 } catch (IllegalAccessError e) { 75 System.err.println(e.toString()); 76 e.printStackTrace(); 77 throw e; 78 } catch (URIException e) { 79 System.err.println(e.toString()); 80 e.printStackTrace(); 81 throw e; 82 } catch (IOException e) { 83 System.err.println(e.toString()); 84 e.printStackTrace(); 85 throw e; 86 } 87 88 } 89 90 private static HttpURL uriToHttpURL(String uri) throws URIException { 91 HttpURL url = null; 92 if (uri.startsWith("http://")) { 93 url = new HttpURL(uri); 94 } else if (uri.startsWith("https://")) { 95 url = new HttpsURL(uri); 96 } else { 97 throw new URIException("Unknown protocol in URL " + uri); 98 } 99 return url; 100 } 101 102 public void disconnect() throws java.lang.UnknownError { 103 try { 104 this.webdavResource.close(); 105 } catch (Exception e) { 106 System.err.println(e.toString()); 107 throw new UnknownError (); 108 } 109 } 110 public void connect() throws java.lang.IllegalAccessError { 111 try { 112 this.webdavResource = new WebdavResource(this.rootURL); 113 } catch (Exception e) { 114 System.err.println(e.toString()); 115 throw new IllegalAccessError (); 116 } 117 } 118 119 public static FileSystemView getFileSystemView() { 120 try { 121 if (fsv == null) { 122 fsv = new WebdavSystemView("http://127.0.0.1", "/", "", ""); 123 } 124 return fsv; 125 } catch (Exception e) { 126 System.err.println(e.toString()); 127 return null; 128 } 129 } 130 131 134 public File createFileObject(File dir, String filename) { 135 File file = null; 136 if (dir == null) { 137 file = new File (filename); 138 } else { 139 file = new File (dir, filename); 140 } 141 return file; 142 } 143 144 147 public File createFileObject(String path) { 148 File f = new File (path); 149 if (isFileSystemRoot(f)) { 150 f = createFileSystemRoot(f); 151 } 152 return f; 153 } 154 155 156 159 public File createNewFolder(File containingDir) throws IOException { 160 try { 161 if (containingDir == null) { 162 throw new IOException ("Containing directory is null:"); 163 } 164 WebdavFile newFolder = null; 165 HttpURL url = null; 166 167 url = this.uriToHttpURL(containingDir.getPath() + 168 WebdavFile.davSeparator + newFolderString); 169 url.setUserinfo(username, password); 171 newFolder = new WebdavFile(url, this.rootURL); 172 174 this.connect(); 175 if (this.webdavResource.mkcolMethod( 176 newFolder.getAbsolutePath())) { 177 return newFolder; 179 } else { 180 System.err.println("failed."); 181 System.err.println(this.webdavResource.getStatusMessage()); 182 throw new IOException ( 183 this.webdavResource.getStatusMessage()); 184 } 185 } catch (IOException e) { 186 throw e; 187 } catch (Exception e) { 188 System.err.println(e.toString()); 189 e.printStackTrace(); 190 return null; 191 } finally { 192 this.disconnect(); 193 } 194 } 195 200 public File [] getRoots() { 201 try { 202 return new WebdavFile[] {this.homedir}; 203 } catch (Exception e) { 204 System.err.println(e.toString()); 205 e.printStackTrace(); 206 return null; 207 } 208 } 209 210 211 221 public Boolean isTraversable(File f) { 222 try { 223 return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE; 231 } catch (Exception e) { 232 System.err.println(e.toString()); 233 e.printStackTrace(); 234 return Boolean.FALSE; 235 } finally { 236 this.disconnect(); 237 } 238 } 239 240 253 public String getSystemDisplayName(File f) { 254 try { 255 return f.getName(); 264 } catch (Exception e) { 265 System.err.println(e.toString()); 266 e.printStackTrace(); 267 return null; 268 } finally { 269 this.disconnect(); 270 } 271 } 272 273 287 public String getSystemTypeDescription(File f) { 288 return null; 289 } 290 291 292 293 302 public boolean isFileSystem(File f) { 303 return true; 304 } 305 306 307 310 public boolean isHiddenFile(File f) { 311 return f.isHidden(); 312 } 313 314 315 323 public boolean isFileSystemRoot(File dir) { 324 try { 325 return (rootURL.getPath().equals(dir.getPath())); 326 } 327 catch (Exception e) { 328 System.err.println("isFileSystemRoot" + e.toString()); 329 e.printStackTrace(); 330 return false; 331 } 332 } 333 334 344 public boolean isDrive(File dir) { 345 return false; 346 } 347 348 358 public boolean isFloppyDrive(File dir) { 359 return false; 360 } 361 362 372 public boolean isComputerNode(File dir) { 373 return false; 374 } 375 376 381 public File getHomeDirectory() { 382 return this.homedir; 383 } 384 385 391 public File getDefaultDirectory() { 392 return this.homedir; 393 } 394 395 396 399 public File [] getFiles(File dir, boolean useFileHiding) { 400 try { 401 402 String filenames[] = null; 403 WebdavFile files[] = null; 404 HttpURL url = null; 405 String path = null; 406 String localDir = null; 407 408 this.connect(); 409 411 path = dir.getPath(); 412 414 if (path.startsWith("http")) { 417 path = path.replaceAll(this.uri, ""); 419 } 420 if (!path.endsWith("/")) { 421 path = path + "/"; 422 } 423 424 426 this.webdavResource.setPath(path); 427 filenames = this.webdavResource.list(); 428 files = new WebdavFile[filenames.length]; 429 for (int i = 0; i < filenames.length; i++) { 430 434 localDir = dir.getPath(); 435 if (!localDir.endsWith("/")) localDir = localDir + "/"; 436 437 String filepath = localDir + filenames[i]; 438 url = this.uriToHttpURL(filepath); 440 url.setUserinfo(username, password); 442 files[i] = new WebdavFile(url, this.rootURL); 443 } 444 return files; 445 } catch (Exception e) { 446 System.err.println(e.toString()); 447 e.printStackTrace(); 448 return null; 449 } finally { 450 this.disconnect(); 451 } 452 } 453 454 455 456 462 public File getParentDirectory(File dir) { 463 if (dir == null) { 465 return null; 466 } else if (dir.equals(this.homedir)) { 467 return this.homedir; 468 } else { 469 return dir.getParentFile(); 471 } 472 } 473 474 482 protected File createFileSystemRoot(File f) { 483 try { 484 return new FileSystemRoot((WebdavFile) f); 485 486 } catch (Exception e) { 487 System.err.println("createFileSystemRoot : " + e.toString()); 488 return null; 489 } 490 } 491 492 static class WebdavFile extends org.apache.webdav.lib.WebdavFile { 493 protected WebdavResource webdavResource = null; 494 protected HttpURL rootUrl = null; 495 496 public WebdavFile(HttpURL pathUrl, HttpURL rootUrl) throws 497 URIException, IOException { 498 super(pathUrl); 499 this.webdavResource = new WebdavResource(pathUrl); 500 this.rootUrl = rootUrl; 501 } 502 503 504 public String getName() { 505 String name = null; 506 507 name = super.getName(); 509 510 if (this.isDirectory()) { 512 name = name + "/"; 513 } 514 return name; 515 } 516 public boolean isRoot() { 517 try { 518 String path = null; 519 String root = null; 520 root = this.rootUrl.getPath(); 521 path = this.webdavResource.getHttpURL().getPath(); 522 525 if (root .equalsIgnoreCase(path)) { 527 return true; 529 } else { 530 return false; 532 } 533 } 534 catch (Exception e) { 535 System.err.println(e.toString()); 536 e.printStackTrace(); 537 return false; 538 } 539 } 540 public String getParent() { 541 try { 542 if (this.isRoot()) { 544 return null; 546 } else { 547 String escapedPath = 549 this.webdavResource.getHttpURL().getPath(); 550 String parent = escapedPath.substring( 551 0, escapedPath.lastIndexOf('/', escapedPath.length() - 552 2) + 1); 553 return super.getParent(); 555 } 556 } 557 catch (Exception e) { 558 System.err.println(e.toString()); 559 e.printStackTrace(); 560 return null; 561 } 562 } 563 public File getParentFile() { 564 try { 565 HttpURL httpURL = null; 566 String parent = null; 567 parent = this.getParent(); 568 if (parent == null) { 569 return null; 571 } else { 572 httpURL = this.rootUrl; 573 httpURL.setPath(parent); 574 return new WebdavFile(httpURL, this.rootUrl); 576 } 577 } catch (Exception e) { 578 System.err.println(e.toString()); 579 e.printStackTrace(); 580 return null; 581 } 582 583 } 584 public boolean exists() { 585 return this.webdavResource.exists(); 587 } 588 public boolean isDirectory() { 589 return this.webdavResource.isCollection(); 592 } 593 } 594 595 596 597 static class FileSystemRoot extends WebdavFile { 598 599 public FileSystemRoot(HttpURL rootUrl) throws URIException, 600 IOException { 601 super(rootUrl, rootUrl); 602 } 603 public FileSystemRoot(WebdavFile webdavFile) throws URIException, 604 IOException { 605 super(webdavFile.rootUrl, webdavFile.rootUrl); 606 } 607 608 } 609 610 public static void main(String args[]) throws Exception { 612 javax.swing.JFrame frame = new javax.swing.JFrame (); 613 614 javax.swing.JFileChooser fc = new javax.swing.JFileChooser ( 616 new WebdavSystemView( 617 "http://localhost:8080", "/slide/files", "root", "root")); 618 fc.showOpenDialog(frame); 619 } 620 621 } 622 | Popular Tags |