1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.slc.vfs;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24
25 import org.apache.commons.io.IOExceptionWithCause;
26 import org.apache.commons.vfs2.FileObject;
27 import org.apache.commons.vfs2.FileSystemException;
28 import org.apache.commons.vfs2.NameScope;
29 import org.argeo.slc.SlcException;
30 import org.springframework.core.io.Resource;
31
32 public class VfsResource implements Resource {
33 private FileObject fileObject;
34
35 public VfsResource(FileObject fileObject) {
36 this.fileObject = fileObject;
37 }
38
39 public Resource createRelative(String relativePath) throws IOException {
40 return new VfsResource(fileObject.resolveFile(relativePath,
41 NameScope.DESCENDENT_OR_SELF));
42 }
43
44 public boolean exists() {
45 try {
46 return fileObject.exists();
47 } catch (FileSystemException e) {
48 throw new SlcException("Cannot find out whether " + fileObject
49 + " exists", e);
50 }
51 }
52
53 public String getDescription() {
54 return "VFS resource " + fileObject;
55 }
56
57 public File getFile() throws IOException {
58 throw new IOException("Cannot access " + getDescription()
59 + " as a local file");
60
61
62
63
64
65 }
66
67 public String getFilename() {
68 return fileObject.getName().getBaseName();
69 }
70
71 public URI getURI() throws IOException {
72 try {
73 return new URI(fileObject.getName().getURI());
74 } catch (URISyntaxException e) {
75 throw new IOExceptionWithCause(e);
76 }
77 }
78
79 public URL getURL() throws IOException {
80 return fileObject.getURL();
81 }
82
83 public boolean isOpen() {
84 return fileObject.isContentOpen();
85 }
86
87 public boolean isReadable() {
88 try {
89 return fileObject.isReadable();
90 } catch (FileSystemException e) {
91 throw new SlcException("Cannot find out whether " + fileObject
92 + " is readable", e);
93 }
94 }
95
96 public long lastModified() throws IOException {
97 return fileObject.getContent().getLastModifiedTime();
98 }
99
100 public InputStream getInputStream() throws IOException {
101 return fileObject.getContent().getInputStream();
102 }
103
104 public FileObject getFileObject() {
105 return fileObject;
106 }
107
108 public long contentLength(){
109 return -1;
110 }
111 }