X-Git-Url: https://gerrit.onap.org/r/gitweb?p=dmaap%2Fmessagerouter%2Fmirroragent.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fcom%2Fatt%2Fnsa%2FdmaapMMAgent%2FTopicUtil.java;fp=src%2Fmain%2Fjava%2Fcom%2Fatt%2Fnsa%2FdmaapMMAgent%2FTopicUtil.java;h=f3b8552607e35a80105d5608de743e43e2cf4c6d;hp=0000000000000000000000000000000000000000;hb=79f026709bde8b26fd9b5f31535ee429497b951d;hpb=187e90c38e53cf155d13c7ed115350d14f98a112 diff --git a/src/main/java/com/att/nsa/dmaapMMAgent/TopicUtil.java b/src/main/java/com/att/nsa/dmaapMMAgent/TopicUtil.java new file mode 100644 index 0000000..f3b8552 --- /dev/null +++ b/src/main/java/com/att/nsa/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 com.att.nsa.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; + } + +}