01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.commons.vfs.provider.url;
18:
19: import org.apache.commons.vfs.Capability;
20: import org.apache.commons.vfs.FileName;
21: import org.apache.commons.vfs.FileObject;
22: import org.apache.commons.vfs.FileSystem;
23: import org.apache.commons.vfs.FileSystemConfigBuilder;
24: import org.apache.commons.vfs.FileSystemException;
25: import org.apache.commons.vfs.FileSystemOptions;
26: import org.apache.commons.vfs.provider.AbstractFileProvider;
27:
28: import java.net.MalformedURLException;
29: import java.net.URL;
30: import java.util.Arrays;
31: import java.util.Collection;
32: import java.util.Collections;
33:
34: /**
35: * A file provider backed by Java's URL API.
36: *
37: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
38: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
39: */
40: public class UrlFileProvider extends AbstractFileProvider {
41: protected final static Collection capabilities = Collections
42: .unmodifiableCollection(Arrays.asList(new Capability[] {
43: Capability.READ_CONTENT, Capability.URI,
44: Capability.GET_LAST_MODIFIED }));
45:
46: public UrlFileProvider() {
47: super ();
48: setFileNameParser(new UrlFileNameParser());
49: }
50:
51: /**
52: * Locates a file object, by absolute URI.
53: */
54: public synchronized FileObject findFile(final FileObject baseFile,
55: final String uri, final FileSystemOptions fileSystemOptions)
56: throws FileSystemException {
57: try {
58: final URL url = new URL(uri);
59:
60: URL rootUrl = new URL(url, "/");
61: final String key = this .getClass().getName()
62: + rootUrl.toString();
63: FileSystem fs = findFileSystem(key, fileSystemOptions);
64: if (fs == null) {
65: String extForm = rootUrl.toExternalForm();
66: final FileName rootName = getContext()
67: .parseURI(extForm);
68: // final FileName rootName =
69: // new BasicFileName(rootUrl, FileName.ROOT_PATH);
70: fs = new UrlFileSystem(rootName, fileSystemOptions);
71: addFileSystem(key, fs);
72: }
73: return fs.resolveFile(url.getPath());
74: } catch (final MalformedURLException e) {
75: throw new FileSystemException(
76: "vfs.provider.url/badly-formed-uri.error", uri, e);
77: }
78: }
79:
80: public FileSystemConfigBuilder getConfigBuilder() {
81: return org.apache.commons.vfs.provider.res.ResourceFileSystemConfigBuilder
82: .getInstance();
83: }
84:
85: public Collection getCapabilities() {
86: return capabilities;
87: }
88: }
|