View Javadoc

1   /*
2    * (C)opyright 2010, Nikolaos Georgosopoulos
3    *
4    * This file is part of URLChecker.
5   
6       URLChecker is free software: you can redistribute it and/or modify
7       it under the terms of the Lesser General Public License as published by
8       the Free Software Foundation, either version 3 of the License, or
9       (at your option) any later version.
10  
11      URLChecker is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14      GNU General Public License for more details.
15  
16      You should have received a copy of the Lesser General Public License
17      along with URLChecker.  If not, see <http://www.gnu.org/licenses/>.
18  
19   */
20  package net.sf.urlchecker.communication.configurers;
21  
22  import java.util.List;
23  
24  import org.apache.commons.configuration.HierarchicalConfiguration;
25  import org.apache.commons.configuration.XMLConfiguration;
26  import org.apache.commons.httpclient.Credentials;
27  import org.apache.commons.httpclient.HttpClient;
28  import org.apache.commons.httpclient.UsernamePasswordCredentials;
29  import org.apache.commons.httpclient.auth.AuthScope;
30  import org.apache.log4j.Logger;
31  
32  /**
33   * The ClientConfigurer class for the old jakarta httpClient. Here the client is
34   * configured for all the hosts and the proxy provided in the configuration and
35   * with preemptive authentication if so is required.
36   * 
37   * <p>
38   * <b> $Id: ClientConfigurer.java 182 2010-12-13 22:51:53Z georgosn $</b>
39   * </p>
40   * 
41   * @author $LastChangedBy: georgosn $
42   * @version $LastChangedRevision: 182 $
43   */
44  public class ClientConfigurer implements Configurer<HttpClient> {
45  
46      /** The Constant LOGGER. */
47      private static final Logger LOGGER = Logger.getLogger(ClientConfigurer.class
48                                                 .getName());
49  
50      /**
51       * Configure the hosts.
52       * 
53       * @param config
54       *            the XML configuration
55       * @param client
56       *            the httpClient
57       */
58      protected void configureHosts(final XMLConfiguration config,
59              final HttpClient client) {
60          if (config.containsKey("hosts.host")) {
61              final List<HierarchicalConfiguration> hosts = config
62                      .configurationsAt("hosts.host");
63              if (null != hosts) {
64                  LOGGER.debug("hosts:" + hosts.size());
65                  for (final HierarchicalConfiguration host : hosts) {
66                      final String username = host.getString("username", "admin");
67                      final String password = host
68                              .getString("password", "admin1");
69                      final String hostName = host.getString("server",
70                              "localhost");
71                      final int port = host.getInt("port", 80);
72  
73                      final Credentials defaultcreds = new UsernamePasswordCredentials(
74                              username, password);
75                      client.getState().setCredentials(
76                              new AuthScope(hostName, port, AuthScope.ANY_REALM),
77                              defaultcreds);
78  
79                  }
80              }
81          }
82      }
83  
84      /**
85       * Configure preemptive authentication.
86       * 
87       * @param config
88       *            the XML configuration
89       * @param client
90       *            the httpClient
91       */
92      protected void configurePreemptive(final XMLConfiguration config,
93              final HttpClient client) {
94  
95          if (config.getBoolean("preemtive", false)) {
96              client.getParams().setAuthenticationPreemptive(true);
97          }
98      }
99  
100     /**
101      * Configure the proxies.
102      * 
103      * @param config
104      *            the XML configuration
105      * @param client
106      *            the httpClient
107      */
108     protected void configureProxy(final XMLConfiguration config,
109             final HttpClient client) {
110         if (config.containsKey("proxy")) {
111             final HierarchicalConfiguration proxy = config
112                     .configurationAt("proxy");
113             if (null != proxy) {
114                 final String username = proxy.getString("username", "admin");
115                 final String password = proxy.getString("password", "admin1");
116                 final String host = proxy.getString("server", "localhost");
117                 final int port = proxy.getInt("port", 80);
118 
119                 final Credentials defaultcreds = new UsernamePasswordCredentials(
120                         username, password);
121                 client.getState().setProxyCredentials(
122                         new AuthScope(host, port, AuthScope.ANY_REALM),
123                         defaultcreds);
124             }
125         }
126     }
127 
128     /*
129      * (non-Javadoc)
130      * 
131      * @see
132      * net.sf.urlchecker.communication.configurers.Configurer#configureWith(org
133      * .apache.commons.configuration.XMLConfiguration, java.lang.Object)
134      */
135     /** {@inheritDoc} */
136     public HttpClient configureWith(XMLConfiguration config) {
137         final HttpClient subject = new HttpClient();
138         configurePreemptive(config, subject);
139         configureHosts(config, subject);
140         configureProxy(config, subject);
141         return subject;
142     }
143 
144 }