1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.commons.performance.http; |
19 | |
|
20 | |
import java.util.logging.Logger; |
21 | |
|
22 | |
import org.apache.commons.httpclient.HttpClient; |
23 | |
import org.apache.commons.httpclient.HttpException; |
24 | |
import org.apache.commons.httpclient.HttpMethod; |
25 | |
import org.apache.commons.httpclient.HttpStatus; |
26 | |
import org.apache.commons.httpclient.methods.GetMethod; |
27 | |
import org.apache.commons.httpclient.methods.PostMethod; |
28 | |
|
29 | |
import org.apache.commons.performance.ClientThread; |
30 | |
import org.apache.commons.performance.Statistics; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class HttpClientThread extends ClientThread { |
41 | |
|
42 | 0 | private HttpClient httpClient = new HttpClient(); |
43 | 0 | private HttpMethod httpMethod = null; |
44 | 0 | private String successKey = null; |
45 | |
|
46 | |
public HttpClientThread(long iterations, long minDelay, long maxDelay, |
47 | |
double sigma, String delayType, long rampPeriod, |
48 | |
long peakPeriod, long troughPeriod, String cycleType, |
49 | |
String rampType, Logger logger, |
50 | |
Statistics stats, String url, String method, |
51 | |
int socketTimeout, String successKey) { |
52 | |
|
53 | 0 | super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod, |
54 | |
peakPeriod, troughPeriod, cycleType, rampType, logger, |
55 | |
stats); |
56 | |
|
57 | 0 | httpClient.getParams().setSoTimeout(socketTimeout); |
58 | 0 | if (method.trim().toUpperCase().equals("POST")) { |
59 | 0 | httpMethod = new PostMethod(url); |
60 | |
} else { |
61 | 0 | httpMethod = new GetMethod(url); |
62 | |
} |
63 | 0 | this.successKey = successKey; |
64 | 0 | } |
65 | |
|
66 | |
|
67 | 0 | public void setUp() throws Exception {} |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public void execute() throws Exception { |
76 | 0 | int statusCode = httpClient.executeMethod(httpMethod); |
77 | 0 | if (statusCode != HttpStatus.SC_OK) { |
78 | 0 | throw new HttpException("Request failed with status code: " |
79 | |
+ statusCode); |
80 | |
} |
81 | |
|
82 | 0 | String responseBody = httpMethod.getResponseBodyAsString(); |
83 | 0 | if (successKey != null && responseBody.indexOf(successKey) < 0 ) { |
84 | 0 | throw new HttpException("Response did not include success key: " |
85 | |
+ successKey); |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
|
90 | |
public void cleanUp() throws Exception { |
91 | 0 | httpMethod.releaseConnection(); |
92 | 0 | } |
93 | |
|
94 | |
} |