KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > isac > plugins > httpinjector10 > tools > HostConfigurationUtils


1 /*
2  *
3  * CLIF is a Load Injection Framework
4  * Copyright (C) 2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * CLIF
21  *
22  * Contact: clif@objectweb.org
23  */

24 package org.objectweb.clif.isac.plugins.httpinjector10.tools;
25
26 import java.net.InetAddress JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.UnknownHostException JavaDoc;
29 import java.util.Hashtable JavaDoc;
30
31 import org.apache.commons.httpclient.HostConfiguration;
32 import org.apache.commons.httpclient.protocol.Protocol;
33 import org.objectweb.clif.isac.plugins.httpinjector10.SessionObject;
34 import org.objectweb.clif.scenario.util.isac.engine.IsacScenarioEngine;
35 import org.objectweb.util.monolog.api.BasicLevel;
36 import org.objectweb.util.monolog.api.Logger;
37
38 import com.sun.jndi.toolkit.url.Uri;
39
40 /**
41  * Created on august 17 2004
42  *
43  * @author JC Meillaud
44  * @author A Peyrard
45  *
46  */

47 public class HostConfigurationUtils {
48
49     static protected Logger log = IsacScenarioEngine.logger
50             .getLogger(HostConfigurationUtils.class.getName());
51
52     /**
53      * Set the HostConfiguration that will be assigned to the httpClient. Holds
54      * String cookiesParameters = (String) params.get("cookieparams;
55      *
56      * all of the variables needed to describe an HTTP connection to a host.
57      * This includes remote host, port and protocol, proxy host and port, local
58      * address, and virtual host.
59      *
60      * @param params
61      * Hashtable containing all the variable
62      * @return the HostConfiguration builded
63      * @throws MalformedURLException
64      * @throws MalformedURLException
65      */

66     public static HostConfiguration setHostConfiguration(
67             SessionObject sessionObject, Hashtable JavaDoc params)
68             throws MalformedURLException JavaDoc {
69         log.log(BasicLevel.DEBUG, "-> [Enter] setHostConfiguration");
70         HostConfiguration hostConfiguration = new HostConfiguration();
71         String JavaDoc proxyHost = (String JavaDoc) params.get(ParametersConstantes.PROXYHOST);
72         String JavaDoc proxyPort = (String JavaDoc) params.get(ParametersConstantes.PROXYPORT);
73         String JavaDoc localAddress = (String JavaDoc) params
74                 .get(ParametersConstantes.LOCALADDRESS);
75
76         Uri uri = new Uri((String JavaDoc) params.get(ParametersConstantes.URI));
77         String JavaDoc schema = uri.getScheme();
78         Protocol protocol = Protocol.getProtocol(schema);
79         String JavaDoc host = uri.getHost();
80         int port = uri.getPort();
81         hostConfiguration.setHost(host,localAddress, port, protocol);
82         //hostConfiguration.setHost(stringUri);
83

84         // set the host and port for the proxy
85
if (proxyHost != null && !proxyHost.equals("")) {
86             int defaultProxyPort = (proxyPort != null && !proxyPort.equals(""))
87                     ? new Integer JavaDoc(proxyPort).intValue()
88                     : ParametersConstantes.PROXYDEFAULTPORT;
89             log.log(BasicLevel.DEBUG, "Set a proxy : " + proxyHost + " : "
90                     + proxyPort);
91             hostConfiguration.setProxy(proxyHost, new Integer JavaDoc(proxyPort)
92                     .intValue());
93         } else {
94             // check if there is a default proxy configuration
95
String JavaDoc defaultProxyHost = sessionObject.getProxyHost();
96             String JavaDoc defaultProxyPort = sessionObject.getProxyPort();
97             if (defaultProxyHost != null && !defaultProxyHost.equals("")
98                     && defaultProxyPort != null && !defaultProxyPort.equals(""))
99                 hostConfiguration.setProxy(defaultProxyHost, new Integer JavaDoc(
100                         defaultProxyPort).intValue());
101         }
102         // set LocalAddress if it is defined
103
if (localAddress != null && !localAddress.equals("")) {
104             InetAddress JavaDoc inetAddress;
105             try {
106                 inetAddress = InetAddress.getByName(localAddress);
107                 hostConfiguration.setLocalAddress(inetAddress);
108             } catch (UnknownHostException JavaDoc e) {
109                 e.printStackTrace();
110             }
111         }
112         log.log(BasicLevel.DEBUG, "<- [Exit] setHostConfiguration");
113         return hostConfiguration;
114     }
115
116 }
Popular Tags