001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.vfs2.provider.url;
018
019 import java.net.MalformedURLException;
020 import java.net.URL;
021 import java.util.Arrays;
022 import java.util.Collection;
023 import java.util.Collections;
024
025 import org.apache.commons.vfs2.Capability;
026 import org.apache.commons.vfs2.FileName;
027 import org.apache.commons.vfs2.FileObject;
028 import org.apache.commons.vfs2.FileSystem;
029 import org.apache.commons.vfs2.FileSystemConfigBuilder;
030 import org.apache.commons.vfs2.FileSystemException;
031 import org.apache.commons.vfs2.FileSystemOptions;
032 import org.apache.commons.vfs2.provider.AbstractFileProvider;
033
034 /**
035 * A file provider backed by Java's URL API.
036 *
037 * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
038 */
039 public class UrlFileProvider
040 extends AbstractFileProvider
041 {
042 /** The provider's capabilities */
043 protected static final Collection<Capability> capabilities =
044 Collections.unmodifiableCollection(Arrays.asList(new Capability[]
045 {
046 Capability.READ_CONTENT,
047 Capability.URI,
048 Capability.GET_LAST_MODIFIED
049 }));
050
051 public UrlFileProvider()
052 {
053 super();
054 setFileNameParser(new UrlFileNameParser());
055 }
056
057 /**
058 * Locates a file object, by absolute URI.
059 * @param baseFile The base FileObject.
060 * @param uri The uri of the file to locate.
061 * @param fileSystemOptions The FileSystemOptions
062 * @return The FileObject
063 * @throws FileSystemException if an error occurs.
064 */
065 public synchronized FileObject findFile(final FileObject baseFile,
066 final String uri,
067 final FileSystemOptions fileSystemOptions)
068 throws FileSystemException
069 {
070 try
071 {
072 final URL url = new URL(uri);
073
074 URL rootUrl = new URL(url, "/");
075 final String key = this.getClass().getName() + rootUrl.toString();
076 FileSystem fs = findFileSystem(key, fileSystemOptions);
077 if (fs == null)
078 {
079 String extForm = rootUrl.toExternalForm();
080 final FileName rootName =
081 getContext().parseURI(extForm);
082 // final FileName rootName =
083 // new BasicFileName(rootUrl, FileName.ROOT_PATH);
084 fs = new UrlFileSystem(rootName, fileSystemOptions);
085 addFileSystem(key, fs);
086 }
087 return fs.resolveFile(url.getPath());
088 }
089 catch (final MalformedURLException e)
090 {
091 throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri, e);
092 }
093 }
094
095 @Override
096 public FileSystemConfigBuilder getConfigBuilder()
097 {
098 return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
099 }
100
101 public Collection<Capability> getCapabilities()
102 {
103 return capabilities;
104 }
105 }