KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > isac > plugins > httpmatrix10 > actions > MatrixSamples


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 package org.objectweb.clif.isac.plugins.httpmatrix10.actions;
24
25 import java.io.IOException JavaDoc;
26 import java.util.Hashtable JavaDoc;
27
28 import org.apache.commons.httpclient.HttpMethod;
29 import org.apache.commons.httpclient.methods.GetMethod;
30 import org.apache.commons.httpclient.methods.PostMethod;
31 import org.objectweb.clif.isac.plugins.httpmatrix10.SessionObject;
32 import org.objectweb.clif.scenario.util.transitions.Transition;
33 import org.objectweb.clif.scenario.util.transitions.TransitionTable;
34 import org.objectweb.clif.storage.api.ActionEvent;
35
36 /**
37  * This class implements the matrix sample methods
38  *
39  * @author JC Meillaud
40  * @author A Peyrard
41  */

42 public class MatrixSamples {
43     // definition of constants
44
private static final String JavaDoc METHOD = "method";
45     private static final String JavaDoc POST_METHOD = "POST";
46     private static final String JavaDoc GET_METHOD = "GET";
47
48     /**
49      * Sample do next, execute the next url
50      *
51      * @param sessionObject
52      * The sessionObject
53      */

54     public static ActionEvent doNext(SessionObject sessionObject, Hashtable JavaDoc params,
55             ActionEvent report) {
56         // get the method value
57
String JavaDoc methodType = (String JavaDoc)params.get(METHOD);
58         // Set current date
59
report.setDate(System.currentTimeMillis());
60         // get the transition table
61
TransitionTable transitionTable = sessionObject.getTransitionTable();
62         // get the next transition
63
Transition transition = transitionTable.getNextTransition();
64
65         // if the table is finished
66
if (transition.isEndSession()) {
67             report.successful = true;
68             report.comment = "endSession";
69             report.duration = 0;
70             // set the finish state in the session object
71
sessionObject.setFinishState(true);
72             return report;
73         }
74         // get the actions for this state
75
String JavaDoc[] actions = transition.getAction();
76         // get the url
77
String JavaDoc url = actions[0];
78         // create the http method
79
HttpMethod method = null;
80         if (methodType.equals(GET_METHOD)) {
81             method = new GetMethod(url);
82         }
83         else if (methodType.equals(POST_METHOD)) {
84             method = new PostMethod(url);
85         }
86         else {
87             report.successful = false;
88             report.comment = "Unknow http injection method : "+methodType;
89             report.duration = 0;
90             return report;
91         }
92         method.setFollowRedirects(true);
93         method.setStrictMode(false);
94         // Set report comment
95
report.comment = url;
96         // exceute the method
97
try {
98             sessionObject.getHttpClient().executeMethod(method);
99             // get response status
100
report.result = method.getStatusCode() + " - "
101                     + method.getStatusText();
102             report.successful = true;
103         } catch (IOException JavaDoc ioex) {
104             report.comment = "Unable to execute http method on " + url + " ->"
105                     + ioex;
106             report.successful = false;
107         } finally {
108             // always release the connection after we're done
109
method.releaseConnection();
110         }
111
112         // update the report datas
113
report.duration = (int) (System.currentTimeMillis() - report.getDate());
114
115         // return the report datas
116
return report;
117     }
118 }
Popular Tags