1 16 package org.apache.commons.vfs.provider.http; 17 18 import org.apache.commons.httpclient.HostConfiguration; 19 import org.apache.commons.httpclient.HttpClient; 20 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 21 import org.apache.commons.httpclient.UsernamePasswordCredentials; 22 import org.apache.commons.httpclient.methods.HeadMethod; 23 import org.apache.commons.vfs.FileSystemException; 24 import org.apache.commons.vfs.FileSystemOptions; 25 26 31 public class HttpClientFactory 32 { 33 private HttpClientFactory() 34 { 35 } 36 37 40 public static HttpClient createConnection(String hostname, int port, String username, String password, FileSystemOptions fileSystemOptions) throws FileSystemException 41 { 42 HttpClient client; 43 try 44 { 45 client = new HttpClient(new MultiThreadedHttpConnectionManager()); 46 final HostConfiguration config = new HostConfiguration(); 47 config.setHost(hostname, port); 48 49 if (fileSystemOptions != null) 50 { 51 String proxyHost = HttpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions); 52 int proxyPort = HttpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions); 53 54 if (proxyHost != null && proxyPort > 0) 55 { 56 config.setProxy(proxyHost, proxyPort); 57 } 58 } 59 60 client.setHostConfiguration(config); 61 62 if (username != null) 63 { 64 final UsernamePasswordCredentials creds = 65 new UsernamePasswordCredentials(username, password); 66 client.getState().setCredentials(null, hostname, creds); 67 } 68 69 client.executeMethod(new HeadMethod()); 70 } 71 catch (final Exception exc) 72 { 73 throw new FileSystemException("vfs.provider.http/connect.error", new Object []{hostname}, exc); 74 } 75 76 return client; 77 } 78 } | Popular Tags |