-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractHttpOperate.java
209 lines (192 loc) · 10.2 KB
/
AbstractHttpOperate.java
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
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.codehaus.jackson.type.JavaType;
import test.HttpException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* Created with IntelliJ IDEA.
* User: WuYifei
* Date: 2017/7/21
* Time: 17:02
*/
public abstract class AbstractHttpOperate {
public static final int BODY_SECURE_MODE = 0;
public static final int PARAMS_SECURE_MODE = 1;
public static final String SECURITY = "security";
public static final String CERT_ID = "certId";
public static final String SECURE_MODE = "SECURE_MODE";
/**
* 执行HTTP请求任务
*
* @param httpJob
* @param <X>
* @return
* @throws HttpException
*/
protected static <X> X execute(HttpJob<X> httpJob) throws HttpException {
try {
JobExecutor.executeInCurrentThread(httpJob);
return httpJob.getResult();
} catch (JobException e) {
throw new HttpException(e.getErrorCode(), e.getMessage(), e.getCause());
}
}
protected void setHttpPostParams(HttpPost httpPost, List<NameValuePair> nvps) throws UnsupportedEncodingException {
if (CollectionUtils.isEmpty(nvps)) {
nvps = new ArrayList<NameValuePair>();
}
List<String> paramList = new ArrayList<String>();
nvps.add(new BasicNameValuePair(CERT_ID, getCertId()));
for (NameValuePair nameValuePair : nvps) {
paramList.add(nameValuePair.getName() + "=" + nameValuePair.getValue());
}
Collections.sort(paramList);
StringBuilder stringBuilder = new StringBuilder();
for (String param : paramList) {
stringBuilder.append(param);
}
String security = generateSecurityContent(stringBuilder.toString());
nvps.add(new BasicNameValuePair(SECURITY, security));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, "UTF-8");
httpPost.setEntity(entity);
}
protected void setHttpPostParam(HttpPost httpPost, String name, String value) throws UnsupportedEncodingException {
// Assert.notNull(name, "参数名不能为空");
// Assert.notNull(value, "参数值不能为空");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair(name, value));
setHttpPostParams(httpPost, nvps);
}
protected String generateSecurityContent(String content) {
byte[] base64 = Base64.encodeBase64(DigestUtils.sha(content + getCertKey()));
return new String(base64, Charset.forName("utf8"));
}
protected void handleResponse(HttpResponse response) throws HttpException, IOException {
JsonResponse jsonResponse = handleJsonResponse(response);
if (JsonResponse.RETURN_SUCCESS != jsonResponse.getReturnCode()) {
throw new HttpException(jsonResponse.getReturnCode(), jsonResponse.getReturnMsg());
}
}
protected JsonResponse handleJsonResponse(HttpResponse response) throws HttpException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
JsonMapper mapper = JsonMapper.getDefault();
return mapper.fromJson(httpEntity.getContent(), JsonResponse.class);
} else {
throw new HttpException(statusLine.getStatusCode(), String.format("HTTP请求发生错误,状态码:%d,错误原因:%s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
}
}
protected <T> T handleResponse(HttpResponse response, Class<T> clazz) throws HttpException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
JsonMapper mapper = JsonMapper.getDefault();
JsonResponse<T> jsonResponse = mapper.fromJson(httpEntity.getContent(), mapper.constructParametricType(JsonResponse.class, clazz));
if (jsonResponse == null) {
jsonResponse = mapper.fromJson(httpEntity.getContent(), mapper.constructParametricType(String.class));
}
if (jsonResponse.getReturnCode() == JsonResponse.RETURN_SUCCESS) {
return jsonResponse.getContent();
} else {
throw new HttpException(jsonResponse.getReturnCode(), jsonResponse.getReturnMsg());
}
} else {
throw new HttpException(statusLine.getStatusCode(), String.format("HTTP请求发生错误,状态码:%d,错误原因:%s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
}
}
protected <T> T handleResponse(HttpResponse response, JavaType javaType) throws HttpException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
JsonMapper mapper = JsonMapper.getDefault();
JsonResponse<T> jsonResponse = mapper.fromJson(httpEntity.getContent(), mapper.constructParametricType(JsonResponse.class, javaType));
if (jsonResponse == null) {
jsonResponse = mapper.fromJson(httpEntity.getContent(), mapper.constructParametricType(String.class));
}
if (jsonResponse.getReturnCode() == JsonResponse.RETURN_SUCCESS) {
return jsonResponse.getContent();
} else {
throw new HttpException(jsonResponse.getReturnCode(), jsonResponse.getReturnMsg());
}
} else {
throw new HttpException(statusLine.getStatusCode(), String.format("HTTP请求发生错误,状态码:%d,错误原因:%s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
}
}
protected <X> List<X> handleListResponse(HttpResponse response, Class clazz) throws HttpException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
JsonMapper mapper = JsonMapper.getDefault();
JavaType pageType = mapper.constructParametricType(List.class, clazz);
JavaType javaType = mapper.constructParametricType(JsonResponse.class, pageType);
JsonResponse<List<X>> jsonResponse = mapper.fromJson(httpEntity.getContent(), javaType);
if (jsonResponse == null) {
pageType = mapper.constructParametricType(List.class, String.class);
javaType = mapper.constructParametricType(JsonResponse.class, pageType);
jsonResponse = mapper.fromJson(httpEntity.getContent(), javaType);
}
if (jsonResponse.getReturnCode() == JsonResponse.RETURN_SUCCESS) {
return jsonResponse.getContent();
} else {
throw new HttpException(jsonResponse.getReturnCode(), jsonResponse.getReturnMsg());
}
} else {
throw new HttpException(statusLine.getStatusCode(), String.format("HTTP请求发生错误,状态码:%d,错误原因:%s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
}
}
protected Map handleMapResponse(HttpResponse response, Class key, Class content) throws HttpException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
JsonMapper mapper = JsonMapper.getDefault();
JavaType sub = mapper.constructParametricType(content);
JavaType keyType = mapper.constructParametricType(key);
JavaType mapType = mapper.constructParametricType(Map.class, new JavaType[]{keyType, sub});
JavaType javaType = mapper.constructParametricType(JsonResponse.class, mapType);
JsonResponse<Map> jsonResponse = mapper.fromJson(httpEntity.getContent(), javaType);
if (jsonResponse.getReturnCode() == JsonResponse.RETURN_SUCCESS) {
return jsonResponse.getContent();
} else {
throw new HttpException(jsonResponse.getReturnCode(), jsonResponse.getReturnMsg());
}
} else {
throw new HttpException(statusLine.getStatusCode(), String.format("HTTP请求发生错误,状态码:%d,错误原因:%s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
}
}
protected Map handleMapListResponse(HttpResponse response, Class key, Class content) throws HttpException, IOException {
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == 200) {
HttpEntity httpEntity = response.getEntity();
JsonMapper mapper = JsonMapper.getDefault();
JavaType sub = mapper.constructParametricType(List.class, content);
JavaType keyType = mapper.constructParametricType(key);
JavaType mapType = mapper.constructParametricType(Map.class, new JavaType[]{keyType, sub});
JavaType javaType = mapper.constructParametricType(JsonResponse.class, mapType);
JsonResponse<Map> jsonResponse = mapper.fromJson(httpEntity.getContent(), javaType);
if (jsonResponse.getReturnCode() == JsonResponse.RETURN_SUCCESS) {
return jsonResponse.getContent();
} else {
throw new HttpException(jsonResponse.getReturnCode(), jsonResponse.getReturnMsg());
}
} else {
throw new HttpException(statusLine.getStatusCode(), String.format("HTTP请求发生错误,状态码:%d,错误原因:%s", statusLine.getStatusCode(), statusLine.getReasonPhrase()));
}
}
public abstract String getCertId();
public abstract String getCertKey();
}