HttpClientHandler.java
10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package com.ecar.apm.http.client;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONPath;
import com.ecar.apm.model.Application;
import com.ecar.apm.model.HttpRequest;
import com.ecar.apm.model.HttpRequest.HttpMethod;
import com.ecar.apm.model.HttpRequest.ResultType;
import com.ecar.apm.model.HttpRequestLog;
import com.ecar.apm.util.XmlUtil;
import com.github.pagehelper.StringUtil;
import com.google.common.xml.XmlEscapers;
public class HttpClientHandler {
protected static final Logger LOGGER = LoggerFactory.getLogger(HttpClientHandler.class);
// Convert mill seconds to second unit
protected static final int MS_TO_S_UNIT = 1000;
// Normal http response code
protected static final String NORMAL_RESPONSE_CODE = "200";
// https prefix
protected static final String HTTPS = "https";
protected static HttpsTrustManager httpsTrustManager = new HttpsTrustManager();
private HttpSequenceHandle httpSequenceHandle;
private HttpEntity httpEntity;
private HttpRequest httpRequest;
protected String output;
public HttpClientHandler(HttpRequest httpRequest, HttpSequenceHandle httpSequenceHandle) {
this.httpRequest = httpRequest;
this.httpSequenceHandle = httpSequenceHandle;
}
public void httpEntity(HttpEntity httpEntity) {
this.httpEntity = httpEntity;
}
/*
* Convert response as string
*/
protected String responseAsString(CloseableHttpResponse response) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
response.getEntity().writeTo(baos);
return new String(baos.toByteArray(), Application.ENCODING);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
public HttpRequestLog execute(){
long start = System.currentTimeMillis();
String statusCode = null;
String body = null;
try{
CloseableHttpResponse response = sendRequest();
statusCode = String.valueOf(getStatusCode(response));
body = getResponseBody(response);
response.close();
validResponse(body, statusCode);
handleVariables(body);
}catch(Exception e){
appendMessage(e.toString());
LOGGER.error("Send request to url[" + httpRequest.getUrl() + "] failed", e);
}
HttpRequestLog requestLog = new HttpRequestLog();
requestLog.setCostTime(System.currentTimeMillis() - start);
requestLog.setStatus(StringUtil.isEmpty(output));
requestLog.setStatusCode(statusCode);
requestLog.setResponseBody(body);
requestLog.setLog(output);
return requestLog;
}
protected CloseableHttpResponse sendRequest() throws ClientProtocolException, IOException {
RequestBuilder builder = createRequestBuilder();
addRequestParams(builder);
setHttpEntity(builder);
HttpUriRequest request = builder.setUri(httpRequest.getUrl()).build();
setHeaders(request);
CloseableHttpClient client = createHttpClient();
return client.execute(request);
}
protected String getResponseBody(CloseableHttpResponse httpResponse) throws ParseException, IOException{
if (httpResponse == null)return null;
HttpEntity entity = httpResponse.getEntity();
if(entity == null)return null;
String webPage = EntityUtils.toString(entity,"UTF-8");
return webPage;
}
protected int getStatusCode(CloseableHttpResponse httpResponse){
int status = httpResponse.getStatusLine().getStatusCode();
return status;
}
protected void validResponse(String body, String statusCode) throws Exception {
switch (httpRequest.getConditionType()) {
case CONTAINS:
if (StringUtil.isEmpty(body) || !body.contains(httpRequest.getCondition())) {
appendMessage(httpRequest.getUrl() + " doesn't contain "
+ XmlEscapers.xmlContentEscaper().escape(httpRequest.getCondition()));
}
break;
case DOESNT_CONTAIN:
if (StringUtil.isEmpty(body) || body.contains(httpRequest.getCondition())) {
appendMessage(httpRequest.getUrl() + " contains "
+ XmlEscapers.xmlContentEscaper().escape(httpRequest.getCondition()));
}
break;
case STATUSCODE:
if (!statusCode.equals(httpRequest.getCondition())) {
appendMessage("Invalid status: " + httpRequest.getUrl() + " required: " + httpRequest.getCondition() + ", received: " + statusCode);
}
break;
default:
if (!"200".equals(statusCode)) {
appendMessage("Invalid status: " + httpRequest.getUrl() + " required: " + 200 + ", received: " + statusCode);
}
break;
}
}
protected void handleVariables(String body) throws Exception {
if(StringUtil.isEmpty(body))return;
HashMap<String, String> variables = httpRequest.getVariablesMap();
ResultType type = httpRequest.getResultType();
if(variables == null || type == null)return;
if(type.equals(ResultType.JSON)){
for (String variableName : variables.keySet()) {
String variablePath = variables.get(variableName);
Object variableValue = JSONPath.read(body, variablePath);
this.httpSequenceHandle.setVariables(variableName, variableValue.toString());
}
}else if(type.equals(ResultType.XML)){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse(IOUtils.toInputStream(XmlUtil.replaceChar(body), "UTF-8"));
XPathFactory xPathfactory = XPathFactory.newInstance();
for (String variableName : variables.keySet()) {
String variablePath = variables.get(variableName);
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(variablePath);
// TODO Retrieve whole XML fragment
String variableValue = expr.evaluate(doc);
this.httpSequenceHandle.setVariables(variableName, variableValue);
}
}
}
public String getOutput() {
return output;
}
protected void appendMessage(String message) {
if (output == null) {
output = "";
}
if (message != null && !message.trim().isEmpty()) {
output += message;
}
}
protected void addRequestParams(RequestBuilder builder) {
HashMap<String, String> map = httpRequest.getParametersMap();
if(map==null || map.size()==0)return;
for (String key : map.keySet()) {
String val = map.get(key);
builder.addParameter(key, useVariable(val));
}
}
protected String useVariable(String val){
if(!val.startsWith("$$")){
return val;
}else{
String result = this.httpSequenceHandle.getVariables(val);
return result == null ? val : result;
}
}
protected void setHttpEntity(RequestBuilder builder) {
if (this.httpEntity != null) {
builder.setEntity(this.httpEntity);
}
}
protected void setHeaders(HttpUriRequest request) {
HashMap<String, String> map = httpRequest.getHeadersMap();
if(map==null || map.size()==0)return;
for (String key : map.keySet()) {
request.addHeader(key, map.get(key));
}
}
protected CloseableHttpClient createHttpClient() {
final RequestConfig requestConfig = requestConfig();
HttpClientBuilder httpClientBuilder;
if (isHttps()) {
// Support SSL
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(createSSLContext());
httpClientBuilder = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslConnectionSocketFactory);
} else {
httpClientBuilder = HttpClients.custom().setDefaultRequestConfig(requestConfig);
}
if(httpSequenceHandle!=null){
httpClientBuilder.setDefaultCookieStore(httpSequenceHandle.cookieStore);
}
return httpClientBuilder.build();
}
private RequestConfig requestConfig() {
final int maxConnMillSeconds = httpRequest.getMaxConnectionSeconds() * MS_TO_S_UNIT;
return RequestConfig.custom().setSocketTimeout(maxConnMillSeconds).setConnectTimeout(maxConnMillSeconds).build();
}
private SSLContext createSSLContext() {
try {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new HttpsTrustManager[] { httpsTrustManager }, null);
return sslContext;
} catch (Exception e) {
throw new IllegalStateException("Create SSLContext error", e);
}
}
protected boolean isHttps() {
return httpRequest.getUrl().toLowerCase().startsWith(HTTPS);
}
protected RequestBuilder createRequestBuilder() {
if (httpRequest.getHttpMethod().equals(HttpMethod.GET)) {
return RequestBuilder.get();
} else if (httpRequest.getHttpMethod().equals(HttpMethod.POST)) {
return RequestBuilder.post();
} else if (httpRequest.getHttpMethod().equals(HttpMethod.HEAD)) {
return RequestBuilder.head();
} else if (httpRequest.getHttpMethod().equals(HttpMethod.PUT)) {
return RequestBuilder.put();
} else if (httpRequest.getHttpMethod().equals(HttpMethod.DELETE)) {
return RequestBuilder.delete();
} else {
return null;
}
}
/**
* Default X509TrustManager implement
*/
private static class HttpsTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
// ignore
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
// ignore
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
}