- public String subscribeTopic(String timeout) {
- String response = "";
- try {
- String requestURL = this.topicURL + "/events/" + this.topicname + "/mirrormakeragent/1?timeout=" + timeout
- + "&limit=1";
- String authString = this.mechid + ":" + this.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;
- }
-
- public String publishTopic(String message) {
- try {
- String requestURL = this.topicURL + "/events/" + this.topicname;
- String authString = this.mechid + ":" + this.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();
- }
- }
-