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.http;
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.AbstractOriginatingFileProvider;
032import org.apache.commons.vfs2.provider.GenericFileName;
033import org.apache.commons.vfs2.util.UserAuthenticatorUtils;
034
035/**
036 * An HTTP provider that uses commons-httpclient.
037 */
038public class HttpFileProvider extends AbstractOriginatingFileProvider {
039    /** Authenticator information. */
040    public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
041            UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
042
043    static final Collection<Capability> capabilities = Collections
044            .unmodifiableCollection(Arrays.asList(new Capability[] { Capability.GET_TYPE, Capability.READ_CONTENT,
045                    Capability.URI, Capability.GET_LAST_MODIFIED, Capability.ATTRIBUTES, Capability.RANDOM_ACCESS_READ,
046                    Capability.DIRECTORY_READ_CONTENT, }));
047
048    /**
049     * Constructs a new provider.
050     */
051    public HttpFileProvider() {
052        super();
053        setFileNameParser(HttpFileNameParser.getInstance());
054    }
055
056    /**
057     * Creates a {@link FileSystem}.
058     */
059    @Override
060    protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
061            throws FileSystemException {
062        // Create the file system
063        final GenericFileName rootName = (GenericFileName) name;
064
065        UserAuthenticationData authData = null;
066        HttpClient httpClient;
067        try {
068            authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
069
070            httpClient = HttpClientFactory.createConnection(rootName.getScheme(), rootName.getHostName(),
071                    rootName.getPort(),
072                    UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
073                            UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
074                    UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
075                            UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
076                    fileSystemOptions);
077        } finally {
078            UserAuthenticatorUtils.cleanup(authData);
079        }
080
081        return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
082    }
083
084    @Override
085    public FileSystemConfigBuilder getConfigBuilder() {
086        return HttpFileSystemConfigBuilder.getInstance();
087    }
088
089    @Override
090    public Collection<Capability> getCapabilities() {
091        return capabilities;
092    }
093}