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