1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  package org.abstracthorizon.danube.webdav.fs;
14  
15  import org.abstracthorizon.danube.http.Status;
16  import org.abstracthorizon.danube.http.util.IOUtils;
17  import org.abstracthorizon.danube.webdav.ResourceAdapter;
18  import org.abstracthorizon.danube.webdav.lock.LockingMechanism;
19  import org.abstracthorizon.danube.webdav.util.NamespacesProvider;
20  import org.abstracthorizon.danube.webdav.util.SimpleNamespacesProvider;
21  import org.abstracthorizon.danube.webdav.xml.dav.DAVNamespace;
22  import org.abstracthorizon.danube.webdav.xml.dav.request.properties.RequestProperty;
23  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.CreationDate;
24  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.DisplayName;
25  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.GetContentLength;
26  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.GetContentType;
27  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.GetETag;
28  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.GetLastModified;
29  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.LockDiscovery;
30  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.ResourceType;
31  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.ResponseProperty;
32  import org.abstracthorizon.danube.webdav.xml.dav.response.properties.SupportedLock;
33  
34  import java.io.File;
35  import java.io.FileInputStream;
36  import java.io.FileOutputStream;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.io.OutputStream;
40  
41  
42  
43  
44  
45  
46  public class FileSystemWebDAVResourceAdapter implements ResourceAdapter {
47  
48      
49      protected File path;
50  
51      
52      protected static ResponseProperty[] filePropertyNames = new ResponseProperty[]{
53          new CreationDate(Status.OK),
54          new DisplayName(Status.OK),
55          new GetContentLength(Status.OK),
56          new GetContentType(Status.OK),
57          new GetETag(Status.OK),
58          new GetLastModified(Status.OK),
59          new LockDiscovery(Status.OK),
60          new ResourceType(Status.OK),
61          
62          new SupportedLock(Status.OK)
63      };
64  
65      
66      protected static ResponseProperty[] dirPropertyNames = new ResponseProperty[]{
67          new CreationDate(Status.OK),
68          new DisplayName(Status.OK),
69          new GetETag(Status.OK),
70          new GetLastModified(Status.OK),
71          new LockDiscovery(Status.OK),
72          new ResourceType(Status.OK),
73          
74          new SupportedLock(Status.OK)
75      };
76  
77      
78      protected static RequestProperty[] fileRequestProperties = new RequestProperty[]{
79          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.CreationDate(null),
80          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.DisplayName(null),
81          new org.abstracthorizon.danube.webdav.fs.properties.GetContentLength(null),
82          new org.abstracthorizon.danube.webdav.fs.properties.GetContentType(null),
83          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.GetETag(null),
84          new org.abstracthorizon.danube.webdav.fs.properties.GetLastModified(null),
85          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.LockDiscovery(null),
86          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.ResourceType(null),
87          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.SupportedLock(null, null)
88      };
89  
90      
91      protected static RequestProperty[] dirRequestProperties = new RequestProperty[]{
92          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.CreationDate(null),
93          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.DisplayName(null),
94          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.GetETag(null),
95          new org.abstracthorizon.danube.webdav.fs.properties.GetLastModified(null),
96          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.LockDiscovery(null),
97          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.ResourceType(null),
98          new org.abstracthorizon.danube.webdav.xml.dav.request.properties.SupportedLock(null, null)
99      };
100 
101     
102     protected NamespacesProvider namespacesProvider = new SimpleNamespacesProvider();
103 
104     
105     protected LockingMechanism lockingMechanism = new FSLockingMechanism();
106 
107     
108     protected DAVNamespace davNamespace = new DAVNamespace();
109 
110     
111 
112 
113     public FileSystemWebDAVResourceAdapter() {
114         initHandlers();
115     }
116 
117     
118 
119 
120 
121     public FileSystemWebDAVResourceAdapter(File path) {
122         setFilePath(path);
123         initHandlers();
124     }
125 
126     
127 
128 
129     protected void initHandlers() {
130         davNamespace.setDAVFactory(new FSDAVFactory());
131         namespacesProvider.addNamespace(davNamespace.getURLString(), davNamespace.getPreferredPrefix(), davNamespace);
132     }
133 
134     
135 
136 
137 
138     public File getFilePath() {
139         return path;
140     }
141 
142     
143 
144 
145 
146     public void setFilePath(File path) {
147         this.path = path;
148     }
149 
150     
151 
152 
153 
154     public NamespacesProvider getNamespacesProvider() {
155         return namespacesProvider;
156     }
157 
158     
159 
160 
161 
162     public void setNamespacesProvider(NamespacesProvider namespacesProvider) {
163         this.namespacesProvider = namespacesProvider;
164     }
165 
166     
167 
168 
169 
170     public LockingMechanism getLockingMechanism() {
171         return lockingMechanism;
172     }
173 
174     
175 
176 
177 
178     public void setLockingMechanism(LockingMechanism lockingMechanism) {
179         this.lockingMechanism = lockingMechanism;
180     }
181 
182     
183 
184 
185 
186     public Object[] collectionElements(Object resource) {
187         File dir = (File)resource;
188         return dir.listFiles();
189     }
190 
191     
192 
193 
194 
195 
196 
197     public void copy(Object source, Object destination, boolean recursive) throws IOException {
198         File from = (File)source;
199         File to = (File)destination;
200         
201         if (!IOUtils.copy(from, to, recursive)) {
202             throw new IOException("Cannot copy");
203         }
204     }
205 
206     
207 
208 
209 
210 
211     public void delete(Object resource) throws IOException {
212         boolean ok;
213         File file = (File)resource;
214         if (file.isDirectory()) {
215             
216             ok = IOUtils.delete(file);
217         } else {
218             ok = file.delete();
219         }
220         if (!ok) {
221             throw new IOException("Cannot delete");
222         }
223     }
224 
225     
226 
227 
228 
229     public boolean exists(Object resource) {
230         File file = (File)resource;
231         return file.exists();
232     }
233 
234     
235 
236 
237 
238 
239     public Object findResource(String path) {
240         File file = new File(this.path, path);
241         if (file.exists()) {
242             try {
243                 return file.getCanonicalFile();
244             } catch (IOException e) {
245             }
246         }
247         return file;
248     }
249 
250     
251 
252 
253 
254 
255     public Object findParentResource(Object resource) {
256         File file = (File)resource;
257         if (file.equals(path)) {
258             return null;
259         } else {
260             return file.getParentFile();
261         }
262     }
263 
264     
265 
266 
267 
268 
269     public RequestProperty[] getDefaultRequestProperties(Object resource) {
270         File file = (File)resource;
271         if (file.isDirectory()) {
272             return dirRequestProperties;
273         } else {
274             return fileRequestProperties;
275         }
276     }
277 
278     
279 
280 
281 
282 
283     public ResponseProperty[] getDefaultResponseProperties(Object resource) {
284         File file = (File)resource;
285         if (file.isDirectory()) {
286             return dirPropertyNames;
287         } else {
288             return filePropertyNames;
289         }
290     }
291 
292     
293 
294 
295 
296 
297     public InputStream getInputStream(Object resource) {
298         File file = (File)resource;
299         try {
300             return new FileInputStream(file);
301         } catch (IOException e) {
302             return null;
303         }
304     }
305 
306     
307 
308 
309 
310 
311 
312 
313     public InputStream getInpusStream(Object resource, long from, long length) {
314         File file = (File)resource;
315         try {
316             return new RandomAccessFileRangeInputStream(file, from, length);
317         } catch (IOException e) {
318             return null;
319         }
320     }
321 
322     
323 
324 
325 
326 
327     public OutputStream getOutputStream(Object resource) {
328         File file = (File)resource;
329         try {
330             return new FileOutputStream(file);
331         } catch (IOException e) {
332             return null;
333         }
334     }
335 
336     
337 
338 
339 
340 
341 
342 
343     public OutputStream getOutputStream(Object resource, long from, long length) {
344         File file = (File)resource;
345         try {
346             return new RandomAccessFileRangeOutputStream(file, from, length);
347         } catch (IOException e) {
348             return null;
349         }
350     }
351 
352     
353 
354 
355 
356 
357     public String getResourceName(Object resource) {
358         File file = (File)resource;
359         return file.getName();
360     }
361 
362     
363 
364 
365 
366 
367     public String getResourceETag(Object resource) {
368         File file = (File)resource;
369         String pathString = path.getAbsolutePath();
370         String eTag = file.getAbsolutePath();
371         eTag = eTag.substring(pathString.length());
372         eTag = "W/\"" + Long.toHexString(file.lastModified()) + "-" + eTag + "\"";
373         return eTag;
374     }
375 
376     
377 
378 
379 
380 
381     public boolean isCollection(Object resource) {
382         File file = (File)resource;
383         return file.isDirectory();
384     }
385 
386     
387 
388 
389 
390 
391     public void makeCollection(Object resource) throws IOException {
392         File file = (File)resource;
393         if (!file.mkdir()) {
394             throw new IOException("Cannot create folder");
395         }
396     }
397 
398     
399 
400 
401 
402 
403 
404     public void move(Object source, Object destination) throws IOException {
405         File from = (File)source;
406         File to = (File)destination;
407         if (!from.renameTo(to)) {
408             throw new IOException("Cannot move");
409         }
410     }
411 
412     
413 
414 
415 
416 
417     public long resourceCreated(Object resource) {
418         File file = (File)resource;
419         return file.lastModified();
420     }
421 
422     
423 
424 
425 
426 
427     public long resourceLastModified(Object resource) {
428         File file = (File)resource;
429         return file.lastModified();
430     }
431 
432     
433 
434 
435 
436 
437     public long resourceLength(Object resource) {
438         File file = (File)resource;
439         return file.length();
440     }
441 
442 }