Change HTTP Requests into HTTPS Ones
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / dmaap / Publisher.java
index 7201f36..ad5109b 100644 (file)
  */\r
 package org.onap.holmes.common.dmaap;\r
 \r
+import org.onap.holmes.common.dmaap.entity.PolicyMsg;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
 import com.alibaba.fastjson.JSON;\r
-import javax.ws.rs.client.Client;\r
-import javax.ws.rs.client.ClientBuilder;\r
-import javax.ws.rs.client.Entity;\r
-import javax.ws.rs.client.WebTarget;\r
+import java.util.HashMap;\r
 import javax.ws.rs.core.MediaType;\r
-import javax.ws.rs.core.Response;\r
 import lombok.Getter;\r
 import lombok.Setter;\r
+import org.apache.http.HttpResponse;\r
 import org.apache.http.HttpStatus;\r
+import org.apache.http.entity.StringEntity;\r
 import org.jvnet.hk2.annotations.Service;\r
-import org.onap.holmes.common.dmaap.entity.PolicyMsg;\r
-import org.onap.holmes.common.exception.CorrelationException;\r
+import org.onap.holmes.common.utils.HttpsUtils;\r
 \r
 @Getter\r
 @Setter\r
@@ -40,20 +39,26 @@ public class Publisher {
     private String authExpDate;\r
 \r
     public boolean publish(PolicyMsg msg) throws CorrelationException {\r
-        Client client = ClientBuilder.newClient();\r
-        String content = JSON.toJSONString(msg);\r
-        WebTarget webTarget = client.target(url);\r
-        Response response = null;\r
+        String content;\r
+        try {\r
+            content = JSON.toJSONString(msg);\r
+        } catch (Exception e) {\r
+            throw new CorrelationException("Failed to convert the message object to a json string.",\r
+                    e);\r
+        }\r
+        HttpResponse httpResponse;\r
+        HashMap<String, String> headers = new HashMap<>();\r
+        headers.put("Accept", MediaType.APPLICATION_JSON);\r
+        headers.put("Content-Type", MediaType.APPLICATION_JSON);\r
         try {\r
-            response = webTarget.request(MediaType.APPLICATION_JSON)\r
-                    .post(Entity.entity(content, MediaType.APPLICATION_JSON));\r
+            httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8"));\r
         } catch (Exception e) {\r
             throw new CorrelationException("Failed to connect to DCAE.", e);\r
         }\r
-        return checkStatus(response);\r
+        return checkStatus(httpResponse);\r
     }\r
 \r
-    private boolean checkStatus(Response response) {\r
-        return response.getStatus() == HttpStatus.SC_OK;\r
+    private boolean checkStatus(HttpResponse httpResponse) {\r
+        return (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ? true : false;\r
     }\r
 }\r