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      public UrlFileProvider() {
43          setFileNameParser(new UrlFileNameParser());
44      }
45  
46      /**
47       * Locates a file object, by absolute URI.
48       *
49       * @param baseFile The base FileObject.
50       * @param fileUri The uri of the file to locate.
51       * @param fileSystemOptions The FileSystemOptions
52       * @return The FileObject
53       * @throws FileSystemException if an error occurs.
54       */
55      @Override
56      public synchronized FileObjectObject.html#FileObject">FileObject findFile(final FileObject baseFile, final String fileUri,
57              final FileSystemOptions fileSystemOptions) throws FileSystemException {
58          try {
59              final URI uri = URI.create(fileUri);
60              final URI rootUri = uri.resolve("/");
61              final String key = this.getClass().getName() + rootUri.toString();
62              FileSystem fs = findFileSystem(key, fileSystemOptions);
63              if (fs == null) {
64                  final String extForm = rootUri.toString();
65                  final FileName rootName = getContext().parseURI(extForm);
66                  // final FileName rootName =
67                  // new BasicFileName(rootUrl, FileName.ROOT_PATH);
68                  fs = new UrlFileSystem(rootName, fileSystemOptions);
69                  addFileSystem(key, fs);
70              }
71              return fs.resolveFile(uri.getPath());
72          } catch (final Exception e) {
73              throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", fileUri, e);
74          }
75      }
76  
77      @Override
78      public FileSystemConfigBuilder getConfigBuilder() {
79          return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
80      }
81  
82      @Override
83      public Collection<Capability> getCapabilities() {
84          return capabilities;
85      }
86  }