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=a4024ea9540ff8ef8e969a2c5263147c4b051016;hb=6af8af01fb0cdceb384cf47cf241736620510465;hp=d95853d2f8f3897c435586dbb41e41bbb7f9614a;hpb=459e4faa15c47f39c7400000cb15ca2d2123e70f;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 d95853d..a4024ea 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 @@ -15,68 +15,23 @@ */ package org.onap.holmes.common.dmaap; -import java.io.IOException; -import lombok.extern.slf4j.Slf4j; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.client.CloseableHttpClient; -import org.onap.holmes.common.dmaap.entity.PolicyMsg; -import org.onap.holmes.common.exception.CorrelationException; -import com.google.gson.Gson; -import java.util.HashMap; -import javax.ws.rs.core.MediaType; import lombok.Getter; import lombok.Setter; -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; +import org.onap.holmes.common.dmaap.entity.PolicyMsg; +import org.onap.holmes.common.utils.JerseyClient; + +import javax.ws.rs.client.Entity; +import java.util.concurrent.TimeUnit; @Getter @Setter @Service -@Slf4j public class Publisher { - - private String topic; private String url; - private String authInfo; - private String authExpDate; - - public boolean publish(PolicyMsg msg) throws CorrelationException { - String content; - try { - //content = JSON.toJSONString(msg); - content = new Gson().toJson(msg); - } catch (Exception e) { - throw new CorrelationException("Failed to convert the message object to a json string.", - e); - } - HttpResponse httpResponse; - HashMap headers = new HashMap<>(); - headers.put("Accept", MediaType.APPLICATION_JSON); - headers.put("Content-Type", MediaType.APPLICATION_JSON); - CloseableHttpClient httpClient = null; - HttpPost httpPost = new HttpPost(url); - try { - httpClient = HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT); - httpResponse = HttpsUtils.post(httpPost, headers, new HashMap<>(), new StringEntity(content, "utf-8"), httpClient); - } catch (Exception e) { - throw new CorrelationException("Failed to connect to DCAE.", e); - } finally { - httpPost.releaseConnection(); - if (httpClient != null) { - try { - httpClient.close(); - } catch (IOException e) { - log.warn("Failed to close http client!"); - } - } - } - return checkStatus(httpResponse); - } + private JerseyClient client = new JerseyClient(TimeUnit.SECONDS.toMillis(30)); - private boolean checkStatus(HttpResponse httpResponse) { - return (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ? true : false; + public void publish(PolicyMsg msg) { + client.post(url, Entity.json(msg)); } }