KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Calendar JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import org.apache.commons.httpclient.Cookie;
32 import org.apache.commons.httpclient.HttpState;
33 import org.apache.commons.httpclient.UsernamePasswordCredentials;
34 import org.apache.commons.httpclient.cookie.CookiePolicy;
35 import org.objectweb.clif.isac.plugins.httpinjector10.SessionObject;
36 import org.objectweb.clif.scenario.util.isac.engine.IsacScenarioEngine;
37 import org.objectweb.util.monolog.api.BasicLevel;
38 import org.objectweb.util.monolog.api.Logger;
39
40 /**
41  * Class that deals with the HttpState of a connection Created on august 17 2004
42  *
43  * @author JC Meillaud
44  * @author A Peyrard
45  *
46  */

47 public class HttpStateUtils {
48
49     // logger
50
static protected Logger log = IsacScenarioEngine.logger
51             .getLogger(HttpStateUtils.class.getName());
52     /**
53      * Function which return the CookiePolicy (an int) corresponding to the
54      * String cookiePolicy
55      *
56      * @param cookiePolicy
57      * String representing what sort of policy
58      * @return the CookiePolicy corresponding
59      */

60     private static int returnCookiePolicy(String JavaDoc cookiePolicy) {
61         if (cookiePolicy.equals(ParametersConstantes.RFC2109)) {
62             return CookiePolicy.RFC2109;
63         } else if (cookiePolicy.equals(ParametersConstantes.NETSCAPE_DRAFT)) {
64             return CookiePolicy.NETSCAPE_DRAFT;
65         } else {
66             return CookiePolicy.COMPATIBILITY;
67         }
68     }
69
70     /**
71      * Method to deserialize the cookie parameters and build and hachtable from
72      * it
73      *
74      * @param inputParameters
75      * the serialized String representing the cookie
76      * @return an Hachtable containing all the variable
77      */

78     private static Hashtable JavaDoc setCookieParameters(String JavaDoc inputParameters) {
79         log.log(BasicLevel.DEBUG, "-> [Enter] setCookieParameters");
80         Hashtable JavaDoc result = new Hashtable JavaDoc();
81         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(inputParameters, "|");
82         while (st.hasMoreTokens()) {
83             String JavaDoc inputParameter = st.nextToken();
84             log.log(BasicLevel.DEBUG, "element : " + inputParameter);
85             StringTokenizer JavaDoc st2 = new StringTokenizer JavaDoc(inputParameter, "=");
86             if (st2.countTokens() == 2) {
87                 String JavaDoc inputParamName = st2.nextToken();
88                 String JavaDoc inputParamValue = st2.nextToken();
89                 log.log(BasicLevel.DEBUG, "CookieParamName : ["
90                         + inputParamName + "] ;CookieParamValue : ["
91                         + inputParamValue + "]");
92                 if (!inputParamName.equals("") && !inputParamValue.equals(""))
93                     result.put(inputParamName, inputParamValue);
94                 else
95                     log
96                             .log(BasicLevel.DEBUG,
97                                     "One of the parameters for the cookie is undefined");
98             }
99         }
100         log.log(BasicLevel.DEBUG, "<- [Exit] setCookieParameters");
101         return result;
102     }
103
104     /**
105      * Method wich return a httpclient cookie described into the input hashtable
106      *
107      * @param params
108      * the hashtable describing the cookie
109      * @return an HttpClient Cookie
110      */

111     private static Cookie setCookie(Hashtable JavaDoc params) {
112         log.log(BasicLevel.DEBUG, "-> [Enter] setCookie");
113         Cookie cookie = new Cookie();
114         String JavaDoc domain = (String JavaDoc) params.get(ParametersConstantes.DOMAIN);
115         String JavaDoc name = (String JavaDoc) params.get(ParametersConstantes.NAME);
116         String JavaDoc value = (String JavaDoc) params.get(ParametersConstantes.VALUE);
117         String JavaDoc path = (String JavaDoc) params.get(ParametersConstantes.PATH);
118         String JavaDoc expires = (String JavaDoc) params.get(ParametersConstantes.EXPIRES);
119         String JavaDoc maxAge = (String JavaDoc) params.get(ParametersConstantes.MAXAGE);
120         String JavaDoc secure = (String JavaDoc) params.get(ParametersConstantes.SECURE);
121         secure = (secure != null) ? secure : "";
122         if (expires != null && !expires.equals("")) {
123             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(expires, "/");
124             Calendar JavaDoc expiresDate = null;
125             if (st.countTokens() == 5) {
126                 // year / month / date / hour / minute / seconde
127
expiresDate.setTime(new Date JavaDoc());
128                 expiresDate.set(new Integer JavaDoc(st.nextToken()).intValue(),
129                         new Integer JavaDoc(st.nextToken()).intValue(), new Integer JavaDoc(st
130                                 .nextToken()).intValue(), new Integer JavaDoc(st
131                                 .nextToken()).intValue(), new Integer JavaDoc(st
132                                 .nextToken()).intValue(), new Integer JavaDoc(st
133                                 .nextToken()).intValue());
134             } else {
135                 log
136                         .log(BasicLevel.DEBUG,
137                                 "There is a problem in the definition of the expire Date of the cookie");
138             }
139             cookie = new Cookie(domain, name, value, path, expiresDate
140                     .getTime(), new Boolean JavaDoc(secure).booleanValue());
141         } else if (maxAge != null && !maxAge.equals("")) {
142             cookie = new Cookie(domain, name, value, path, new Integer JavaDoc(maxAge)
143                     .intValue(), new Boolean JavaDoc(secure).booleanValue());
144         } else {
145             cookie = new Cookie(domain, name, value);
146         }
147         cookie
148                 .setDomainAttributeSpecified(domain != null
149                         && !domain.equals(""));
150         cookie.setPathAttributeSpecified(path != null && !path.equals(""));
151
152         log.log(BasicLevel.DEBUG, "<- [Exit] setCookie");
153         return cookie;
154     }
155
156     /**
157      * build an httpState that is a container for HTTP attributes that may
158      * persist from request to request, such as cookies and authentication
159      * credentials.
160      *
161      * @param params
162      * Hashtable containing all the variable
163      * @return an HttpState containing all Http attributes
164      */

165     public static HttpState setHttpState(SessionObject sessionObject,
166             Hashtable JavaDoc params) {
167         log.log(BasicLevel.DEBUG, "-> [Enter] setHttpState");
168         HttpState httpState = (sessionObject.getHttpClient().getState() != null)
169                 ? sessionObject.getHttpClient().getState()
170                 : new HttpState();
171         // Firstly configure the credentials if they are defined
172
if (params.containsKey(ParametersConstantes.PROXYUSERNAME)) {
173             String JavaDoc proxyLogin = (String JavaDoc) params
174                     .get(ParametersConstantes.PROXYUSERNAME);
175             String JavaDoc proxyPassword = (String JavaDoc) params
176                     .get(ParametersConstantes.PROXYUSERPASS);
177             if (proxyLogin != null && proxyLogin != "" && proxyPassword != null
178                     && proxyPassword != "") {
179                 httpState.setProxyCredentials(null, null,
180                         new UsernamePasswordCredentials(proxyLogin,
181                                 proxyPassword));
182             }
183         }
184
185         // Secondly build the cookies defined and add them to the HttpState
186
Cookie cookie;
187         if (params.containsKey(ParametersConstantes.COOKIES)) {
188
189             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc((String JavaDoc) params
190                     .get(ParametersConstantes.COOKIES), ";");
191             while (st.hasMoreTokens()) {
192                 String JavaDoc cookieParameters = st.nextToken();
193                 httpState
194                         .addCookie(setCookie(setCookieParameters(cookieParameters)));
195             }
196         }
197         // Lastly configure or reconfigure the general policy for the cookies
198
if (params.containsKey(ParametersConstantes.COOKIEPOLICY)) {
199             httpState.setCookiePolicy(returnCookiePolicy((String JavaDoc) params
200                     .get(ParametersConstantes.COOKIEPOLICY)));
201         }
202         log.log(BasicLevel.DEBUG, "<- [Exit] setHttpState");
203         return httpState;
204
205     }
206
207     // DEBUG Method
208

209     /**
210      * Method to print the Cookies which are in the SessionObject
211      *
212      * @param sessionObject
213      * The SessionObject
214      */

215     public static void printCookies(SessionObject sessionObject) {
216         Cookie[] cookies = sessionObject.getHttpClient().getState()
217                 .getCookies();
218         log.log(BasicLevel.DEBUG, "Cookie Present : " + cookies.length);
219         for (int i = 0; i < cookies.length; i++) {
220             log.log(BasicLevel.DEBUG, " - " + cookies[i].toExternalForm());
221         }
222     }
223
224 }
Popular Tags