Simplified HTTP(s) Related Tools
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / dmaap / Publisher.java
index d95853d..a4024ea 100644 (file)
  */\r
 package org.onap.holmes.common.dmaap;\r
 \r
-import java.io.IOException;\r
-import lombok.extern.slf4j.Slf4j;\r
-import org.apache.http.client.methods.HttpPost;\r
-import org.apache.http.impl.client.CloseableHttpClient;\r
-import org.onap.holmes.common.dmaap.entity.PolicyMsg;\r
-import org.onap.holmes.common.exception.CorrelationException;\r
-import com.google.gson.Gson;\r
-import java.util.HashMap;\r
-import javax.ws.rs.core.MediaType;\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.utils.HttpsUtils;\r
+import org.onap.holmes.common.dmaap.entity.PolicyMsg;\r
+import org.onap.holmes.common.utils.JerseyClient;\r
+\r
+import javax.ws.rs.client.Entity;\r
+import java.util.concurrent.TimeUnit;\r
 \r
 @Getter\r
 @Setter\r
 @Service\r
-@Slf4j\r
 public class Publisher {\r
-\r
-    private String topic;\r
     private String url;\r
-    private String authInfo;\r
-    private String authExpDate;\r
-\r
-    public boolean publish(PolicyMsg msg) throws CorrelationException {\r
-        String content;\r
-        try {\r
-            //content = JSON.toJSONString(msg);\r
-               content = new Gson().toJson(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
-        CloseableHttpClient httpClient = null;\r
-        HttpPost httpPost = new HttpPost(url);\r
-        try {\r
-            httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT);\r
-            httpResponse = HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content, "utf-8"), httpClient);\r
-        } catch (Exception e) {\r
-            throw new CorrelationException("Failed to connect to DCAE.", e);\r
-        } finally {\r
-            httpPost.releaseConnection();\r
-            if (httpClient != null) {\r
-                try {\r
-                    httpClient.close();\r
-                } catch (IOException e) {\r
-                    log.warn("Failed to close http client!");\r
-                }\r
-            }\r
-        }\r
-        return checkStatus(httpResponse);\r
-    }\r
+    private JerseyClient client = new JerseyClient(TimeUnit.SECONDS.toMillis(30));\r
 \r
-    private boolean checkStatus(HttpResponse httpResponse) {\r
-        return (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ? true : false;\r
+    public void publish(PolicyMsg msg) {\r
+        client.post(url, Entity.json(msg));\r
     }\r
 }\r