Initialize the publisher 85/7785/1
authorGuangrong Fu <fu.guangrong@zte.com.cn>
Thu, 17 Aug 2017 01:54:46 +0000 (09:54 +0800)
committerGuangrong Fu <fu.guangrong@zte.com.cn>
Thu, 17 Aug 2017 01:54:46 +0000 (09:54 +0800)
Add a new class for the publisher

Change-Id: Ie487dfd239b9ebbb2644bae178db949dee02d96f
Issue-ID: HOLMES-30
Signed-off-by: Guangrong Fu <fu.guangrong@zte.com.cn>
holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java [new file with mode: 0644]

diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java
new file mode 100644 (file)
index 0000000..eaeaf43
--- /dev/null
@@ -0,0 +1,62 @@
+/*\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.holmes.common.dmaap;\r
+\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\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 javax.ws.rs.core.MediaType;\r
+import javax.ws.rs.core.Response;\r
+import lombok.Getter;\r
+import lombok.Setter;\r
+import org.glassfish.jersey.client.ClientConfig;\r
+import org.onap.holmes.common.dmaap.entity.PolicyMsg;\r
+import org.onap.holmes.common.exception.CorrelationException;\r
+\r
+@Getter\r
+@Setter\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
+        Client client = ClientBuilder.newClient(new ClientConfig());\r
+        ObjectMapper mapper = new ObjectMapper();\r
+        String content = null;\r
+        try {\r
+            content = mapper.writeValueAsString(msg);\r
+        } catch (JsonProcessingException e) {\r
+            throw new CorrelationException("Failed to convert the message object to a json string.", e);\r
+        }\r
+\r
+        WebTarget webTarget = client.target(url);\r
+        Response response = webTarget.request(MediaType.APPLICATION_JSON)\r
+                .post(Entity.entity(content, MediaType.APPLICATION_JSON));\r
+\r
+        return checkStatus(response);\r
+    }\r
+\r
+    private boolean checkStatus(Response response) {\r
+        return false;\r
+    }\r
+}\r