diff options
author | sunil unnava <sunil.unnava@att.com> | 2018-12-27 16:13:13 -0500 |
---|---|---|
committer | sunil unnava <sunil.unnava@att.com> | 2018-12-27 16:15:40 -0500 |
commit | b07822276c0bba8896d05555094c6e53f69ca2fb (patch) | |
tree | b24509898dd58cbe63b6ef6fe8dcb30906567fb5 /src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java | |
parent | fbc20fa03d5df5fff8a6188dd572bafa2ff80e6b (diff) |
add mirror maker agent code
Issue-ID: DMAAP-910
Change-Id: Ibff92499729d5b34fdbe500783678ef65d6894d5
Signed-off-by: sunil unnava <sunil.unnava@att.com>
Diffstat (limited to 'src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java')
-rw-r--r-- | src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java new file mode 100644 index 0000000..e2dc0ef --- /dev/null +++ b/src/main/java/org/onap/dmaap/mr/dmaapMMAgent/TopicUtil.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * ============LICENSE_START======================================================= + * org.onap.dmaap + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * + *******************************************************************************/ + +package org.onap.dmaap.mr.dmaapMMAgent; + +import java.io.BufferedReader; +import java.io.DataOutputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.apache.log4j.Logger; + +import com.google.gson.Gson; +import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; + +public class TopicUtil { + + static final Logger logger = Logger.getLogger(TopicUtil.class); + + public String publishTopic(String topicURL, String topicname, String mechid, String password, String message) { + try { + String requestURL = topicURL + "/events/" + topicname; + String authString = mechid + ":" + password; + String authStringEnc = Base64.encode(authString.getBytes()); + URL url = new URL(requestURL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setDoOutput(true); + connection.setRequestProperty("Authorization", "Basic " + authStringEnc); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Content-Length", Integer.toString(message.length())); + DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); + wr.write(message.getBytes()); + + InputStream content = (InputStream) connection.getInputStream(); + BufferedReader in = new BufferedReader(new InputStreamReader(content)); + String line; + String response = ""; + while ((line = in.readLine()) != null) { + response = response + line; + } + return response; + + } catch (Exception e) { + logger.error(" Exception Occered " + e); + return "ERROR:" + e.getLocalizedMessage(); + } + } + + public String subscribeTopic(String topicURL, String topicname, String timeout, String mechid, String password) { + String response = ""; + try { + String requestURL = topicURL + "/events/" + topicname + "/mirrormakeragent/1?timeout=" + timeout + + "&limit=1"; + String authString = mechid + ":" + password; + String authStringEnc = Base64.encode(authString.getBytes()); + URL url = new URL(requestURL); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setDoOutput(true); + connection.setRequestProperty("Authorization", "Basic " + authStringEnc); + connection.setRequestProperty("Content-Type", "application/json"); + InputStream content = (InputStream) connection.getInputStream(); + BufferedReader in = new BufferedReader(new InputStreamReader(content)); + String line; + + while ((line = in.readLine()) != null) { + response = response + line; + } + Gson g = new Gson(); + // get message as JSON String Array + String[] topicMessage = g.fromJson(response, String[].class); + if (topicMessage.length != 0) { + return topicMessage[0]; + } + } catch (Exception e) { + logger.error(" Exception Occered " + e); + return "ERROR:" + e.getMessage() + " Server Response is:" + response; + } + return null; + } + +} |