1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
23 package org.onap.dmaap.mr.dmaapMMAgent;
25 import com.google.gson.Gson;
26 import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
27 import java.io.BufferedReader;
28 import java.io.DataOutputStream;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.net.HttpURLConnection;
33 import org.apache.logging.log4j.LogManager;
34 import org.apache.logging.log4j.Logger;
36 public class TopicUtil {
38 private static final Logger logger = LogManager.getLogger(TopicUtil.class);
40 public String publishTopic(String topicURL, String topicname, String mechid, String password, String message) {
42 String requestURL = topicURL + "/events/" + topicname;
43 String authString = mechid + ":" + password;
44 String authStringEnc = Base64.encode(authString.getBytes());
45 URL url = new URL(requestURL);
46 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
47 connection.setRequestMethod("POST");
48 connection.setDoOutput(true);
49 connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
50 connection.setRequestProperty("Content-Type", "application/json");
51 connection.setRequestProperty("Content-Length", Integer.toString(message.length()));
52 DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
53 wr.write(message.getBytes());
55 InputStream content = (InputStream) connection.getInputStream();
56 BufferedReader in = new BufferedReader(new InputStreamReader(content));
59 while ((line = in.readLine()) != null) {
60 response = response + line;
64 } catch (Exception e) {
65 logger.error(" Exception Occered " + e);
66 return "ERROR:" + e.getLocalizedMessage();
70 public String subscribeTopic(String topicURL, String topicname, String timeout, String mechid, String password) {
73 String requestURL = topicURL + "/events/" + topicname + "/mirrormakeragent/1?timeout=" + timeout
75 String authString = mechid + ":" + password;
76 String authStringEnc = Base64.encode(authString.getBytes());
77 URL url = new URL(requestURL);
78 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
79 connection.setRequestMethod("GET");
80 connection.setDoOutput(true);
81 connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
82 connection.setRequestProperty("Content-Type", "application/json");
83 InputStream content = (InputStream) connection.getInputStream();
84 BufferedReader in = new BufferedReader(new InputStreamReader(content));
87 while ((line = in.readLine()) != null) {
88 response = response + line;
91 // get message as JSON String Array
92 String[] topicMessage = g.fromJson(response, String[].class);
93 if (topicMessage.length != 0) {
94 return topicMessage[0];
96 } catch (Exception e) {
97 logger.error(" Exception Occered " + e);
98 return "ERROR:" + e.getMessage() + " Server Response is:" + response;