View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  		// TODO: access local files
61  		// if(fileObject instanceof LocalFile){
62  		// ((LocalFile)fileObject).
63  		// }
64  		// return null;
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 }