1 23 24 package org.apache.webdav.lib; 25 26 import java.io.File ; 27 import java.io.FileFilter ; 28 import java.io.FilenameFilter ; 29 import java.net.MalformedURLException ; 30 import java.net.URL ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import org.apache.commons.httpclient.HttpURL; 35 import org.apache.commons.httpclient.HttpsURL; 36 import org.apache.commons.httpclient.URIException; 37 import org.apache.commons.httpclient.util.URIUtil; 38 39 43 public class WebdavFile extends File { 44 45 46 public static final char davSeparatorChar = '/'; 47 48 public static final String davSeparator = String.valueOf(davSeparatorChar); 49 50 private HttpURL httpUrl = null; 52 private String relPath = null; 53 54 58 public WebdavFile(WebdavFile parent, String child) throws URIException { 59 this( 60 parent.getAbsolutePath() + davSeparator + child, 61 parent.getUser(), 62 parent.getPass() 63 ); 64 } 65 66 71 public WebdavFile(String pathname, String user, String pass) throws URIException { 72 this(new HttpURL(user, pass, null, -1, pathname)); 73 } 74 75 80 public WebdavFile(URL url, String user, String pass) throws URIException { 81 this(url.getProtocol().equals("https") 82 ? new HttpsURL(user, pass, url.getHost(), url.getPort(), url.getPath()) 83 : new HttpURL(user, pass, url.getHost(), url.getPort(), url.getPath())); 84 } 85 91 public WebdavFile(String parent, String child, String user, String pass) throws URIException { 92 this(parent + davSeparator + child, user, pass); 93 } 94 95 98 public WebdavFile(HttpURL httpUrl) throws URIException { 99 super(httpUrl.getURI()); 100 this.httpUrl = httpUrl; 101 } 102 103 109 public WebdavFile(String aPath) { 110 super(aPath); 111 relPath = aPath; 112 } 113 114 private WebdavResource createRes() { 115 try { 116 if(httpUrl==null) 117 throw new WebdavException(WebdavException.RELATIVE_FILE); 118 return new WebdavResource(httpUrl); 119 } catch(Exception e) { 120 throw new WebdavException(e); 121 } 122 } 123 124 private void closeRes(WebdavResource res) { 125 try { 126 if(res!=null) 127 res.close(); 128 } catch(Exception e) { 129 throw new WebdavException(e); 130 } 131 } 132 133 private File [] toFileArray(List fileList) { 134 File files [] = new File [fileList.size()]; 135 Iterator it = fileList.iterator(); 136 for(int i=0; i<files.length; i++) 137 files[i] = (WebdavFile)it.next(); 138 return files; 139 } 140 141 public String getUser() throws URIException { 142 if(relPath!=null) 143 return null; 144 return httpUrl.getUser(); 145 } 146 147 public String getPass() throws URIException { 148 if(relPath!=null) 149 return null; 150 return httpUrl.getPassword(); 151 } 152 153 public String getName() { 154 if(relPath!=null) 155 return relPath; 156 String escapedPath = httpUrl.getEscapedPath(); 157 String escapedName = 158 URIUtil.getName(escapedPath.endsWith("/") 159 ? escapedPath.substring(0, escapedPath.length() - 1) 160 : escapedPath); 161 try { 162 return URIUtil.decode(escapedName); 163 } catch (URIException e) { 164 return escapedName; 165 } 166 } 167 168 public String getParent() { 169 if(relPath!=null) 170 return null; 171 String escapedPath = httpUrl.getEscapedPath(); 172 String parent = escapedPath.substring( 173 0, escapedPath.lastIndexOf('/', escapedPath.length() - 2) + 1); 174 if (parent.length() <= 1) 175 return null; 176 try { 177 return URIUtil.decode(parent); 178 } catch (URIException e) { 179 return parent; 180 } 181 } 182 183 public File getParentFile() { 184 String parent = getParent(); 185 if(parent==null) 186 return null; 187 188 try { 189 return new WebdavFile(parent, getUser(), getPass()); 190 } catch(URIException e) { 191 throw new WebdavException(e); 192 } 193 } 194 195 public String getPath() { 196 if(relPath!=null) 197 return relPath; 198 try { 199 return httpUrl.getURI(); 200 } catch (URIException e) { 201 throw new WebdavException(e); 202 } 203 } 204 205 public boolean isAbsolute() { 206 return relPath==null; 207 } 208 209 public String getAbsolutePath() { 210 return getPath(); 211 } 212 213 public File getAbsoluteFile() { 214 return this; 215 } 216 217 public String getCanonicalPath() { 218 return getPath(); 219 } 220 221 public File getCanonicalFile() { 222 return this; 223 } 224 225 public URL toURL() throws MalformedURLException { 226 if(relPath!=null) 227 return null; 228 try { 229 return new URL (httpUrl.getURI()); 230 } catch (URIException e) { 231 throw new MalformedURLException (e.getMessage()); 232 } 233 } 234 235 public boolean canRead() { 236 return true; 237 } 238 239 public boolean canWrite() { 240 WebdavResource res = null; 241 try { 242 res = createRes(); 243 return !res.isLocked(); 244 } catch(Exception e) { 245 throw new WebdavException(e); 246 } finally { 247 closeRes(res); 248 } 249 } 250 251 public boolean exists() { 252 WebdavResource res = null; 253 try { 254 res = createRes(); 255 return res.exists(); 256 } catch(Exception e) { 257 throw new WebdavException(e); 258 } finally { 259 closeRes(res); 260 } 261 } 262 263 public boolean isDirectory() { 264 WebdavResource res = null; 265 try { 266 res = createRes(); 267 return res.isCollection(); 268 } catch(Exception e) { 269 throw new WebdavException(e); 270 } finally { 271 closeRes(res); 272 } 273 } 274 275 public boolean isFile() { 276 return !isDirectory(); 277 } 278 279 public boolean isHidden() { 280 WebdavResource res = null; 281 try { 282 res = createRes(); 283 return res.getIsHidden(); 284 } catch(Exception e) { 285 throw new WebdavException(e); 286 } finally { 287 closeRes(res); 288 } 289 } 290 291 public long lastModified() { 292 WebdavResource res = null; 293 try { 294 res = createRes(); 295 return res.getGetLastModified(); 296 } catch(Exception e) { 297 throw new WebdavException(e); 298 } finally { 299 closeRes(res); 300 } 301 } 302 303 public long length() { 304 WebdavResource res = null; 305 try { 306 res = createRes(); 307 return res.getGetContentLength(); 308 } catch(Exception e) { 309 throw new WebdavException(e); 310 } finally { 311 closeRes(res); 312 } 313 } 314 315 public boolean createNewFile() { 316 WebdavResource res = null; 317 try { 318 res = createRes(); 319 return res.putMethod(""); 320 } catch(Exception e) { 321 throw new WebdavException(e); 322 } finally { 323 closeRes(res); 324 } 325 } 326 327 public boolean delete() { 328 WebdavResource res = null; 329 try { 330 res = createRes(); 331 return res.deleteMethod(); 332 } catch(Exception e) { 333 throw new WebdavException(e); 334 } finally { 335 closeRes(res); 336 } 337 } 338 339 public void deleteOnExit() { 340 throw new WebdavException(WebdavException.NOT_IMPLEMENTED); 341 } 342 343 public String [] list() { 344 return list(null); 345 } 346 347 public String [] list(FilenameFilter filter) { 348 File [] files = listFiles(filter); 349 String [] names = new String [files.length]; 350 for(int i=0; i<names.length; i++) 351 names[i] = files[i].getAbsolutePath(); 352 353 return names; 354 } 355 356 public File [] listFiles() { 357 return listFiles((FilenameFilter )null); 358 } 359 360 public File [] listFiles(FilenameFilter filter) { 361 362 WebdavResource res = null; 363 try { 364 res = createRes(); 365 WebdavResource allFiles [] = res.listWebdavResources(); 366 if(allFiles==null) 367 return null; 368 369 ArrayList filtered = new ArrayList (); 370 for(int i=0; i<allFiles.length; i++) { 371 if(filter==null || filter.accept(this, allFiles[i].getDisplayName())) 372 filtered.add( new WebdavFile(allFiles[i].getHttpURL()) ); 373 } 374 375 return toFileArray(filtered); 376 377 } catch(Exception e) { 378 throw new WebdavException(e); 379 } finally { 380 closeRes(res); 381 } 382 } 383 384 public File [] listFiles(FileFilter filter) { 385 WebdavResource res = null; 386 try { 387 res = createRes(); 388 WebdavResource allFiles [] = res.listWebdavResources(); 389 if(allFiles==null) 390 return null; 391 392 ArrayList filtered = new ArrayList (); 393 for(int i=0; i<allFiles.length; i++) { 394 WebdavFile file = new WebdavFile(allFiles[i].getHttpURL()); 395 if(filter==null || filter.accept(file)) 396 filtered.add(file); 397 } 398 399 return toFileArray(filtered); 400 401 } catch(Exception e) { 402 throw new WebdavException(e); 403 } finally { 404 closeRes(res); 405 } 406 } 407 408 public boolean mkdir() { 409 WebdavResource res = null; 410 try { 411 res = createRes(); 412 return res.mkcolMethod(); 413 } catch(Exception e) { 414 throw new WebdavException(e); 415 } finally { 416 closeRes(res); 417 } 418 } 419 420 public boolean mkdirs() { 421 return mkdir(); 422 } 423 424 public boolean renameTo(File dest) { 425 WebdavResource res = null; 426 try { 427 res = createRes(); 428 return res.moveMethod(dest.getAbsolutePath()); 429 } catch(Exception e) { 430 throw new WebdavException(e); 431 } finally { 432 closeRes(res); 433 } 434 } 435 436 public boolean setLastModified(long time) { 437 throw new WebdavException(WebdavException.NOT_IMPLEMENTED); 438 } 439 440 public boolean setReadOnly() { 441 WebdavResource res = null; 442 try { 443 res = createRes(); 444 res.setOverwrite(false); 445 return true; 446 } catch(Exception e) { 447 throw new WebdavException(e); 448 } finally { 449 closeRes(res); 450 } 451 } 452 453 454 public static File [] listRoots() { 455 throw new WebdavException(WebdavException.NOT_IMPLEMENTED); 456 } 457 458 459 public static File createTempFile(String prefix, String suffix, File directory) { 460 throw new WebdavException(WebdavException.NOT_IMPLEMENTED); 461 } 462 463 464 public static File createTempFile(String prefix, String suffix) { 465 return WebdavFile.createTempFile(prefix, suffix, null); 466 } 467 468 public String toString() { 469 if(relPath!=null) 470 return relPath; 471 return httpUrl.getEscapedURI(); 472 } 473 474 public int compareTo(File pathname) { 475 if(pathname instanceof WebdavFile) { 476 WebdavFile df = (WebdavFile)pathname; 477 return df.getPath().compareTo(getPath()); 478 } 479 return -1; 480 } 481 482 public int compareTo(Object o) { 483 return compareTo((File )o); 484 } 485 486 public boolean equals(Object x) { 487 if(x==null) 488 return false; 489 490 if(x instanceof WebdavFile) { 491 WebdavFile xf = (WebdavFile)x; 492 return xf.getPath().equals(getPath()); 493 } 494 495 return false; 496 } 497 498 public int hashCode() { 499 return getPath().hashCode(); 500 } 501 } 502 503 | Popular Tags |