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