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      /**
49       * Constructs a new instance.
50       */
51      public Webdav4sFileProvider() {
52          setFileNameParser(Webdav4sFileNameParser.getInstance());
53      }
54  
55      /**
56       * Creates a {@link FileSystem}.
57       * <p>
58       * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
59       * credentials configuration through to this method so it's used for resolving a
60       * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
61       * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
62       * </p>
63       *
64       * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
65       */
66      @Override
67      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
68              throws FileSystemException {
69          // Create the file system
70          final GenericFileName rootName = (GenericFileName) name;
71          // TODO: need to check null to create a non-null here???
72          final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;
73  
74          UserAuthenticationData authData = null;
75          final HttpClient httpClient;
76          final HttpClientContext httpClientContext;
77  
78          try {
79              final Webdav4FileSystemConfigBuilder builder = Webdav4FileSystemConfigBuilder.getInstance();
80              authData = UserAuthenticatorUtils.authenticate(fsOpts, Webdav4FileProvider.AUTHENTICATOR_TYPES);
81              httpClientContext = createHttpClientContext(builder, rootName, fsOpts, authData);
82              httpClient = createHttpClient(builder, rootName, fsOpts);
83          } finally {
84              UserAuthenticatorUtils.cleanup(authData);
85          }
86  
87          return new Webdav4FileSystem(rootName, fsOpts, httpClient, httpClientContext) {
88          };
89      }
90  
91      @Override
92      public Collection<Capability> getCapabilities() {
93          return capabilities;
94      }
95  
96      @Override
97      public FileSystemConfigBuilder getConfigBuilder() {
98          return Webdav4FileSystemConfigBuilder.getInstance();
99      }
100 
101 }