X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=dmaap-dsa%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fholmes%2Fdsa%2Fdmaappolling%2FSubscriber.java;h=d696deefedcd55e73da77ef5eacf6e9723cb2f5c;hb=bfe7264117cdcf6aa570db8643676e73a7158ef0;hp=3563b660d3bfe7cdffd90f834894850498168c1a;hpb=ac1c9d5d03755543254779129468a48ced972c2c;p=holmes%2Fdsa.git diff --git a/dmaap-dsa/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java b/dmaap-dsa/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java index 3563b66..d696dee 100644 --- a/dmaap-dsa/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java +++ b/dmaap-dsa/src/main/java/org/onap/holmes/dsa/dmaappolling/Subscriber.java @@ -18,24 +18,28 @@ package org.onap.holmes.dsa.dmaappolling; import java.io.IOException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; -import javax.inject.Inject; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.Response; +import java.util.UUID; + import lombok.Getter; import lombok.Setter; -import org.glassfish.jersey.client.ClientConfig; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.HttpResponse; +import org.apache.http.impl.client.CloseableHttpClient; import org.onap.holmes.common.api.stat.VesAlarm; +import org.onap.holmes.common.dropwizard.ioc.utils.ServiceLocatorHolder; import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.GsonUtil; +import org.onap.holmes.common.utils.HttpsUtils; @Getter @Setter +@Slf4j public class Subscriber { - @Inject - private DMaaPResponseUtil dMaaPResponseUtil; + private DMaaPResponseUtil dMaaPResponseUtil = ServiceLocatorHolder.getLocator() + .getService(DMaaPResponseUtil.class); /** * The number of milliseconds to wait for messages if none are immediately available. This @@ -57,13 +61,19 @@ public class Subscriber { private boolean secure; private String topic; private String url; - private String consumerGroup = "g0"; - private String consumer = "u1"; + private String uuid = UUID.randomUUID() + ""; + private String consumerGroup = "homlesGroup" + uuid; + private String consumer = "homles" + uuid; private String authInfo; private String authExpDate; public List subscribe() throws CorrelationException { - List response = getDMaaPData(); + List response; + try { + response = getDMaaPData(); + } catch (Exception e) { + throw new CorrelationException("Failed to get data from DMaaP.", e); + } try { return extractVesAlarm(response); } catch (Exception e) { @@ -71,14 +81,29 @@ public class Subscriber { } } - private List getDMaaPData() { - Client client = ClientBuilder.newClient(new ClientConfig()); - WebTarget webTarget = client.target(url); - Response response = webTarget.path(topic).path(consumerGroup).path(consumer).request().get(); - return response.readEntity(List.class); + private List getDMaaPData() throws Exception { + String response; + CloseableHttpClient closeableHttpClient = null; + try { + closeableHttpClient = HttpsUtils.getHttpClient(timeout); + HttpResponse httpResponse = HttpsUtils + .get(url + "/" + consumerGroup + "/" + consumer, new HashMap<>(), closeableHttpClient); + response = HttpsUtils.extractResponseEntity(httpResponse); + } catch (Exception e) { + throw e; + } finally { + if (closeableHttpClient != null) { + try { + closeableHttpClient.close(); + } catch (IOException e) { + log.warn("Failed to close http client!"); + } + } + } + return GsonUtil.jsonToBean(response, List.class); } - List extractVesAlarm(List responseEntity) throws IOException { + private List extractVesAlarm(List responseEntity) throws IOException { List vesAlarmList = new ArrayList<>(); for (String entity : responseEntity) { vesAlarmList.add(dMaaPResponseUtil.convertJsonToVesAlarm(entity));