|
1 | 1 | package com.nexttimespace.utilities.diffbox; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
3 | 4 | import java.nio.charset.Charset; |
4 | 5 | import java.security.KeyManagementException; |
5 | 6 | import java.security.KeyStoreException; |
|
11 | 12 | import javax.net.ssl.SSLContext; |
12 | 13 | import java.security.cert.X509Certificate; |
13 | 14 |
|
| 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; |
14 | 23 | import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
| 24 | +import org.apache.http.entity.StringEntity; |
15 | 25 | import org.apache.http.impl.client.CloseableHttpClient; |
16 | 26 | import org.apache.http.impl.client.HttpClientBuilder; |
17 | 27 | import org.apache.http.impl.client.HttpClients; |
18 | 28 | import org.apache.http.ssl.TrustStrategy; |
19 | | -import org.springframework.http.HttpEntity; |
| 29 | +import org.apache.http.util.EntityUtils; |
20 | 30 | import org.springframework.http.HttpHeaders; |
21 | 31 | import org.springframework.http.HttpMethod; |
22 | 32 | import org.springframework.http.MediaType; |
@@ -97,10 +107,12 @@ public static class Const { |
97 | 107 | } |
98 | 108 |
|
99 | 109 |
|
| 110 | + |
| 111 | + |
100 | 112 |
|
101 | 113 | public Map<String, Object> getResponse(Map<String, Object> properties) { |
102 | 114 | HttpHeaders headers = new HttpHeaders(); |
103 | | - HttpEntity requestEntity = null; |
| 115 | + org.springframework.http.HttpEntity requestEntity = null; |
104 | 116 | Map<String, Object> responseReturn = new LinkedHashMap<>(); |
105 | 117 | HttpMethod method = null; |
106 | 118 | 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) { |
144 | 156 |
|
145 | 157 | if (properties.get(Const.METHOD).toString().equals("GET")) { |
146 | 158 | method = HttpMethod.GET; |
147 | | - requestEntity = new HttpEntity<String>("", headers); |
| 159 | + requestEntity = new org.springframework.http.HttpEntity<String>("", headers); |
148 | 160 | } else if (properties.get(Const.METHOD).toString().equals("POST")) { |
149 | 161 | 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); |
151 | 163 | }else if (properties.get(Const.METHOD).toString().equals("PUT")) { |
152 | 164 | 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); |
154 | 166 | }else if (properties.get(Const.METHOD).toString().equals("DELETE")) { |
155 | 167 | 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); |
157 | 169 | } |
158 | 170 | //StopWatch stopwatch = StopWatch.createStarted(); |
159 | 171 | ResponseEntity<String> response = null; |
@@ -200,5 +212,115 @@ public Map<String, Object> getResponse(Map<String, Object> properties) { |
200 | 212 | //responseReturn.put(Const.TIME_ELASPED, stopwatch.getTime(TimeUnit.MILLISECONDS)); |
201 | 213 | return responseReturn; |
202 | 214 | } |
| 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 | + } |
203 | 325 |
|
204 | 326 | } |
0 commit comments