-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathHttpClient.java
More file actions
315 lines (275 loc) · 9.3 KB
/
Copy pathHttpClient.java
File metadata and controls
315 lines (275 loc) · 9.3 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
/**
* © Copyright IBM Corporation 2016.
* © Copyright HCL Technologies Ltd. 2017.
* LICENSE: Apache License, Version 2.0 https://www.apache.org/licenses/LICENSE-2.0
*/
package com.hcl.appscan.sdk.http;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class HttpClient {
private String m_boundary;
private long m_totalMultipartLength;
private long m_uploadedMultipartLength;
private static final String CR_LF = "\r\n"; //$NON-NLS-1$
private static final String TWO_HYPHENS = "--"; //$NON-NLS-1$
private IHttpProgress m_progressAdapter;
public enum Method {
GET, POST, PUT, DELETE;
}
public HttpClient(IHttpProgress progressAdapter) {
m_progressAdapter = progressAdapter;
}
public HttpClient() {
this(new DefaultHttpProgress());
}
// ==============================
// HTTP request methods
// ==============================
/**
* Submit a get request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param body An optional request body or payload as a string.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse get(String url,
Map<String, String> headerProperties, String body)
throws IOException {
return makeRequest(Method.GET, url, headerProperties, body);
}
/**
* Submit a post request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param body An optional request body or payload as a string.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse post(String url,
Map<String, String> headerProperties, String body)
throws IOException {
if (body==null) body = ""; //$NON-NLS-1$
return makeRequest(Method.POST, url, headerProperties, body);
}
/**
* Submit a put request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param body An optional request body or payload as a string.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse put(String url,
Map<String, String> headerProperties, String body)
throws IOException {
return makeRequest(Method.PUT, url, headerProperties, body);
}
/**
* Submit a delete request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param body An optional request body or payload as a string.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse delete(String url,
Map<String, String> headerProperties, String body)
throws IOException {
return makeRequest(Method.DELETE, url, headerProperties, body);
}
/**
* Submit a form with parameters using the get request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param params An optional Map of parameters.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse getForm(String url,
Map<String, String> headerProperties, Map<String, String> params)
throws IOException {
String body = buildQueryString(params);
return get(url, headerProperties, body);
}
/**
* Submit a form with parameters using the post request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param params An optional Map of parameters.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse postForm(String url,
Map<String, String> headerProperties, Map<String, String> params)
throws IOException {
String body = buildQueryString(params);
return post(url, headerProperties, body);
}
/**
* Submit a form with parameters using the put request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param params An optional Map of parameters.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse putForm(String url,
Map<String, String> headerProperties, Map<String, String> params)
throws IOException {
String body = buildQueryString(params);
return put(url, headerProperties, body);
}
/**
* Submit a form with parameters using the delete request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param params An optional Map of parameters.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse deleteForm(String url,
Map<String, String> headerProperties, Map<String, String> params)
throws IOException {
String body = buildQueryString(params);
return delete(url, headerProperties, body);
}
/**
* Submit a multipart entity using the post request.
*
* @param url The URL string.
* @param headerProperties An optional Map of header properties.
* @param params An optional Map of parameters.
* @return The response as a byte array.
* @throws IOException
*/
public HttpResponse postMultipart(String url,
Map<String, String> headerProperties, List<HttpPart> parts)
throws IOException {
m_boundary = "*****"+Long.toString(System.currentTimeMillis())+"*****"; //$NON-NLS-1$ //$NON-NLS-2$
headerProperties.put("Content-Type", "multipart/form-data; boundary=" + m_boundary); //$NON-NLS-1$ //$NON-NLS-2$
return makeMultipartRequest(Method.POST, url, headerProperties, parts);
}
private HttpResponse makeMultipartRequest(Method method, String url,
Map<String, String> headerProperties, List<HttpPart> parts)
throws IOException {
HttpURLConnection conn = makeConnection(url, method, headerProperties);
DataOutputStream outputStream = null;
conn.setChunkedStreamingMode(1024);
if (parts!=null && !parts.isEmpty()) {
conn.setDoOutput(true);
conn.setUseCaches(false);
outputStream = new DataOutputStream(conn.getOutputStream());
m_uploadedMultipartLength = 0;
m_totalMultipartLength = getTotalPartsLength(parts);
updateProgress();
StringBuilder builder;
Map<String, String> partHeaders;
for (HttpPart part : parts) {
builder = new StringBuilder();
builder.append(TWO_HYPHENS+m_boundary+CR_LF);
partHeaders = part.getPartHeaders();
Iterator<String> headers = partHeaders.keySet().iterator();
String header;
while (headers.hasNext()) {
header = headers.next();
builder.append(header + ": " +partHeaders.get(header)+CR_LF); //$NON-NLS-1$
}
outputStream.writeBytes(builder.toString()+CR_LF);
outputStream.flush();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = part.getPartBodyInputStream().read(buffer)) != -1)
{
outputStream.write(buffer, 0, bytesRead);
m_uploadedMultipartLength += bytesRead;
updateProgress();
}
outputStream.writeBytes(CR_LF);
outputStream.flush();
}
outputStream.writeBytes(TWO_HYPHENS+m_boundary+TWO_HYPHENS+CR_LF);
outputStream.flush();
updateProgress();
}
outputStream.flush();
outputStream.close();
m_progressAdapter.endProgress();
return new HttpResponse(conn);
}
private HttpResponse makeRequest(Method method, String url,
Map<String, String> headerProperties, String payload)
throws IOException {
HttpURLConnection conn = makeConnection(url, method, headerProperties);
// Write payload
if (payload != null) {
conn.setDoOutput(true);
DataOutputStream writer = new DataOutputStream(
conn.getOutputStream());
writer.writeBytes(payload);
writer.flush();
writer.close();
}
return new HttpResponse(conn);
}
private HttpURLConnection makeConnection(String url, Method method,
Map<String, String> headerProperties) throws IOException {
URL requestURL = new URL(url);
HttpURLConnection conn = null;
conn = (HttpURLConnection) requestURL.openConnection();
conn.setRequestMethod(method.name());
conn.setReadTimeout(0);
// HTTP headers
if (headerProperties != null) {
for (String key : headerProperties.keySet()) {
conn.setRequestProperty(key, headerProperties.get(key));
}
}
return conn;
}
private long getTotalPartsLength(List<HttpPart> parts) {
long totalSize = 0;
for (HttpPart part : parts) {
totalSize += part.getPartLength();
}
return totalSize;
}
private void updateProgress() {
int progress = (int) ((float)m_uploadedMultipartLength/m_totalMultipartLength*100);
m_progressAdapter.setProgress(progress);
}
private String buildQueryString(Map<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
if (params == null)
return ""; //$NON-NLS-1$
Iterator<String> iter = params.keySet().iterator();
while (iter.hasNext()) {
String key = iter.next();
String value = params.get(key);
if (first)
first = false;
else
result.append("&"); //$NON-NLS-1$
result.append(URLEncoder.encode(key, "UTF-8")); //$NON-NLS-1$
result.append("="); //$NON-NLS-1$
result.append(URLEncoder.encode(value, "UTF-8")); //$NON-NLS-1$
}
return result.toString();
}
}