[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / datatypes / http / HttpRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.ci.tests.datatypes.http;
22
23 import java.io.BufferedReader;
24 import java.io.DataOutputStream;
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.StringWriter;
30 import java.net.HttpURLConnection;
31 import java.net.URI;
32 import java.net.URL;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Map.Entry;
36 import java.util.Scanner;
37
38 import javax.net.ssl.HttpsURLConnection;
39
40 import org.apache.commons.codec.binary.Base64;
41 import org.apache.commons.io.IOUtils;
42 import org.apache.http.HttpEntity;
43 import org.apache.http.annotation.NotThreadSafe;
44 import org.apache.http.client.ClientProtocolException;
45 import org.apache.http.client.methods.CloseableHttpResponse;
46 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
47 import org.apache.http.client.methods.HttpPost;
48 import org.apache.http.entity.ContentType;
49 import org.apache.http.entity.StringEntity;
50 import org.apache.http.entity.mime.MultipartEntityBuilder;
51 import org.apache.http.entity.mime.content.FileBody;
52 import org.apache.http.entity.mime.content.StringBody;
53 import org.apache.http.impl.client.CloseableHttpClient;
54 import org.apache.http.impl.client.HttpClients;
55 import org.apache.http.util.EntityUtils;
56 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 import com.google.gson.Gson;
61
62 public class HttpRequest {
63         static Logger logger = LoggerFactory.getLogger(HttpRequest.class.getName());
64         
65         public RestResponse httpSendGet(String url, Map<String, String> headers) throws IOException {
66
67                 RestResponse restResponse = new RestResponse();
68                 url = url.replaceAll("\\s", "%20");
69                 URL obj = new URL(url);
70                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
71                 // optional default is GET
72                 con.setRequestMethod("GET");
73                 // add request header
74                 if (headers != null) {
75                         for (Entry<String, String> header : headers.entrySet()) {
76                                 String key = header.getKey();
77                                 String value = header.getValue();
78                                 con.setRequestProperty(key, value);
79                         }
80
81                 }
82
83                 int responseCode = con.getResponseCode();
84                 logger.debug("Send GET http request, url: {}",url);
85                 logger.debug("Response Code: {}",responseCode);
86
87                 StringBuffer response = new StringBuffer();
88                 String result;
89
90                 try {
91
92                         result = IOUtils.toString(con.getInputStream());
93                         response.append(result);
94
95                 } catch (Exception e) {                 
96                 }
97
98                 try {
99
100                         result = IOUtils.toString(con.getErrorStream());
101                         response.append(result);
102
103                 } catch (Exception e) {
104                 }
105
106                 logger.debug("Response body: {}" ,response);
107
108                 // print result
109
110                 restResponse.setErrorCode(responseCode);
111
112                 if (response != null) {
113                         restResponse.setResponse(response.toString());
114                 }
115
116                 restResponse.setErrorCode(responseCode);
117                 Map<String, List<String>> headerFields = con.getHeaderFields();
118                 restResponse.setHeaderFields(headerFields);
119                 String responseMessage = con.getResponseMessage();
120                 restResponse.setResponseMessage(responseMessage);
121
122                 con.disconnect();
123
124                 return restResponse;
125         }
126
127         public RestResponse httpsSendGet(String url, Map<String, String> headers) throws IOException {
128
129                 RestResponse restResponse = new RestResponse();
130                 URL obj = new URL(url);
131                 HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
132                 // optional default is GET
133                 con.setRequestMethod("GET");
134                 // add request header
135                 if (headers != null) {
136                         for (Entry<String, String> header : headers.entrySet()) {
137                                 String key = header.getKey();
138                                 String value = header.getValue();
139                                 con.setRequestProperty(key, value);
140                         }
141
142                 }
143
144                 int responseCode = con.getResponseCode();
145                 logger.debug("Send GET http request, url: {}",url);
146                 logger.debug("Response Code: {}",responseCode);
147
148                 StringBuffer response = new StringBuffer();
149                 try {
150                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
151                         String inputLine;
152                         while ((inputLine = in.readLine()) != null) {
153                                 response.append(inputLine);
154                         }
155                         in.close();
156                 } catch (Exception e) {
157                         logger.debug("response body is null");
158                 }
159
160                 String result;
161
162                 try {
163
164                         result = IOUtils.toString(con.getErrorStream());
165                         response.append(result);
166
167                 } catch (Exception e2) {
168                         // result = null;
169                 }
170                 logger.debug("Response body: {}",response);
171
172                 // print result
173
174                 restResponse.setErrorCode(responseCode);
175
176                 if (response != null) {
177                         restResponse.setResponse(response.toString());
178                 }
179
180                 restResponse.setErrorCode(responseCode);
181                 // restResponse.setResponse(result);
182                 Map<String, List<String>> headerFields = con.getHeaderFields();
183                 restResponse.setHeaderFields(headerFields);
184                 String responseMessage = con.getResponseMessage();
185                 restResponse.setResponseMessage(responseMessage);
186
187                 con.disconnect();
188
189                 return restResponse;
190         }
191
192         public RestResponse httpSendByMethod(String url, String method, String body, Map<String, String> headers)
193                         throws IOException {
194
195                 RestResponse restResponse = new RestResponse();
196                 URL obj = new URL(url);
197                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
198
199                 // add request method
200                 con.setRequestMethod(method);
201
202                 // add request headers
203                 if (headers != null) {
204                         for (Entry<String, String> header : headers.entrySet()) {
205                                 String key = header.getKey();
206                                 String value = header.getValue();
207                                 con.setRequestProperty(key, value);
208                         }
209
210                 }
211                 if (body != null && !body.isEmpty() && !method.equals("DELETE")) {
212                         // Send post request
213                         con.setDoOutput(true);
214                         DataOutputStream wr = new DataOutputStream(con.getOutputStream());
215                         wr.writeBytes(body);
216                         wr.flush();
217                         wr.close();
218                 }
219
220                 // con.connect();
221
222                 int responseCode = con.getResponseCode();
223                 logger.debug("Send {} http request, url: {}",method,url);
224                 logger.debug("Response Code: {}",responseCode);
225
226                 StringBuffer response = new StringBuffer();
227
228                 try {
229                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
230                         String inputLine;
231                         while ((inputLine = in.readLine()) != null) {
232                                 response.append(inputLine);
233                         }
234                         in.close();
235                 } catch (Exception e) {
236                         // response = null;
237                         logger.debug("response body is null");
238                 }
239
240                 String result;
241                 try {
242
243                         result = IOUtils.toString(con.getErrorStream());
244                         response.append(result);
245
246                 } catch (Exception e2) {
247                         result = null;
248                 }
249                 logger.debug("Response body: {}",response);
250
251                 // print result
252
253                 restResponse.setErrorCode(responseCode);
254                 // if (response == null) {
255                 // restResponse.setResponse(null);
256                 // } else {
257                 // restResponse.setResponse(response.toString());
258                 // }
259
260                 if (response != null) {
261                         restResponse.setResponse(response.toString());
262                 }
263                 Map<String, List<String>> headerFields = con.getHeaderFields();
264                 restResponse.setHeaderFields(headerFields);
265                 String responseMessage = con.getResponseMessage();
266                 restResponse.setResponseMessage(responseMessage);
267
268                 con.disconnect();
269                 return restResponse;
270
271         }
272
273         public RestResponse sendHttpPost(String url, String body, Map<String, String> headers) throws IOException {
274
275                 RestResponse restResponse = new RestResponse();
276                 URL obj = new URL(url);
277                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
278
279                 // add request method
280                 con.setRequestMethod("POST");
281
282                 // add request headers
283                 if (headers != null) {
284                         for (Entry<String, String> header : headers.entrySet()) {
285                                 String key = header.getKey();
286                                 String value = header.getValue();
287                                 con.setRequestProperty(key, value);
288                         }
289                 }
290
291                 // Send post request
292                 if (body != null) {
293                         con.setDoOutput(true);
294                         DataOutputStream wr = new DataOutputStream(con.getOutputStream());
295                         wr.writeBytes(body);
296                         wr.flush();
297                         wr.close();
298                 }
299
300                 // con.connect();
301
302                 int responseCode = con.getResponseCode();
303                 logger.debug("Send POST http request, url: {}",url);
304                 logger.debug("Response Code: {}",responseCode);
305
306                 StringBuffer response = new StringBuffer();
307                 try {
308                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
309                         String inputLine;
310                         while ((inputLine = in.readLine()) != null) {
311                                 response.append(inputLine);
312                         }
313                         in.close();
314                 } catch (Exception e) {
315                         logger.debug("response body is null");
316                 }
317
318                 String result;
319
320                 try {
321
322                         result = IOUtils.toString(con.getErrorStream());
323                         response.append(result);
324
325                 } catch (Exception e2) {
326                         result = null;
327                 }
328                 logger.debug("Response body: {}",response);
329
330                 // print result
331
332                 restResponse.setErrorCode(responseCode);
333
334                 if (response != null) {
335                         restResponse.setResponse(response.toString());
336                 }
337
338                 Map<String, List<String>> headerFields = con.getHeaderFields();
339                 restResponse.setHeaderFields(headerFields);
340                 String responseMessage = con.getResponseMessage();
341                 restResponse.setResponseMessage(responseMessage);
342
343                 con.disconnect();
344                 return restResponse;
345
346         }
347
348         public RestResponse httpSendPost(String url, String body, Map<String, String> headers) throws IOException {
349                 return httpSendPost(url, body, headers, "POST");
350         }
351
352         public RestResponse httpSendPut(String url, String body, Map<String, String> headers) throws IOException {
353                 return httpSendPost(url, body, headers, "PUT");
354         }
355
356         public RestResponse httpSendPost(String url, String body, Map<String, String> headers, String methodType)
357                         throws IOException {
358
359                 RestResponse restResponse = new RestResponse();
360                 URL obj = new URL(url);
361                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
362
363                 // add request method
364                 con.setRequestMethod(methodType);
365
366                 // add request headers
367                 if (headers != null) {
368                         for (Entry<String, String> header : headers.entrySet()) {
369                                 String key = header.getKey();
370                                 String value = header.getValue();
371                                 con.setRequestProperty(key, value);
372                         }
373                 }
374
375                 // Send post request
376                 if (body != null) {
377                         con.setDoOutput(true);
378                         DataOutputStream wr = new DataOutputStream(con.getOutputStream());
379                         wr.writeBytes(body);
380                         wr.flush();
381                         wr.close();
382                 }
383
384                 // con.connect();
385
386                 int responseCode = con.getResponseCode();
387                 logger.debug("Send POST http request, url: {}",url);
388                 logger.debug("Response Code: {}",responseCode);
389
390                 StringBuffer response = new StringBuffer();
391                 try {
392                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
393                         String inputLine;
394                         while ((inputLine = in.readLine()) != null) {
395                                 response.append(inputLine);
396                         }
397                         in.close();
398                 } catch (Exception e) {
399                         logger.debug("response body is null");
400                 }
401
402                 String result;
403
404                 try {
405
406                         result = IOUtils.toString(con.getErrorStream());
407                         response.append(result);
408
409                 } catch (Exception e2) {
410                         result = null;
411                 }
412                 logger.debug("Response body: {}",response);
413
414                 // print result
415
416                 restResponse.setErrorCode(responseCode);
417
418                 if (response != null) {
419                         restResponse.setResponse(response.toString());
420                 }
421
422                 Map<String, List<String>> headerFields = con.getHeaderFields();
423                 restResponse.setHeaderFields(headerFields);
424                 String responseMessage = con.getResponseMessage();
425                 restResponse.setResponseMessage(responseMessage);
426
427                 con.disconnect();
428                 return restResponse;
429
430         }
431
432         public RestResponse httpSendDeleteWithBody2(String url, String body, Map<String, String> headers)
433                         throws ClientProtocolException, IOException {
434
435                 CloseableHttpClient httpclient = HttpClients.createDefault();
436                 RestResponse restResponse = new RestResponse();
437                 HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
438
439                 // add request headers
440                 if (headers != null) {
441                         for (Entry<String, String> header : headers.entrySet()) {
442                                 String key = header.getKey();
443                                 String value = header.getValue();
444                                 httpDelete.addHeader(key, value);
445                         }
446                 }
447
448                 // add body to request
449                 StringEntity input = new StringEntity(body, ContentType.APPLICATION_JSON);
450                 httpDelete.setEntity(input);
451
452                 // execute request
453                 CloseableHttpResponse response = httpclient.execute(httpDelete);
454
455                 restResponse.setErrorCode(response.getStatusLine().getStatusCode());
456
457                 return restResponse;
458         }
459
460         public RestResponse httpSendDeleteWithBody(String url, String body, Map<String, String> headers)
461                         throws IOException {
462
463                 RestResponse restResponse = new RestResponse();
464                 URL obj = new URL(url);
465                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
466
467                 // add request method
468                 con.setRequestMethod("DELETE");
469
470                 // add request headers
471                 if (headers != null) {
472                         for (Entry<String, String> header : headers.entrySet()) {
473                                 String key = header.getKey();
474                                 String value = header.getValue();
475                                 con.setRequestProperty(key, value);
476                         }
477                 }
478
479                 // Send post request
480                 con.setDoOutput(true);
481                 DataOutputStream wr = new DataOutputStream(con.getOutputStream());
482                 wr.writeBytes(body);
483                 wr.flush();
484                 wr.close();
485
486                 // con.connect();
487
488                 int responseCode = con.getResponseCode();
489                 logger.debug("Send DELETE http request, url: {}",url);
490                 logger.debug("Response Code: {}",responseCode);
491
492                 StringBuffer response = new StringBuffer();
493                 try {
494                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
495                         String inputLine;
496                         while ((inputLine = in.readLine()) != null) {
497                                 response.append(inputLine);
498                         }
499                         in.close();
500                 } catch (Exception e) {
501                         logger.debug("response body is null");
502                 }
503
504                 String result;
505
506                 try {
507
508                         result = IOUtils.toString(con.getErrorStream());
509                         response.append(result);
510
511                 } catch (Exception e2) {
512                         result = null;
513                 }
514                 logger.debug("Response body: {}", response);
515
516                 // print result
517
518                 restResponse.setErrorCode(responseCode);
519
520                 if (response != null) {
521                         restResponse.setResponse(response.toString());
522                 }
523
524                 Map<String, List<String>> headerFields = con.getHeaderFields();
525                 restResponse.setHeaderFields(headerFields);
526                 String responseMessage = con.getResponseMessage();
527                 restResponse.setResponseMessage(responseMessage);
528
529                 con.disconnect();
530                 return restResponse;
531
532         }
533
534         public RestResponse httpSendPostWithOutBody(String url, Map<String, String> headers) throws IOException {
535
536                 RestResponse restResponse = new RestResponse();
537                 URL obj = new URL(url);
538                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
539
540                 // add request method
541                 con.setRequestMethod("POST");
542
543                 // add request headers
544                 if (headers != null) {
545                         for (Entry<String, String> header : headers.entrySet()) {
546                                 String key = header.getKey();
547                                 String value = header.getValue();
548                                 con.setRequestProperty(key, value);
549                         }
550                 }
551
552                 // con.connect();
553
554                 int responseCode = con.getResponseCode();
555                 logger.debug("Send POST http request, url: {}",url);
556                 logger.debug("Response Code: {}",responseCode);
557
558                 StringBuffer response = new StringBuffer();
559
560                 try {
561                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
562                         String inputLine;
563                         while ((inputLine = in.readLine()) != null) {
564                                 response.append(inputLine);
565                         }
566                         in.close();
567                 } catch (Exception e) {
568                         // response = null;
569                         logger.debug("response body is null");
570                 }
571
572                 String result;
573                 try {
574
575                         result = IOUtils.toString(con.getErrorStream());
576                         response.append(result);
577
578                 } catch (Exception e2) {
579                         result = null;
580                 }
581                 logger.debug("Response body: {}",response);
582
583                 // print result
584
585                 restResponse.setErrorCode(responseCode);
586                 // if (response == null) {
587                 // restResponse.setResponse(null);
588                 // } else {
589                 // restResponse.setResponse(response.toString());
590                 // }
591
592                 if (response != null) {
593                         restResponse.setResponse(response.toString());
594                 }
595
596                 Map<String, List<String>> headerFields = con.getHeaderFields();
597                 restResponse.setHeaderFields(headerFields);
598                 String responseMessage = con.getResponseMessage();
599                 restResponse.setResponseMessage(responseMessage);
600
601                 con.disconnect();
602                 return restResponse;
603
604         }
605
606         public RestResponse httpSendPostMultipart(String url, Map<String, String> headers, String jsonLocation,
607                         String zipLocation) throws IOException {
608
609                 Gson gson = new Gson();
610                 String gsonToSend = null;
611                 RestResponse restResponse = new RestResponse();
612                 BufferedReader br = null;
613                 //
614                 //
615                 //
616                 //
617                 // try {
618                 //
619                 // String sCurrentLine;
620                 //
621                 // br = new BufferedReader(new FileReader(jsonLocation));
622                 //
623                 // while ((sCurrentLine = br.readLine()) != null) {
624                 // System.out.println(sCurrentLine);
625                 // }
626                 //
627                 // } catch (IOException e) {
628                 // e.printStackTrace();
629                 // } finally {
630                 // try {
631                 // if (br != null)br.close();
632                 // gsonToSend = br.toString();
633                 // } catch (IOException ex) {
634                 // ex.printStackTrace();
635                 // }
636                 // }
637
638                 gsonToSend = new Scanner(new File(jsonLocation)).useDelimiter("\\Z").next();
639                 logger.debug("gsonToSend: {}",gsonToSend);
640
641                 MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create();
642                 mpBuilder.addPart("resourceZip", new FileBody(new File(zipLocation)));
643                 mpBuilder.addPart("resourceMetadata", new StringBody(gsonToSend, ContentType.APPLICATION_JSON));
644
645                 HttpPost httpPost = new HttpPost(url);
646                 httpPost.addHeader("USER_ID", "adminid");
647                 httpPost.setEntity(mpBuilder.build());
648
649                 CloseableHttpClient client = HttpClients.createDefault();
650                 CloseableHttpResponse response = client.execute(httpPost);
651                 try {
652                         logger.debug("----------------------------------------");
653                         logger.debug("response.getStatusLine(): {}",response.getStatusLine());
654                         HttpEntity resEntity = response.getEntity();
655                         if (resEntity != null) {
656                                 logger.debug("Response content length: {}",resEntity.getContentLength());
657                         }
658                         EntityUtils.consume(resEntity);
659                 } finally {
660
661                         response.close();
662                         client.close();
663                 }
664
665                 restResponse.setErrorCode(response.getStatusLine().getStatusCode());
666                 restResponse.setResponse(response.getEntity().toString());
667
668                 return restResponse;
669
670         }
671
672         public RestResponse httpSendPostWithAuth(String url, String body, Map<String, String> headers, String username,
673                         String password) throws IOException {
674
675                 String userPassword = username + ":" + password;
676                 String encoding = Base64.encodeBase64String(userPassword.getBytes());
677                 RestResponse restResponse = new RestResponse();
678                 URL obj = new URL(url);
679                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
680
681                 // add request method
682                 con.setRequestMethod("POST");
683
684                 con.setRequestProperty("Authorization", "Basic " + encoding);
685
686                 // add request headers
687                 if (headers != null) {
688                         for (Entry<String, String> header : headers.entrySet()) {
689                                 String key = header.getKey();
690                                 String value = header.getValue();
691                                 con.setRequestProperty(key, value);
692                         }
693
694                 }
695
696                 // Send post request
697                 con.setDoOutput(true);
698                 DataOutputStream wr = new DataOutputStream(con.getOutputStream());
699                 wr.writeBytes(body);
700                 wr.flush();
701                 wr.close();
702
703                 // con.connect();
704
705                 int responseCode = con.getResponseCode();
706                 logger.debug("Send POST http request, url: {}",url);
707                 logger.debug("Response Code: {}",responseCode);
708
709                 StringBuffer response = new StringBuffer();
710                 try {
711                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
712                         String inputLine;
713                         while ((inputLine = in.readLine()) != null) {
714                                 response.append(inputLine);
715                         }
716                         in.close();
717                 } catch (Exception e) {
718                         response = null;
719
720                 }
721                 logger.debug("Response body: {}",response);
722
723                 // print result
724
725                 restResponse.setErrorCode(responseCode);
726                 if (response == null) {
727                         restResponse.setResponse(null);
728                 } else {
729                         restResponse.setResponse(response.toString());
730                 }
731
732                 Map<String, List<String>> headerFields = con.getHeaderFields();
733                 restResponse.setHeaderFields(headerFields);
734                 String responseMessage = con.getResponseMessage();
735                 restResponse.setResponseMessage(responseMessage);
736
737                 con.disconnect();
738                 return restResponse;
739
740         }
741
742         public RestResponse httpSendDelete(String url, Map<String, String> headers) throws IOException {
743
744                 RestResponse restResponse = new RestResponse();
745                 URL obj = new URL(url);
746                 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
747
748                 if (headers != null) {
749                         for (Entry<String, String> header : headers.entrySet()) {
750                                 String key = header.getKey();
751                                 String value = header.getValue();
752                                 con.setRequestProperty(key, value);
753                         }
754
755                 }
756
757                 con.setDoOutput(true);
758                 con.setRequestMethod("DELETE");
759                 int responseCode = con.getResponseCode();
760                 logger.debug("Send DELETE http request, url: {}",url);
761                 logger.debug("Response Code: {}",responseCode);
762
763                 StringBuffer response = new StringBuffer();
764
765                 try {
766                         BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
767                         String inputLine;
768                         while ((inputLine = in.readLine()) != null) {
769                                 response.append(inputLine);
770                         }
771                         in.close();
772                 } catch (Exception e) {
773                         logger.debug("response body is null");
774                 }
775
776                 String result;
777
778                 try {
779
780                         result = IOUtils.toString(con.getErrorStream());
781                         response.append(result);
782
783                 } catch (Exception e2) {
784                         result = null;
785                 }
786                 logger.debug("Response body: {}",response);
787
788                 // print result
789
790                 restResponse.setErrorCode(responseCode);
791
792                 if (response != null) {
793                         restResponse.setResponse(response.toString());
794                 }
795
796                 restResponse.setErrorCode(con.getResponseCode());
797                 Map<String, List<String>> headerFields = con.getHeaderFields();
798                 restResponse.setHeaderFields(headerFields);
799                 String responseMessage = con.getResponseMessage();
800                 restResponse.setResponseMessage(responseMessage);
801
802                 con.disconnect();
803
804                 return restResponse;
805         }
806
807         public static RestResponse sendHttpPostWithEntity(HttpEntity requestEntity, String url, Map<String, String> headers)
808                         throws IOException, ClientProtocolException {
809                 CloseableHttpResponse response = null;
810                 CloseableHttpClient client = HttpClients.createDefault();
811                 try {
812                         HttpPost httpPost = new HttpPost(url);
813                         RestResponse restResponse = new RestResponse();
814                         for (Entry<String, String> entry : headers.entrySet()) {
815                                 httpPost.addHeader(entry.getKey(), entry.getValue());
816                         }
817
818                         httpPost.setEntity(requestEntity);
819                         response = client.execute(httpPost);
820                         HttpEntity responseEntity = response.getEntity();
821                         String responseBody = null;
822                         if (responseEntity != null) {
823                                 InputStream instream = responseEntity.getContent();
824                                 StringWriter writer = new StringWriter();
825                                 IOUtils.copy(instream, writer);
826                                 responseBody = writer.toString();
827                                 try {
828
829                                 } finally {
830                                         instream.close();
831                                 }
832                         }
833
834                         restResponse.setErrorCode(response.getStatusLine().getStatusCode());
835                         restResponse.setResponse(responseBody);
836
837                         return restResponse;
838
839                 } finally {
840                         closeResponse(response);
841                         closeHttpClient(client);
842
843                 }
844         }
845
846         private static void closeHttpClient(CloseableHttpClient client) {
847                 try {
848                         if (client != null) {
849                                 client.close();
850                         }
851                 } catch (IOException e) {
852                         logger.debug("failed to close client or response: ", e);
853                 }
854         }
855
856         private static void closeResponse(CloseableHttpResponse response) {
857                 try {
858                         if (response != null) {
859                                 response.close();
860                         }
861                 } catch (IOException e) {
862                         logger.debug("failed to close client or response: ", e);
863                 }
864         }
865
866         @NotThreadSafe
867         class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
868                 public static final String METHOD_NAME = "DELETE";
869
870                 public String getMethod() {
871                         return METHOD_NAME;
872                 }
873
874                 public HttpDeleteWithBody(final String uri) {
875                         super();
876                         setURI(URI.create(uri));
877                 }
878
879                 public HttpDeleteWithBody(final URI uri) {
880                         super();
881                         setURI(uri);
882                 }
883
884                 public HttpDeleteWithBody() {
885                         super();
886                 }
887         }
888
889 }