View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      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.vfs2.provider.url;
18  
19  import java.net.URI;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.Collections;
23  
24  import org.apache.commons.vfs2.Capability;
25  import org.apache.commons.vfs2.FileName;
26  import org.apache.commons.vfs2.FileObject;
27  import org.apache.commons.vfs2.FileSystem;
28  import org.apache.commons.vfs2.FileSystemConfigBuilder;
29  import org.apache.commons.vfs2.FileSystemException;
30  import org.apache.commons.vfs2.FileSystemOptions;
31  import org.apache.commons.vfs2.provider.AbstractFileProvider;
32  
33  /**
34   * A file provider backed by Java's URL API.
35   */
36  public class UrlFileProvider extends AbstractFileProvider {
37  
38      /** The provider's capabilities */
39      protected static final Collection<Capability> capabilities = Collections.unmodifiableCollection(
40              Arrays.asList(Capability.READ_CONTENT, Capability.URI, Capability.GET_LAST_MODIFIED));
41  
42      /**
43       * Constructs a new instance.
44       */
45      public UrlFileProvider() {
46          setFileNameParser(new UrlFileNameParser());
47      }
48  
49      /**
50       * Locates a file object, by absolute URI.
51       *
52       * @param baseFile The base FileObject.
53       * @param fileUri The uri of the file to locate.
54       * @param fileSystemOptions The FileSystemOptions
55       * @return The FileObject
56       * @throws FileSystemException if an error occurs.
57       */
58      @Override
59      public synchronized FileObject findFile(final FileObject baseFile, final String fileUri,
60              final FileSystemOptions fileSystemOptions) throws FileSystemException {
61          try {
62              final URI uri = URI.create(fileUri);
63              final URI rootUri = uri.resolve("/");
64              final String key = this.getClass().getName() + rootUri;
65              FileSystem fs = findFileSystem(key, fileSystemOptions);
66              if (fs == null) {
67                  final String extForm = rootUri.toString();
68                  final FileName rootName = getContext().parseURI(extForm);
69                  // final FileName rootName =
70                  // new BasicFileName(rootUrl, FileName.ROOT_PATH);
71                  fs = new UrlFileSystem(rootName, fileSystemOptions);
72                  addFileSystem(key, fs);
73              }
74              return fs.resolveFile(uri.getPath());
75          } catch (final Exception e) {
76              throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", fileUri, e);
77          }
78      }
79  
80      @Override
81      public Collection<Capability> getCapabilities() {
82          return capabilities;
83      }
84  
85      @Override
86      public FileSystemConfigBuilder getConfigBuilder() {
87          return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
88      }
89  }