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.http;
    18
    19  import org.apache.commons.httpclient.HttpClient;
    20  import org.apache.commons.vfs.Capability;
    21  import org.apache.commons.vfs.FileName;
    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.UserAuthenticationData;
    27  import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
    28  import org.apache.commons.vfs.provider.GenericFileName;
    29  import org.apache.commons.vfs.util.UserAuthenticatorUtils;
    30
    31  import java.util.Arrays;
    32  import java.util.Collection;
    33  import java.util.Collections;
    34
    35
    36  /***
    37   * An HTTP provider that uses commons-httpclient.
    38   *
    39   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    40   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    41   */
    42  public class HttpFileProvider
    43      extends AbstractOriginatingFileProvider
    44  {
    45      final static Collection capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
    46      {
    47          Capability.GET_TYPE,
    48          Capability.READ_CONTENT,
    49          Capability.URI,
    50          Capability.GET_LAST_MODIFIED,
    51          Capability.ATTRIBUTES,
    52          Capability.RANDOM_ACCESS_READ
    53      }));
    54
    55  	public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
    56  		{
    57  			UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
    58  		};
    59
    60  	public HttpFileProvider()
    61      {
    62          super();
    63          setFileNameParser(HttpFileNameParser.getInstance());
    64      }
    65
    66      /***
    67       * Creates a {@link FileSystem}.
    68       */
    69      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
    70          throws FileSystemException
    71      {
    72          // Create the file system
    73          final GenericFileName rootName = (GenericFileName) name;
    74
    75  		UserAuthenticationData authData = null;
    76  		HttpClient httpClient;
    77  		try
    78  		{
    79  			authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
    80
    81  			httpClient = HttpClientFactory.createConnection(
    82                  rootName.getScheme(),
    83                  rootName.getHostName(),
    84  				rootName.getPort(),
    85  				UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
    86  				UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
    87  				fileSystemOptions);
    88  		}
    89  		finally
    90  		{
    91  			UserAuthenticatorUtils.cleanup(authData);
    92  		}
    93
    94  		return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
    95      }
    96
    97      public FileSystemConfigBuilder getConfigBuilder()
    98      {
    99          return HttpFileSystemConfigBuilder.getInstance();
    100     }
    101
    102     public Collection getCapabilities()
    103     {
    104         return capabilities;
    105     }
    106 }