KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > WebdavFile


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/WebdavFile.java,v 1.5 2004/07/28 09:31:39 ib Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/07/28 09:31:39 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib;
25
26 import java.io.File JavaDoc;
27 import java.io.FileFilter JavaDoc;
28 import java.io.FilenameFilter JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
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 /**
40  * Implements a file for WebDav
41  *
42  */

43 public class WebdavFile extends File JavaDoc {
44
45   /** Directory separator */
46   public static final char davSeparatorChar = '/';
47   /** Directory separator */
48   public static final String JavaDoc davSeparator = String.valueOf(davSeparatorChar);
49
50   // WebdavFile may be absolute or relative
51
private HttpURL httpUrl = null;
52   private String JavaDoc relPath = null;
53
54   /**
55    * @param parent directory
56    * @param child element in parent
57    */

58   public WebdavFile(WebdavFile parent, String JavaDoc child) throws URIException {
59     this(
60       parent.getAbsolutePath() + davSeparator + child,
61       parent.getUser(),
62       parent.getPass()
63     );
64   }
65
66   /**
67    * @param pathname complete path to element
68    * @param user user name
69    * @param pass password
70    */

71   public WebdavFile(String JavaDoc pathname, String JavaDoc user, String JavaDoc pass) throws URIException {
72     this(new HttpURL(user, pass, null, -1, pathname));
73   }
74
75   /**
76    * @param url file url
77    * @param user user name
78    * @param pass password
79    */

80   public WebdavFile(URL JavaDoc url, String JavaDoc user, String JavaDoc 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   /**
86    * @param parent parent name
87    * @param child name of element in parent
88    * @param user user name
89    * @param pass password
90    */

91   public WebdavFile(String JavaDoc parent, String JavaDoc child, String JavaDoc user, String JavaDoc pass) throws URIException {
92     this(parent + davSeparator + child, user, pass);
93   }
94
95   /**
96    * @param httpUrl Webdav URL
97    */

98   public WebdavFile(HttpURL httpUrl) throws URIException {
99     super(httpUrl.getURI());
100     this.httpUrl = httpUrl;
101   }
102
103   /**
104    * A WebdavFile with a relative file. Hence nobody keeps track
105    * of a "working directory" the resulting object is only
106    * a container for a String (pathname). You cannot do anything
107    * usefull with an instance created this way
108    */

109   public WebdavFile(String JavaDoc 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 JavaDoc 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 JavaDoc e) {
129       throw new WebdavException(e);
130     }
131   }
132
133   private File JavaDoc [] toFileArray(List JavaDoc fileList) {
134     File JavaDoc files [] = new File JavaDoc [fileList.size()];
135     Iterator JavaDoc 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 JavaDoc getUser() throws URIException {
142     if(relPath!=null)
143       return null;
144     return httpUrl.getUser();
145   }
146
147   public String JavaDoc getPass() throws URIException {
148     if(relPath!=null)
149       return null;
150     return httpUrl.getPassword();
151   }
152
153   public String JavaDoc getName() {
154     if(relPath!=null)
155       return relPath;
156     String JavaDoc escapedPath = httpUrl.getEscapedPath();
157     String JavaDoc 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 JavaDoc getParent() {
169     if(relPath!=null)
170       return null;
171     String JavaDoc escapedPath = httpUrl.getEscapedPath();
172     String JavaDoc 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 JavaDoc getParentFile() {
184     String JavaDoc 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 JavaDoc 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 JavaDoc getAbsolutePath() {
210     return getPath();
211   }
212
213   public File JavaDoc getAbsoluteFile() {
214     return this;
215   }
216
217   public String JavaDoc getCanonicalPath() {
218     return getPath();
219   }
220
221   public File JavaDoc getCanonicalFile() {
222     return this;
223   }
224
225   public URL JavaDoc toURL() throws MalformedURLException JavaDoc {
226     if(relPath!=null)
227       return null;
228     try {
229         return new URL JavaDoc(httpUrl.getURI());
230     } catch (URIException e) {
231         throw new MalformedURLException JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc[] list() {
344     return list(null);
345   }
346
347   public String JavaDoc[] list(FilenameFilter JavaDoc filter) {
348     File JavaDoc [] files = listFiles(filter);
349     String JavaDoc [] names = new String JavaDoc[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 JavaDoc [] listFiles() {
357     return listFiles((FilenameFilter JavaDoc)null);
358   }
359
360   public File JavaDoc [] listFiles(FilenameFilter JavaDoc 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 JavaDoc filtered = new ArrayList JavaDoc();
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 JavaDoc e) {
378       throw new WebdavException(e);
379     } finally {
380       closeRes(res);
381     }
382   }
383
384   public File JavaDoc [] listFiles(FileFilter JavaDoc 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 JavaDoc filtered = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc dest) {
425     WebdavResource res = null;
426     try {
427       res = createRes();
428       return res.moveMethod(dest.getAbsolutePath());
429     } catch(Exception JavaDoc 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 JavaDoc e) {
447       throw new WebdavException(e);
448     } finally {
449       closeRes(res);
450     }
451   }
452
453   /** todo */
454   public static File JavaDoc[] listRoots() {
455     throw new WebdavException(WebdavException.NOT_IMPLEMENTED);
456   }
457
458   /** todo */
459   public static File JavaDoc createTempFile(String JavaDoc prefix, String JavaDoc suffix, File JavaDoc directory) {
460     throw new WebdavException(WebdavException.NOT_IMPLEMENTED);
461   }
462
463   /** todo */
464   public static File JavaDoc createTempFile(String JavaDoc prefix, String JavaDoc suffix) {
465     return WebdavFile.createTempFile(prefix, suffix, null);
466   }
467
468   public String JavaDoc toString() {
469     if(relPath!=null)
470       return relPath;
471     return httpUrl.getEscapedURI();
472   }
473
474   public int compareTo(File JavaDoc 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 JavaDoc o) {
483     return compareTo((File JavaDoc)o);
484   }
485
486   public boolean equals(Object JavaDoc 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