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