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.webdav;
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.GenericFileName;
32  import org.apache.commons.vfs2.provider.http.HttpClientFactory;
33  import org.apache.commons.vfs2.provider.http.HttpFileProvider;
34  import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
35  
36  /**
37   * A provider for WebDAV.
38   *
39   * @since 2.0
40   */
41  public class WebdavFileProvider extends HttpFileProvider {
42  
43      /**
44       * The authenticator types used by the WebDAV provider.
45       *
46       * @deprecated Might be removed in the next major version.
47       */
48      @Deprecated
49      public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
50              UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
51  
52      /** The capabilities of the WebDAV provider */
53      protected static final Collection<Capability> capabilities = Collections
54              .unmodifiableCollection(Arrays.asList(new Capability[] { Capability.CREATE, Capability.DELETE,
55                      Capability.RENAME, Capability.GET_TYPE, Capability.LIST_CHILDREN, Capability.READ_CONTENT,
56                      Capability.URI, Capability.WRITE_CONTENT, Capability.GET_LAST_MODIFIED, Capability.ATTRIBUTES,
57                      Capability.RANDOM_ACCESS_READ, Capability.DIRECTORY_READ_CONTENT, }));
58  
59      public WebdavFileProvider() {
60          super();
61  
62          setFileNameParser(WebdavFileNameParser.getInstance());
63      }
64  
65      /**
66       * Creates a {@link FileSystem}.
67       * <p>
68       * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
69       * credentials configuration through to this method so it's used for resolving a
70       * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
71       * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
72       * </p>
73       *
74       * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
75       */
76      @Override
77      protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
78              throws FileSystemException {
79          // Create the file system
80          final GenericFileName/../../org/apache/commons/vfs2/provider/GenericFileName.html#GenericFileName">GenericFileName rootName = (GenericFileName) name;
81          final FileSystemOptioFileSystemOptionsileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;
82  
83          UserAuthenticationData authData = null;
84          HttpClient httpClient;
85          try {
86              authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);
87  
88              httpClient = HttpClientFactory.createConnection(WebdavFileSystemConfigBuilder.getInstance(), "http",
89                      rootName.getHostName(), rootName.getPort(),
90                      UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
91                              UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
92                      UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
93                              UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
94                      fsOpts);
95          } finally {
96              UserAuthenticatorUtils.cleanup(authData);
97          }
98  
99          return new WebdavFileSystem(rootName, httpClient, fsOpts);
100     }
101 
102     @Override
103     public FileSystemConfigBuilder getConfigBuilder() {
104         return WebdavFileSystemConfigBuilder.getInstance();
105     }
106 
107     @Override
108     public Collection<Capability> getCapabilities() {
109         return capabilities;
110     }
111 }