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=09bb0139e21d7938f940657ac8af815291885821;hb=ca7eb991c910002e2c798d29e52a812caed7729c;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..09bb013 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,31 @@ * 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 java.io.IOException; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +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.alibaba.fastjson.JSON; +import com.google.gson.Gson; +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 +@Slf4j public class Publisher { private String topic; @@ -40,23 +46,39 @@ 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); + content = new Gson().toJson(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); + CloseableHttpClient httpClient = null; + HttpPost httpPost = new HttpPost(url); + try { + httpClient = HttpsUtils.getHttpClient(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 boolean checkStatus(Response response) { - return false; + private boolean checkStatus(HttpResponse httpResponse) { + return (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) ? true : false; } }