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.webdav4s;
018
019import java.util.Collection;
020
021import org.apache.commons.vfs2.Capability;
022import org.apache.commons.vfs2.FileName;
023import org.apache.commons.vfs2.FileObject;
024import org.apache.commons.vfs2.FileSystem;
025import org.apache.commons.vfs2.FileSystemConfigBuilder;
026import org.apache.commons.vfs2.FileSystemException;
027import org.apache.commons.vfs2.FileSystemOptions;
028import org.apache.commons.vfs2.UserAuthenticationData;
029import org.apache.commons.vfs2.provider.GenericFileName;
030import org.apache.commons.vfs2.provider.http4s.Http4sFileProvider;
031import org.apache.commons.vfs2.provider.webdav4.Webdav4FileProvider;
032import org.apache.commons.vfs2.provider.webdav4.Webdav4FileSystem;
033import org.apache.commons.vfs2.provider.webdav4.Webdav4FileSystemConfigBuilder;
034import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
035import org.apache.http.client.HttpClient;
036import org.apache.http.client.protocol.HttpClientContext;
037
038/**
039 * A provider for WebDAV based on HTTP4S.
040 *
041 * @since 2.5.0
042 */
043public class Webdav4sFileProvider extends Http4sFileProvider {
044
045    /** The capabilities of the WebDAV provider. */
046    protected static final Collection<Capability> capabilities = Webdav4FileProvider.DEFAULT_CAPABILITIES;
047
048    /**
049     * Constructs a new instance.
050     */
051    public Webdav4sFileProvider() {
052        setFileNameParser(Webdav4sFileNameParser.getInstance());
053    }
054
055    /**
056     * Creates a {@link FileSystem}.
057     * <p>
058     * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
059     * credentials configuration through to this method so it's used for resolving a
060     * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
061     * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
062     * </p>
063     *
064     * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
065     */
066    @Override
067    protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
068            throws FileSystemException {
069        // Create the file system
070        final GenericFileName rootName = (GenericFileName) name;
071        // TODO: need to check null to create a non-null here???
072        final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;
073
074        UserAuthenticationData authData = null;
075        final HttpClient httpClient;
076        final HttpClientContext httpClientContext;
077
078        try {
079            final Webdav4FileSystemConfigBuilder builder = Webdav4FileSystemConfigBuilder.getInstance();
080            authData = UserAuthenticatorUtils.authenticate(fsOpts, Webdav4FileProvider.AUTHENTICATOR_TYPES);
081            httpClientContext = createHttpClientContext(builder, rootName, fsOpts, authData);
082            httpClient = createHttpClient(builder, rootName, fsOpts);
083        } finally {
084            UserAuthenticatorUtils.cleanup(authData);
085        }
086
087        return new Webdav4FileSystem(rootName, fsOpts, httpClient, httpClientContext) {
088        };
089    }
090
091    @Override
092    public Collection<Capability> getCapabilities() {
093        return capabilities;
094    }
095
096    @Override
097    public FileSystemConfigBuilder getConfigBuilder() {
098        return Webdav4FileSystemConfigBuilder.getInstance();
099    }
100
101}