View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.vfs.provider.http;
    18
    19  import org.apache.commons.httpclient.HostConfiguration;
    20  import org.apache.commons.httpclient.HttpClient;
    21  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
    22  import org.apache.commons.httpclient.UsernamePasswordCredentials;
    23  import org.apache.commons.httpclient.Cookie;
    24  import org.apache.commons.httpclient.methods.HeadMethod;
    25  import org.apache.commons.vfs.FileSystemException;
    26  import org.apache.commons.vfs.FileSystemOptions;
    27  import org.apache.commons.vfs.UserAuthenticator;
    28  import org.apache.commons.vfs.UserAuthenticationData;
    29  import org.apache.commons.vfs.util.UserAuthenticatorUtils;
    30
    31  /***
    32   * Create a HttpClient instance
    33   *
    34   * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
    35   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    36   */
    37  public class HttpClientFactory
    38  {
    39      private HttpClientFactory()
    40      {
    41      }
    42
    43      /***
    44       * Creates a new connection to the server.
    45       */
    46      public static HttpClient createConnection(String scheme, String hostname, int port, String username, String password, FileSystemOptions fileSystemOptions) throws FileSystemException
    47      {
    48          HttpClient client;
    49          try
    50          {
    51              client = new HttpClient(new MultiThreadedHttpConnectionManager());
    52              final HostConfiguration config = new HostConfiguration();
    53              config.setHost(hostname, port, scheme);
    54
    55              if (fileSystemOptions != null)
    56              {
    57                  String proxyHost = HttpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
    58                  int proxyPort = HttpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
    59
    60                  if (proxyHost != null && proxyPort > 0)
    61                  {
    62                      config.setProxy(proxyHost, proxyPort);
    63                  }
    64
    65                  UserAuthenticator proxyAuth = HttpFileSystemConfigBuilder.getInstance().getProxyAuthenticator(fileSystemOptions);
    66                  if (proxyAuth != null)
    67                  {
    68                      UserAuthenticationData authData = UserAuthenticatorUtils.authenticate(proxyAuth, new UserAuthenticationData.Type[]
    69                          {
    70                              UserAuthenticationData.USERNAME,
    71                              UserAuthenticationData.PASSWORD
    72                          });
    73
    74                      if (authData != null)
    75                      {
    76                          final UsernamePasswordCredentials proxyCreds =
    77                              new UsernamePasswordCredentials(
    78                                  UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.USERNAME, null)),
    79                                  UserAuthenticatorUtils.toString(UserAuthenticatorUtils.getData(authData, UserAuthenticationData.PASSWORD, null)));
    80
    81                          client.getState().setProxyCredentials(null, proxyHost, proxyCreds);
    82                      }
    83                  }
    84
    85                  Cookie[] cookies = HttpFileSystemConfigBuilder.getInstance().getCookies(fileSystemOptions);
    86                  if (cookies != null)
    87                  {
    88                      client.getState().addCookies(cookies);
    89                  }
    90              }
    91
    92              client.setHostConfiguration(config);
    93
    94              if (username != null)
    95              {
    96                  final UsernamePasswordCredentials creds =
    97                      new UsernamePasswordCredentials(username, password);
    98                  client.getState().setCredentials(null, hostname, creds);
    99              }
    100
    101             client.executeMethod(new HeadMethod());
    102         }
    103         catch (final Exception exc)
    104         {
    105             throw new FileSystemException("vfs.provider.http/connect.error", new Object[]{hostname}, exc);
    106         }
    107
    108         return client;
    109     }
    110 }