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.webdav4;
18  
19  import java.util.Arrays;
20  import java.util.Collection;
21  import java.util.Collections;
22  
23  import org.apache.commons.vfs2.Capability;
24  import org.apache.commons.vfs2.FileName;
25  import org.apache.commons.vfs2.FileObject;
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.GenericFileName;
32  import org.apache.commons.vfs2.provider.http4.Http4FileProvider;
33  import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
34  import org.apache.http.client.HttpClient;
35  import org.apache.http.client.protocol.HttpClientContext;
36  
37  /**
38   * A provider for WebDAV based on HTTP4.
39   *
40   * @since 2.5.0
41   */
42  public class Webdav4FileProvider extends Http4FileProvider {
43  
44      /**
45       * The authenticator types used by the WebDAV provider.
46       *
47       * @deprecated Might be removed in the next major version.
48       */
49      @Deprecated
50      public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = {
51              UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
52  
53      /** The capabilities of the WebDAV provider. */
54      public static final Collection<Capability> DEFAULT_CAPABILITIES =
55              Collections.unmodifiableCollection(
56                      Arrays.asList(
57                              Capability.CREATE,
58                              Capability.DELETE,
59                              Capability.RENAME,
60                              Capability.GET_TYPE,
61                              Capability.LIST_CHILDREN,
62                              Capability.READ_CONTENT,
63                              Capability.URI,
64                              Capability.WRITE_CONTENT,
65                              Capability.GET_LAST_MODIFIED,
66                              Capability.ATTRIBUTES,
67                              Capability.RANDOM_ACCESS_READ,
68                              Capability.DIRECTORY_READ_CONTENT
69                              )
70                      );
71  
72      /** The capabilities of the WebDAV provider. */
73      protected static final Collection<Capability> capabilities = DEFAULT_CAPABILITIES;
74  
75      /**
76       * Constructs a new instance.
77       */
78      public Webdav4FileProvider() {
79          setFileNameParser(Webdav4FileNameParser.getInstance());
80      }
81  
82      /**
83       * Creates a {@link FileSystem}.
84       * <p>
85       * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
86       * credentials configuration through to this method so it's used for resolving a
87       * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
88       * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
89       *
90       * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
91       */
92      @Override
93      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
94              throws FileSystemException {
95          // Create the file system
96          final GenericFileName rootName = (GenericFileName) name;
97          // TODO: need to check null to create a non-null here???
98          final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;
99  
100         UserAuthenticationData authData = null;
101         final HttpClient httpClient;
102         final HttpClientContext httpClientContext;
103 
104         try {
105             final Webdav4FileSystemConfigBuilder builder = Webdav4FileSystemConfigBuilder.getInstance();
106             authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);
107             httpClientContext = createHttpClientContext(builder, rootName, fsOpts, authData);
108             httpClient = createHttpClient(builder, rootName, fsOpts);
109         } finally {
110             UserAuthenticatorUtils.cleanup(authData);
111         }
112 
113         return new Webdav4FileSystem(rootName, fsOpts, httpClient, httpClientContext);
114     }
115 
116     @Override
117     public Collection<Capability> getCapabilities() {
118         return capabilities;
119     }
120 
121     @Override
122     public FileSystemConfigBuilder getConfigBuilder() {
123         return Webdav4FileSystemConfigBuilder.getInstance();
124     }
125 }