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 extends AbstractFileProvider {
038    /** The provider's capabilities */
039    protected static final Collection<Capability> capabilities = Collections.unmodifiableCollection(
040            Arrays.asList(new Capability[] { Capability.READ_CONTENT, Capability.URI, Capability.GET_LAST_MODIFIED }));
041
042    public UrlFileProvider() {
043        super();
044        setFileNameParser(new UrlFileNameParser());
045    }
046
047    /**
048     * Locates a file object, by absolute URI.
049     *
050     * @param baseFile The base FileObject.
051     * @param uri The uri of the file to locate.
052     * @param fileSystemOptions The FileSystemOptions
053     * @return The FileObject
054     * @throws FileSystemException if an error occurs.
055     */
056    @Override
057    public synchronized FileObject findFile(final FileObject baseFile, final String uri,
058            final FileSystemOptions fileSystemOptions) throws FileSystemException {
059        try {
060            final URL url = new URL(uri);
061
062            final URL rootUrl = new URL(url, "/");
063            final String key = this.getClass().getName() + rootUrl.toString();
064            FileSystem fs = findFileSystem(key, fileSystemOptions);
065            if (fs == null) {
066                final String extForm = rootUrl.toExternalForm();
067                final FileName rootName = getContext().parseURI(extForm);
068                // final FileName rootName =
069                // new BasicFileName(rootUrl, FileName.ROOT_PATH);
070                fs = new UrlFileSystem(rootName, fileSystemOptions);
071                addFileSystem(key, fs);
072            }
073            return fs.resolveFile(url.getPath());
074        } catch (final MalformedURLException e) {
075            throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri, e);
076        }
077    }
078
079    @Override
080    public FileSystemConfigBuilder getConfigBuilder() {
081        return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
082    }
083
084    @Override
085    public Collection<Capability> getCapabilities() {
086        return capabilities;
087    }
088}