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 private HttpClient httpClient = new HttpClient();
43 private HttpMethod httpMethod = null;
44 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 super(iterations, minDelay, maxDelay, sigma, delayType, rampPeriod,
54 peakPeriod, troughPeriod, cycleType, rampType, logger,
55 stats);
56
57 httpClient.getParams().setSoTimeout(socketTimeout);
58 if (method.trim().toUpperCase().equals("POST")) {
59 httpMethod = new PostMethod(url);
60 } else {
61 httpMethod = new GetMethod(url);
62 }
63 this.successKey = successKey;
64 }
65
66
67 public void setUp() throws Exception {}
68
69
70
71
72
73
74
75 public void execute() throws Exception {
76 int statusCode = httpClient.executeMethod(httpMethod);
77 if (statusCode != HttpStatus.SC_OK) {
78 throw new HttpException("Request failed with status code: "
79 + statusCode);
80 }
81
82 String responseBody = httpMethod.getResponseBodyAsString();
83 if (successKey != null && responseBody.indexOf(successKey) < 0 ) {
84 throw new HttpException("Response did not include success key: "
85 + successKey);
86 }
87 }
88
89
90 public void cleanUp() throws Exception {
91 httpMethod.releaseConnection();
92 }
93
94 }