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: */
017: package org.apache.commons.vfs.provider.http;
018:
019: import org.apache.commons.httpclient.HttpClient;
020: import org.apache.commons.vfs.Capability;
021: import org.apache.commons.vfs.FileName;
022: import org.apache.commons.vfs.FileSystem;
023: import org.apache.commons.vfs.FileSystemConfigBuilder;
024: import org.apache.commons.vfs.FileSystemException;
025: import org.apache.commons.vfs.FileSystemOptions;
026: import org.apache.commons.vfs.UserAuthenticationData;
027: import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
028: import org.apache.commons.vfs.provider.GenericFileName;
029: import org.apache.commons.vfs.util.UserAuthenticatorUtils;
030:
031: import java.util.Arrays;
032: import java.util.Collection;
033: import java.util.Collections;
034:
035: /**
036: * An HTTP provider that uses commons-httpclient.
037: *
038: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
039: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
040: */
041: public class HttpFileProvider extends AbstractOriginatingFileProvider {
042: final static Collection capabilities = Collections
043: .unmodifiableCollection(Arrays.asList(new Capability[] {
044: Capability.GET_TYPE, Capability.READ_CONTENT,
045: Capability.URI, Capability.GET_LAST_MODIFIED,
046: Capability.ATTRIBUTES,
047: Capability.RANDOM_ACCESS_READ }));
048:
049: public final static UserAuthenticationData.Type[] AUTHENTICATOR_TYPES = new UserAuthenticationData.Type[] {
050: UserAuthenticationData.USERNAME,
051: UserAuthenticationData.PASSWORD };
052:
053: public HttpFileProvider() {
054: super ();
055: setFileNameParser(HttpFileNameParser.getInstance());
056: }
057:
058: /**
059: * Creates a {@link FileSystem}.
060: */
061: protected FileSystem doCreateFileSystem(final FileName name,
062: final FileSystemOptions fileSystemOptions)
063: throws FileSystemException {
064: // Create the file system
065: final GenericFileName rootName = (GenericFileName) name;
066:
067: UserAuthenticationData authData = null;
068: HttpClient httpClient;
069: try {
070: authData = UserAuthenticatorUtils.authenticate(
071: fileSystemOptions, AUTHENTICATOR_TYPES);
072:
073: httpClient = HttpClientFactory.createConnection(rootName
074: .getScheme(), rootName.getHostName(), rootName
075: .getPort(), UserAuthenticatorUtils
076: .toString(UserAuthenticatorUtils.getData(authData,
077: UserAuthenticationData.USERNAME,
078: UserAuthenticatorUtils.toChar(rootName
079: .getUserName()))),
080: UserAuthenticatorUtils
081: .toString(UserAuthenticatorUtils.getData(
082: authData,
083: UserAuthenticationData.PASSWORD,
084: UserAuthenticatorUtils
085: .toChar(rootName
086: .getPassword()))),
087: fileSystemOptions);
088: } finally {
089: UserAuthenticatorUtils.cleanup(authData);
090: }
091:
092: return new HttpFileSystem(rootName, httpClient,
093: fileSystemOptions);
094: }
095:
096: public FileSystemConfigBuilder getConfigBuilder() {
097: return HttpFileSystemConfigBuilder.getInstance();
098: }
099:
100: public Collection getCapabilities() {
101: return capabilities;
102: }
103: }
|