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 *******************************************************************************/
22 package org.onap.dmaap.service;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.Date;
28 import java.util.List;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.Context;
36 import org.json.JSONObject;
37 import org.apache.commons.io.IOUtils;
38 import org.apache.commons.lang.StringUtils;
39 import org.apache.http.HttpStatus;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Qualifier;
45 import org.springframework.stereotype.Component;
47 import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
48 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
49 import org.onap.dmaap.dmf.mr.utils.Utils;
50 import com.att.nsa.configs.ConfigDbException;
51 import org.onap.dmaap.mmagent.*;
52 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
53 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
54 import com.google.gson.Gson;
55 import com.google.gson.JsonSyntaxException;
57 import edu.emory.mathcs.backport.java.util.Arrays;
59 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
60 import org.onap.dmaap.dmf.mr.CambriaApiException;
61 import org.onap.dmaap.dmf.mr.backends.ConsumerFactory.UnavailableException;
63 import org.json.JSONArray;
64 import org.json.JSONException;
65 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
66 import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
67 import org.onap.dmaap.dmf.mr.exception.DMaaPErrorMessages;
68 import org.onap.dmaap.dmf.mr.exception.DMaaPResponseCode;
69 import org.onap.dmaap.dmf.mr.exception.ErrorResponse;
70 import org.onap.dmaap.dmf.mr.metabroker.Broker.TopicExistsException;
71 import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticator;
72 import org.onap.dmaap.dmf.mr.security.DMaaPAAFAuthenticatorImpl;
73 import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
74 import org.onap.dmaap.dmf.mr.service.MMService;
77 * Rest Service class for Mirror Maker proxy Rest Services
79 * @author <a href="mailto:"></a>
85 public class MMRestService {
87 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(MMRestService.class);
88 private static final String NO_ADMIN_PERMISSION = "No Mirror Maker Admin permission.";
89 private static final String NO_USER_PERMISSION = "No Mirror Maker User permission.";
90 private static final String NO_USER_CREATE_PERMISSION = "No Mirror Maker User Create permission.";
91 private static final String NAME_DOES_NOT_MEET_REQUIREMENT = "Mirror Maker name can only contain alpha numeric";
92 private static final String INVALID_IPPORT = "This is not a valid IP:Port";
93 private static final String MIRROR_MAKERADMIN = "msgRtr.mirrormakeradmin.aaf";
94 private static final String MIRROR_MAKERUSER = "msgRtr.mirrormakeruser.aaf";
95 private static final String UTF_8 = "UTF-8";
96 private static final String MESSAGE = "message";
97 private static final String LISTMIRRORMAKER = "listMirrorMaker";
98 private static final String ERROR = "error";
99 private static final String NAMESPACE = "namespace";
101 private String topic;
103 private String consumergroup;
104 private String consumerid;
107 @Qualifier("configurationReader")
108 private ConfigurationReader configReader;
111 private HttpServletRequest request;
114 private HttpServletResponse response;
117 private MMService mirrorService;
120 private DMaaPErrorMessages errorMessages;
122 private ErrorResponse errResJson = new ErrorResponse(HttpStatus.SC_BAD_REQUEST,
123 DMaaPResponseCode.INCORRECT_JSON.getResponseCode(), "", null, Utils.getFormattedDate(new Date()), topic,
124 null, null, "mirrorMakerAgent", null);
126 private DMaaPAAFAuthenticator dmaapAAFauthenticator = new DMaaPAAFAuthenticatorImpl();
129 * This method is used for taking Configuration Object,HttpServletRequest
130 * Object,HttpServletRequest HttpServletResponse Object,HttpServletSession
133 * @return DMaaPContext object from where user can get Configuration
134 * Object,HttpServlet Object
137 private DMaaPContext getDmaapContext() {
138 DMaaPContext dmaapContext = new DMaaPContext();
139 dmaapContext.setRequest(request);
140 dmaapContext.setResponse(response);
141 dmaapContext.setConfigReader(configReader);
142 dmaapContext.setConsumerRequestTime(Utils.getFormattedDate(new Date()));
148 @Produces("application/json")
150 public void callCreateMirrorMaker(InputStream msg) throws Exception {
152 DMaaPContext ctx = getDmaapContext();
153 if (checkMirrorMakerPermission(ctx,
154 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))) {
158 String randomStr = getRandomNum();
160 InputStream inStream = null;
161 Gson gson = new Gson();
162 CreateMirrorMaker createMirrorMaker = new CreateMirrorMaker();
163 LOGGER.info("Starting Create MirrorMaker");
165 input = IOUtils.toString(msg, "UTF-8");
167 if (input != null && input.length() > 0) {
168 input = removeExtraChar(input);
171 // Check if the request has CreateMirrorMaker
173 createMirrorMaker = gson.fromJson(input, CreateMirrorMaker.class);
175 } catch (JsonSyntaxException ex) {
177 errResJson.setErrorMessage(errorMessages.getIncorrectJson() + ex.getMessage());
178 LOGGER.info(errResJson.toString());
179 throw new CambriaApiException(errResJson);
182 // send error message if it is not a CreateMirrorMaker request.
183 if (createMirrorMaker.getCreateMirrorMaker() == null) {
185 errResJson.setErrorMessage("This is not a CreateMirrorMaker request. Please try again.");
186 LOGGER.info(errResJson.toString());
187 throw new CambriaApiException(errResJson);
189 createMirrorMaker.validateJSON();
192 String name = createMirrorMaker.getCreateMirrorMaker().getName();
194 // if empty, blank name is entered
195 if (StringUtils.isBlank(name)) {
197 errResJson.setErrorMessage("Name can not be empty or blank.");
198 LOGGER.info(errResJson.toString());
199 throw new CambriaApiException(errResJson);
202 // Check if the name contains only Alpha Numeric
203 else if (!isAlphaNumeric(name)) {
205 errResJson.setErrorMessage(NAME_DOES_NOT_MEET_REQUIREMENT);
206 LOGGER.info(errResJson.toString());
207 throw new CambriaApiException(errResJson);
211 if (null == createMirrorMaker.getMessageID() || createMirrorMaker.getMessageID().isEmpty()) {
212 createMirrorMaker.setMessageID(randomStr);
214 inStream = IOUtils.toInputStream(gson.toJson(createMirrorMaker), "UTF-8");
215 JSONObject existMirrorMaker = validateMMExists(ctx, name);
216 if (!(boolean) existMirrorMaker.get("exists")) {
217 JSONObject finalJsonObj = callPubSub(createMirrorMaker.getMessageID(), ctx, inStream, name,
219 DMaaPResponseBuilder.respondOk(ctx, finalJsonObj);
222 errResJson.setErrorMessage("MirrorMaker " + name + " already exists");
223 LOGGER.info(errResJson.toString());
224 throw new CambriaApiException(errResJson);
229 } catch (IOException e) {
234 // Send error response if user does not provide Authorization
236 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
237 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(), NO_ADMIN_PERMISSION, null,
238 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
239 ctx.getRequest().getRemoteHost());
240 LOGGER.info(errRes.toString());
241 throw new CambriaApiException(errRes);
247 @Produces("application/json")
249 public void callListAllMirrorMaker(InputStream msg) throws Exception {
251 DMaaPContext ctx = getDmaapContext();
253 if (checkMirrorMakerPermission(ctx,
254 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))) {
259 Gson gson = new Gson();
262 input = IOUtils.toString(msg, "UTF-8");
264 if (input != null && input.length() > 0) {
265 input = removeExtraChar(input);
268 String randomStr = getRandomNum();
269 JSONObject jsonOb = null;
272 jsonOb = new JSONObject(input);
274 } catch (JSONException ex) {
275 errResJson.setErrorMessage(errorMessages.getIncorrectJson());
276 LOGGER.info(errResJson.toString());
277 throw new CambriaApiException(errResJson);
280 // Check if request has listAllMirrorMaker and
281 // listAllMirrorMaker is empty
282 if (jsonOb.has("listAllMirrorMaker") && jsonOb.getJSONObject("listAllMirrorMaker").length() == 0) {
284 if (!jsonOb.has("messageID") || jsonOb.isNull("messageID")) {
285 jsonOb.put("messageID", randomStr);
288 InputStream inStream = null;
289 MirrorMaker mirrormaker = gson.fromJson(input, MirrorMaker.class);
292 inStream = IOUtils.toInputStream(jsonOb.toString(), "UTF-8");
294 } catch (IOException ioe) {
298 JSONObject responseJson = callPubSub(jsonOb.getString("messageID"), ctx, inStream, mirrormaker.name,
300 DMaaPResponseBuilder.respondOk(ctx, responseJson);
304 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
305 DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode(),
306 "This is not a ListAllMirrorMaker request. Please try again.", null,
307 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
308 ctx.getRequest().getRemoteHost());
309 LOGGER.info(errRes.toString());
310 throw new CambriaApiException(errRes);
313 } catch (IOException ioe) {
320 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
321 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(), NO_ADMIN_PERMISSION, null,
322 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
323 ctx.getRequest().getRemoteHost());
324 LOGGER.info(errRes.toString());
325 throw new CambriaApiException(errRes);
330 @Produces("application/json")
332 public void callUpdateMirrorMaker(InputStream msg) throws Exception {
333 DMaaPContext ctx = getDmaapContext();
334 if (checkMirrorMakerPermission(ctx,
335 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))) {
339 String randomStr = getRandomNum();
341 InputStream inStream = null;
342 Gson gson = new Gson();
343 UpdateMirrorMaker updateMirrorMaker = new UpdateMirrorMaker();
344 JSONObject jsonOb, jsonObInput = null;
347 input = IOUtils.toString(msg, "UTF-8");
349 if (input != null && input.length() > 0) {
350 input = removeExtraChar(input);
353 // Check if the request has UpdateMirrorMaker
355 updateMirrorMaker = gson.fromJson(input, UpdateMirrorMaker.class);
356 jsonOb = new JSONObject(input);
357 jsonObInput = (JSONObject) jsonOb.get("updateMirrorMaker");
359 } catch (JsonSyntaxException ex) {
361 errResJson.setErrorMessage(errorMessages.getIncorrectJson() + ex.getMessage());
362 LOGGER.info(errResJson.toString());
363 throw new CambriaApiException(errResJson);
366 // send error message if it is not a UpdateMirrorMaker request.
367 if (updateMirrorMaker.getUpdateMirrorMaker() == null) {
369 errResJson.setErrorMessage("This is not a UpdateMirrorMaker request. Please try again.");
370 LOGGER.info(errResJson.toString());
371 throw new CambriaApiException(errResJson);
373 updateMirrorMaker.validateJSON(jsonObInput);
376 String name = updateMirrorMaker.getUpdateMirrorMaker().getName();
377 // if empty, blank name is entered
378 if (StringUtils.isBlank(name)) {
380 errResJson.setErrorMessage("Name can not be empty or blank.");
381 LOGGER.info(errResJson.toString());
382 throw new CambriaApiException(errResJson);
385 // Check if the name contains only Alpha Numeric
386 else if (!isAlphaNumeric(name)) {
387 errResJson.setErrorMessage(NAME_DOES_NOT_MEET_REQUIREMENT);
388 LOGGER.info(errResJson.toString());
389 throw new CambriaApiException(errResJson);
393 // Set a random number as messageID, convert Json Object to
394 // InputStream and finally call publisher and subscriber
397 if (null == updateMirrorMaker.getMessageID() || updateMirrorMaker.getMessageID().isEmpty()) {
398 updateMirrorMaker.setMessageID(randomStr);
401 JSONObject existMirrorMaker = validateMMExists(ctx, name);
403 if ((boolean) existMirrorMaker.get("exists")) {
404 JSONObject existMM = (JSONObject) existMirrorMaker.get("listMirrorMaker");
406 if (!jsonObInput.has("numStreams")) {
407 updateMirrorMaker.getUpdateMirrorMaker().setNumStreams(existMM.getInt("numStreams"));
409 if (!jsonObInput.has("enablelogCheck")) {
410 updateMirrorMaker.getUpdateMirrorMaker()
411 .setEnablelogCheck(existMM.getBoolean("enablelogCheck"));
413 inStream = IOUtils.toInputStream(gson.toJson(updateMirrorMaker), "UTF-8");
414 JSONObject finalJsonObj = callPubSub(updateMirrorMaker.getMessageID(), ctx, inStream, name,
416 DMaaPResponseBuilder.respondOk(ctx, finalJsonObj);
419 errResJson.setErrorMessage("MirrorMaker " + name + " does not exist");
420 LOGGER.info(errResJson.toString());
421 throw new CambriaApiException(errResJson);
427 } catch (IOException e) {
428 LOGGER.error("Error in callUpdateMirrorMaker:", e);
431 // Send error response if user does not provide Authorization
433 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
434 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(), NO_ADMIN_PERMISSION, null,
435 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
436 ctx.getRequest().getRemoteHost());
437 LOGGER.info(errRes.toString());
438 throw new CambriaApiException(errRes);
443 @Produces("application/json")
445 public void callDeleteMirrorMaker(InputStream msg) throws JSONException, Exception {
446 DMaaPContext ctx = getDmaapContext();
448 if (checkMirrorMakerPermission(ctx,
449 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeradmin.aaf"))) {
454 Gson gson = new Gson();
455 MirrorMaker mirrormaker = new MirrorMaker();
458 input = IOUtils.toString(msg, "UTF-8");
460 if (input != null && input.length() > 0) {
461 input = removeExtraChar(input);
464 String randomStr = getRandomNum();
465 JSONObject jsonOb = null;
468 jsonOb = new JSONObject(input);
469 mirrormaker = gson.fromJson(input, MirrorMaker.class);
471 } catch (JSONException ex) {
473 errResJson.setErrorMessage(errorMessages.getIncorrectJson());
474 LOGGER.info(errResJson.toString());
475 throw new CambriaApiException(errResJson);
478 // Check if request has DeleteMirrorMaker and
479 // DeleteMirrorMaker has MirrorMaker object with name variable
480 // and check if the name contain only alpha numeric
482 if (jsonOb.has("deleteMirrorMaker") && jsonOb.getJSONObject("deleteMirrorMaker").length() == 1
483 && jsonOb.getJSONObject("deleteMirrorMaker").has("name")
484 && !StringUtils.isBlank(jsonOb.getJSONObject("deleteMirrorMaker").getString("name"))
485 && isAlphaNumeric(jsonOb.getJSONObject("deleteMirrorMaker").getString("name"))) {
487 if (!jsonOb.has("messageID") || jsonOb.isNull("messageID")) {
488 jsonOb.put("messageID", randomStr);
491 InputStream inStream = null;
494 inStream = IOUtils.toInputStream(jsonOb.toString(), "UTF-8");
496 } catch (IOException ioe) {
497 LOGGER.error("Error while converting string to an input stream:", ioe);
499 JSONObject deleteMM = jsonOb.getJSONObject("deleteMirrorMaker");
501 JSONObject existMirrorMaker = validateMMExists(ctx, deleteMM.getString("name"));
503 if ((boolean) existMirrorMaker.get("exists")) {
505 JSONObject finalJsonObj = callPubSub(jsonOb.getString("messageID"), ctx, inStream,
506 mirrormaker.name, false);
507 DMaaPResponseBuilder.respondOk(ctx, finalJsonObj);
510 errResJson.setErrorMessage("MirrorMaker " + deleteMM.getString("name") + " does not exist");
511 LOGGER.info(errResJson.toString());
512 throw new CambriaApiException(errResJson);
518 errResJson.setErrorMessage("This is not a DeleteMirrorMaker request. Please try again.");
519 LOGGER.info(errResJson.toString());
520 throw new CambriaApiException(errResJson);
524 } catch (IOException ioe) {
525 LOGGER.error("Error in callDeleteMirrorMaker:", ioe);
531 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
532 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(), NO_ADMIN_PERMISSION, null,
533 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
534 ctx.getRequest().getRemoteHost());
535 LOGGER.info(errRes.toString());
536 throw new CambriaApiException(errRes);
540 public boolean isListMirrorMaker(String msg, String messageID) {
541 String topicmsg = msg;
542 topicmsg = removeExtraChar(topicmsg);
543 JSONObject jObj = new JSONObject();
544 JSONArray jArray = null;
545 boolean exist = false;
547 if (!StringUtils.isBlank(topicmsg) && topicmsg.length() > 2) {
548 jArray = new JSONArray(topicmsg);
550 for (int i = 0; i < jArray.length(); i++) {
551 jObj = jArray.getJSONObject(i);
553 if (jObj.has("messageID") && jObj.get("messageID").equals(messageID) && jObj.has("listMirrorMaker")) {
562 private void loadProperty() {
564 this.timeout = Integer.parseInt(
565 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout").trim());
566 this.topic = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic").trim();
567 this.consumergroup = AJSCPropertiesMap
568 .getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup").trim();
569 this.consumerid = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid")
573 private String removeExtraChar(String message) {
574 String str = message;
575 str = checkJsonFormate(str);
577 if (str != null && str.length() > 0) {
578 str = str.replace("\\", "");
579 str = str.replace("\"{", "{");
580 str = str.replace("}\"", "}");
585 private String getRandomNum() {
586 long random = Math.round(Math.random() * 89999) + 10000;
587 String strLong = Long.toString(random);
591 private boolean isAlphaNumeric(String name) {
592 String pattern = "^[a-zA-Z0-9]*$";
593 if (name.matches(pattern)) {
599 private String checkJsonFormate(String jsonStr) {
601 String json = jsonStr;
602 if (jsonStr != null && jsonStr.length() > 0 && jsonStr.startsWith("[") && !jsonStr.endsWith("]")) {
608 private boolean checkMirrorMakerPermission(DMaaPContext ctx, String permission) {
610 boolean hasPermission = false;
612 if (dmaapAAFauthenticator.aafAuthentication(ctx.getRequest(), permission)) {
613 hasPermission = true;
615 return hasPermission;
618 public JSONObject callPubSub(String randomstr, DMaaPContext ctx, InputStream inStream, String name, boolean listAll)
621 JSONObject jsonObj = new JSONObject();
622 JSONObject finalJsonObj = new JSONObject();
623 JSONArray jsonArray = null;
625 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
626 mirrorService.pushEvents(ctx, topic, inStream, null, null);
627 long startTime = System.currentTimeMillis();
629 while (!isListMirrorMaker(msgFrmSubscribe, randomstr)
630 && ((System.currentTimeMillis() - startTime) < timeout)) {
631 msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
635 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
636 && isListMirrorMaker(msgFrmSubscribe, randomstr)) {
637 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
639 jsonArray = new JSONArray(msgFrmSubscribe);
640 jsonObj = jsonArray.getJSONObject(0);
641 if (jsonObj.has("listMirrorMaker")) {
642 jsonArray = (JSONArray) jsonObj.get("listMirrorMaker");
643 if (true == listAll) {
646 for (int i = 0; i < jsonArray.length(); i++) {
647 jsonObj = jsonArray.getJSONObject(i);
648 if (null != name && !name.isEmpty()) {
649 if (jsonObj.getString("name").equals(name)) {
650 finalJsonObj.put("listMirrorMaker", jsonObj);
654 finalJsonObj.put("listMirrorMaker", jsonObj);
664 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
665 DMaaPResponseCode.RESOURCE_NOT_FOUND.getResponseCode(),
666 "listMirrorMaker is not available, please make sure MirrorMakerAgent is running", null,
667 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
668 ctx.getRequest().getRemoteHost());
669 LOGGER.info(errRes.toString());
670 throw new CambriaApiException(errRes);
674 } catch (Exception e) {
675 LOGGER.error("Error in callPubSub", e);
681 private void sendErrResponse(DMaaPContext ctx, String errMsg) {
682 JSONObject err = new JSONObject();
683 err.append(ERROR, errMsg);
686 DMaaPResponseBuilder.respondOk(ctx, err);
687 LOGGER.error(errMsg);
689 } catch (JSONException | IOException e) {
690 LOGGER.error("Error at sendErrResponse method:" + errMsg + "Exception name:" + e);
694 @SuppressWarnings("unchecked")
696 @Produces("application/json")
697 @Path("/listallwhitelist")
698 public void listWhiteList(InputStream msg) throws Exception {
700 DMaaPContext ctx = getDmaapContext();
701 if (checkMirrorMakerPermission(ctx,
702 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))) {
708 input = IOUtils.toString(msg, "UTF-8");
710 if (input != null && input.length() > 0) {
711 input = removeExtraChar(input);
714 // Check if it is correct Json object
715 JSONObject jsonOb = null;
718 jsonOb = new JSONObject(input);
720 } catch (JSONException ex) {
722 errResJson.setErrorMessage(errorMessages.getIncorrectJson());
723 LOGGER.info(errResJson.toString());
724 throw new CambriaApiException(errResJson);
728 // Check if the request has name and name contains only alpha
730 // and check if the request has namespace and namespace contains
731 // only alpha numeric
732 if (jsonOb.length() == 2 && jsonOb.has("name") && !StringUtils.isBlank(jsonOb.getString("name"))
733 && isAlphaNumeric(jsonOb.getString("name")) && jsonOb.has("namespace")
734 && !StringUtils.isBlank(jsonOb.getString("namespace"))) {
736 String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
737 "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString("namespace") + "|create";
739 // Check if the user have create permission for the
741 if (checkMirrorMakerPermission(ctx, permission)) {
743 JSONObject listAll = new JSONObject();
744 JSONObject emptyObject = new JSONObject();
746 // Create a listAllMirrorMaker Json object
748 listAll.put("listAllMirrorMaker", emptyObject);
750 } catch (JSONException e) {
752 errResJson.setErrorMessage(errorMessages.getIncorrectJson());
753 LOGGER.info(errResJson.toString());
754 throw new CambriaApiException(errResJson);
757 // set a random number as messageID
758 String randomStr = getRandomNum();
759 listAll.put("messageID", randomStr);
760 InputStream inStream = null;
762 // convert listAll Json object to InputStream object
764 inStream = IOUtils.toInputStream(listAll.toString(), "UTF-8");
766 } catch (IOException ioe) {
767 LOGGER.error("Error while converting string to an input stream:", ioe);
769 JSONObject listMirrorMaker = new JSONObject();
770 listMirrorMaker = callPubSub(randomStr, ctx, inStream, null, true);
772 String whitelist = null;
773 JSONArray listMMArray = new JSONArray();
774 if (listMirrorMaker.has("listMirrorMaker")) {
775 listMMArray = (JSONArray) listMirrorMaker.get("listMirrorMaker");
776 for (int i = 0; i < listMMArray.length(); i++) {
778 JSONObject mm = new JSONObject();
779 mm = listMMArray.getJSONObject(i);
780 String name = mm.getString("name");
782 if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
783 whitelist = mm.getString("whitelist");
788 if (!StringUtils.isBlank(whitelist)) {
790 List<String> topicList = new ArrayList<String>();
791 List<String> finalTopicList = new ArrayList<String>();
792 topicList = Arrays.asList(whitelist.split(","));
794 for (String topic : topicList) {
795 if (topic != null && !topic.equals("null")
796 && getNamespace(topic).equals(jsonOb.getString("namespace"))) {
798 finalTopicList.add(topic);
802 String topicNames = "";
804 if (finalTopicList.size() > 0) {
805 topicNames = StringUtils.join(finalTopicList, ",");
808 JSONObject listAllWhiteList = new JSONObject();
809 listAllWhiteList.put("name", jsonOb.getString("name"));
810 listAllWhiteList.put("whitelist", topicNames);
812 DMaaPResponseBuilder.respondOk(ctx, listAllWhiteList);
817 errResJson.setErrorMessage(
818 "listWhiteList is not available, please make sure MirrorMakerAgent is running");
819 LOGGER.info(errResJson.toString());
820 throw new CambriaApiException(errResJson);
825 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
826 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(), NO_USER_CREATE_PERMISSION,
827 null, Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
828 ctx.getRequest().getRemoteHost());
829 LOGGER.info(errRes.toString());
830 throw new CambriaApiException(errRes);
835 errResJson.setErrorMessage("This is not a ListAllWhitelist request. Please try again.");
836 LOGGER.info(errResJson.toString());
837 throw new CambriaApiException(errResJson);
840 } catch (IOException e) {
841 LOGGER.error("Error in listWhiteList", e);
844 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
845 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(), NO_USER_PERMISSION, null,
846 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
847 ctx.getRequest().getRemoteHost());
848 LOGGER.info(errRes.toString());
849 throw new CambriaApiException(errRes);
853 @SuppressWarnings("unchecked")
855 @Produces("application/json")
856 @Path("/createwhitelist")
857 public void createWhiteList(InputStream msg) throws Exception {
859 DMaaPContext ctx = getDmaapContext();
860 if (checkMirrorMakerPermission(ctx,
861 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))) {
867 input = IOUtils.toString(msg, "UTF-8");
869 if (input != null && input.length() > 0) {
870 input = removeExtraChar(input);
873 // Check if it is correct Json object
874 JSONObject jsonOb = null;
877 jsonOb = new JSONObject(input);
879 } catch (JSONException ex) {
880 errResJson.setErrorMessage(errorMessages.getIncorrectJson());
881 LOGGER.info(errResJson.toString());
882 throw new CambriaApiException(errResJson);
885 // Check if the request has name and name contains only alpha
887 // check if the request has namespace and
888 // check if the request has whitelistTopicName
889 // check if the topic name contains only alpha numeric
890 if (jsonOb.length() == 3 && jsonOb.has("name") && !StringUtils.isBlank(jsonOb.getString("name"))
891 && isAlphaNumeric(jsonOb.getString("name")) && jsonOb.has("namespace")
892 && !StringUtils.isBlank(jsonOb.getString("namespace")) && jsonOb.has("whitelistTopicName")
893 && !StringUtils.isBlank(jsonOb.getString("whitelistTopicName"))
894 && isAlphaNumeric(jsonOb.getString("whitelistTopicName").substring(
895 jsonOb.getString("whitelistTopicName").lastIndexOf(".") + 1, jsonOb
896 .getString("whitelistTopicName").length()))) {
898 String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
899 "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString("namespace") + "|create";
901 // Check if the user have create permission for the
903 if (checkMirrorMakerPermission(ctx, permission)) {
905 JSONObject listAll = new JSONObject();
906 JSONObject emptyObject = new JSONObject();
908 // Create a listAllMirrorMaker Json object
910 listAll.put("listAllMirrorMaker", emptyObject);
912 } catch (JSONException e) {
914 errResJson.setErrorMessage(errorMessages.getIncorrectJson());
915 LOGGER.info(errResJson.toString());
916 throw new CambriaApiException(errResJson);
919 // set a random number as messageID
920 String randomStr = getRandomNum();
921 listAll.put("messageID", randomStr);
922 InputStream inStream = null;
924 // convert listAll Json object to InputStream object
926 inStream = IOUtils.toInputStream(listAll.toString(), "UTF-8");
928 } catch (IOException ioe) {
929 LOGGER.error("Error while converting string to an input stream:", ioe);
931 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
932 // call listAllMirrorMaker
933 mirrorService.pushEvents(ctx, topic, inStream, null, null);
935 // subscribe for listMirrorMaker
936 long startTime = System.currentTimeMillis();
938 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
939 && (System.currentTimeMillis() - startTime) < timeout) {
940 msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
943 JSONArray listMirrorMaker = null;
945 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
946 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
948 listMirrorMaker = getListMirrorMaker(msgFrmSubscribe, randomStr);
949 String whitelist = null;
951 for (int i = 0; i < listMirrorMaker.length(); i++) {
952 JSONObject mm = new JSONObject();
953 mm = listMirrorMaker.getJSONObject(i);
954 String name = mm.getString("name");
956 if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
957 whitelist = mm.getString("whitelist");
962 List<String> topicList = new ArrayList<String>();
963 List<String> finalTopicList = new ArrayList<String>();
965 if (whitelist != null) {
966 topicList = Arrays.asList(whitelist.split(","));
969 for (String st : topicList) {
970 if (!StringUtils.isBlank(st)) {
971 finalTopicList.add(st);
975 String newTopic = jsonOb.getString("whitelistTopicName");
977 if (!topicList.contains(newTopic)
978 && getNamespace(newTopic).equals(jsonOb.getString("namespace"))) {
980 UpdateWhiteList updateWhiteList = new UpdateWhiteList();
981 MirrorMaker mirrorMaker = new MirrorMaker();
982 mirrorMaker.setName(jsonOb.getString("name"));
983 finalTopicList.add(newTopic);
984 String newWhitelist = "";
986 if (finalTopicList.size() > 0) {
987 newWhitelist = StringUtils.join(finalTopicList, ",");
990 mirrorMaker.setWhitelist(newWhitelist);
992 String newRandom = getRandomNum();
993 updateWhiteList.setMessageID(newRandom);
994 updateWhiteList.setUpdateWhiteList(mirrorMaker);
997 g.toJson(updateWhiteList);
998 InputStream inputStream = null;
999 inputStream = IOUtils.toInputStream(g.toJson(updateWhiteList), "UTF-8");
1000 // callPubSub(newRandom, ctx, inputStream);
1001 callPubSubForWhitelist(newRandom, ctx, inputStream, jsonOb);
1003 } else if (topicList.contains(newTopic)) {
1004 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
1005 DMaaPResponseCode.INCORRECT_JSON.getResponseCode(), "The topic already exist.",
1006 null, Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1007 ctx.getRequest().getRemoteHost());
1008 LOGGER.info(errRes.toString());
1009 throw new CambriaApiException(errRes);
1011 } else if (!getNamespace(newTopic).equals(jsonOb.getString("namespace"))) {
1012 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR,
1013 DMaaPResponseCode.INCORRECT_JSON.getResponseCode(),
1014 "The namespace of the topic does not match with the namespace you provided.",
1015 null, Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1016 ctx.getRequest().getRemoteHost());
1017 LOGGER.info(errRes.toString());
1018 throw new CambriaApiException(errRes);
1022 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
1023 DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode(),
1024 "listWhiteList is not available, please make sure MirrorMakerAgent is running",
1025 null, Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1026 ctx.getRequest().getRemoteHost());
1027 LOGGER.info(errRes.toString());
1028 throw new CambriaApiException(errRes);
1032 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
1033 DMaaPResponseCode.UNABLE_TO_AUTHORIZE.getResponseCode(), NO_USER_CREATE_PERMISSION,
1034 null, Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1035 ctx.getRequest().getRemoteHost());
1036 LOGGER.info(errRes.toString());
1037 throw new CambriaApiException(errRes);
1042 errResJson.setErrorMessage("This is not a createWhitelist request. Please try again.");
1043 LOGGER.info(errResJson.toString());
1044 throw new CambriaApiException(errResJson);
1047 } catch (IOException | CambriaApiException | ConfigDbException | AccessDeniedException
1048 | TopicExistsException | missingReqdSetting | UnavailableException e) {
1053 // Send error response if user does not provide Authorization
1055 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
1056 DMaaPResponseCode.UNABLE_TO_AUTHORIZE.getResponseCode(), NO_USER_PERMISSION, null,
1057 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1058 ctx.getRequest().getRemoteHost());
1059 LOGGER.info(errRes.toString());
1060 throw new CambriaApiException(errRes);
1065 @SuppressWarnings("unchecked")
1067 @Produces("application/json")
1068 @Path("/deletewhitelist")
1069 public void deleteWhiteList(InputStream msg) throws Exception {
1071 DMaaPContext ctx = getDmaapContext();
1072 if (checkMirrorMakerPermission(ctx,
1073 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormakeruser.aaf"))) {
1076 String input = null;
1079 input = IOUtils.toString(msg, "UTF-8");
1081 if (input != null && input.length() > 0) {
1082 input = removeExtraChar(input);
1085 // Check if it is correct Json object
1086 JSONObject jsonOb = null;
1089 jsonOb = new JSONObject(input);
1091 } catch (JSONException ex) {
1093 errResJson.setErrorMessage(errorMessages.getIncorrectJson());
1094 LOGGER.info(errResJson.toString());
1095 throw new CambriaApiException(errResJson);
1099 // Check if the request has name and name contains only alpha
1101 // check if the request has namespace and
1102 // check if the request has whitelistTopicName
1103 if (jsonOb.length() == 3 && jsonOb.has("name") && isAlphaNumeric(jsonOb.getString("name"))
1104 && jsonOb.has("namespace") && jsonOb.has("whitelistTopicName")
1105 && isAlphaNumeric(jsonOb.getString("whitelistTopicName").substring(
1106 jsonOb.getString("whitelistTopicName").lastIndexOf(".") + 1,
1107 jsonOb.getString("whitelistTopicName").length()))) {
1109 String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
1110 "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString("namespace") + "|create";
1112 // Check if the user have create permission for the
1114 if (checkMirrorMakerPermission(ctx, permission)) {
1116 JSONObject listAll = new JSONObject();
1117 JSONObject emptyObject = new JSONObject();
1119 // Create a listAllMirrorMaker Json object
1121 listAll.put("listAllMirrorMaker", emptyObject);
1123 } catch (JSONException e) {
1125 errResJson.setErrorMessage(errorMessages.getIncorrectJson());
1126 LOGGER.info(errResJson.toString());
1127 throw new CambriaApiException(errResJson);
1130 // set a random number as messageID
1131 String randomStr = getRandomNum();
1132 listAll.put("messageID", randomStr);
1133 InputStream inStream = null;
1135 // convert listAll Json object to InputStream object
1137 inStream = IOUtils.toInputStream(listAll.toString(), "UTF-8");
1139 } catch (IOException ioe) {
1140 LOGGER.error("Error while converting string to an input stream:", ioe);
1142 // call listAllMirrorMaker
1143 mirrorService.pushEvents(ctx, topic, inStream, null, null);
1145 // subscribe for listMirrorMaker
1146 long startTime = System.currentTimeMillis();
1147 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1149 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
1150 && (System.currentTimeMillis() - startTime) < timeout) {
1151 msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1154 JSONObject jsonObj = new JSONObject();
1155 JSONArray jsonArray = null;
1156 JSONArray listMirrorMaker = null;
1158 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
1159 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
1160 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1161 jsonArray = new JSONArray(msgFrmSubscribe);
1163 for (int i = 0; i < jsonArray.length(); i++) {
1164 jsonObj = jsonArray.getJSONObject(i);
1166 if (jsonObj.has("messageID") && jsonObj.get("messageID").equals(randomStr)
1167 && jsonObj.has("listMirrorMaker")) {
1168 listMirrorMaker = jsonObj.getJSONArray("listMirrorMaker");
1172 String whitelist = null;
1173 for (int i = 0; i < listMirrorMaker.length(); i++) {
1175 JSONObject mm = new JSONObject();
1176 mm = listMirrorMaker.getJSONObject(i);
1177 String name = mm.getString("name");
1179 if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
1180 whitelist = mm.getString("whitelist");
1185 List<String> topicList = new ArrayList<String>();
1187 if (whitelist != null) {
1188 topicList = Arrays.asList(whitelist.split(","));
1190 boolean removeTopic = false;
1191 String topicToRemove = jsonOb.getString("whitelistTopicName");
1193 if (topicList.contains(topicToRemove)) {
1196 errResJson.setErrorMessage(errorMessages.getTopicNotExist());
1197 LOGGER.info(errResJson.toString());
1198 throw new CambriaApiException(errResJson);
1202 UpdateWhiteList updateWhiteList = new UpdateWhiteList();
1203 MirrorMaker mirrorMaker = new MirrorMaker();
1205 mirrorMaker.setName(jsonOb.getString("name"));
1207 if (StringUtils.isNotBlank((removeTopic(whitelist, topicToRemove)))) {
1208 mirrorMaker.setWhitelist(removeTopic(whitelist, topicToRemove));
1211 String newRandom = getRandomNum();
1213 updateWhiteList.setMessageID(newRandom);
1214 updateWhiteList.setUpdateWhiteList(mirrorMaker);
1216 Gson g = new Gson();
1217 g.toJson(updateWhiteList);
1219 InputStream inputStream = null;
1220 inputStream = IOUtils.toInputStream(g.toJson(updateWhiteList), "UTF-8");
1221 callPubSubForWhitelist(newRandom, ctx, inputStream, jsonOb);
1222 // mmAgentUtil.getNamespace(topicToRemove));
1227 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
1228 DMaaPResponseCode.SERVER_UNAVAILABLE.getResponseCode(),
1229 "listWhiteList is not available, please make sure MirrorMakerAgent is running",
1230 null, Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1231 ctx.getRequest().getRemoteHost());
1232 LOGGER.info(errRes.toString());
1233 throw new CambriaApiException(errRes);
1237 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
1238 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(), NO_USER_CREATE_PERMISSION,
1239 null, Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1240 ctx.getRequest().getRemoteHost());
1241 LOGGER.info(errRes.toString());
1242 throw new CambriaApiException(errRes);
1247 errResJson.setErrorMessage("This is not a DeleteAllWhitelist request. Please try again.");
1248 LOGGER.info(errResJson.toString());
1249 throw new CambriaApiException(errResJson);
1253 } catch (IOException | CambriaApiException | ConfigDbException | AccessDeniedException
1254 | TopicExistsException | missingReqdSetting | UnavailableException e) {
1259 // Send error response if user does not provide Authorization
1261 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_UNAUTHORIZED,
1262 DMaaPResponseCode.ACCESS_NOT_PERMITTED.getResponseCode(), NO_USER_PERMISSION, null,
1263 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1264 ctx.getRequest().getRemoteHost());
1265 LOGGER.info(errRes.toString());
1266 throw new CambriaApiException(errRes);
1271 private String getNamespace(String topic) {
1272 return topic.substring(0, topic.lastIndexOf("."));
1275 private String removeTopic(String whitelist, String topicToRemove) {
1276 List<String> topicList = new ArrayList<>();
1277 List<String> newTopicList = new ArrayList<>();
1279 if (whitelist.contains(",")) {
1280 topicList = Arrays.asList(whitelist.split(","));
1284 if (topicList.contains(topicToRemove)) {
1285 for (String topic : topicList) {
1286 if (!topic.equals(topicToRemove)) {
1287 newTopicList.add(topic);
1292 String newWhitelist = StringUtils.join(newTopicList, ",");
1294 return newWhitelist;
1297 public void callPubSubForWhitelist(String randomStr, DMaaPContext ctx, InputStream inStream, JSONObject jsonOb) {
1301 String namespace = jsonOb.getString("namespace");
1302 String mmName = jsonOb.getString("name");
1304 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1305 mirrorService.pushEvents(ctx, topic, inStream, null, null);
1306 long startTime = System.currentTimeMillis();
1308 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
1309 && (System.currentTimeMillis() - startTime) < timeout) {
1310 msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1313 JSONObject jsonObj = new JSONObject();
1314 JSONArray jsonArray = null;
1315 JSONArray jsonArrayNamespace = null;
1317 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
1318 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
1319 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1320 jsonArray = new JSONArray(msgFrmSubscribe);
1322 for (int i = 0; i < jsonArray.length(); i++) {
1323 jsonObj = jsonArray.getJSONObject(i);
1325 if (jsonObj.has("messageID") && jsonObj.get("messageID").equals(randomStr)
1326 && jsonObj.has("listMirrorMaker")) {
1327 jsonArrayNamespace = jsonObj.getJSONArray("listMirrorMaker");
1331 JSONObject finalJasonObj = new JSONObject();
1332 JSONArray finalJsonArray = new JSONArray();
1334 if (jsonArrayNamespace != null) {
1335 for (int i = 0; i < jsonArrayNamespace.length(); i++) {
1337 JSONObject mmObj = new JSONObject();
1338 mmObj = jsonArrayNamespace.getJSONObject(i);
1339 if (mmObj.has("name") && mmName.equals(mmObj.getString("name"))) {
1341 finalJsonArray.put(mmObj);
1346 finalJasonObj.put("listMirrorMaker", finalJsonArray);
1348 DMaaPResponseBuilder.respondOk(ctx, finalJasonObj);
1352 ErrorResponse errRes = new ErrorResponse(HttpStatus.SC_SERVICE_UNAVAILABLE,
1353 DMaaPResponseCode.RESOURCE_NOT_FOUND.getResponseCode(),
1354 "listMirrorMaker is not available, please make sure MirrorMakerAgent is running", null,
1355 Utils.getFormattedDate(new Date()), topic, null, null, "mirrorMakerAgent",
1356 ctx.getRequest().getRemoteHost());
1357 LOGGER.info(errRes.toString());
1358 throw new CambriaApiException(errRes);
1361 } catch (Exception e) {
1362 LOGGER.error("Error in callPubSubForWhitelist:", e);
1367 public JSONArray getListMirrorMaker(String msgFrmSubscribe, String randomStr) {
1368 JSONObject jsonObj = new JSONObject();
1369 JSONArray jsonArray = new JSONArray();
1370 JSONArray listMirrorMaker = new JSONArray();
1372 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1373 jsonArray = new JSONArray(msgFrmSubscribe);
1374 jsonObj = jsonArray.getJSONObject(0);
1376 for (int i = 0; i < jsonArray.length(); i++) {
1377 jsonObj = jsonArray.getJSONObject(i);
1379 if (jsonObj.has("messageID") && jsonObj.get("messageID").equals(randomStr) && jsonObj.has("listMirrorMaker")) {
1380 listMirrorMaker = jsonObj.getJSONArray("listMirrorMaker");
1384 return listMirrorMaker;
1388 public JSONObject validateMMExists(DMaaPContext ctx, String name) throws Exception {
1389 // Create a listAllMirrorMaker Json object
1390 JSONObject listAll = new JSONObject();
1392 listAll.put("listAllMirrorMaker", new JSONObject());
1394 } catch (JSONException e) {
1395 LOGGER.error("Error while creating a listAllMirrorMaker Json object:", e);
1398 // set a random number as messageID
1399 String randomStr = getRandomNum();
1400 listAll.put("messageID", randomStr);
1401 InputStream inStream = null;
1403 // convert listAll Json object to InputStream object
1405 inStream = IOUtils.toInputStream(listAll.toString(), "UTF-8");
1407 } catch (IOException ioe) {
1408 LOGGER.error("Error while converting string to an input stream:", ioe);
1410 JSONObject listMirrorMaker = new JSONObject();
1411 listMirrorMaker = callPubSub(randomStr, ctx, inStream, name, false);
1412 if (null != listMirrorMaker && listMirrorMaker.length() > 0) {
1413 listMirrorMaker.put("exists", true);
1414 return listMirrorMaker;
1418 if(null != listMirrorMaker) {
1419 listMirrorMaker.put("exists", false);
1422 return listMirrorMaker;