[DMAAP MM] Update info.yaml
[dmaap/messagerouter/mirroragent.git] / src / main / java / org / onap / dmaap / mr / dmaapMMAgent / TopicUtil.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22
23 package org.onap.dmaap.mr.dmaapMMAgent;
24
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;
32 import java.net.URL;
33 import org.apache.logging.log4j.LogManager;
34 import org.apache.logging.log4j.Logger;
35
36 public class TopicUtil {
37
38         private static final Logger logger = LogManager.getLogger(TopicUtil.class);
39
40         public String publishTopic(String topicURL, String topicname, String mechid, String password, String message) {
41                 try {
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());
54
55                         InputStream content = (InputStream) connection.getInputStream();
56                         BufferedReader in = new BufferedReader(new InputStreamReader(content));
57                         String line;
58                         String response = "";
59                         while ((line = in.readLine()) != null) {
60                                 response = response + line;
61                         }
62                         return response;
63
64                 } catch (Exception e) {
65                         logger.error(" Exception Occered " + e);
66                         return "ERROR:" + e.getLocalizedMessage();
67                 }
68         }
69
70         public String subscribeTopic(String topicURL, String topicname, String timeout, String mechid, String password) {
71                 String response = "";
72                 try {
73                         String requestURL = topicURL + "/events/" + topicname + "/mirrormakeragent/1?timeout=" + timeout
74                                         + "&limit=1";
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));
85                         String line;
86
87                         while ((line = in.readLine()) != null) {
88                                 response = response + line;
89                         }
90                         Gson g = new Gson();
91                         // get message as JSON String Array
92                         String[] topicMessage = g.fromJson(response, String[].class);
93                         if (topicMessage.length != 0) {
94                                 return topicMessage[0];
95                         }
96                 } catch (Exception e) {
97                         logger.error(" Exception Occered " + e);
98                         return "ERROR:" + e.getMessage() + " Server Response is:" + response;
99                 }
100                 return null;
101         }
102
103 }