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 */
017package org.apache.commons.vfs2.provider.url;
018
019import java.net.MalformedURLException;
020import java.net.URL;
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.Collections;
024
025import org.apache.commons.vfs2.Capability;
026import org.apache.commons.vfs2.FileName;
027import org.apache.commons.vfs2.FileObject;
028import org.apache.commons.vfs2.FileSystem;
029import org.apache.commons.vfs2.FileSystemConfigBuilder;
030import org.apache.commons.vfs2.FileSystemException;
031import org.apache.commons.vfs2.FileSystemOptions;
032import org.apache.commons.vfs2.provider.AbstractFileProvider;
033
034/**
035 * A file provider backed by Java's URL API.
036 */
037public class UrlFileProvider
038    extends AbstractFileProvider
039{
040    /** The provider's capabilities */
041    protected static final Collection<Capability> capabilities =
042        Collections.unmodifiableCollection(Arrays.asList(new Capability[]
043    {
044        Capability.READ_CONTENT,
045        Capability.URI,
046        Capability.GET_LAST_MODIFIED
047    }));
048
049    public UrlFileProvider()
050    {
051        super();
052        setFileNameParser(new UrlFileNameParser());
053    }
054
055    /**
056     * Locates a file object, by absolute URI.
057     * @param baseFile The base FileObject.
058     * @param uri The uri of the file to locate.
059     * @param fileSystemOptions The FileSystemOptions
060     * @return The FileObject
061     * @throws FileSystemException if an error occurs.
062     */
063    @Override
064    public synchronized FileObject findFile(final FileObject baseFile,
065                                            final String uri,
066                                            final FileSystemOptions fileSystemOptions)
067        throws FileSystemException
068    {
069        try
070        {
071            final URL url = new URL(uri);
072
073            final URL rootUrl = new URL(url, "/");
074            final String key = this.getClass().getName() + rootUrl.toString();
075            FileSystem fs = findFileSystem(key, fileSystemOptions);
076            if (fs == null)
077            {
078                final String extForm = rootUrl.toExternalForm();
079                final FileName rootName =
080                    getContext().parseURI(extForm);
081                // final FileName rootName =
082                //    new BasicFileName(rootUrl, FileName.ROOT_PATH);
083                fs = new UrlFileSystem(rootName, fileSystemOptions);
084                addFileSystem(key, fs);
085            }
086            return fs.resolveFile(url.getPath());
087        }
088        catch (final MalformedURLException e)
089        {
090            throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri, e);
091        }
092    }
093
094    @Override
095    public FileSystemConfigBuilder getConfigBuilder()
096    {
097        return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
098    }
099
100    @Override
101    public Collection<Capability> getCapabilities()
102    {
103        return capabilities;
104    }
105}