Merge changes from topics 'cghosh-4jan-sonarfix', 'cghosh-4jan-sonar-fix'
[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 java.io.BufferedReader;
26 import java.io.DataOutputStream;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.net.HttpURLConnection;
30 import java.net.URL;
31
32 import org.apache.log4j.Logger;
33
34 import com.google.gson.Gson;
35 import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
36
37 public class TopicUtil {
38
39         static final Logger logger = Logger.getLogger(TopicUtil.class);
40
41         public String publishTopic(String topicURL, String topicname, String mechid, String password, String message) {
42                 try {
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());
55
56                         InputStream content = (InputStream) connection.getInputStream();
57                         BufferedReader in = new BufferedReader(new InputStreamReader(content));
58                         String line;
59                         String response = "";
60                         while ((line = in.readLine()) != null) {
61                                 response = response + line;
62                         }
63                         return response;
64
65                 } catch (Exception e) {
66                         logger.error(" Exception Occered " + e);
67                         return "ERROR:" + e.getLocalizedMessage();
68                 }
69         }
70
71         public String subscribeTopic(String topicURL, String topicname, String timeout, String mechid, String password) {
72                 String response = "";
73                 try {
74                         String requestURL = topicURL + "/events/" + topicname + "/mirrormakeragent/1?timeout=" + timeout
75                                         + "&limit=1";
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));
86                         String line;
87
88                         while ((line = in.readLine()) != null) {
89                                 response = response + line;
90                         }
91                         Gson g = new Gson();
92                         // get message as JSON String Array
93                         String[] topicMessage = g.fromJson(response, String[].class);
94                         if (topicMessage.length != 0) {
95                                 return topicMessage[0];
96                         }
97                 } catch (Exception e) {
98                         logger.error(" Exception Occered " + e);
99                         return "ERROR:" + e.getMessage() + " Server Response is:" + response;
100                 }
101                 return null;
102         }
103
104 }