1 23 24 package org.objectweb.clif.isac.plugins.httpinjector10; 25 26 import java.util.Hashtable ; 27 28 import org.apache.commons.httpclient.Credentials; 29 import org.apache.commons.httpclient.Header; 30 import org.apache.commons.httpclient.HostConfiguration; 31 import org.apache.commons.httpclient.HttpClient; 32 import org.apache.commons.httpclient.HttpState; 33 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager; 34 import org.apache.commons.httpclient.UsernamePasswordCredentials; 35 import org.apache.log4j.PropertyConfigurator; 36 import org.objectweb.clif.isac.plugins.httpinjector10.actions.HttpInjectorSamples; 37 import org.objectweb.clif.isac.plugins.httpinjector10.actions.HttpInjectorTests; 38 import org.objectweb.clif.isac.plugins.httpinjector10.actions.HttpInjectorTimers; 39 import org.objectweb.clif.isac.plugins.httpinjector10.tools.ParametersConstantes; 40 import org.objectweb.clif.scenario.util.isac.engine.IsacScenarioEngine; 41 import org.objectweb.clif.scenario.util.isac.exception.IsacRuntimeException; 42 import org.objectweb.clif.scenario.util.isac.plugin.SampleAction; 43 import org.objectweb.clif.scenario.util.isac.plugin.TestAction; 44 import org.objectweb.clif.scenario.util.isac.plugin.TimerAction; 45 import org.objectweb.clif.scenario.util.isac.util.IntHolder; 46 import org.objectweb.clif.scenario.util.isac.util.SessionObjectAction; 47 import org.objectweb.clif.scenario.util.isac.FileName; 48 import org.objectweb.clif.storage.api.ActionEvent; 49 import org.objectweb.util.monolog.api.BasicLevel; 50 import org.objectweb.util.monolog.api.Logger; 51 52 59 public class SessionObject 60 implements 61 SampleAction, 62 TimerAction, 63 TestAction, 64 SessionObjectAction { 65 66 static protected Logger log = IsacScenarioEngine.logger 68 .getLogger(SessionObject.class.getName()); 69 70 static 71 { 72 PropertyConfigurator.configure(FileName.LOG4J_PROPERTIES); 73 } 74 75 public final static boolean DEBUG_ON = true; 76 77 public static final int SAMPLE_GET = 0; 78 79 public static final int SAMPLE_POST = 1; 80 81 public static final int SAMPLE_MULTIPARTPOST = 2; 82 83 public static final int SAMPLE_HEAD = 3; 84 85 public static final int SAMPLE_OPTIONS = 4; 86 87 public static final int SAMPLE_PUT = 5; 88 89 public static final int SAMPLE_DELETE = 6; 90 91 public static final int TIMER_SIMPLETIMER = 0; 93 94 public static final int TEST_IS404RESPONSE = 0; 96 97 public static final int TEST_ISSTATUSCODERESPONSE = 1; 98 99 public static final int TEST_ISHEADERVALUE = 2; 100 101 private static final String HTTPCLIENTUSERAGENT = "httpclient.useragent"; 102 103 private int lastStatusCode = -1; 105 106 private Header[] lastHeader = null; 107 108 private String lastResponseBody = null; 109 110 private HttpClient httpClient; 112 113 private String proxyHost; 115 116 private String proxyPort; 117 118 private String proxyUserName; 119 120 private String proxyUserPass; 121 122 private String preemptiveAuthentification; 123 private static String USERNAME = "ClifUser"; 125 private String userName; 126 private IntHolder userNameNumber; 127 128 134 public SessionObject(Hashtable params) { 135 userNameNumber = new IntHolder(0) ; 137 this.userNameNumber.setIntValue(this.userNameNumber.getIntValue()+1); 139 this.userName = new String (USERNAME + userNameNumber.getIntValue()); 140 141 String userAgent = (String ) params.get(ParametersConstantes.USERAGENT); 143 String customUserAgent = (String ) params.get(ParametersConstantes.CUSTOMUSERAGENT); 144 if (userAgent!=null) { 145 if (userAgent.equals(ParametersConstantes.CUSTOM)) { 146 if (customUserAgent!=null && !customUserAgent.equals("")){ 147 System.setProperty(HTTPCLIENTUSERAGENT,customUserAgent); 148 } 149 } else { 150 System.setProperty(HTTPCLIENTUSERAGENT,userAgent); 151 } 152 } 153 154 String proxyHost = (String ) params.get(ParametersConstantes.PROXYHOST); 155 log.log(BasicLevel.DEBUG, proxyHost); 156 String proxyPort = (String ) params.get(ParametersConstantes.PROXYPORT); 157 log.log(BasicLevel.DEBUG, proxyHost); 158 String proxyUserName = (String ) params 159 .get(ParametersConstantes.PROXYUSERNAME); 160 String proxyUserPass = (String ) params 161 .get(ParametersConstantes.PROXYUSERPASS); 162 String preemptiveAuthentification = (String ) params 163 .get(ParametersConstantes.PREEMPTIVEAUTHENTIFICATION); 164 165 this.httpClient = new HttpClient( 167 new MultiThreadedHttpConnectionManager()); 168 this.proxyPort = (proxyPort != null) 170 ? proxyPort 171 : ParametersConstantes.EMPTY; 172 this.proxyHost = (proxyHost != null) 173 ? proxyHost 174 : ParametersConstantes.EMPTY; 175 this.proxyUserName = (proxyUserName != null) 176 ? proxyUserName 177 : ParametersConstantes.EMPTY; 178 this.proxyUserPass = (proxyUserPass != null) 179 ? proxyUserPass 180 : ParametersConstantes.EMPTY; 181 this.preemptiveAuthentification = (preemptiveAuthentification != null) 182 ? preemptiveAuthentification 183 : ParametersConstantes.EMPTY; 184 185 try { 186 if (!this.proxyHost.equals(ParametersConstantes.EMPTY)) { 187 this.setProxy(this.proxyHost, (new Integer (this.proxyPort)).intValue()); 188 } 189 if (!this.proxyUserName.equals(ParametersConstantes.EMPTY) 190 && !this.proxyUserPass.equals(ParametersConstantes.EMPTY)) { 191 this.setProxyAuthentification(this.proxyUserName, 192 this.proxyUserPass); 193 } else if (System.getProperty("http.proxyUserName") != null) { 194 this.setProxyAuthentification(System 195 .getProperty("http.proxyUserName"), System 196 .getProperty("http.proxyPassword")); 197 } 198 if (!this.preemptiveAuthentification.equals(ParametersConstantes.EMPTY) 199 && this.preemptiveAuthentification 200 .equals(ParametersConstantes.ENABLED)) { 201 HttpState state = new HttpState(); 202 state.setAuthenticationPreemptive(true); 203 this.httpClient.setState(state); 204 } 205 } catch (Exception e) { 206 throw new IsacRuntimeException( 207 "Unable to set the SessionObject for the plugin HttpInjector -> " 208 + e); 209 } 210 211 } 212 213 219 private SessionObject(SessionObject so) { 220 this.proxyHost = so.getProxyHost(); 222 this.proxyPort = so.getProxyPort(); 223 this.proxyUserName = so.getProxyUserName(); 224 this.proxyUserPass = so.getProxyUserPass(); 225 this.preemptiveAuthentification = so.getAuthentificationPreemptive(); 226 227 this.httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); 229 try { 232 if (!proxyHost.equals("") && !proxyPort.equals("")) { 233 this.setProxy(proxyHost, (new Integer (proxyPort)).intValue()); 234 } 235 if (!proxyUserName.equals("") && !proxyUserPass.equals("")) { 236 this.setProxyAuthentification(proxyUserName, proxyUserPass); 237 } else if (System.getProperty("http.proxyUserName") != null) { 238 this.setProxyAuthentification(System 239 .getProperty("http.proxyUserName"), System 240 .getProperty("http.proxyPassword")); 241 } 242 if (!preemptiveAuthentification.equals("") 243 && preemptiveAuthentification 244 .equals(ParametersConstantes.ENABLED)) { 245 HttpState state = new HttpState(); 246 state.setAuthenticationPreemptive(true); 247 this.httpClient.setState(state); 248 } 250 } catch (Exception e) { 251 throw new IsacRuntimeException( 252 "Unable to set the SessionObject for the plugin HttpInjector -> " 253 + e); 254 } 255 this.userNameNumber = so.userNameNumber; 257 this.userNameNumber.setIntValue(this.userNameNumber.getIntValue()+1); 258 this.userName = new String (USERNAME + userNameNumber.getIntValue()); 259 } 260 261 262 public ActionEvent doSample(int number, Hashtable params, ActionEvent report) { 263 log.log(BasicLevel.DEBUG, "-> Entering doSample"); 265 if (!params.get(ParametersConstantes.URI).equals(ParametersConstantes.EMPTY)) { 266 267 268 switch (number) { 269 case SAMPLE_GET : 270 return HttpInjectorSamples.doGet(this, report, params); 271 case SAMPLE_POST : 272 return HttpInjectorSamples.doPost(this, report, params); 273 case SAMPLE_MULTIPARTPOST : 274 return HttpInjectorSamples.doMultiPost(this, report, params); 275 case SAMPLE_HEAD : 276 return HttpInjectorSamples.doHead(this, report, params); 277 case SAMPLE_OPTIONS : 278 return HttpInjectorSamples.doOptions(this, report, params); 279 case SAMPLE_PUT : 280 return HttpInjectorSamples.doPut(this, report, params); 281 case SAMPLE_DELETE : 282 return HttpInjectorSamples.doDelete(this, report, params); 283 default : 284 throw new IsacRuntimeException( 285 "Unable to find this sample in HttpInjector plugin : " 286 + number); 287 } 288 } else throw new IsacRuntimeException( 289 "No Uri is defined" 290 + number); 291 } 292 293 294 public long doTimer(int number, Hashtable params) { 295 switch (number) { 297 case TIMER_SIMPLETIMER : 298 return HttpInjectorTimers.simpleTimer(); 299 default : 300 throw new IsacRuntimeException( 301 "Unable to find this timer in HttpInjector plugin : " 302 + number); 303 } 304 } 305 306 307 public boolean doTest(int number, Hashtable params) { 308 switch (number) { 310 case TEST_IS404RESPONSE : 311 return HttpInjectorTests.is404Response(this); 312 case TEST_ISSTATUSCODERESPONSE : 313 int statusCode = new Integer ((String ) params 314 .get(ParametersConstantes.STATUSCODE)).intValue(); 315 if (statusCode != -1) 316 return HttpInjectorTests.isStatusCodeResponse(this, 317 statusCode); 318 else 319 return false; 320 case TEST_ISHEADERVALUE : 321 String headerParameterType = (String ) params 322 .get(ParametersConstantes.HEADERPARAMETERTYPE); 323 String headerParameterValue = (String ) params 324 .get(ParametersConstantes.HEADERPARAMETERVALUE); 325 return (headerParameterType != null && headerParameterValue != null) 326 ? HttpInjectorTests.isHeaderValue(this, 327 headerParameterType, headerParameterValue) 328 : false; 329 default : 330 throw new IsacRuntimeException( 331 "Unable to find this test in HttpInjector plugin : " 332 + number); 333 } 334 } 335 336 338 346 private void setProxy(String host, int port) { 347 HostConfiguration hostConfiguration = new HostConfiguration(); 349 hostConfiguration.setProxy(host, port); 350 this.httpClient.setHostConfiguration(hostConfiguration); 352 } 353 354 private void setProxyAuthentification(String userName, String userPass) { 355 Credentials customCrds = new UsernamePasswordCredentials(userName, 356 userPass); 357 this.httpClient.getState().setProxyCredentials(null, null, customCrds); 358 359 } 360 361 365 368 public Object createNewSessionObject() { 369 return new SessionObject(this); 370 } 371 372 375 public void close() { 376 } 378 379 382 public void reset() { 383 this.lastStatusCode = -1; 384 this.lastHeader = null; 385 this.userNameNumber.setIntValue(this.userNameNumber.getIntValue()+1); 387 this.userName = new String (USERNAME + this.userNameNumber.getIntValue()); 388 } 389 390 394 397 public HttpClient getHttpClient() { 398 return httpClient; 399 } 400 401 405 public void setHttpClient(HttpClient httpClient) { 406 this.httpClient = httpClient; 407 } 408 409 412 public String getProxyHost() { 413 return proxyHost; 414 } 415 416 419 public String getProxyPort() { 420 return proxyPort; 421 } 422 423 426 public String getProxyUserName() { 427 return proxyUserName; 428 } 429 430 434 public void setProxyUserName(String proxyUserName) { 435 this.proxyUserName = proxyUserName; 436 } 437 438 441 public String getProxyUserPass() { 442 return proxyUserPass; 443 } 444 445 449 public void setProxyUserPass(String proxyUserPass) { 450 this.proxyUserPass = proxyUserPass; 451 } 452 453 456 public String getAuthentificationPreemptive() { 457 return preemptiveAuthentification; 458 } 459 460 464 public void setAuthentificationPreemptive(String authentificationPreemptive) { 465 this.preemptiveAuthentification = authentificationPreemptive; 466 } 467 468 487 492 public int getLastStatusCode() { 493 return lastStatusCode; 494 } 495 496 502 public void setLastStatusCode(int lastStatusCode) { 503 this.lastStatusCode = lastStatusCode; 504 } 505 506 511 public Header[] getLastHeader() { 512 return lastHeader; 513 } 514 515 521 public void setLastHeader(Header[] lastHeader) { 522 this.lastHeader = lastHeader; 523 } 524 525 530 public String getLastResponseBody() { 531 return lastResponseBody; 532 } 533 538 public void setLastResponseBody(String lastResponseBody) { 539 this.lastResponseBody = lastResponseBody; 540 } 541 544 public String getUserName() { 545 return userName; 546 } 547 } | Popular Tags |