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