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.http;
18  
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.Collections;
22  
23  import org.apache.commons.httpclient.HttpClient;
24  import org.apache.commons.vfs2.Capability;
25  import org.apache.commons.vfs2.FileName;
26  import org.apache.commons.vfs2.FileSystem;
27  import org.apache.commons.vfs2.FileSystemConfigBuilder;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.apache.commons.vfs2.FileSystemOptions;
30  import org.apache.commons.vfs2.UserAuthenticationData;
31  import org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider;
32  import org.apache.commons.vfs2.provider.GenericFileName;
33  import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
34  
35  /**
36   * An HTTP provider that uses commons-httpclient.
37   */
38  public class HttpFileProvider extends AbstractOriginatingFileProvider {
39  
40      /** Authenticator information. */
41      public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
42              UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
43  
44      static final Collection<Capability> capabilities = Collections
45              .unmodifiableCollection(Arrays.asList(new Capability[] { Capability.GET_TYPE, Capability.READ_CONTENT,
46                      Capability.URI, Capability.GET_LAST_MODIFIED, Capability.ATTRIBUTES, Capability.RANDOM_ACCESS_READ,
47                      Capability.DIRECTORY_READ_CONTENT, }));
48  
49      /**
50       * Constructs a new provider.
51       */
52      public HttpFileProvider() {
53          super();
54          setFileNameParser(HttpFileNameParser.getInstance());
55      }
56  
57      /**
58       * Creates a {@link FileSystem}.
59       */
60      @Override
61      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
62              throws FileSystemException {
63          // Create the file system
64          final GenericFileName/../../org/apache/commons/vfs2/provider/GenericFileName.html#GenericFileName">GenericFileName rootName = (GenericFileName) name;
65  
66          UserAuthenticationData authData = null;
67          HttpClient httpClient;
68          try {
69              authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
70  
71              final String fileScheme = rootName.getScheme();
72              final char lastChar = fileScheme.charAt(fileScheme.length() - 1);
73              final String internalScheme = (lastChar == 's' || lastChar == 'S') ? "https" : "http";
74  
75              httpClient = HttpClientFactory.createConnection(internalScheme, rootName.getHostName(),
76                      rootName.getPort(),
77                      UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
78                              UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
79                      UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
80                              UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
81                      fileSystemOptions);
82          } finally {
83              UserAuthenticatorUtils.cleanup(authData);
84          }
85  
86          return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
87      }
88  
89      @Override
90      public FileSystemConfigBuilder getConfigBuilder() {
91          return HttpFileSystemConfigBuilder.getInstance();
92      }
93  
94      @Override
95      public Collection<Capability> getCapabilities() {
96          return capabilities;
97      }
98  }