1 24 package org.objectweb.clif.isac.plugins.httpinjector10.tools; 25 26 import java.util.Calendar ; 27 import java.util.Date ; 28 import java.util.Hashtable ; 29 import java.util.StringTokenizer ; 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 47 public class HttpStateUtils { 48 49 static protected Logger log = IsacScenarioEngine.logger 51 .getLogger(HttpStateUtils.class.getName()); 52 60 private static int returnCookiePolicy(String 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 78 private static Hashtable setCookieParameters(String inputParameters) { 79 log.log(BasicLevel.DEBUG, "-> [Enter] setCookieParameters"); 80 Hashtable result = new Hashtable (); 81 StringTokenizer st = new StringTokenizer (inputParameters, "|"); 82 while (st.hasMoreTokens()) { 83 String inputParameter = st.nextToken(); 84 log.log(BasicLevel.DEBUG, "element : " + inputParameter); 85 StringTokenizer st2 = new StringTokenizer (inputParameter, "="); 86 if (st2.countTokens() == 2) { 87 String inputParamName = st2.nextToken(); 88 String 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 111 private static Cookie setCookie(Hashtable params) { 112 log.log(BasicLevel.DEBUG, "-> [Enter] setCookie"); 113 Cookie cookie = new Cookie(); 114 String domain = (String ) params.get(ParametersConstantes.DOMAIN); 115 String name = (String ) params.get(ParametersConstantes.NAME); 116 String value = (String ) params.get(ParametersConstantes.VALUE); 117 String path = (String ) params.get(ParametersConstantes.PATH); 118 String expires = (String ) params.get(ParametersConstantes.EXPIRES); 119 String maxAge = (String ) params.get(ParametersConstantes.MAXAGE); 120 String secure = (String ) params.get(ParametersConstantes.SECURE); 121 secure = (secure != null) ? secure : ""; 122 if (expires != null && !expires.equals("")) { 123 StringTokenizer st = new StringTokenizer (expires, "/"); 124 Calendar expiresDate = null; 125 if (st.countTokens() == 5) { 126 expiresDate.setTime(new Date ()); 128 expiresDate.set(new Integer (st.nextToken()).intValue(), 129 new Integer (st.nextToken()).intValue(), new Integer (st 130 .nextToken()).intValue(), new Integer (st 131 .nextToken()).intValue(), new Integer (st 132 .nextToken()).intValue(), new Integer (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 (secure).booleanValue()); 141 } else if (maxAge != null && !maxAge.equals("")) { 142 cookie = new Cookie(domain, name, value, path, new Integer (maxAge) 143 .intValue(), new Boolean (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 165 public static HttpState setHttpState(SessionObject sessionObject, 166 Hashtable params) { 167 log.log(BasicLevel.DEBUG, "-> [Enter] setHttpState"); 168 HttpState httpState = (sessionObject.getHttpClient().getState() != null) 169 ? sessionObject.getHttpClient().getState() 170 : new HttpState(); 171 if (params.containsKey(ParametersConstantes.PROXYUSERNAME)) { 173 String proxyLogin = (String ) params 174 .get(ParametersConstantes.PROXYUSERNAME); 175 String proxyPassword = (String ) 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 Cookie cookie; 187 if (params.containsKey(ParametersConstantes.COOKIES)) { 188 189 StringTokenizer st = new StringTokenizer ((String ) params 190 .get(ParametersConstantes.COOKIES), ";"); 191 while (st.hasMoreTokens()) { 192 String cookieParameters = st.nextToken(); 193 httpState 194 .addCookie(setCookie(setCookieParameters(cookieParameters))); 195 } 196 } 197 if (params.containsKey(ParametersConstantes.COOKIEPOLICY)) { 199 httpState.setCookiePolicy(returnCookiePolicy((String ) params 200 .get(ParametersConstantes.COOKIEPOLICY))); 201 } 202 log.log(BasicLevel.DEBUG, "<- [Exit] setHttpState"); 203 return httpState; 204 205 } 206 207 209 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 |