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