001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.vfs2.provider.webdav;
018    
019    import java.util.Arrays;
020    import java.util.Collection;
021    import java.util.Collections;
022    
023    import org.apache.commons.httpclient.HttpClient;
024    import org.apache.commons.vfs2.Capability;
025    import org.apache.commons.vfs2.FileName;
026    import org.apache.commons.vfs2.FileObject;
027    import org.apache.commons.vfs2.FileSystem;
028    import org.apache.commons.vfs2.FileSystemConfigBuilder;
029    import org.apache.commons.vfs2.FileSystemException;
030    import org.apache.commons.vfs2.FileSystemManager;
031    import org.apache.commons.vfs2.FileSystemOptions;
032    import org.apache.commons.vfs2.UserAuthenticationData;
033    import org.apache.commons.vfs2.provider.GenericFileName;
034    import org.apache.commons.vfs2.provider.http.HttpClientFactory;
035    import org.apache.commons.vfs2.provider.http.HttpFileProvider;
036    import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
037    
038    /**
039     * A provider for WebDAV.
040     *
041     * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
042     * @since 2.0
043     */
044    public class WebdavFileProvider
045        extends HttpFileProvider
046    {
047        /** The authenticator types used by the WebDAV provider. */
048        public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
049            {
050                UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
051            };
052    
053        /** The capabilities of the WebDAV provider */
054        protected static final Collection<Capability> capabilities =
055                Collections.unmodifiableCollection(Arrays.asList(new Capability[]
056        {
057            Capability.CREATE,
058            Capability.DELETE,
059            Capability.RENAME,
060            Capability.GET_TYPE,
061            Capability.LIST_CHILDREN,
062            Capability.READ_CONTENT,
063            Capability.URI,
064            Capability.WRITE_CONTENT,
065            Capability.GET_LAST_MODIFIED,
066            Capability.ATTRIBUTES,
067            Capability.RANDOM_ACCESS_READ,
068            Capability.DIRECTORY_READ_CONTENT,
069        }));
070    
071        public WebdavFileProvider()
072        {
073            super();
074    
075            setFileNameParser(WebdavFileNameParser.getInstance());
076        }
077        /**
078         * Creates a {@link FileSystem}.  If you're looking at this method and wondering how to
079         * get a FileSystemOptions object bearing the proxy host and credentials configuration through
080         * to this method so it's used for resolving a {@link FileObject} in the FileSystem, then be sure
081         * to use correct signature of the {@link FileSystemManager} resolveFile method.
082         * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
083         */
084        @Override
085        protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
086            throws FileSystemException
087        {
088            // Create the file system
089            final GenericFileName rootName = (GenericFileName) name;
090            FileSystemOptions fsOpts = (fileSystemOptions == null) ? new FileSystemOptions() : fileSystemOptions;
091    
092            UserAuthenticationData authData = null;
093            HttpClient httpClient;
094            try
095            {
096                authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);
097    
098                httpClient = HttpClientFactory.createConnection(
099                    WebdavFileSystemConfigBuilder.getInstance(),
100                    "http",
101                    rootName.getHostName(),
102                    rootName.getPort(),
103                    UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
104                            UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
105                    UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
106                            UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
107                    fsOpts);
108            }
109            finally
110            {
111                UserAuthenticatorUtils.cleanup(authData);
112            }
113    
114            return new WebdavFileSystem(rootName, httpClient, fsOpts);
115        }
116    
117        @Override
118        public FileSystemConfigBuilder getConfigBuilder()
119        {
120            return WebdavFileSystemConfigBuilder.getInstance();
121        }
122    
123    
124        @Override
125        public Collection<Capability> getCapabilities()
126        {
127            return capabilities;
128        }
129    }