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 */
017package org.apache.commons.vfs2.provider.webdav;
018
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.Collections;
022
023import org.apache.commons.httpclient.HttpClient;
024import org.apache.commons.vfs2.Capability;
025import org.apache.commons.vfs2.FileName;
026import org.apache.commons.vfs2.FileSystem;
027import org.apache.commons.vfs2.FileSystemConfigBuilder;
028import org.apache.commons.vfs2.FileSystemException;
029import org.apache.commons.vfs2.FileSystemOptions;
030import org.apache.commons.vfs2.UserAuthenticationData;
031import org.apache.commons.vfs2.provider.GenericFileName;
032import org.apache.commons.vfs2.provider.http.HttpClientFactory;
033import org.apache.commons.vfs2.provider.http.HttpFileProvider;
034import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
035
036/**
037 * A provider for WebDAV.
038 *
039 * @since 2.0
040 */
041public class WebdavFileProvider
042    extends HttpFileProvider
043{
044    
045    /**
046     * The authenticator types used by the WebDAV provider.
047     * 
048     * @deprecated Might be removed in the next major version.
049     */
050    @Deprecated
051    public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[]
052        {
053            UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD
054        };
055   
056    /** The capabilities of the WebDAV provider */
057    protected static final Collection<Capability> capabilities =
058            Collections.unmodifiableCollection(Arrays.asList(new Capability[]
059    {
060        Capability.CREATE,
061        Capability.DELETE,
062        Capability.RENAME,
063        Capability.GET_TYPE,
064        Capability.LIST_CHILDREN,
065        Capability.READ_CONTENT,
066        Capability.URI,
067        Capability.WRITE_CONTENT,
068        Capability.GET_LAST_MODIFIED,
069        Capability.ATTRIBUTES,
070        Capability.RANDOM_ACCESS_READ,
071        Capability.DIRECTORY_READ_CONTENT,
072    }));
073
074    public WebdavFileProvider()
075    {
076        super();
077
078        setFileNameParser(WebdavFileNameParser.getInstance());
079    }
080    /**
081     * Creates a {@link FileSystem}.
082     * <p>
083     * If you're looking at this method and wondering how to get a FileSystemOptions
084     * object bearing the proxy host and credentials configuration through
085     * to this method so it's used for resolving a
086     * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure
087     * to use correct signature of the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager}
088     * resolveFile method.
089     * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
090     */
091    @Override
092    protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
093        throws FileSystemException
094    {
095        // Create the file system
096        final GenericFileName rootName = (GenericFileName) name;
097        final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;
098
099        UserAuthenticationData authData = null;
100        HttpClient httpClient;
101        try
102        {
103            authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);
104
105            httpClient = HttpClientFactory.createConnection(
106                WebdavFileSystemConfigBuilder.getInstance(),
107                "http",
108                rootName.getHostName(),
109                rootName.getPort(),
110                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
111                        UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
112                UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
113                        UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
114                fsOpts);
115        }
116        finally
117        {
118            UserAuthenticatorUtils.cleanup(authData);
119        }
120
121        return new WebdavFileSystem(rootName, httpClient, fsOpts);
122    }
123
124    @Override
125    public FileSystemConfigBuilder getConfigBuilder()
126    {
127        return WebdavFileSystemConfigBuilder.getInstance();
128    }
129
130
131    @Override
132    public Collection<Capability> getCapabilities()
133    {
134        return capabilities;
135    }
136}