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.vfs.provider.url;
    18
    19  import org.apache.commons.vfs.Capability;
    20  import org.apache.commons.vfs.FileName;
    21  import org.apache.commons.vfs.FileObject;
    22  import org.apache.commons.vfs.FileSystem;
    23  import org.apache.commons.vfs.FileSystemConfigBuilder;
    24  import org.apache.commons.vfs.FileSystemException;
    25  import org.apache.commons.vfs.FileSystemOptions;
    26  import org.apache.commons.vfs.provider.AbstractFileProvider;
    27
    28  import java.net.MalformedURLException;
    29  import java.net.URL;
    30  import java.util.Arrays;
    31  import java.util.Collection;
    32  import java.util.Collections;
    33
    34  /***
    35   * A file provider backed by Java's URL API.
    36   *
    37   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    38   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    39   */
    40  public class UrlFileProvider
    41      extends AbstractFileProvider
    42  {
    43      protected final static Collection capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
    44      {
    45          Capability.READ_CONTENT,
    46          Capability.URI,
    47          Capability.GET_LAST_MODIFIED
    48      }));
    49
    50      public UrlFileProvider()
    51      {
    52          super();
    53          setFileNameParser(new UrlFileNameParser());
    54      }
    55
    56      /***
    57       * Locates a file object, by absolute URI.
    58       */
    59      public synchronized FileObject findFile(final FileObject baseFile,
    60                                              final String uri,
    61                                              final FileSystemOptions fileSystemOptions)
    62          throws FileSystemException
    63      {
    64          try
    65          {
    66              final URL url = new URL(uri);
    67
    68              URL rootUrl = new URL(url, "/");
    69              final String key = this.getClass().getName() + rootUrl.toString();
    70              FileSystem fs = findFileSystem(key, fileSystemOptions);
    71              if (fs == null)
    72              {
    73                  String extForm = rootUrl.toExternalForm();
    74                  final FileName rootName =
    75                      getContext().parseURI(extForm);
    76                  // final FileName rootName =
    77                  //    new BasicFileName(rootUrl, FileName.ROOT_PATH);
    78                  fs = new UrlFileSystem(rootName, fileSystemOptions);
    79                  addFileSystem(key, fs);
    80              }
    81              return fs.resolveFile(url.getPath());
    82          }
    83          catch (final MalformedURLException e)
    84          {
    85              throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri, e);
    86          }
    87      }
    88
    89      public FileSystemConfigBuilder getConfigBuilder()
    90      {
    91          return org.apache.commons.vfs.provider.res.ResourceFileSystemConfigBuilder.getInstance();
    92      }
    93
    94      public Collection getCapabilities()
    95      {
    96          return capabilities;
    97      }
    98  }