KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > isac > plugins > httpinjector10 > SessionObject


1 /*
2  * CLIF is a Load Injection Framework
3  * Copyright (C) 2004 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * CLIF
20  *
21  * Contact: clif@objectweb.org
22  */

23
24 package org.objectweb.clif.isac.plugins.httpinjector10;
25
26 import java.util.Hashtable JavaDoc;
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 /**
53  * This class is the implementation of a session object for the http matrix
54  * plugin
55  *
56  * @author JC Meillaud
57  * @author A Peyrard
58  */

59 public class SessionObject
60         implements
61             SampleAction,
62             TimerAction,
63             TestAction,
64             SessionObjectAction {
65
66     // logger
67
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     // timer constantes definition
92
public static final int TIMER_SIMPLETIMER = 0;
93
94     // test constantes definition
95
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 JavaDoc HTTPCLIENTUSERAGENT = "httpclient.useragent";
102
103     // int representing the last errorcode received
104
private int lastStatusCode = -1;
105
106     private Header[] lastHeader = null;
107
108     private String JavaDoc lastResponseBody = null;
109
110     // http client will be used to execute the http methods
111
private HttpClient httpClient;
112
113     // host and port for the proxy
114
private String JavaDoc proxyHost;
115
116     private String JavaDoc proxyPort;
117
118     private String JavaDoc proxyUserName;
119
120     private String JavaDoc proxyUserPass;
121
122     private String JavaDoc preemptiveAuthentification;
123     // XXX USERNAME GENERATED FOR PRESENTATION
124
private static String JavaDoc USERNAME = "ClifUser";
125     private String JavaDoc userName;
126     private IntHolder userNameNumber;
127
128     /**
129      * Constructor
130      *
131      * @param params
132      * defining the httpclient
133      */

134     public SessionObject(Hashtable JavaDoc params) {
135         // XXX Presentation
136
userNameNumber = new IntHolder(0) ;
137         // XXX Presentation
138
this.userNameNumber.setIntValue(this.userNameNumber.getIntValue()+1);
139         this.userName = new String JavaDoc(USERNAME + userNameNumber.getIntValue());
140
141         // Set the User-Agent
142
String JavaDoc userAgent = (String JavaDoc) params.get(ParametersConstantes.USERAGENT);
143         String JavaDoc customUserAgent = (String JavaDoc) 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 JavaDoc proxyHost = (String JavaDoc) params.get(ParametersConstantes.PROXYHOST);
155         log.log(BasicLevel.DEBUG, proxyHost);
156         String JavaDoc proxyPort = (String JavaDoc) params.get(ParametersConstantes.PROXYPORT);
157         log.log(BasicLevel.DEBUG, proxyHost);
158         String JavaDoc proxyUserName = (String JavaDoc) params
159                 .get(ParametersConstantes.PROXYUSERNAME);
160         String JavaDoc proxyUserPass = (String JavaDoc) params
161                 .get(ParametersConstantes.PROXYUSERPASS);
162         String JavaDoc preemptiveAuthentification = (String JavaDoc) params
163                 .get(ParametersConstantes.PREEMPTIVEAUTHENTIFICATION);
164
165         // init the http client
166
this.httpClient = new HttpClient(
167                 new MultiThreadedHttpConnectionManager());
168         // init variables to EMPTY if they are null
169
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 JavaDoc(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 JavaDoc e) {
206             throw new IsacRuntimeException(
207                     "Unable to set the SessionObject for the plugin HttpInjector -> "
208                             + e);
209         }
210
211     }
212
213     /**
214      * Constructor used in cloneObject method
215      *
216      * @param so
217      * The session object to clone
218      */

219     private SessionObject(SessionObject so) {
220         // init the host, port, username, userpass
221
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         // init the http client
228
this.httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
229         // if the proxy has been configured set it to
230
// the connection manager
231
try {
232             if (!proxyHost.equals("") && !proxyPort.equals("")) {
233                 this.setProxy(proxyHost, (new Integer JavaDoc(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                 //System.setProperties(java.net.Au,"true");
249
}
250         } catch (Exception JavaDoc e) {
251             throw new IsacRuntimeException(
252                     "Unable to set the SessionObject for the plugin HttpInjector -> "
253                             + e);
254         }
255         // XXX Presentation
256
this.userNameNumber = so.userNameNumber;
257         this.userNameNumber.setIntValue(this.userNameNumber.getIntValue()+1);
258         this.userName = new String JavaDoc(USERNAME + userNameNumber.getIntValue());
259     }
260
261
262     public ActionEvent doSample(int number, Hashtable JavaDoc params, ActionEvent report) {
263         // switch between the existing sample method
264
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 JavaDoc params) {
295         // switch between the existing timer method
296
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 JavaDoc params) {
308         // switch between the existing test method
309
switch (number) {
310             case TEST_IS404RESPONSE :
311                 return HttpInjectorTests.is404Response(this);
312             case TEST_ISSTATUSCODERESPONSE :
313                 int statusCode = new Integer JavaDoc((String JavaDoc) 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 JavaDoc headerParameterType = (String JavaDoc) params
322                         .get(ParametersConstantes.HEADERPARAMETERTYPE);
323                 String JavaDoc headerParameterValue = (String JavaDoc) 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     //
337

338     /**
339      * Method which configure the proxy
340      *
341      * @param host
342      * The hostname of the proxy server
343      * @param port
344      * The port name to go through the proxy
345      */

346     private void setProxy(String JavaDoc host, int port) {
347         // configure the proxy
348
HostConfiguration hostConfiguration = new HostConfiguration();
349         hostConfiguration.setProxy(host, port);
350         // add it to the connection manager
351
this.httpClient.setHostConfiguration(hostConfiguration);
352     }
353
354     private void setProxyAuthentification(String JavaDoc userName, String JavaDoc userPass) {
355         Credentials customCrds = new UsernamePasswordCredentials(userName,
356                 userPass);
357         this.httpClient.getState().setProxyCredentials(null, null, customCrds);
358
359     }
360
361     /////////////////////////////////////////
362
// SessionObjectAction implementation
363
/////////////////////////////////////////
364

365     /**
366      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#createNewSessionObject()
367      */

368     public Object JavaDoc createNewSessionObject() {
369         return new SessionObject(this);
370     }
371
372     /**
373      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#close()
374      */

375     public void close() {
376         // nothing to close
377
}
378
379     /**
380      * @see org.objectweb.clif.scenario.util.isac.util.SessionObjectAction#reset()
381      */

382     public void reset() {
383         this.lastStatusCode = -1;
384         this.lastHeader = null;
385         // XXX Presentation
386
this.userNameNumber.setIntValue(this.userNameNumber.getIntValue()+1);
387         this.userName = new String JavaDoc(USERNAME + this.userNameNumber.getIntValue());
388     }
389
390     ///////////////////////////////////////////////////
391
// Attribute getters and setters
392
///////////////////////////////////////////////////
393

394     /**
395      * @return Returns the httpClient.
396      */

397     public HttpClient getHttpClient() {
398         return httpClient;
399     }
400
401     /**
402      * @param httpClient
403      * The httpClient to set.
404      */

405     public void setHttpClient(HttpClient httpClient) {
406         this.httpClient = httpClient;
407     }
408
409     /**
410      * @return Returns the proxy host.
411      */

412     public String JavaDoc getProxyHost() {
413         return proxyHost;
414     }
415
416     /**
417      * @return Returns the proxy port.
418      */

419     public String JavaDoc getProxyPort() {
420         return proxyPort;
421     }
422
423     /**
424      * @return Returns the proxyUserName.
425      */

426     public String JavaDoc getProxyUserName() {
427         return proxyUserName;
428     }
429
430     /**
431      * @param proxyUserName
432      * The proxyUserName to set.
433      */

434     public void setProxyUserName(String JavaDoc proxyUserName) {
435         this.proxyUserName = proxyUserName;
436     }
437
438     /**
439      * @return Returns the proxyUserPass.
440      */

441     public String JavaDoc getProxyUserPass() {
442         return proxyUserPass;
443     }
444
445     /**
446      * @param proxyUserPass
447      * The proxyUserPass to set.
448      */

449     public void setProxyUserPass(String JavaDoc proxyUserPass) {
450         this.proxyUserPass = proxyUserPass;
451     }
452
453     /**
454      * @return Returns the authentificationPreemptive.
455      */

456     public String JavaDoc getAuthentificationPreemptive() {
457         return preemptiveAuthentification;
458     }
459
460     /**
461      * @param authentificationPreemptive
462      * The authentificationPreemptive to set.
463      */

464     public void setAuthentificationPreemptive(String JavaDoc authentificationPreemptive) {
465         this.preemptiveAuthentification = authentificationPreemptive;
466     }
467
468 // this method is only used in debug mode
469
// private static void waitTime(int milli) {
470
// try {
471
// System.out.print("waiting_time=" + milli);
472
// for (int i = 0; i < milli; i++) {
473
// // each 100 millisec wait 100 millisec
474
// if (i % 100 == 0) {
475
// System.out.print(".");
476
// System.out.flush();
477
// Thread.sleep(100);
478
// }
479
// }
480
// } catch (Exception e) {
481
// log
482
// .log(BasicLevel.DEBUG,
483
// "Exception occurs during waiting time...");
484
// }
485
// }
486

487     /**
488      * Method to get the StatusCode of the lastMethod executed
489      *
490      * @return Returns the lastStatusCode.
491      */

492     public int getLastStatusCode() {
493         return lastStatusCode;
494     }
495
496     /**
497      * Method to set the StatusCode of the lastMethod executed
498      *
499      * @param lastStatusCode
500      * The lastStatusCode to set.
501      */

502     public void setLastStatusCode(int lastStatusCode) {
503         this.lastStatusCode = lastStatusCode;
504     }
505
506     /**
507      * Method to get the Headers from the last method executed
508      *
509      * @return Returns the lastHeader.
510      */

511     public Header[] getLastHeader() {
512         return lastHeader;
513     }
514
515     /**
516      * Method to set the Headers of the last method executed
517      *
518      * @param lastHeader
519      * The lastHeader to set.
520      */

521     public void setLastHeader(Header[] lastHeader) {
522         this.lastHeader = lastHeader;
523     }
524
525     /**
526      * Method to get the Response from the last method executed
527      *
528      * @return Returns the lastResponseByte.
529      */

530     public String JavaDoc getLastResponseBody() {
531         return lastResponseBody;
532     }
533     /**
534      * Method to set the Response body of the last method executed
535      *
536      * @param lastResponseBody he body part of the last response received
537      */

538     public void setLastResponseBody(String JavaDoc lastResponseBody) {
539         this.lastResponseBody = lastResponseBody;
540     }
541     /**
542      * @return Returns the userName.
543      */

544     public String JavaDoc getUserName() {
545         return userName;
546     }
547 }
Popular Tags