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.Cookie;
020: import org.apache.commons.httpclient.HostConfiguration;
021: import org.apache.commons.httpclient.HttpClient;
022: import org.apache.commons.httpclient.UsernamePasswordCredentials;
023: import org.apache.commons.httpclient.methods.HeadMethod;
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.UserAuthenticator;
028: import org.apache.commons.vfs.util.UserAuthenticatorUtils;
029:
030: /**
031: * Create a HttpClient instance
032: *
033: * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
034: * @version $Revision: 548717 $ $Date: 2007-06-19 06:07:40 -0700 (Tue, 19 Jun 2007) $
035: */
036: public class HttpClientFactory {
037: private HttpClientFactory() {
038: }
039:
040: /**
041: * Creates a new connection to the server.
042: */
043: public static HttpClient createConnection(String scheme,
044: String hostname, int port, String username,
045: String password, FileSystemOptions fileSystemOptions)
046: throws FileSystemException {
047: HttpClient client;
048: try {
049: // client = new HttpClient(new MultiThreadedHttpConnectionManager());
050: client = new HttpClient(
051: new ThreadLocalHttpConnectionManager());
052:
053: final HostConfiguration config = new HostConfiguration();
054: config.setHost(hostname, port, scheme);
055:
056: if (fileSystemOptions != null) {
057: String proxyHost = HttpFileSystemConfigBuilder
058: .getInstance().getProxyHost(fileSystemOptions);
059: int proxyPort = HttpFileSystemConfigBuilder
060: .getInstance().getProxyPort(fileSystemOptions);
061:
062: if (proxyHost != null && proxyPort > 0) {
063: config.setProxy(proxyHost, proxyPort);
064: }
065:
066: UserAuthenticator proxyAuth = HttpFileSystemConfigBuilder
067: .getInstance().getProxyAuthenticator(
068: fileSystemOptions);
069: if (proxyAuth != null) {
070: UserAuthenticationData authData = UserAuthenticatorUtils
071: .authenticate(
072: proxyAuth,
073: new UserAuthenticationData.Type[] {
074: UserAuthenticationData.USERNAME,
075: UserAuthenticationData.PASSWORD });
076:
077: if (authData != null) {
078: final UsernamePasswordCredentials proxyCreds = new UsernamePasswordCredentials(
079: UserAuthenticatorUtils
080: .toString(UserAuthenticatorUtils
081: .getData(
082: authData,
083: UserAuthenticationData.USERNAME,
084: null)),
085: UserAuthenticatorUtils
086: .toString(UserAuthenticatorUtils
087: .getData(
088: authData,
089: UserAuthenticationData.PASSWORD,
090: null)));
091:
092: client.getState().setProxyCredentials(null,
093: proxyHost, proxyCreds);
094: }
095: }
096:
097: Cookie[] cookies = HttpFileSystemConfigBuilder
098: .getInstance().getCookies(fileSystemOptions);
099: if (cookies != null) {
100: client.getState().addCookies(cookies);
101: }
102: }
103:
104: client.setHostConfiguration(config);
105:
106: if (username != null) {
107: final UsernamePasswordCredentials creds = new UsernamePasswordCredentials(
108: username, password);
109: client.getState().setCredentials(null, hostname, creds);
110: }
111:
112: client.executeMethod(new HeadMethod());
113: } catch (final Exception exc) {
114: throw new FileSystemException(
115: "vfs.provider.http/connect.error",
116: new Object[] { hostname }, exc);
117: }
118:
119: return client;
120: }
121: }
|