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 *
038 * @deprecated Use {@link org.apache.commons.vfs2.provider.http5}.
039 */
040@Deprecated
041public class HttpFileProvider extends AbstractOriginatingFileProvider {
042
043    /** Authenticator information. */
044    public static final UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = {
045            UserAuthenticationData.USERNAME, UserAuthenticationData.PASSWORD };
046
047    static final Collection<Capability> CAPABILITIES = Collections
048            .unmodifiableCollection(Arrays.asList(Capability.GET_TYPE, Capability.READ_CONTENT, Capability.URI, Capability.GET_LAST_MODIFIED,
049                    Capability.ATTRIBUTES, Capability.RANDOM_ACCESS_READ, Capability.DIRECTORY_READ_CONTENT));
050
051    /**
052     * Constructs a new provider.
053     */
054    public HttpFileProvider() {
055        setFileNameParser(HttpFileNameParser.getInstance());
056    }
057
058    /**
059     * Creates a {@link FileSystem}.
060     *
061     * @return a new FileSystem, never null.
062     */
063    @Override
064    protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions)
065            throws FileSystemException {
066        // Create the file system
067        final GenericFileName rootName = (GenericFileName) name;
068        UserAuthenticationData authData = null;
069        HttpClient httpClient;
070        try {
071            authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES);
072            final String fileScheme = rootName.getScheme();
073            final char lastChar = fileScheme.charAt(fileScheme.length() - 1);
074            final String internalScheme = lastChar == 's' || lastChar == 'S' ? "https" : "http";
075            httpClient = HttpClientFactory.createConnection(internalScheme, rootName.getHostName(),
076                    rootName.getPort(),
077                    UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
078                            UserAuthenticationData.USERNAME, UserAuthenticatorUtils.toChar(rootName.getUserName()))),
079                    UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData,
080                            UserAuthenticationData.PASSWORD, UserAuthenticatorUtils.toChar(rootName.getPassword()))),
081                    fileSystemOptions);
082        } finally {
083            UserAuthenticatorUtils.cleanup(authData);
084        }
085        return new HttpFileSystem(rootName, httpClient, fileSystemOptions);
086    }
087
088    @Override
089    public Collection<Capability> getCapabilities() {
090        return CAPABILITIES;
091    }
092
093    @Override
094    public FileSystemConfigBuilder getConfigBuilder() {
095        return HttpFileSystemConfigBuilder.getInstance();
096    }
097}