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.webdav4s;
18  
19  import java.util.Collection;
20  
21  import org.apache.commons.vfs2.Capability;
22  import org.apache.commons.vfs2.FileName;
23  import org.apache.commons.vfs2.FileObject;
24  import org.apache.commons.vfs2.FileSystem;
25  import org.apache.commons.vfs2.FileSystemConfigBuilder;
26  import org.apache.commons.vfs2.FileSystemException;
27  import org.apache.commons.vfs2.FileSystemOptions;
28  import org.apache.commons.vfs2.UserAuthenticationData;
29  import org.apache.commons.vfs2.provider.GenericFileName;
30  import org.apache.commons.vfs2.provider.http4s.Http4sFileProvider;
31  import org.apache.commons.vfs2.provider.webdav4.Webdav4FileProvider;
32  import org.apache.commons.vfs2.provider.webdav4.Webdav4FileSystem;
33  import org.apache.commons.vfs2.provider.webdav4.Webdav4FileSystemConfigBuilder;
34  import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
35  import org.apache.http.client.HttpClient;
36  import org.apache.http.client.protocol.HttpClientContext;
37  
38  /**
39   * A provider for WebDAV based on HTTP4S.
40   *
41   * @since 2.5.0
42   */
43  public class Webdav4sFileProvider extends Http4sFileProvider {
44  
45      /** The capabilities of the WebDAV provider */
46      protected static final Collection<Capability> capabilities = Webdav4FileProvider.DEFAULT_CAPABILITIES;
47  
48      public Webdav4sFileProvider() {
49          setFileNameParser(Webdav4sFileNameParser.getInstance());
50      }
51  
52      /**
53       * Creates a {@link FileSystem}.
54       * <p>
55       * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
56       * credentials configuration through to this method so it's used for resolving a
57       * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
58       * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
59       *
60       * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
61       */
62      @Override
63      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
64              throws FileSystemException {
65          // Create the file system
66          final GenericFileName/../../org/apache/commons/vfs2/provider/GenericFileName.html#GenericFileName">GenericFileName rootName = (GenericFileName) name;
67          // TODO: need to check null to create a non-null here???
68          final FileSystemOptioFileSystemOptionsileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;
69  
70          UserAuthenticationData authData = null;
71          final HttpClient httpClient;
72          final HttpClientContext httpClientContext;
73  
74          try {
75              final Webdav4FileSystemConfigBuilder builder = Webdav4FileSystemConfigBuilder.getInstance();
76              authData = UserAuthenticatorUtils.authenticate(fsOpts, Webdav4FileProvider.AUTHENTICATOR_TYPES);
77              httpClientContext = createHttpClientContext(builder, rootName, fsOpts, authData);
78              httpClient = createHttpClient(builder, rootName, fsOpts);
79          } finally {
80              UserAuthenticatorUtils.cleanup(authData);
81          }
82  
83          return new Webdav4FileSystem(rootName, fsOpts, httpClient, httpClientContext) {
84          };
85      }
86  
87      @Override
88      public FileSystemConfigBuilder getConfigBuilder() {
89          return Webdav4FileSystemConfigBuilder.getInstance();
90      }
91  
92      @Override
93      public Collection<Capability> getCapabilities() {
94          return capabilities;
95      }
96  
97  }