Sync Integ to Master
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / datatypes / http / HttpRequest.java
index a8ca77d..2435b10 100644 (file)
 
 package org.openecomp.sdc.ci.tests.datatypes.http;
 
-import java.io.BufferedReader;
-import java.io.DataOutputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.StringWriter;
-import java.net.HttpURLConnection;
-import java.net.URI;
-import java.net.URL;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Scanner;
-
-import javax.net.ssl.HttpsURLConnection;
-
-import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.annotation.NotThreadSafe;
@@ -45,23 +27,29 @@ import org.apache.http.client.ClientProtocolException;
 import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
 import org.apache.http.client.methods.HttpPost;
-import org.apache.http.entity.ContentType;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.entity.mime.MultipartEntityBuilder;
-import org.apache.http.entity.mime.content.FileBody;
-import org.apache.http.entity.mime.content.StringBody;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
-import org.apache.http.util.EntityUtils;
+import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import sun.net.www.protocol.https.DefaultHostnameVerifier;
 
-import com.google.gson.Gson;
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URI;
+import java.net.URL;
+import java.nio.charset.Charset;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 
 public class HttpRequest {
        static Logger logger = LoggerFactory.getLogger(HttpRequest.class.getName());
-       
-       public RestResponse httpSendGet(String url, Map<String, String> headers) throws IOException {
+
+//     -----------------------------Http------------------------------------------------------------------------
+       public RestResponse httpSendGetInternal(String url, Map<String, String> headers) throws IOException {
 
                RestResponse restResponse = new RestResponse();
                url = url.replaceAll("\\s", "%20");
@@ -69,15 +57,7 @@ public class HttpRequest {
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                // optional default is GET
                con.setRequestMethod("GET");
-               // add request header
-               if (headers != null) {
-                       for (Entry<String, String> header : headers.entrySet()) {
-                               String key = header.getKey();
-                               String value = header.getValue();
-                               con.setRequestProperty(key, value);
-                       }
-
-               }
+               addHttpRequestHEaders(headers, con);
 
                int responseCode = con.getResponseCode();
                logger.debug("Send GET http request, url: {}",url);
@@ -85,209 +65,246 @@ public class HttpRequest {
 
                StringBuffer response = new StringBuffer();
                String result;
-
                try {
 
                        result = IOUtils.toString(con.getInputStream());
                        response.append(result);
-
-               } catch (Exception e) {                 
+               } catch (Exception e) {
+                       logger.debug("Fail with exception", e);
                }
-
                try {
-
                        result = IOUtils.toString(con.getErrorStream());
                        response.append(result);
-
                } catch (Exception e) {
+//                     logger.debug("Fail with exception", e);
                }
 
                logger.debug("Response body: {}" ,response);
 
                // print result
-
-               restResponse.setErrorCode(responseCode);
-
-               if (response != null) {
-                       restResponse.setResponse(response.toString());
-               }
-
-               restResponse.setErrorCode(responseCode);
-               Map<String, List<String>> headerFields = con.getHeaderFields();
-               restResponse.setHeaderFields(headerFields);
-               String responseMessage = con.getResponseMessage();
-               restResponse.setResponseMessage(responseMessage);
-
+               setHttpResponseToObject(restResponse, con, responseCode, response);
                con.disconnect();
 
                return restResponse;
        }
 
-       public RestResponse httpsSendGet(String url, Map<String, String> headers) throws IOException {
+       public RestResponse httpSendByMethodInternal(String url, String method, String body, Map<String, String> headers) throws IOException {
 
                RestResponse restResponse = new RestResponse();
                URL obj = new URL(url);
-               HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
-               // optional default is GET
-               con.setRequestMethod("GET");
-               // add request header
-               if (headers != null) {
-                       for (Entry<String, String> header : headers.entrySet()) {
-                               String key = header.getKey();
-                               String value = header.getValue();
-                               con.setRequestProperty(key, value);
-                       }
-
+               HttpURLConnection con = (HttpURLConnection) obj.openConnection();
+// add request method
+               con.setRequestMethod(method);
+// add request headers
+               addHttpRequestHEaders(headers, con);
+               if (body != null && !body.isEmpty() && !method.equals("DELETE")) {
+// Send post request
+                       con.setDoOutput(true);
+                       DataOutputStream wr = new DataOutputStream(con.getOutputStream());
+                       wr.writeBytes(body);
+                       wr.flush();
+                       wr.close();
                }
 
                int responseCode = con.getResponseCode();
-               logger.debug("Send GET http request, url: {}",url);
+               logger.debug("Send {} http request, url: {}",method,url);
                logger.debug("Response Code: {}",responseCode);
 
-               StringBuffer response = new StringBuffer();
-               try {
-                       BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                       String inputLine;
-                       while ((inputLine = in.readLine()) != null) {
-                               response.append(inputLine);
-                       }
-                       in.close();
-               } catch (Exception e) {
-                       logger.debug("response body is null");
-               }
-
+               StringBuffer response = generateHttpResponse(con, false);
                String result;
-
                try {
-
                        result = IOUtils.toString(con.getErrorStream());
                        response.append(result);
-
-               } catch (Exception e2) {
-                       // result = null;
+               } catch (Exception e) {
+//                     logger.debug("Fail with exception", e);
                }
                logger.debug("Response body: {}",response);
+// print result
+               setHttpResponseToObject(restResponse, con, responseCode, response);
+               con.disconnect();
 
-               // print result
+               return restResponse;
+       }
 
-               restResponse.setErrorCode(responseCode);
+       public RestResponse httpSendDelete(String url, Map<String, String> headers) throws IOException {
+               if (url.matches("^(https)://.*$")){
+                       return httpsSendDelete(url, headers);
+               }
+               return httpSendDeleteInternal(url, headers);
+       }
 
-               if (response != null) {
-                       restResponse.setResponse(response.toString());
+       public RestResponse httpSendGet(String url, Map<String, String> headers) throws IOException {
+               if (url.matches("^(https)://.*$")){
+                       return httpsSendGet(url, headers);
                }
+               return httpSendGetInternal(url, headers);
+       }
 
-               restResponse.setErrorCode(responseCode);
-               // restResponse.setResponse(result);
-               Map<String, List<String>> headerFields = con.getHeaderFields();
-               restResponse.setHeaderFields(headerFields);
-               String responseMessage = con.getResponseMessage();
-               restResponse.setResponseMessage(responseMessage);
+       public RestResponse httpSendByMethod(String url, String method, String body, Map<String, String> headers) throws IOException {
+               if (url.matches("^(https)://.*$")){
+                       return httpsSendByMethod(url, method, body, headers);
+               }
+               return httpSendByMethodInternal(url, method, body, headers);
+       }
 
-               con.disconnect();
+       public RestResponse httpSendPost(String url, String body, Map<String, String> headers) throws IOException {
+               if (url.matches("^(https)://.*$")){
+                       return httpsSendByMethod(url, "POST", body, headers);
+               }
+               return httpSendByMethod(url, "POST", body, headers);
+       }
 
-               return restResponse;
+       public RestResponse httpSendPut(String url, String body, Map<String, String> headers) throws IOException {
+               if (url.matches("^(https)://.*$")){
+                       return httpsSendByMethod(url, "PUT", body, headers);
+               }
+               return httpSendByMethod(url, "PUT", body, headers);
        }
 
-       public RestResponse httpSendByMethod(String url, String method, String body, Map<String, String> headers)
-                       throws IOException {
+
+       public RestResponse httpSendDeleteInternal(String url, Map<String, String> headers) throws IOException {
 
                RestResponse restResponse = new RestResponse();
                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 
-               // add request method
-               con.setRequestMethod(method);
-
-               // add request headers
-               if (headers != null) {
-                       for (Entry<String, String> header : headers.entrySet()) {
-                               String key = header.getKey();
-                               String value = header.getValue();
-                               con.setRequestProperty(key, value);
-                       }
-
-               }
-               if (body != null && !body.isEmpty() && !method.equals("DELETE")) {
-                       // Send post request
-                       con.setDoOutput(true);
-                       DataOutputStream wr = new DataOutputStream(con.getOutputStream());
-                       wr.writeBytes(body);
-                       wr.flush();
-                       wr.close();
-               }
-
-               // con.connect();
+               addHttpRequestHEaders(headers, con);
 
+               con.setDoOutput(true);
+               con.setRequestMethod("DELETE");
                int responseCode = con.getResponseCode();
-               logger.debug("Send {} http request, url: {}",method,url);
+               logger.debug("Send DELETE http request, url: {}",url);
                logger.debug("Response Code: {}",responseCode);
 
-               StringBuffer response = new StringBuffer();
-
+               StringBuffer response = generateHttpResponse(con, false);
+               String result;
                try {
-                       BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                       String inputLine;
-                       while ((inputLine = in.readLine()) != null) {
-                               response.append(inputLine);
-                       }
-                       in.close();
+                       result = IOUtils.toString(con.getErrorStream());
+                       response.append(result);
                } catch (Exception e) {
-                       // response = null;
-                       logger.debug("response body is null");
+//                     logger.debug("Fail with exception", e);
                }
+               logger.debug("Response body: {}",response);
 
-               String result;
+// print result
+               setHttpResponseToObject(restResponse, con, responseCode, response);
+               con.disconnect();
+
+               return restResponse;
+       }
+
+       public static RestResponse sendHttpPostWithEntity(HttpEntity requestEntity, String url, Map<String, String> headers) throws IOException {
+               CloseableHttpResponse response = null;
+               CloseableHttpClient client = HttpClients.createDefault();
                try {
+                       HttpPost httpPost = new HttpPost(url);
+                       RestResponse restResponse = new RestResponse();
+                       for (Entry<String, String> entry : headers.entrySet()) {
+                               httpPost.addHeader(entry.getKey(), entry.getValue());
+                       }
 
-                       result = IOUtils.toString(con.getErrorStream());
-                       response.append(result);
+                       httpPost.setEntity(requestEntity);
+                       response = client.execute(httpPost);
+                       HttpEntity responseEntity = response.getEntity();
+                       String responseBody = null;
+                       if (responseEntity != null) {
+                               InputStream instream = responseEntity.getContent();
+                               StringWriter writer = new StringWriter();
+                               IOUtils.copy(instream, writer);
+                               responseBody = writer.toString();
+                               try {
 
-               } catch (Exception e2) {
-                       result = null;
-               }
-               logger.debug("Response body: {}",response);
+                               } finally {
+                                       instream.close();
+                               }
+                       }
 
-               // print result
+                       restResponse.setErrorCode(response.getStatusLine().getStatusCode());
+                       restResponse.setResponse(responseBody);
 
-               restResponse.setErrorCode(responseCode);
-               // if (response == null) {
-               // restResponse.setResponse(null);
-               // } else {
-               // restResponse.setResponse(response.toString());
-               // }
+                       return restResponse;
+
+               } finally {
+                       closeResponse(response);
+                       closeHttpClient(client);
 
-               if (response != null) {
-                       restResponse.setResponse(response.toString());
                }
-               Map<String, List<String>> headerFields = con.getHeaderFields();
-               restResponse.setHeaderFields(headerFields);
-               String responseMessage = con.getResponseMessage();
-               restResponse.setResponseMessage(responseMessage);
+       }
 
-               con.disconnect();
-               return restResponse;
+       private static void closeHttpClient(CloseableHttpClient client) {
+               try {
+                       if (client != null) {
+                               client.close();
+                       }
+               } catch (IOException e) {
+                       logger.debug("failed to close client or response: ", e);
+               }
+       }
 
+       private static void closeResponse(CloseableHttpResponse response) {
+               try {
+                       if (response != null) {
+                               response.close();
+                       }
+               } catch (IOException e) {
+                       logger.debug("failed to close client or response: ", e);
+               }
        }
 
-       public RestResponse sendHttpPost(String url, String body, Map<String, String> headers) throws IOException {
 
-               RestResponse restResponse = new RestResponse();
-               URL obj = new URL(url);
-               HttpURLConnection con = (HttpURLConnection) obj.openConnection();
+       //      -----------------------------Https------------------------------------------------------------------------
+       public RestResponse httpsSendGet(String url, Map<String, String> headers) throws IOException {
 
-               // add request method
-               con.setRequestMethod("POST");
+               RestResponse restResponse = new RestResponse();
+               url = url.replaceAll("\\s", "%20");
+               URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
+               HttpsURLConnection con = (HttpsURLConnection)obj.openConnection();
+// optional default is GET
+               con.setRequestMethod("GET");
+               HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();
+               con.setHostnameVerifier(hostnameVerifier);
+               addHttpsRequestHeaders(headers, con);
+               Boolean multiPart = false;
+               if(headers.get(HttpHeaderEnum.ACCEPT.getValue()) != null) {
+                       if (headers.get(HttpHeaderEnum.ACCEPT.getValue()).equals(BaseRestUtils.acceptMultipartHeader)) {
+                               multiPart = true;
+                       }
+               }
+               int responseCode = con.getResponseCode();
+               logger.debug("Send GET http request, url: {}",url);
+               logger.debug("Response Code: {}",responseCode);
 
-               // add request headers
-               if (headers != null) {
-                       for (Entry<String, String> header : headers.entrySet()) {
-                               String key = header.getKey();
-                               String value = header.getValue();
-                               con.setRequestProperty(key, value);
+               StringBuffer response = generateHttpsResponse(con, multiPart);
+               String result;
+               try {
+                       if(con.getErrorStream()!=null) {
+                               result = IOUtils.toString(con.getErrorStream());
+                               response.append(result);
                        }
+               } catch (Exception e) {
+//                     logger.debug("Fail with exception", e);
                }
+               logger.debug("Response body: {}",response);
+// print result
+               setHttpsResponseToObject(restResponse, con, responseCode, response);
+               con.disconnect();
+
+               return restResponse;
+       }
 
-               // Send post request
+
+       public RestResponse httpsSendPost(String url, String body, Map<String, String> headers) throws IOException {
+
+               RestResponse restResponse = new RestResponse();
+               URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
+               HttpsURLConnection con = (HttpsURLConnection)obj.openConnection();
+               HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();
+               con.setHostnameVerifier(hostnameVerifier);
+// add request method
+               con.setRequestMethod("POST");
+// add request headers
+               addHttpRequestHEaders(headers, con);
+// Send post request
                if (body != null) {
                        con.setDoOutput(true);
                        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
@@ -295,394 +312,120 @@ public class HttpRequest {
                        wr.flush();
                        wr.close();
                }
-
-               // con.connect();
-
                int responseCode = con.getResponseCode();
                logger.debug("Send POST http request, url: {}",url);
                logger.debug("Response Code: {}",responseCode);
 
-               StringBuffer response = new StringBuffer();
+               StringBuffer response = generateHttpsResponse(con, false);
+               String result;
                try {
-                       BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                       String inputLine;
-                       while ((inputLine = in.readLine()) != null) {
-                               response.append(inputLine);
+                       if(con.getErrorStream()!=null) {
+                               result = IOUtils.toString(con.getErrorStream());
+                               response.append(result);
                        }
-                       in.close();
                } catch (Exception e) {
-                       logger.debug("response body is null");
+//                     logger.debug("Fail with exception", e);
                }
+               logger.debug("Response body: {}",response);
+// print result
+               setHttpResponseToObject(restResponse, con, responseCode, response);
+               con.disconnect();
 
-               String result;
-
-               try {
+               return restResponse;
+       }
 
-                       result = IOUtils.toString(con.getErrorStream());
-                       response.append(result);
+       public RestResponse httpsSendByMethod(String url, String method, String body, Map<String, String> headers) throws IOException {
 
-               } catch (Exception e2) {
-                       result = null;
+               RestResponse restResponse = new RestResponse();
+               URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
+               HttpsURLConnection con = (HttpsURLConnection)obj.openConnection();
+               HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();
+               con.setHostnameVerifier(hostnameVerifier);
+// add request method
+               con.setRequestMethod(method);
+// add request headers
+               addHttpRequestHEaders(headers, con);
+               if (body != null && !body.isEmpty() && !method.equals("DELETE")) {
+// Send post request
+                       con.setDoOutput(true);
+                       DataOutputStream wr = new DataOutputStream(con.getOutputStream());
+                       wr.writeBytes(body);
+                       wr.flush();
+                       wr.close();
                }
-               logger.debug("Response body: {}",response);
-
-               // print result
-
-               restResponse.setErrorCode(responseCode);
-
-               if (response != null) {
-                       restResponse.setResponse(response.toString());
-               }
-
-               Map<String, List<String>> headerFields = con.getHeaderFields();
-               restResponse.setHeaderFields(headerFields);
-               String responseMessage = con.getResponseMessage();
-               restResponse.setResponseMessage(responseMessage);
-
-               con.disconnect();
-               return restResponse;
-
-       }
-
-       public RestResponse httpSendPost(String url, String body, Map<String, String> headers) throws IOException {
-               return httpSendPost(url, body, headers, "POST");
-       }
-
-       public RestResponse httpSendPut(String url, String body, Map<String, String> headers) throws IOException {
-               return httpSendPost(url, body, headers, "PUT");
-       }
-
-       public RestResponse httpSendPost(String url, String body, Map<String, String> headers, String methodType)
-                       throws IOException {
-
-               RestResponse restResponse = new RestResponse();
-               URL obj = new URL(url);
-               HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
-               // add request method
-               con.setRequestMethod(methodType);
-
-               // add request headers
-               if (headers != null) {
-                       for (Entry<String, String> header : headers.entrySet()) {
-                               String key = header.getKey();
-                               String value = header.getValue();
-                               con.setRequestProperty(key, value);
-                       }
-               }
-
-               // Send post request
-               if (body != null) {
-                       con.setDoOutput(true);
-                       DataOutputStream wr = new DataOutputStream(con.getOutputStream());
-                       wr.writeBytes(body);
-                       wr.flush();
-                       wr.close();
-               }
-
-               // con.connect();
 
                int responseCode = con.getResponseCode();
-               logger.debug("Send POST http request, url: {}",url);
+               logger.debug("Send {} http request, url: {}",method,url);
                logger.debug("Response Code: {}",responseCode);
 
-               StringBuffer response = new StringBuffer();
+               StringBuffer response = generateHttpResponse(con, false);
+               String result;
                try {
-                       BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                       String inputLine;
-                       while ((inputLine = in.readLine()) != null) {
-                               response.append(inputLine);
+                       if(con.getErrorStream()!=null) {
+                               result = IOUtils.toString(con.getErrorStream());
+                               response.append(result);
                        }
-                       in.close();
                } catch (Exception e) {
-                       logger.debug("response body is null");
-               }
-
-               String result;
-
-               try {
-
-                       result = IOUtils.toString(con.getErrorStream());
-                       response.append(result);
-
-               } catch (Exception e2) {
-                       result = null;
+//                     logger.debug("Fail with exception", e);
                }
                logger.debug("Response body: {}",response);
-
-               // print result
-
-               restResponse.setErrorCode(responseCode);
-
-               if (response != null) {
-                       restResponse.setResponse(response.toString());
-               }
-
-               Map<String, List<String>> headerFields = con.getHeaderFields();
-               restResponse.setHeaderFields(headerFields);
-               String responseMessage = con.getResponseMessage();
-               restResponse.setResponseMessage(responseMessage);
-
+// print result
+               setHttpResponseToObject(restResponse, con, responseCode, response);
                con.disconnect();
-               return restResponse;
-
-       }
-
-       public RestResponse httpSendDeleteWithBody2(String url, String body, Map<String, String> headers)
-                       throws ClientProtocolException, IOException {
-
-               CloseableHttpClient httpclient = HttpClients.createDefault();
-               RestResponse restResponse = new RestResponse();
-               HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(url);
-
-               // add request headers
-               if (headers != null) {
-                       for (Entry<String, String> header : headers.entrySet()) {
-                               String key = header.getKey();
-                               String value = header.getValue();
-                               httpDelete.addHeader(key, value);
-                       }
-               }
-
-               // add body to request
-               StringEntity input = new StringEntity(body, ContentType.APPLICATION_JSON);
-               httpDelete.setEntity(input);
-
-               // execute request
-               CloseableHttpResponse response = httpclient.execute(httpDelete);
-
-               restResponse.setErrorCode(response.getStatusLine().getStatusCode());
 
                return restResponse;
        }
 
-       public RestResponse httpSendDeleteWithBody(String url, String body, Map<String, String> headers)
-                       throws IOException {
 
-               RestResponse restResponse = new RestResponse();
-               URL obj = new URL(url);
-               HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
-               // add request method
-               con.setRequestMethod("DELETE");
+       public RestResponse httpsSendDelete(String url, Map<String, String> headers) throws IOException {
 
-               // add request headers
-               if (headers != null) {
-                       for (Entry<String, String> header : headers.entrySet()) {
-                               String key = header.getKey();
-                               String value = header.getValue();
-                               con.setRequestProperty(key, value);
-                       }
-               }
+               RestResponse restResponse = new RestResponse();
+               URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
+               HttpsURLConnection con = (HttpsURLConnection)obj.openConnection();
+               HostnameVerifier hostnameVerifier = new DefaultHostnameVerifier();
+               con.setHostnameVerifier(hostnameVerifier);
+// add request headers
+               addHttpRequestHEaders(headers, con);
 
-               // Send post request
                con.setDoOutput(true);
-               DataOutputStream wr = new DataOutputStream(con.getOutputStream());
-               wr.writeBytes(body);
-               wr.flush();
-               wr.close();
-
-               // con.connect();
-
+               con.setRequestMethod("DELETE");
                int responseCode = con.getResponseCode();
                logger.debug("Send DELETE http request, url: {}",url);
                logger.debug("Response Code: {}",responseCode);
 
-               StringBuffer response = new StringBuffer();
+               StringBuffer response = generateHttpsResponse(con, false);
+               String result;
                try {
-                       BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                       String inputLine;
-                       while ((inputLine = in.readLine()) != null) {
-                               response.append(inputLine);
+                       if(con.getErrorStream()!=null) {
+                               result = IOUtils.toString(con.getErrorStream());
+                               response.append(result);
                        }
-                       in.close();
                } catch (Exception e) {
-                       logger.debug("response body is null");
-               }
-
-               String result;
-
-               try {
-
-                       result = IOUtils.toString(con.getErrorStream());
-                       response.append(result);
-
-               } catch (Exception e2) {
-                       result = null;
+//                     logger.debug("Fail with exception", e);
                }
-               logger.debug("Response body: {}", response);
-
-               // print result
-
-               restResponse.setErrorCode(responseCode);
-
-               if (response != null) {
-                       restResponse.setResponse(response.toString());
-               }
-
-               Map<String, List<String>> headerFields = con.getHeaderFields();
-               restResponse.setHeaderFields(headerFields);
-               String responseMessage = con.getResponseMessage();
-               restResponse.setResponseMessage(responseMessage);
-
+               logger.debug("Response body: {}",response);
+// print result
+               setHttpResponseToObject(restResponse, con, responseCode, response);
                con.disconnect();
-               return restResponse;
 
+               return restResponse;
        }
 
-       public RestResponse httpSendPostWithOutBody(String url, Map<String, String> headers) throws IOException {
-
-               RestResponse restResponse = new RestResponse();
-               URL obj = new URL(url);
-               HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
-               // add request method
-               con.setRequestMethod("POST");
-
-               // add request headers
+       //      ---------------------------------------
+       private void addHttpsRequestHeaders(Map<String, String> headers, HttpsURLConnection con) {
+               // add request header
                if (headers != null) {
                        for (Entry<String, String> header : headers.entrySet()) {
                                String key = header.getKey();
                                String value = header.getValue();
                                con.setRequestProperty(key, value);
                        }
-               }
-
-               // con.connect();
-
-               int responseCode = con.getResponseCode();
-               logger.debug("Send POST http request, url: {}",url);
-               logger.debug("Response Code: {}",responseCode);
-
-               StringBuffer response = new StringBuffer();
-
-               try {
-                       BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                       String inputLine;
-                       while ((inputLine = in.readLine()) != null) {
-                               response.append(inputLine);
-                       }
-                       in.close();
-               } catch (Exception e) {
-                       // response = null;
-                       logger.debug("response body is null");
-               }
-
-               String result;
-               try {
-
-                       result = IOUtils.toString(con.getErrorStream());
-                       response.append(result);
-
-               } catch (Exception e2) {
-                       result = null;
-               }
-               logger.debug("Response body: {}",response);
-
-               // print result
-
-               restResponse.setErrorCode(responseCode);
-               // if (response == null) {
-               // restResponse.setResponse(null);
-               // } else {
-               // restResponse.setResponse(response.toString());
-               // }
-
-               if (response != null) {
-                       restResponse.setResponse(response.toString());
-               }
-
-               Map<String, List<String>> headerFields = con.getHeaderFields();
-               restResponse.setHeaderFields(headerFields);
-               String responseMessage = con.getResponseMessage();
-               restResponse.setResponseMessage(responseMessage);
-
-               con.disconnect();
-               return restResponse;
-
-       }
-
-       public RestResponse httpSendPostMultipart(String url, Map<String, String> headers, String jsonLocation,
-                       String zipLocation) throws IOException {
-
-               Gson gson = new Gson();
-               String gsonToSend = null;
-               RestResponse restResponse = new RestResponse();
-               BufferedReader br = null;
-               //
-               //
-               //
-               //
-               // try {
-               //
-               // String sCurrentLine;
-               //
-               // br = new BufferedReader(new FileReader(jsonLocation));
-               //
-               // while ((sCurrentLine = br.readLine()) != null) {
-               // System.out.println(sCurrentLine);
-               // }
-               //
-               // } catch (IOException e) {
-               // e.printStackTrace();
-               // } finally {
-               // try {
-               // if (br != null)br.close();
-               // gsonToSend = br.toString();
-               // } catch (IOException ex) {
-               // ex.printStackTrace();
-               // }
-               // }
-
-               gsonToSend = new Scanner(new File(jsonLocation)).useDelimiter("\\Z").next();
-               logger.debug("gsonToSend: {}",gsonToSend);
-
-               MultipartEntityBuilder mpBuilder = MultipartEntityBuilder.create();
-               mpBuilder.addPart("resourceZip", new FileBody(new File(zipLocation)));
-               mpBuilder.addPart("resourceMetadata", new StringBody(gsonToSend, ContentType.APPLICATION_JSON));
-
-               HttpPost httpPost = new HttpPost(url);
-               httpPost.addHeader("USER_ID", "adminid");
-               httpPost.setEntity(mpBuilder.build());
-
-               CloseableHttpClient client = HttpClients.createDefault();
-               CloseableHttpResponse response = client.execute(httpPost);
-               try {
-                       logger.debug("----------------------------------------");
-                       logger.debug("response.getStatusLine(): {}",response.getStatusLine());
-                       HttpEntity resEntity = response.getEntity();
-                       if (resEntity != null) {
-                               logger.debug("Response content length: {}",resEntity.getContentLength());
-                       }
-                       EntityUtils.consume(resEntity);
-               } finally {
 
-                       response.close();
-                       client.close();
                }
-
-               restResponse.setErrorCode(response.getStatusLine().getStatusCode());
-               restResponse.setResponse(response.getEntity().toString());
-
-               return restResponse;
-
        }
 
-       public RestResponse httpSendPostWithAuth(String url, String body, Map<String, String> headers, String username,
-                       String password) throws IOException {
-
-               String userPassword = username + ":" + password;
-               String encoding = Base64.encodeBase64String(userPassword.getBytes());
-               RestResponse restResponse = new RestResponse();
-               URL obj = new URL(url);
-               HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
-               // add request method
-               con.setRequestMethod("POST");
-
-               con.setRequestProperty("Authorization", "Basic " + encoding);
-
-               // add request headers
+       private void addHttpRequestHEaders(Map<String, String> headers, HttpURLConnection con) {
+               // add request header
                if (headers != null) {
                        for (Entry<String, String> header : headers.entrySet()) {
                                String key = header.getKey();
@@ -691,40 +434,12 @@ public class HttpRequest {
                        }
 
                }
+       }
 
-               // Send post request
-               con.setDoOutput(true);
-               DataOutputStream wr = new DataOutputStream(con.getOutputStream());
-               wr.writeBytes(body);
-               wr.flush();
-               wr.close();
-
-               // con.connect();
-
-               int responseCode = con.getResponseCode();
-               logger.debug("Send POST http request, url: {}",url);
-               logger.debug("Response Code: {}",responseCode);
-
-               StringBuffer response = new StringBuffer();
-               try {
-                       BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                       String inputLine;
-                       while ((inputLine = in.readLine()) != null) {
-                               response.append(inputLine);
-                       }
-                       in.close();
-               } catch (Exception e) {
-                       response = null;
-
-               }
-               logger.debug("Response body: {}",response);
-
-               // print result
-
+       private void setHttpResponseToObject(RestResponse restResponse, HttpURLConnection con, int responseCode, StringBuffer response) throws IOException {
                restResponse.setErrorCode(responseCode);
-               if (response == null) {
-                       restResponse.setResponse(null);
-               } else {
+
+               if (response != null) {
                        restResponse.setResponse(response.toString());
                }
 
@@ -732,135 +447,67 @@ public class HttpRequest {
                restResponse.setHeaderFields(headerFields);
                String responseMessage = con.getResponseMessage();
                restResponse.setResponseMessage(responseMessage);
-
-               con.disconnect();
-               return restResponse;
-
        }
 
-       public RestResponse httpSendDelete(String url, Map<String, String> headers) throws IOException {
-
-               RestResponse restResponse = new RestResponse();
-               URL obj = new URL(url);
-               HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
-               if (headers != null) {
-                       for (Entry<String, String> header : headers.entrySet()) {
-                               String key = header.getKey();
-                               String value = header.getValue();
-                               con.setRequestProperty(key, value);
-                       }
-
-               }
-
-               con.setDoOutput(true);
-               con.setRequestMethod("DELETE");
-               int responseCode = con.getResponseCode();
-               logger.debug("Send DELETE http request, url: {}",url);
-               logger.debug("Response Code: {}",responseCode);
-
+       private StringBuffer generateHttpResponse(HttpURLConnection con, Boolean isMultiPart) {
                StringBuffer response = new StringBuffer();
-
+               StringWriter writer = new StringWriter();
                try {
-                       BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                       String inputLine;
-                       while ((inputLine = in.readLine()) != null) {
-                               response.append(inputLine);
+                       if(isMultiPart) {
+                               IOUtils.copy((con.getInputStream()), writer, Charset.forName("UTF-8"));
+                               response = writer.getBuffer();
+                       }else {
+
+                               BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
+                               String inputLine;
+                               while ((inputLine = in.readLine()) != null) {
+                                       response.append(inputLine);
+                               }
+                               in.close();
                        }
-                       in.close();
                } catch (Exception e) {
                        logger.debug("response body is null");
                }
 
-               String result;
-
-               try {
-
-                       result = IOUtils.toString(con.getErrorStream());
-                       response.append(result);
-
-               } catch (Exception e2) {
-                       result = null;
-               }
-               logger.debug("Response body: {}",response);
-
-               // print result
-
-               restResponse.setErrorCode(responseCode);
+               return response;
+       }
 
+       private void setHttpsResponseToObject(RestResponse restResponse, HttpsURLConnection con, int responseCode, StringBuffer response) throws IOException {
                if (response != null) {
                        restResponse.setResponse(response.toString());
                }
 
-               restResponse.setErrorCode(con.getResponseCode());
+               restResponse.setErrorCode(responseCode);
+               // restResponse.setResponse(result);
                Map<String, List<String>> headerFields = con.getHeaderFields();
                restResponse.setHeaderFields(headerFields);
                String responseMessage = con.getResponseMessage();
                restResponse.setResponseMessage(responseMessage);
-
-               con.disconnect();
-
-               return restResponse;
        }
 
-       public static RestResponse sendHttpPostWithEntity(HttpEntity requestEntity, String url, Map<String, String> headers)
-                       throws IOException, ClientProtocolException {
-               CloseableHttpResponse response = null;
-               CloseableHttpClient client = HttpClients.createDefault();
+       private StringBuffer generateHttpsResponse(HttpsURLConnection con, Boolean isMultiPart) {
+               StringBuffer response = new StringBuffer();
+               StringWriter writer = new StringWriter();
                try {
-                       HttpPost httpPost = new HttpPost(url);
-                       RestResponse restResponse = new RestResponse();
-                       for (Entry<String, String> entry : headers.entrySet()) {
-                               httpPost.addHeader(entry.getKey(), entry.getValue());
-                       }
-
-                       httpPost.setEntity(requestEntity);
-                       response = client.execute(httpPost);
-                       HttpEntity responseEntity = response.getEntity();
-                       String responseBody = null;
-                       if (responseEntity != null) {
-                               InputStream instream = responseEntity.getContent();
-                               StringWriter writer = new StringWriter();
-                               IOUtils.copy(instream, writer);
-                               responseBody = writer.toString();
-                               try {
-
-                               } finally {
-                                       instream.close();
+                       if(isMultiPart) {
+                               IOUtils.copy((con.getInputStream()), writer, Charset.forName("UTF-8"));
+                               response = writer.getBuffer();
+                       }else {
+
+                               BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
+                               String inputLine;
+                               while ((inputLine = in.readLine()) != null) {
+                                       response.append(inputLine);
                                }
+                               in.close();
                        }
-
-                       restResponse.setErrorCode(response.getStatusLine().getStatusCode());
-                       restResponse.setResponse(responseBody);
-
-                       return restResponse;
-
-               } finally {
-                       closeResponse(response);
-                       closeHttpClient(client);
-
+               } catch (Exception e) {
+                       logger.debug("response body is null");
                }
-       }
 
-       private static void closeHttpClient(CloseableHttpClient client) {
-               try {
-                       if (client != null) {
-                               client.close();
-                       }
-               } catch (IOException e) {
-                       logger.debug("failed to close client or response: ", e);
-               }
+               return response;
        }
 
-       private static void closeResponse(CloseableHttpResponse response) {
-               try {
-                       if (response != null) {
-                               response.close();
-                       }
-               } catch (IOException e) {
-                       logger.debug("failed to close client or response: ", e);
-               }
-       }
 
        @NotThreadSafe
        class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {