Skip to content

Commit 464e5eb

Browse files
Updated HTTP call to apache http client
1 parent 4875f88 commit 464e5eb

3 files changed

Lines changed: 137 additions & 11 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.nexttimespace.utilities</groupId>
66
<artifactId>json-diff-report</artifactId>
7-
<version>0.0.2</version>
7+
<version>0.0.3</version>
88
<description>Provides JSON differences in HTML format</description>
99

1010
<build>

src/main/java/com/nexttimespace/utilities/diffbox/Executor.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void main(String... s) throws IOException, KeyManagementException,
4949
reportFile = prepareReport();
5050
}
5151
int index = 0;
52-
52+
int lastPercent = 0;
5353
List<Map<String, String>> finalList = new ArrayList<>();
5454
for (InputRequest.Input request : inputRequest.getInputs()) {
5555
Map<String, String> list = new LinkedHashMap<>();
@@ -67,7 +67,7 @@ public static void main(String... s) throws IOException, KeyManagementException,
6767
if (StringUtils.isNotBlank(request.getRequestSource())) {
6868
httpRequest.put(HTTPClientManager.Const.REQUEST, request.getRequestSource());
6969
}
70-
Map<String, Object> response = httpClient.getResponse(httpRequest);
70+
Map<String, Object> response = httpClient.getResponseV2(httpRequest);
7171

7272
httpRequest = new LinkedHashMap<>();
7373
httpRequest.put(HTTPClientManager.Const.METHOD, StringUtils.isNotBlank(request.getMethod()) ? request.getMethod() : "GET");
@@ -78,7 +78,7 @@ public static void main(String... s) throws IOException, KeyManagementException,
7878
if (StringUtils.isNotBlank(request.getRequestCompare())) {
7979
httpRequest.put(HTTPClientManager.Const.REQUEST, request.getRequestCompare());
8080
}
81-
Map<String, Object> responseCompare = httpClient.getResponse(httpRequest);
81+
Map<String, Object> responseCompare = httpClient.getResponseV2(httpRequest);
8282
String status = "Failed";
8383
try {
8484
status = appendCompareReport(response, responseCompare, reportFile, "" + index, request.getUrlSource(), request.getUrlCompare());
@@ -94,7 +94,11 @@ public static void main(String... s) throws IOException, KeyManagementException,
9494

9595
finalList.add(list);
9696
int percentOfClassPassed = (int) ((double)index * 100 / inputRequest.getInputs().size());
97-
System.out.println(percentOfClassPassed + "% Completed");
97+
98+
if(lastPercent != percentOfClassPassed) {
99+
System.out.println(percentOfClassPassed + "% Completed");
100+
}
101+
lastPercent = percentOfClassPassed;
98102
}
99103
addListReport(finalList, reportFile);
100104

src/main/java/com/nexttimespace/utilities/diffbox/HTTPClientManager.java

Lines changed: 128 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.nexttimespace.utilities.diffbox;
22

3+
import java.io.IOException;
34
import java.nio.charset.Charset;
45
import java.security.KeyManagementException;
56
import java.security.KeyStoreException;
@@ -11,12 +12,21 @@
1112
import javax.net.ssl.SSLContext;
1213
import java.security.cert.X509Certificate;
1314

15+
import org.apache.http.HttpEntity;
16+
import org.apache.http.HttpResponse;
17+
import org.apache.http.client.methods.HttpDelete;
18+
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
19+
import org.apache.http.client.methods.HttpGet;
20+
import org.apache.http.client.methods.HttpPost;
21+
import org.apache.http.client.methods.HttpPut;
22+
import org.apache.http.client.methods.HttpRequestBase;
1423
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
24+
import org.apache.http.entity.StringEntity;
1525
import org.apache.http.impl.client.CloseableHttpClient;
1626
import org.apache.http.impl.client.HttpClientBuilder;
1727
import org.apache.http.impl.client.HttpClients;
1828
import org.apache.http.ssl.TrustStrategy;
19-
import org.springframework.http.HttpEntity;
29+
import org.apache.http.util.EntityUtils;
2030
import org.springframework.http.HttpHeaders;
2131
import org.springframework.http.HttpMethod;
2232
import org.springframework.http.MediaType;
@@ -97,10 +107,12 @@ public static class Const {
97107
}
98108

99109

110+
111+
100112

101113
public Map<String, Object> getResponse(Map<String, Object> properties) {
102114
HttpHeaders headers = new HttpHeaders();
103-
HttpEntity requestEntity = null;
115+
org.springframework.http.HttpEntity requestEntity = null;
104116
Map<String, Object> responseReturn = new LinkedHashMap<>();
105117
HttpMethod method = null;
106118
if (properties.get(Const.REQUEST_HEADER) != null && properties.get(Const.REQUEST_HEADER) instanceof String) {
@@ -144,16 +156,16 @@ public Map<String, Object> getResponse(Map<String, Object> properties) {
144156

145157
if (properties.get(Const.METHOD).toString().equals("GET")) {
146158
method = HttpMethod.GET;
147-
requestEntity = new HttpEntity<String>("", headers);
159+
requestEntity = new org.springframework.http.HttpEntity<String>("", headers);
148160
} else if (properties.get(Const.METHOD).toString().equals("POST")) {
149161
method = HttpMethod.POST;
150-
requestEntity = new HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
162+
requestEntity = new org.springframework.http.HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
151163
}else if (properties.get(Const.METHOD).toString().equals("PUT")) {
152164
method = HttpMethod.PUT;
153-
requestEntity = new HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
165+
requestEntity = new org.springframework.http.HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
154166
}else if (properties.get(Const.METHOD).toString().equals("DELETE")) {
155167
method = HttpMethod.DELETE;
156-
requestEntity = new HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
168+
requestEntity = new org.springframework.http.HttpEntity<String>(properties.get(Const.REQUEST).toString(), headers);
157169
}
158170
//StopWatch stopwatch = StopWatch.createStarted();
159171
ResponseEntity<String> response = null;
@@ -200,5 +212,115 @@ public Map<String, Object> getResponse(Map<String, Object> properties) {
200212
//responseReturn.put(Const.TIME_ELASPED, stopwatch.getTime(TimeUnit.MILLISECONDS));
201213
return responseReturn;
202214
}
215+
216+
public Map<String, Object> getResponseV2(Map<String, Object> properties) {
217+
CloseableHttpClient client = HttpClients.createDefault();
218+
HttpHeaders headers = new HttpHeaders();
219+
HttpEntity requestEntity = null;
220+
Map<String, Object> responseReturn = new LinkedHashMap<>();
221+
HttpMethod method = null;
222+
if (properties.get(Const.REQUEST_HEADER) != null && properties.get(Const.REQUEST_HEADER) instanceof String) {
223+
String header = properties.get(Const.REQUEST_HEADER).toString().trim();
224+
if (!header.isEmpty()) {
225+
String[] splitHeaderNewLine = header.split("\\r?\\n");
226+
if (splitHeaderNewLine.length > 0) {
227+
for (String headerIterator : splitHeaderNewLine) {
228+
if (headerIterator.contains(":")) {
229+
String[] splitHedaer = headerIterator.split(":", 2);
230+
if(splitHedaer[0].equalsIgnoreCase("accept")){
231+
if(splitHedaer.length > 1){
232+
headers.setAccept(MediaType.parseMediaTypes(splitHedaer[1]));
233+
}
234+
}else{
235+
if (splitHedaer.length >= 2) {
236+
headers.add(splitHedaer[0], splitHedaer[1]);
237+
} else {
238+
headers.add(splitHedaer[0], "");
239+
}
240+
}
241+
}
242+
}
243+
}
244+
}
245+
}
246+
247+
if (properties.get(Const.REQUEST_HEADER) != null && properties.get(Const.REQUEST_HEADER) instanceof Map) {
248+
Map<String, String> headersRequest = (Map<String, String>) properties.get(Const.REQUEST_HEADER);
249+
250+
for(Map.Entry<String, String> headerEntry : headersRequest.entrySet()) {
251+
headers.add(headerEntry.getKey(), headerEntry.getValue());
252+
}
253+
}
254+
255+
if(!commonHeaders.isEmpty()) {
256+
for(Entry<String, String> entry: commonHeaders.entrySet()) {
257+
headers.add(entry.getKey(), entry.getValue());
258+
}
259+
}
260+
261+
try {
262+
263+
HttpRequestBase httpMethod = null;
264+
if (properties.get(Const.METHOD).toString().equals("GET")) {
265+
httpMethod = new HttpGet(properties.get(Const.URL).toString());
266+
} else if (properties.get(Const.METHOD).toString().equals("POST")) {
267+
httpMethod = new HttpPost(properties.get(Const.URL).toString());
268+
StringEntity entity = new StringEntity(properties.get(Const.REQUEST).toString());
269+
((HttpPost)httpMethod).setEntity(entity);
270+
}else if (properties.get(Const.METHOD).toString().equals("PUT")) {
271+
httpMethod = new HttpPut(properties.get(Const.URL).toString());
272+
StringEntity entity = new StringEntity(properties.get(Const.REQUEST).toString());
273+
((HttpPost)httpMethod).setEntity(entity);
274+
}else if (properties.get(Const.METHOD).toString().equals("DELETE")) {
275+
httpMethod = new HttpDelete(properties.get(Const.URL).toString());
276+
}
277+
278+
if (properties.get(Const.REQUEST_HEADER) != null && properties.get(Const.REQUEST_HEADER) instanceof Map) {
279+
Map<String, String> headersRequest = (Map<String, String>) properties.get(Const.REQUEST_HEADER);
280+
281+
for(Map.Entry<String, String> headerEntry : headersRequest.entrySet()) {
282+
httpMethod.setHeader(headerEntry.getKey(), headerEntry.getValue());
283+
}
284+
}
285+
286+
if(!commonHeaders.isEmpty()) {
287+
for(Entry<String, String> entry: commonHeaders.entrySet()) {
288+
httpMethod.setHeader(entry.getKey(), entry.getValue());
289+
}
290+
}
291+
292+
293+
HttpResponse response = client.execute(httpMethod);
294+
HttpEntity entity = response.getEntity();
295+
String body = EntityUtils.toString(entity, "UTF-8");
296+
if(properties.get(Const.CHAR_SET) != null) {
297+
try {
298+
String targetCharSet = properties.get(Const.TARGET_CHAR_SET) != null ? properties.get(Const.TARGET_CHAR_SET).toString() : "UTF-8";
299+
body = new String(body.getBytes(Charset.forName(properties.get(Const.CHAR_SET).toString())), targetCharSet);
300+
body = body.replaceAll("[^\\x00-\\x7F]", "");
301+
}catch(Exception e) {
302+
System.out.println("Error using charSet" + e);
303+
}
304+
}
305+
//responseReturn.put(Const.TIME_ELASPED, stopwatch.getTime(TimeUnit.MILLISECONDS));
306+
responseReturn.put(Const.RESPONSE_STATUS, response.getStatusLine());
307+
308+
responseReturn.put(Const.RESPONSE_LENGTH, body!=null?body.getBytes().length:0);
309+
responseReturn.put(Const.RESPONSE, body!=null?body:"");
310+
responseReturn.put(Const.RESPONSE_HEADER, response.getAllHeaders());
311+
} catch(Exception exception){
312+
responseReturn.put(Const.RESPONSE_LENGTH, "0");
313+
responseReturn.put(Const.RESPONSE, exception.getMessage());
314+
responseReturn.put(Const.RESPONSE_HEADER,"NA" );
315+
responseReturn.put(Const.RESPONSE_STATUS, "NA");
316+
responseReturn.put(Const.ERROR_RESPONSE, "true");
317+
}
318+
try {
319+
client.close();
320+
} catch (IOException e) {
321+
System.err.println(e.getMessage());
322+
}
323+
return responseReturn;
324+
}
203325

204326
}

0 commit comments

Comments
 (0)