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 java.io.BufferedReader;
26 import java.io.DataOutputStream;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.net.HttpURLConnection;
32 import org.apache.log4j.Logger;
34 import com.google.gson.Gson;
35 import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
37 public class TopicUtil {
39 static final Logger logger = Logger.getLogger(TopicUtil.class);
41 public String publishTopic(String topicURL, String topicname, String mechid, String password, String message) {
43 String requestURL = topicURL + "/events/" + topicname;
44 String authString = mechid + ":" + password;
45 String authStringEnc = Base64.encode(authString.getBytes());
46 URL url = new URL(requestURL);
47 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
48 connection.setRequestMethod("POST");
49 connection.setDoOutput(true);
50 connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
51 connection.setRequestProperty("Content-Type", "application/json");
52 connection.setRequestProperty("Content-Length", Integer.toString(message.length()));
53 DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
54 wr.write(message.getBytes());
56 InputStream content = (InputStream) connection.getInputStream();
57 BufferedReader in = new BufferedReader(new InputStreamReader(content));
60 while ((line = in.readLine()) != null) {
61 response = response + line;
65 } catch (Exception e) {
66 logger.error(" Exception Occered " + e);
67 return "ERROR:" + e.getLocalizedMessage();
71 public String subscribeTopic(String topicURL, String topicname, String timeout, String mechid, String password) {
74 String requestURL = topicURL + "/events/" + topicname + "/mirrormakeragent/1?timeout=" + timeout
76 String authString = mechid + ":" + password;
77 String authStringEnc = Base64.encode(authString.getBytes());
78 URL url = new URL(requestURL);
79 HttpURLConnection connection = (HttpURLConnection) url.openConnection();
80 connection.setRequestMethod("GET");
81 connection.setDoOutput(true);
82 connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
83 connection.setRequestProperty("Content-Type", "application/json");
84 InputStream content = (InputStream) connection.getInputStream();
85 BufferedReader in = new BufferedReader(new InputStreamReader(content));
88 while ((line = in.readLine()) != null) {
89 response = response + line;
92 // get message as JSON String Array
93 String[] topicMessage = g.fromJson(response, String[].class);
94 if (topicMessage.length != 0) {
95 return topicMessage[0];
97 } catch (Exception e) {
98 logger.error(" Exception Occered " + e);
99 return "ERROR:" + e.getMessage() + " Server Response is:" + response;