-
Notifications
You must be signed in to change notification settings - Fork 4
/
HttpUtils.java
241 lines (209 loc) · 8.54 KB
/
HttpUtils.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
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
/**
* Copyright 2018 BlazeMeter Inc.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.blazemeter.api.http;
import com.blazemeter.api.exception.InterruptRuntimeException;
import com.blazemeter.api.logging.Logger;
import net.sf.json.JSONObject;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.apache.commons.lang.StringUtils;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.*;
/**
* Class for working with HTTP requests
*/
public class HttpUtils {
public static final String PROXY_HOST = "http.proxyHost";
public static final String PROXY_PORT = "http.proxyPort";
public static final String PROXY_USER = "http.proxyUser";
public static final String PROXY_PASS = "http.proxyPass";
protected static final String ACCEPT = "Accept";
protected static final String APP_JSON = "application/json";
protected static final String CONTENT_TYPE = "Content-type";
protected static final String APP_JSON_UTF_8 = "application/json; charset=UTF-8";
protected static final String AUTHORIZATION = "Authorization";
protected static final MediaType JSON_CONTENT = MediaType.parse("application/json; charset=utf-8");
protected static final MediaType FILE_STREAM = MediaType.parse("application/x-www-form-urlencoded");
protected Logger logger;
protected OkHttpClient httpClient;
protected final ExecutorService service = Executors.newFixedThreadPool(4);
public HttpUtils(Logger logger) {
this.logger = logger;
this.httpClient = createHTTPClient();
}
/**
* Create Get Request
*/
public Request createGet(String url) {
return createRequestBuilder(url).get().build();
}
/**
* Create Post Request with json body
*/
public Request createPost(String url, RequestBody data) {
return createRequestBuilder(url).post(data).build();
}
/**
* Create Post Request with json body
*/
public Request createPost(String url, String data) {
return createRequestBuilder(url).post(RequestBody.create(JSON_CONTENT, data)).build();
}
/**
* Create Post Request with json body
*/
public Request createPost(String url, File data) {
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.addPart(MultipartBody.Part.createFormData("file", data.getAbsolutePath(), RequestBody.create(FILE_STREAM, data)));
bodyBuilder.setType(MultipartBody.FORM);
return createRequestBuilder(url).post(bodyBuilder.build()).build();
}
/**
* Create Patch Request
*/
public Request createPatch(String url, String data) {
return createRequestBuilder(url).patch(RequestBody.create(JSON_CONTENT, data)).build();
}
/**
* Create Patch Request
*/
public Request createPatch(String url, RequestBody data) {
return createRequestBuilder(url).patch(data).build();
}
/**
* Execute Http request
* @param request - HTTP Request
* @return - response in JSONObject
*/
public JSONObject execute(Request request) throws IOException {
return processResponse(executeRequest(request));
}
protected JSONObject processResponse(String response) {
return JSONObject.fromObject(response);
}
/**
* Execute Http request
* @param request - HTTP Request
* @return - response in String
*/
public String executeRequest(Request request) throws IOException {
RequestTask task = new RequestTask(httpClient, request);
Future<Response> future = service.submit(task);
Response response;
try {
response = future.get();
} catch (InterruptedException e) {
future.cancel(true);
logger.warn("Caught InterruptedException ", e);
throw new InterruptRuntimeException("Request has been interrupted", e);
} catch (ExecutionException e) {
future.cancel(true);
logger.warn("Caught ExecutionException ", e);
throw new IOException(e.getMessage(), e);
}
return response.body().string();
}
protected String extractErrorMessage(String response) {
return response;
}
protected Request.Builder addRequiredHeader(Request.Builder requestBuilder) {
// NOOP
return requestBuilder;
}
private Request.Builder createRequestBuilder(String url) {
final Request.Builder builder = new Request.Builder().url(modifyRequestUrl(url)).
addHeader(ACCEPT, APP_JSON).
addHeader(CONTENT_TYPE, APP_JSON_UTF_8);
return addRequiredHeader(builder);
}
/**
* Override this method if you want add some require additional params to your URL
*/
protected String modifyRequestUrl(String url) {
return url;
}
public Logger getLogger() {
return logger;
}
public void setLogger(Logger logger) {
this.logger = logger;
}
protected OkHttpClient createHTTPClient() {
Proxy proxy = Proxy.NO_PROXY;
Authenticator auth = Authenticator.NONE;
try {
String proxyHost = System.getProperty(PROXY_HOST);
if (!StringUtils.isBlank(proxyHost)) {
logger.info("Using http.proxyHost = " + proxyHost);
int proxyPort = getProxyPort();
proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
auth = createAuthenticator();
}
HttpLoggingInterceptor httpLog = new HttpLoggingInterceptor(new HttpLogger(logger));
httpLog.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder()
.addInterceptor(new RetryInterceptor(logger))
.addInterceptor(httpLog)
.connectTimeout(180, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.proxy(proxy)
.proxyAuthenticator(auth).build();
} catch (Exception ex) {
logger.warn("ERROR Instantiating HTTPClient. Exception received: " + ex.getMessage(), ex);
throw new RuntimeException("ERROR Instantiating HTTPClient. Exception received: " + ex.getMessage(), ex);
}
}
private int getProxyPort() {
try {
int proxyPort = Integer.parseInt(System.getProperty(PROXY_PORT));
logger.info("Using http.proxyPort = " + proxyPort);
return proxyPort;
} catch (NumberFormatException nfe) {
logger.warn("Failed to read http.proxyPort: ", nfe);
return 8080;
}
}
private Authenticator createAuthenticator() {
final String proxyUser = System.getProperty(PROXY_USER);
logger.info("Using http.proxyUser = " + proxyUser);
final String proxyPass = System.getProperty(PROXY_PASS);
logger.info("Using http.proxyPass = " + StringUtils.left(proxyPass, 4));
if (!StringUtils.isBlank(proxyUser) && !StringUtils.isBlank(proxyPass)) {
return new AuthenticatorExt(proxyUser, proxyPass);
}
return Authenticator.NONE;
}
protected static class AuthenticatorExt implements Authenticator {
private final String proxyUser;
private final String proxyPass;
public AuthenticatorExt(String proxyUser, String proxyPass) {
this.proxyUser = proxyUser;
this.proxyPass = proxyPass;
}
// https://github.com/square/okhttp/wiki/Recipes#handling-authentication
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(proxyUser, proxyPass);
if (response.request().header("Proxy-Authorization") != null) {
return null; // Give up, we've already attempted to authenticate.
}
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
}
}