X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=holmes-actions%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Fcommon%2Fdmaap%2FPublisher.java;h=ad5109b775d8151b5d42b7d2d0ed89f96420b503;hb=bf20ddf00200c5468da7a0090caf28beebb93e9c;hp=eaeaf430e97dd73b7fc5308df0f64ffc7ab658e3;hpb=8280d2b7b61920c0bc6311ae97181b28eef9fa44;p=holmes%2Fcommon.git 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 index eaeaf43..ad5109b 100644 --- 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 @@ -13,25 +13,24 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.holmes.common.dmaap; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.Entity; -import javax.ws.rs.client.WebTarget; +import org.onap.holmes.common.dmaap.entity.PolicyMsg; +import org.onap.holmes.common.exception.CorrelationException; +import com.alibaba.fastjson.JSON; +import java.util.HashMap; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import lombok.Getter; import lombok.Setter; -import org.glassfish.jersey.client.ClientConfig; -import org.onap.holmes.common.dmaap.entity.PolicyMsg; -import org.onap.holmes.common.exception.CorrelationException; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.entity.StringEntity; +import org.jvnet.hk2.annotations.Service; +import org.onap.holmes.common.utils.HttpsUtils; @Getter @Setter +@Service public class Publisher { private String topic; @@ -40,23 +39,26 @@ public class Publisher { private String authExpDate; public boolean publish(PolicyMsg msg) throws CorrelationException { - Client client = ClientBuilder.newClient(new ClientConfig()); - ObjectMapper mapper = new ObjectMapper(); - String content = null; + String content; try { - content = mapper.writeValueAsString(msg); - } catch (JsonProcessingException e) { - throw new CorrelationException("Failed to convert the message object to a json string.", e); + content = JSON.toJSONString(msg); + } catch (Exception e) { + throw new CorrelationException("Failed to convert the message object to a json string.", + e); } - - WebTarget webTarget = client.target(url); - Response response = webTarget.request(MediaType.APPLICATION_JSON) - .post(Entity.entity(content, MediaType.APPLICATION_JSON)); - - return checkStatus(response); + HttpResponse httpResponse; + HashMap headers = new HashMap<>(); + headers.put("Accept", MediaType.APPLICATION_JSON); + headers.put("Content-Type", MediaType.APPLICATION_JSON); + try { + httpResponse = HttpsUtils.post(url, headers, new HashMap<>(), new StringEntity(content, "utf-8")); + } catch (Exception e) { + throw new CorrelationException("Failed to connect to DCAE.", e); + } + return checkStatus(httpResponse); } - private boolean checkStatus(Response response) { - return false; + private boolean checkStatus(HttpResponse httpResponse) { + return (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ? true : false; } }