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.webdav4;
018
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.Collections;
022
023import org.apache.commons.vfs2.Capability;
024import org.apache.commons.vfs2.FileName;
025import org.apache.commons.vfs2.FileObject;
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.http4.Http4FileProvider;
033import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
034import org.apache.http.client.HttpClient;
035import org.apache.http.client.protocol.HttpClientContext;
036
037/**
038 * A provider for WebDAV based on HTTP4.
039 *
040 * @since 2.5.0
041 */
042public class Webdav4FileProvider extends Http4FileProvider {
043
044    /**
045     * The authenticator types used by the WebDAV provider.
046     *
047     * @deprecated Might be removed in the next major version.
048     */
049    @Deprecated
050    public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = {
051            UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
052
053    /** The capabilities of the WebDAV provider. */
054    public static final Collection<Capability> DEFAULT_CAPABILITIES =
055            Collections.unmodifiableCollection(
056                    Arrays.asList(
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
072    /** The capabilities of the WebDAV provider. */
073    protected static final Collection<Capability> capabilities = DEFAULT_CAPABILITIES;
074
075    /**
076     * Constructs a new instance.
077     */
078    public Webdav4FileProvider() {
079        setFileNameParser(Webdav4FileNameParser.getInstance());
080    }
081
082    /**
083     * Creates a {@link FileSystem}.
084     * <p>
085     * If you're looking at this method and wondering how to get a FileSystemOptions object bearing the proxy host and
086     * credentials configuration through to this method so it's used for resolving a
087     * {@link org.apache.commons.vfs2.FileObject FileObject} in the FileSystem, then be sure to use correct signature of
088     * the {@link org.apache.commons.vfs2.FileSystemManager FileSystemManager} resolveFile method.
089     *
090     * @see org.apache.commons.vfs2.impl.DefaultFileSystemManager#resolveFile(FileObject, String, FileSystemOptions)
091     */
092    @Override
093    protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
094            throws FileSystemException {
095        // Create the file system
096        final GenericFileName rootName = (GenericFileName) name;
097        // TODO: need to check null to create a non-null here???
098        final FileSystemOptions fsOpts = fileSystemOptions == null ? new FileSystemOptions() : fileSystemOptions;
099
100        UserAuthenticationData authData = null;
101        final HttpClient httpClient;
102        final HttpClientContext httpClientContext;
103
104        try {
105            final Webdav4FileSystemConfigBuilder builder = Webdav4FileSystemConfigBuilder.getInstance();
106            authData = UserAuthenticatorUtils.authenticate(fsOpts, AUTHENTICATOR_TYPES);
107            httpClientContext = createHttpClientContext(builder, rootName, fsOpts, authData);
108            httpClient = createHttpClient(builder, rootName, fsOpts);
109        } finally {
110            UserAuthenticatorUtils.cleanup(authData);
111        }
112
113        return new Webdav4FileSystem(rootName, fsOpts, httpClient, httpClientContext);
114    }
115
116    @Override
117    public Collection<Capability> getCapabilities() {
118        return capabilities;
119    }
120
121    @Override
122    public FileSystemConfigBuilder getConfigBuilder() {
123        return Webdav4FileSystemConfigBuilder.getInstance();
124    }
125}