KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > http > HttpClientFactory


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
27  * Create a HttpClient instance
28  *
29  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
30  */

31 public class HttpClientFactory
32 {
33     private HttpClientFactory()
34     {
35     }
36
37     /**
38      * Creates a new connection to the server.
39      */

40     public static HttpClient createConnection(String JavaDoc hostname, int port, String JavaDoc username, String JavaDoc 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 JavaDoc 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 JavaDoc exc)
72         {
73             throw new FileSystemException("vfs.provider.http/connect.error", new Object JavaDoc[]{hostname}, exc);
74         }
75
76         return client;
77     }
78 }
Popular Tags