sonar critical for Exception handling
[dmaap/messagerouter/messageservice.git] / src / main / java / com / att / nsa / dmaap / service / MMRestService.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 package com.att.nsa.dmaap.service;
23
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;
29
30
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.Produces;
36 import javax.ws.rs.core.Context;
37 import org.json.JSONObject;
38 import org.apache.commons.io.IOUtils;
39 import org.apache.commons.lang.StringUtils;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Qualifier;
44 import org.springframework.stereotype.Component;
45
46 import com.att.nsa.cambria.utils.ConfigurationReader;
47 import com.att.nsa.cambria.utils.DMaaPResponseBuilder;
48 import com.att.nsa.cambria.utils.Utils;
49 import com.att.nsa.configs.ConfigDbException;
50 import com.att.nsa.dmaap.mmagent.*;
51 import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
52 import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
53 import com.google.gson.Gson;
54 import com.google.gson.JsonSyntaxException;
55
56 import edu.emory.mathcs.backport.java.util.Arrays;
57
58 import com.att.ajsc.filemonitor.AJSCPropertiesMap;
59 import com.att.nsa.cambria.CambriaApiException;
60 import com.att.nsa.cambria.backends.ConsumerFactory.UnavailableException;
61
62 import org.json.JSONArray;
63 import org.json.JSONException;
64 import com.att.nsa.cambria.beans.DMaaPContext;
65 import com.att.nsa.cambria.constants.CambriaConstants;
66 import com.att.nsa.cambria.exception.DMaaPErrorMessages;
67 import com.att.nsa.cambria.metabroker.Broker.TopicExistsException;
68 import com.att.nsa.cambria.security.DMaaPAAFAuthenticator;
69 import com.att.nsa.cambria.security.DMaaPAAFAuthenticatorImpl;
70 import com.att.nsa.cambria.service.MMService;
71
72 /**
73  * Rest Service class for Mirror Maker proxy Rest Services
74  * 
75  * @author <a href="mailto:"></a>
76  *
77  * @since May 25, 2016
78  */
79
80 @Component
81 public class MMRestService {
82
83         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(MMRestService.class);
84         private static final String NO_ADMIN_PERMISSION = "No Mirror Maker Admin permission.";
85         private static final String NO_USER_PERMISSION = "No Mirror Maker User permission.";
86         private static final String NO_USER_CREATE_PERMISSION = "No Mirror Maker User Create permission.";
87         private static final String NAME_DOES_NOT_MEET_REQUIREMENT = "Mirror Maker name can only contain alpha numeric";
88         private static final String INVALID_IPPORT = "This is not a valid IP:Port";
89         private static final String MIRROR_MAKERADMIN = "msgRtr.mirrormakeradmin.aaf";
90         private static final String MIRROR_MAKERUSER = "msgRtr.mirrormakeruser.aaf";
91         private static final String UTF_8 = "UTF-8";
92         private static final String MESSAGE = "message";
93         private static final String LISTMIRRORMAKER = "listMirrorMaker";
94         private static final String ERROR = "error";
95         private static final String NAMESPACE = "namespace";
96
97         private String topic;
98         private int timeout;
99         private String consumergroup;
100         private String consumerid;
101
102         @Autowired
103         @Qualifier("configurationReader")
104         private ConfigurationReader configReader;
105
106         @Context
107         private HttpServletRequest request;
108
109         @Context
110         private HttpServletResponse response;
111
112         @Autowired
113         private MMService mirrorService;
114
115         @Autowired
116         private DMaaPErrorMessages errorMessages;
117
118         /**
119          * This method is used for taking Configuration Object,HttpServletRequest
120          * Object,HttpServletRequest HttpServletResponse Object,HttpServletSession
121          * Object.
122          * 
123          * @return DMaaPContext object from where user can get Configuration
124          *         Object,HttpServlet Object
125          * 
126          */
127         private DMaaPContext getDmaapContext() {
128                 DMaaPContext dmaapContext = new DMaaPContext();
129                 dmaapContext.setRequest(request);
130                 dmaapContext.setResponse(response);
131                 dmaapContext.setConfigReader(configReader);
132                 dmaapContext.setConsumerRequestTime(Utils.getFormattedDate(new Date()));
133
134                 return dmaapContext;
135         }
136
137         @POST
138         @Produces("application/json")
139         @Path("/create")
140         public void callCreateMirrorMaker(InputStream msg) {
141
142                 DMaaPContext ctx = getDmaapContext();
143                 if (checkMirrorMakerPermission(ctx,
144                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERADMIN))) {
145
146                         loadProperty();
147                         String input = null;
148                         String randomStr = getRandomNum();
149
150                         InputStream inStream = null;
151                         Gson gson = new Gson();
152                         CreateMirrorMaker createMirrorMaker = new CreateMirrorMaker();
153
154                         try {
155                                 input = IOUtils.toString(msg, UTF_8);
156
157                                 if (input != null && input.length() > 0) {
158                                         input = removeExtraChar(input);
159                                 }
160
161                                 // Check if the request has CreateMirrorMaker
162                                 try {
163                                         createMirrorMaker = gson.fromJson(input, CreateMirrorMaker.class);
164
165                                 } catch (JsonSyntaxException ex) {
166
167                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
168                                         LOGGER.error("JsonSyntaxException: ", ex);
169                                 }
170                                 String name = createMirrorMaker.getCreateMirrorMaker().getName();
171                                 // send error message if it is not a CreateMirrorMaker request.
172                                 if (createMirrorMaker.getCreateMirrorMaker() == null) {
173                                         sendErrResponse(ctx, "This is not a CreateMirrorMaker request. Please try again.");
174                                 }
175
176                                 // MirrorMaker whitelist and status should not be passed
177                                 else if (createMirrorMaker.getCreateMirrorMaker().getWhitelist() != null
178                                                 || createMirrorMaker.getCreateMirrorMaker().getStatus() != null) {
179                                         sendErrResponse(ctx, "This is not a CreateMirrorMaker request. Please try again.");
180                                 }
181                                 
182                                 // if empty, blank name is entered
183                                 else if (StringUtils.isBlank(name)) {
184                                         sendErrResponse(ctx, "Name can not be empty or blank.");
185                                 }
186
187                                 // Check if the name contains only Alpha Numeric
188                                 else if (!isAlphaNumeric(name)) {
189                                         sendErrResponse(ctx, NAME_DOES_NOT_MEET_REQUIREMENT);
190
191                                 }
192
193                                 // Validate the IP and Port
194                                 else if (!StringUtils.isBlank(createMirrorMaker.getCreateMirrorMaker().getConsumer())
195                                                 && !StringUtils.isBlank(createMirrorMaker.getCreateMirrorMaker().getProducer())
196                                                 && !validateIPPort(createMirrorMaker.getCreateMirrorMaker().getConsumer())
197                                                 || !validateIPPort(createMirrorMaker.getCreateMirrorMaker().getProducer())) {
198                                         sendErrResponse(ctx, INVALID_IPPORT);
199
200                                 }
201                                 // Set a random number as messageID, convert Json Object to
202                                 // InputStream and finally call publisher and subscriber
203                                 else if (isAlphaNumeric(name) && validateIPPort(createMirrorMaker.getCreateMirrorMaker().getConsumer())
204                                                 && validateIPPort(createMirrorMaker.getCreateMirrorMaker().getProducer())) {
205
206                                         createMirrorMaker.setMessageID(randomStr);
207                                         inStream = IOUtils.toInputStream(gson.toJson(createMirrorMaker), UTF_8);
208                                         callPubSub(randomStr, ctx, inStream);
209                                 }
210
211                         } catch (IOException e) {
212
213                                 LOGGER.error("IOException: ", e);
214                         }
215                 }
216                 // Send error response if user does not provide Authorization
217                 else {
218                         sendErrResponse(ctx, NO_ADMIN_PERMISSION);
219                 }
220         }
221
222         @POST
223         @Produces("application/json")
224         @Path("/listall")
225         public void callListAllMirrorMaker(InputStream msg) throws CambriaApiException {
226                 DMaaPContext ctx = getDmaapContext();
227
228                 if (checkMirrorMakerPermission(ctx,
229                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERADMIN))) {
230
231                         loadProperty();
232
233                         String input = null;
234
235                         try {
236                                 input = IOUtils.toString(msg, UTF_8);
237
238                                 if (input != null && input.length() > 0) {
239                                         input = removeExtraChar(input);
240                                 }
241
242                                 String randomStr = getRandomNum();
243                                 JSONObject jsonOb = null;
244
245                                 try {
246                                         jsonOb = new JSONObject(input);
247
248                                 } catch (JSONException ex) {
249
250                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
251                                         LOGGER.error("JSONException: ", ex);
252                                 }
253
254                                 // Check if request has listAllMirrorMaker and
255                                 // listAllMirrorMaker is empty
256                                 if ((jsonOb != null) && (jsonOb.has("listAllMirrorMaker") &&
257                                     jsonOb.getJSONObject("listAllMirrorMaker").length() == 0)) {
258                                         jsonOb.put("messageID", randomStr);
259                                         InputStream inStream = null;
260
261                                         try {
262                                                 inStream = IOUtils.toInputStream(jsonOb.toString(), UTF_8);
263
264                                         } catch (IOException ioe) {
265                                                 LOGGER.error("IOException: ", ioe);
266                                         }
267
268                                         callPubSub(randomStr, ctx, inStream);
269
270                                 } else {
271
272                                         sendErrResponse(ctx, "This is not a ListAllMirrorMaker request. Please try again.");
273                                 }
274
275                         } catch (IOException ioe) {
276
277                                 LOGGER.error("IOException: ", ioe);
278                         }
279
280                 } else {
281
282                         sendErrResponse(getDmaapContext(), NO_ADMIN_PERMISSION);
283                 }
284         }
285
286         @POST
287         @Produces("application/json")
288         @Path("/update")
289         public void callUpdateMirrorMaker(InputStream msg) throws CambriaApiException {
290
291                 DMaaPContext ctx = getDmaapContext();
292                 if (checkMirrorMakerPermission(ctx,
293                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERADMIN))) {
294
295                         loadProperty();
296                         String input = null;
297                         String randomStr = getRandomNum();
298
299                         InputStream inStream = null;
300                         Gson gson = new Gson();
301                         UpdateMirrorMaker updateMirrorMaker = new UpdateMirrorMaker();
302
303                         try {
304                                 input = IOUtils.toString(msg, UTF_8);
305
306                                 if (input != null && input.length() > 0) {
307                                         input = removeExtraChar(input);
308                                 }
309
310                                 // Check if the request has UpdateMirrorMaker
311                                 try {
312                                         updateMirrorMaker = gson.fromJson(input, UpdateMirrorMaker.class);
313
314                                 } catch (JsonSyntaxException ex) {
315
316                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
317                                         LOGGER.error("JsonSyntaxException: ", ex);
318
319                                 }
320                                 String name = updateMirrorMaker.getUpdateMirrorMaker().getName();
321
322                                 // send error message if it is not a UpdateMirrorMaker request.
323                                 if (updateMirrorMaker.getUpdateMirrorMaker() == null) {
324                                         sendErrResponse(ctx, "This is not a UpdateMirrorMaker request. Please try again.");
325                                 }
326
327                                 // MirrorMaker whitelist and status should not be passed
328                                 else if (updateMirrorMaker.getUpdateMirrorMaker().getWhitelist() != null
329                                                 || updateMirrorMaker.getUpdateMirrorMaker().getStatus() != null) {
330                                         sendErrResponse(ctx, "This is not a UpdateMirrorMaker request. Please try again.");
331                                 }
332                                 
333                                 // if empty, blank name is entered
334                                 else if (StringUtils.isBlank(name)) {
335                                         sendErrResponse(ctx, "Name can not be empty or blank.");
336                                 }
337
338                                 // Check if the name contains only Alpha Numeric
339                                 else if (!isAlphaNumeric(name)) {
340                                         sendErrResponse(ctx, NAME_DOES_NOT_MEET_REQUIREMENT);
341
342                                 }
343
344                                 // Validate the IP and Port
345                                 else if (!StringUtils.isBlank(updateMirrorMaker.getUpdateMirrorMaker().getConsumer())
346                                                 && !StringUtils.isBlank(updateMirrorMaker.getUpdateMirrorMaker().getProducer())
347                                                 && !validateIPPort(updateMirrorMaker.getUpdateMirrorMaker().getConsumer())
348                                                 || !validateIPPort(updateMirrorMaker.getUpdateMirrorMaker().getProducer())) {
349                                         sendErrResponse(ctx, INVALID_IPPORT);
350
351                                 }
352                                 // Set a random number as messageID, convert Json Object to
353                                 // InputStream and finally call publisher and subscriber
354                                 else if (isAlphaNumeric(name) && validateIPPort(updateMirrorMaker.getUpdateMirrorMaker().getConsumer())
355                                                 && validateIPPort(updateMirrorMaker.getUpdateMirrorMaker().getProducer())) {
356
357                                         updateMirrorMaker.setMessageID(randomStr);
358                                         inStream = IOUtils.toInputStream(gson.toJson(updateMirrorMaker), UTF_8);
359                                         callPubSub(randomStr, ctx, inStream);
360                                 }
361
362                         } catch (IOException e) {
363
364                                 LOGGER.error("IOException: ", e);
365                         }
366                 }
367                 // Send error response if user does not provide Authorization
368                 else {
369                         sendErrResponse(ctx, NO_ADMIN_PERMISSION);
370                 }
371         }
372
373         @POST
374         @Produces("application/json")
375         @Path("/delete")
376         public void callDeleteMirrorMaker(InputStream msg) throws CambriaApiException {
377                 DMaaPContext ctx = getDmaapContext();
378
379                 if (checkMirrorMakerPermission(ctx,
380                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERADMIN))) {
381
382                         loadProperty();
383
384                         String input = null;
385
386                         try {
387                                 input = IOUtils.toString(msg, UTF_8);
388
389                                 if (input != null && input.length() > 0) {
390                                         input = removeExtraChar(input);
391                                 }
392
393                                 String randomStr = getRandomNum();
394                                 JSONObject jsonOb = null;
395
396                                 try {
397                                         jsonOb = new JSONObject(input);
398
399                                 } catch (JSONException ex) {
400
401                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
402                                         LOGGER.error("JSONException: ", ex);
403                                 }
404
405                                 // Check if request has DeleteMirrorMaker and
406                                 // DeleteMirrorMaker has MirrorMaker object with name variable
407                                 // and check if the name contain only alpha numeric
408                                 if ((jsonOb != null) && (jsonOb.has("deleteMirrorMaker") 
409                                                 && jsonOb.getJSONObject("deleteMirrorMaker").length() == 1
410                                                 && jsonOb.getJSONObject("deleteMirrorMaker").has("name") 
411                                                 && !StringUtils.isBlank(jsonOb.getJSONObject("deleteMirrorMaker").getString("name"))
412                                                 && isAlphaNumeric(jsonOb.getJSONObject("deleteMirrorMaker").getString("name")))) {
413
414                                         jsonOb.put("messageID", randomStr);
415                                         InputStream inStream = null;
416
417                                         try {
418                                                 inStream = IOUtils.toInputStream(jsonOb.toString(), UTF_8);
419
420                                         } catch (IOException ioe) {
421                                                 LOGGER.error("IOException: ", ioe);
422                                         }
423
424                                         callPubSub(randomStr, ctx, inStream);
425
426                                 } else {
427
428                                         sendErrResponse(ctx, "This is not a DeleteMirrorMaker request. Please try again.");
429                                 }
430
431                         } catch (IOException ioe) {
432
433                                 ioe.printStackTrace();
434                                 LOGGER.error("IOException: ", ioe);
435                         }
436
437                 } else {
438
439                         sendErrResponse(getDmaapContext(), NO_ADMIN_PERMISSION);
440                 }
441         }
442
443         private boolean isListMirrorMaker(String msg, String messageID) {
444                 String topicmsg = msg;
445                 topicmsg = removeExtraChar(topicmsg);
446
447                 JSONObject jObj;
448                 JSONArray jArray;
449                 boolean exist = false;
450
451                 if (!StringUtils.isBlank(topicmsg) && topicmsg.length() > 2) {
452                         jArray = new JSONArray(topicmsg);
453
454                         for (int i = 0; i < jArray.length(); i++) {
455                                 jObj = jArray.getJSONObject(i);
456                                 
457                                 JSONObject obj = new JSONObject();
458                                 if (jObj.has(MESSAGE)) {
459                                         obj = jObj.getJSONObject(MESSAGE);
460                                 }
461                                 if (obj.has("messageID") && obj.get("messageID").equals(messageID) && obj.has(LISTMIRRORMAKER)) {
462                                         exist = true;
463                                         break;
464                                 }
465                         }
466                 }
467                 return exist;
468         }
469
470         private void loadProperty() {
471
472                 this.timeout = Integer.parseInt(
473                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout").trim());
474                 this.topic = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic").trim();
475                 this.consumergroup = AJSCPropertiesMap
476                                 .getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup").trim();
477                 this.consumerid = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid")
478                                 .trim();
479         }
480
481         private String removeExtraChar(String message) {
482                 String str = message;
483                 str = checkJsonFormate(str);
484
485                 if (str != null && str.length() > 0) {
486                         str = str.replace("\\", "");
487                         str = str.replace("\"{", "{");
488                         str = str.replace("}\"", "}");
489                 }
490                 return str;
491         }
492
493         private String getRandomNum() {
494                 long random = Math.round(Math.random() * 89999) + 10000;
495                 String strLong = Long.toString(random);
496                 return strLong;
497         }
498
499         private boolean isAlphaNumeric(String name) {
500                 String pattern = "^[a-zA-Z0-9]*$";
501                 if (name.matches(pattern)) {
502                         return true;
503                 }
504                 return false;
505         }
506
507         // This method validate IPv4
508         private boolean validateIPPort(String ipPort) {
509                 String pattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
510                                 + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5]):"
511                                 + "([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$";
512                 if (ipPort.matches(pattern)) {
513                         return true;
514                 }
515                 return false;
516         }
517
518         private String checkJsonFormate(String jsonStr) {
519
520                 String json = jsonStr;
521                 if (jsonStr != null && jsonStr.length() > 0 && jsonStr.startsWith("[") && !jsonStr.endsWith("]")) {
522                         json = json + "]";
523                 }
524                 return json;
525         }
526
527         private boolean checkMirrorMakerPermission(DMaaPContext ctx, String permission) {
528
529                 boolean hasPermission = false;
530
531                 DMaaPAAFAuthenticator aaf = new DMaaPAAFAuthenticatorImpl();
532
533                 if (aaf.aafAuthentication(ctx.getRequest(), permission)) {
534                         hasPermission = true;
535                 }
536                 return hasPermission;
537         }
538
539         private void callPubSub(String randomstr, DMaaPContext ctx, InputStream inStream) {
540                 try {
541                         mirrorService.pushEvents(ctx, topic, inStream, null, null);
542                         long startTime = System.currentTimeMillis();
543                         String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
544
545                         while (!isListMirrorMaker(msgFrmSubscribe, randomstr)
546                                         && (System.currentTimeMillis() - startTime) < timeout) {
547                                 msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
548                         }
549
550                         JSONObject jsonObj;
551                         JSONObject finalJsonObj = new JSONObject();
552                         JSONArray jsonArray;
553
554                         if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
555                                         && isListMirrorMaker(msgFrmSubscribe, randomstr)) {
556                                 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
557                                 jsonArray = new JSONArray(msgFrmSubscribe);
558
559                                 for (int i = 0; i < jsonArray.length(); i++) {
560                                         jsonObj = jsonArray.getJSONObject(i);
561                                         
562                                         JSONObject obj = new JSONObject();
563                                         if (jsonObj.has(MESSAGE)) {
564                                                 obj = jsonObj.getJSONObject(MESSAGE);
565                                         }
566                                         if (obj.has("messageID") && obj.get("messageID").equals(randomstr) && obj.has(LISTMIRRORMAKER)) {
567                                                 finalJsonObj.put(LISTMIRRORMAKER, obj.get(LISTMIRRORMAKER));
568                                                 break;
569                                         }
570                                 }
571
572                                 DMaaPResponseBuilder.respondOk(ctx, finalJsonObj);
573
574                         } else {
575
576                                 JSONObject err = new JSONObject();
577                                 err.append(ERROR, "listMirrorMaker is not available, please make sure MirrorMakerAgent is running");
578                                 DMaaPResponseBuilder.respondOk(ctx, err);
579                         }
580
581                 } catch (Exception e) {
582                         e.printStackTrace();
583                         LOGGER.error("Exception: ", e);                 
584                 }
585         }
586
587         private void sendErrResponse(DMaaPContext ctx, String errMsg) {
588                 JSONObject err = new JSONObject();
589                 err.append(ERROR, errMsg);
590
591                 try {
592                         DMaaPResponseBuilder.respondOk(ctx, err);
593                         LOGGER.error(errMsg);
594
595                 } catch (JSONException | IOException e) {
596                         LOGGER.error(errMsg);
597                 }
598         }
599
600         @SuppressWarnings("unchecked")
601         @POST
602         @Produces("application/json")
603         @Path("/listallwhitelist")
604         public void listWhiteList(InputStream msg) {
605
606                 DMaaPContext ctx = getDmaapContext();
607                 if (checkMirrorMakerPermission(ctx,
608                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERUSER))) {
609
610                         loadProperty();
611                         String input = null;
612
613                         try {
614                                 input = IOUtils.toString(msg, UTF_8);
615
616                                 if (input != null && input.length() > 0) {
617                                         input = removeExtraChar(input);
618                                 }
619
620                                 // Check if it is correct Json object
621                                 JSONObject jsonOb = null;
622
623                                 try {
624                                         jsonOb = new JSONObject(input);
625
626                                 } catch (JSONException ex) {
627
628                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
629                                         LOGGER.error("JSONException: ", ex);
630                                 }
631
632                                 // Check if the request has name and name contains only alpha
633                                 // numeric
634                                 // and check if the request has namespace and namespace contains
635                                 // only alpha numeric
636                                 if (jsonOb != null 
637                                                 && jsonOb.length() == 2 && jsonOb.has("name")
638                                                 && !StringUtils.isBlank(jsonOb.getString("name"))
639                                                 && isAlphaNumeric(jsonOb.getString("name")) && jsonOb.has(NAMESPACE)
640                                                 && !StringUtils.isBlank(jsonOb.getString(NAMESPACE))) {
641
642                                         String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
643                                                         "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString(NAMESPACE) + "|create";
644
645                                         // Check if the user have create permission for the
646                                         // namespace
647                                         if (checkMirrorMakerPermission(ctx, permission)) {
648
649                                                 JSONObject listAll = new JSONObject();
650                                                 JSONObject emptyObject = new JSONObject();
651
652                                                 // Create a listAllMirrorMaker Json object
653                                                 try {
654                                                         listAll.put("listAllMirrorMaker", emptyObject);
655
656                                                 } catch (JSONException e) {
657
658                                                         LOGGER.error("JSONException: ", e);
659                                                 }
660
661                                                 // set a random number as messageID
662                                                 String randomStr = getRandomNum();
663                                                 listAll.put("messageID", randomStr);
664                                                 InputStream inStream = null;
665
666                                                 // convert listAll Json object to InputStream object
667                                                 try {
668                                                         inStream = IOUtils.toInputStream(listAll.toString(), UTF_8);
669
670                                                 } catch (IOException ioe) {
671                                                         ioe.printStackTrace();
672                                                         LOGGER.error("IOException: ", ioe);
673                                                 }
674                                                 // call listAllMirrorMaker
675                                                 mirrorService.pushEvents(ctx, topic, inStream, null, null);
676
677                                                 // subscribe for listMirrorMaker
678                                                 long startTime = System.currentTimeMillis();
679                                                 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
680
681                                                 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
682                                                                 && (System.currentTimeMillis() - startTime) < timeout) {
683                                                         msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
684                                                 }
685
686                                                 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
687                                                                 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
688                                                         
689                                                         JSONArray listMirrorMaker;
690                                                         listMirrorMaker = getListMirrorMaker(msgFrmSubscribe, randomStr);
691
692                                                         String whitelist = null;
693                                                         for (int i = 0; i < listMirrorMaker.length(); i++) {
694
695                                                                 JSONObject mm;
696                                                                 mm = listMirrorMaker.getJSONObject(i);
697                                                                 String name = mm.getString("name");
698
699                                                                 if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
700                                                                         whitelist = mm.getString("whitelist");
701                                                                         break;
702                                                                 }
703                                                         }
704
705                                                         if (!StringUtils.isBlank(whitelist)) {
706
707                                                                 List<String> topicList = new ArrayList<>();
708                                                                 List<String> finalTopicList = new ArrayList<>();
709                                                                 topicList = Arrays.asList(whitelist.split(","));
710
711                                                                 for (String topic : topicList) {
712                                                                         if (topic != null && !topic.equals("null")
713                                                                                         && getNamespace(topic).equals(jsonOb.getString(NAMESPACE))) {
714
715                                                                                 finalTopicList.add(topic);
716                                                                         }
717                                                                 }
718
719                                                                 String topicNames = "";
720
721                                                                 if (!finalTopicList.isEmpty()) {
722                                                                         topicNames = StringUtils.join(finalTopicList, ",");
723                                                                 }
724
725                                                                 JSONObject listAllWhiteList = new JSONObject();
726                                                                 listAllWhiteList.put("name", jsonOb.getString("name"));
727                                                                 listAllWhiteList.put("whitelist", topicNames);
728
729                                                                 DMaaPResponseBuilder.respondOk(ctx, listAllWhiteList);
730                                                         }
731
732                                                 } else {
733
734                                                         JSONObject err = new JSONObject();
735                                                         err.append(ERROR,
736                                                                         "listWhiteList is not available, please make sure MirrorMakerAgent is running");
737                                                         DMaaPResponseBuilder.respondOk(ctx, err);
738                                                 }
739
740                                         } else {
741                                                 sendErrResponse(ctx, NO_USER_CREATE_PERMISSION);
742                                         }
743
744                                 } else {
745
746                                         sendErrResponse(ctx, "This is not a ListAllWhitelist request. Please try again.");
747                                 }
748
749                         } catch (IOException | CambriaApiException | ConfigDbException | AccessDeniedException
750                                         | TopicExistsException | missingReqdSetting | UnavailableException e) {
751
752                                 LOGGER.error("IOException: ", e);
753                         }
754                 } else {
755                         sendErrResponse(ctx, NO_USER_PERMISSION);
756                 }
757         }
758
759         @SuppressWarnings("unchecked")
760         @POST
761         @Produces("application/json")
762         @Path("/createwhitelist")
763         public void createWhiteList(InputStream msg) {
764
765                 DMaaPContext ctx = getDmaapContext();
766                 if (checkMirrorMakerPermission(ctx,
767                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERUSER))) {
768
769                         loadProperty();
770                         String input = null;
771
772                         try {
773                                 input = IOUtils.toString(msg, UTF_8);
774
775                                 if (input != null && input.length() > 0) {
776                                         input = removeExtraChar(input);
777                                 }
778
779                                 // Check if it is correct Json object
780                                 JSONObject jsonOb = null;
781
782                                 try {
783                                         jsonOb = new JSONObject(input);
784
785                                 } catch (JSONException ex) {
786
787                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
788                                         LOGGER.error("JSONException: ", ex);
789                                 }
790
791                                 // Check if the request has name and name contains only alpha numeric,
792                                 // check if the request has namespace and
793                                 // check if the request has whitelistTopicName
794                                 // check if the topic name contains only alpha numeric
795                                 if (jsonOb != null && jsonOb.length() == 3 && jsonOb.has("name")
796                                                 && !StringUtils.isBlank(jsonOb.getString("name")) 
797                                                 && isAlphaNumeric(jsonOb.getString("name")) 
798                                                 && jsonOb.has(NAMESPACE) && !StringUtils.isBlank(jsonOb.getString(NAMESPACE))
799                                                 && jsonOb.has("whitelistTopicName") && !StringUtils.isBlank(jsonOb.getString("whitelistTopicName"))
800                                                 && isAlphaNumeric(jsonOb.getString("whitelistTopicName").substring(jsonOb.getString("whitelistTopicName").lastIndexOf(".")+1, 
801                                                                 jsonOb.getString("whitelistTopicName").length()))) {
802
803                                         String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
804                                                         "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString(NAMESPACE) + "|create";
805
806                                         // Check if the user have create permission for the
807                                         // namespace
808                                         if (checkMirrorMakerPermission(ctx, permission)) {
809
810                                                 JSONObject listAll = new JSONObject();
811                                                 JSONObject emptyObject = new JSONObject();
812
813                                                 // Create a listAllMirrorMaker Json object
814                                                 try {
815                                                         listAll.put("listAllMirrorMaker", emptyObject);
816
817                                                 } catch (JSONException e) {
818
819                                                         LOGGER.error("JSONException: ", e);
820                                                 }
821
822                                                 // set a random number as messageID
823                                                 String randomStr = getRandomNum();
824                                                 listAll.put("messageID", randomStr);
825                                                 InputStream inStream = null;
826
827                                                 // convert listAll Json object to InputStream object
828                                                 try {
829                                                         inStream = IOUtils.toInputStream(listAll.toString(), UTF_8);
830
831                                                 } catch (IOException ioe) {
832                                                         ioe.printStackTrace();
833                                                         LOGGER.error("IOException: ", ioe);
834                                                 }
835                                                 // call listAllMirrorMaker
836                                                 mirrorService.pushEvents(ctx, topic, inStream, null, null);
837
838                                                 // subscribe for listMirrorMaker
839                                                 long startTime = System.currentTimeMillis();
840                                                 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
841
842                                                 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
843                                                                 && (System.currentTimeMillis() - startTime) < timeout) {
844                                                         msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
845                                                 }
846
847                                                 JSONArray listMirrorMaker;
848
849                                                 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
850                                                                 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
851                                                         
852                                                         listMirrorMaker = getListMirrorMaker(msgFrmSubscribe, randomStr);
853                                                         String whitelist = null;
854
855                                                         for (int i = 0; i < listMirrorMaker.length(); i++) {
856                                                                 JSONObject mm;
857                                                                 mm = listMirrorMaker.getJSONObject(i);
858                                                                 String name = mm.getString("name");
859
860                                                                 if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
861                                                                         whitelist = mm.getString("whitelist");
862                                                                         break;
863                                                                 }
864                                                         }
865
866                                                         List<String> topicList = new ArrayList<>();
867                                                         List<String> finalTopicList = new ArrayList<>();
868
869                                                         if (whitelist != null) {
870                                                                 topicList = Arrays.asList(whitelist.split(","));
871                                                         }
872
873                                                         for (String st : topicList) {
874                                                                 if (!StringUtils.isBlank(st)) {
875                                                                         finalTopicList.add(st);
876                                                                 }
877                                                         }
878
879                                                         String newTopic = jsonOb.getString("whitelistTopicName");
880
881                                                         if (!topicList.contains(newTopic)
882                                                                         && getNamespace(newTopic).equals(jsonOb.getString(NAMESPACE))) {
883
884                                                                 UpdateWhiteList updateWhiteList = new UpdateWhiteList();
885                                                                 MirrorMaker mirrorMaker = new MirrorMaker();
886                                                                 mirrorMaker.setName(jsonOb.getString("name"));
887                                                                 finalTopicList.add(newTopic);
888                                                                 String newWhitelist = "";
889
890                                                                 if (!finalTopicList.isEmpty()) {
891                                                                         newWhitelist = StringUtils.join(finalTopicList, ",");
892                                                                 }
893
894                                                                 mirrorMaker.setWhitelist(newWhitelist);
895
896                                                                 String newRandom = getRandomNum();
897                                                                 updateWhiteList.setMessageID(newRandom);
898                                                                 updateWhiteList.setUpdateWhiteList(mirrorMaker);
899
900                                                                 Gson g = new Gson();
901                                                                 g.toJson(updateWhiteList);
902                                                                 InputStream inputStream;
903                                                                 inputStream = IOUtils.toInputStream(g.toJson(updateWhiteList), UTF_8);
904                                                                 // callPubSub(newRandom, ctx, inputStream);
905                                                                 callPubSubForWhitelist(newRandom, ctx, inputStream, jsonOb.getString(NAMESPACE));
906
907                                                         } else if (topicList.contains(newTopic)) {
908                                                                 sendErrResponse(ctx, "The topic already exist.");
909
910                                                         } else if (!getNamespace(newTopic).equals(jsonOb.getString(NAMESPACE))) {
911                                                                 sendErrResponse(ctx,
912                                                                                 "The namespace of the topic does not match with the namespace you provided.");
913                                                         }
914                                                 } else {
915
916                                                         JSONObject err = new JSONObject();
917                                                         err.append(ERROR,
918                                                                         "listWhiteList is not available, please make sure MirrorMakerAgent is running");
919                                                         DMaaPResponseBuilder.respondOk(ctx, err);
920                                                 }
921
922                                         } else {
923                                                 sendErrResponse(ctx, NO_USER_CREATE_PERMISSION);
924                                         }
925
926                                 } else {
927
928                                         sendErrResponse(ctx, "This is not a createWhitelist request. Please try again.");
929                                 }
930
931                         } catch (IOException | CambriaApiException | ConfigDbException | AccessDeniedException
932                                         | TopicExistsException | missingReqdSetting | UnavailableException e) {
933
934                                 LOGGER.error("IOException: ", e);
935                         }
936                 }
937                 // Send error response if user does not provide Authorization
938                 else {
939                         sendErrResponse(ctx, NO_USER_PERMISSION);
940                 }
941         }
942
943         @SuppressWarnings("unchecked")
944         @POST
945         @Produces("application/json")
946         @Path("/deletewhitelist")
947         public void deleteWhiteList(InputStream msg) {
948
949                 DMaaPContext ctx = getDmaapContext();
950                 if (checkMirrorMakerPermission(ctx,
951                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERUSER))) {
952
953                         loadProperty();
954                         String input = null;
955
956                         try {
957                                 input = IOUtils.toString(msg, UTF_8);
958
959                                 if (input != null && input.length() > 0) {
960                                         input = removeExtraChar(input);
961                                 }
962
963                                 // Check if it is correct Json object
964                                 JSONObject jsonOb = null;
965
966                                 try {
967                                         jsonOb = new JSONObject(input);
968
969                                 } catch (JSONException ex) {
970
971                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
972                                         LOGGER.error("JSONException: ", ex);
973                                 }
974
975                                 // Check if the request has name and name contains only alpha numeric,
976                                 // check if the request has namespace and
977                                 // check if the request has whitelistTopicName
978                                 if (jsonOb != null && jsonOb.length() == 3 && jsonOb.has("name") && isAlphaNumeric(jsonOb.getString("name"))
979                                                 && jsonOb.has(NAMESPACE) && jsonOb.has("whitelistTopicName") 
980                                                 && isAlphaNumeric(jsonOb.getString("whitelistTopicName").substring(jsonOb.getString("whitelistTopicName").lastIndexOf(".")+1, 
981                                                                 jsonOb.getString("whitelistTopicName").length()))) {
982
983                                         String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
984                                                         "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString(NAMESPACE) + "|create";
985
986                                         // Check if the user have create permission for the
987                                         // namespace
988                                         if (checkMirrorMakerPermission(ctx, permission)) {
989
990                                                 JSONObject listAll = new JSONObject();
991                                                 JSONObject emptyObject = new JSONObject();
992
993                                                 // Create a listAllMirrorMaker Json object
994                                                 try {
995                                                         listAll.put("listAllMirrorMaker", emptyObject);
996
997                                                 } catch (JSONException e) {
998
999                                                         LOGGER.error("JSONException: ", e);
1000                                                 }
1001
1002                                                 // set a random number as messageID
1003                                                 String randomStr = getRandomNum();
1004                                                 listAll.put("messageID", randomStr);
1005                                                 InputStream inStream = null;
1006
1007                                                 // convert listAll Json object to InputStream object
1008                                                 try {
1009                                                         inStream = IOUtils.toInputStream(listAll.toString(), UTF_8);
1010
1011                                                 } catch (IOException ioe) {
1012                                                         ioe.printStackTrace();
1013                                                         LOGGER.error("IOException: ", ioe);
1014                                                 }
1015                                                 // call listAllMirrorMaker
1016                                                 mirrorService.pushEvents(ctx, topic, inStream, null, null);
1017
1018                                                 // subscribe for listMirrorMaker
1019                                                 long startTime = System.currentTimeMillis();
1020                                                 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1021
1022                                                 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
1023                                                                 && (System.currentTimeMillis() - startTime) < timeout) {
1024                                                         msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1025                                                 }
1026
1027                                                 JSONObject jsonObj;
1028                                                 JSONArray jsonArray;
1029                                                 JSONArray listMirrorMaker = null;
1030
1031                                                 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
1032                                                                 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
1033                                                         msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1034                                                         jsonArray = new JSONArray(msgFrmSubscribe);
1035
1036                                                         for (int i = 0; i < jsonArray.length(); i++) {
1037                                                                 jsonObj = jsonArray.getJSONObject(i);
1038                                                                 
1039                                                                 JSONObject obj = new JSONObject();
1040                                                                 if (jsonObj.has(MESSAGE)) {
1041                                                                         obj = jsonObj.getJSONObject(MESSAGE);
1042                                                                 }
1043                                                                 if (obj.has("messageID") && obj.get("messageID").equals(randomStr) && obj.has(LISTMIRRORMAKER)) {
1044                                                                         listMirrorMaker = obj.getJSONArray(LISTMIRRORMAKER);
1045                                                                         break;
1046                                                                 }
1047                                                         }
1048                                                         String whitelist = null;
1049                                                         if (listMirrorMaker != null) {
1050                                                                 for (int i = 0; i < listMirrorMaker.length(); i++) {
1051
1052                                                                         JSONObject mm = new JSONObject();
1053                                                                         mm = listMirrorMaker.getJSONObject(i);
1054                                                                         String name = mm.getString("name");
1055
1056                                                                         if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
1057                                                                                 whitelist = mm.getString("whitelist");
1058                                                                                 break;
1059                                                                         }
1060                                                                 }
1061                                                         }
1062
1063                                                         List<String> topicList = new ArrayList<>();
1064
1065                                                         if (whitelist != null) {
1066                                                                 topicList = Arrays.asList(whitelist.split(","));
1067                                                         }
1068                                                         boolean removeTopic = false;
1069                                                         String topicToRemove = jsonOb.getString("whitelistTopicName");
1070
1071                                                         if (topicList.contains(topicToRemove)) {
1072                                                                 removeTopic = true;
1073                                                         } else {
1074                                                                 sendErrResponse(ctx, "The topic does not exist.");
1075                                                         }
1076
1077
1078                                                         if (removeTopic) {
1079                                                                 UpdateWhiteList updateWhiteList = new UpdateWhiteList();
1080                                                                 MirrorMaker mirrorMaker = new MirrorMaker();
1081                                                                 
1082                                                                 mirrorMaker.setName(jsonOb.getString("name"));
1083                                                                 mirrorMaker.setWhitelist(removeTopic(whitelist, topicToRemove));
1084                                                         
1085                                                                 String newRandom = getRandomNum();
1086                                                                 
1087                                                                 updateWhiteList.setMessageID(newRandom);
1088                                                                 updateWhiteList.setUpdateWhiteList(mirrorMaker);
1089                                                                 
1090                                                                 Gson g = new Gson();
1091                                                                 g.toJson(updateWhiteList);
1092                                                                 
1093                                                                 InputStream inputStream;
1094                                                                 inputStream = IOUtils.toInputStream(g.toJson(updateWhiteList), UTF_8);
1095                                                                 callPubSubForWhitelist(newRandom, ctx, inputStream, getNamespace(topicToRemove));
1096                                                         }
1097
1098                                                 } else {
1099
1100                                                         JSONObject err = new JSONObject();
1101                                                         err.append(ERROR,
1102                                                                         "listWhiteList is not available, please make sure MirrorMakerAgent is running");
1103                                                         DMaaPResponseBuilder.respondOk(ctx, err);
1104                                                 }
1105
1106                                         } else {
1107                                                 sendErrResponse(ctx, NO_USER_CREATE_PERMISSION);
1108                                         }
1109
1110                                 } else {
1111
1112                                         sendErrResponse(ctx, "This is not a DeleteAllWhitelist request. Please try again.");
1113                                 }
1114
1115                         } catch (IOException | CambriaApiException | ConfigDbException | AccessDeniedException
1116                                         | TopicExistsException | missingReqdSetting | UnavailableException e) {
1117
1118                                 LOGGER.error("IOException: ", e);
1119                         }
1120                 }
1121                 // Send error response if user does not provide Authorization
1122                 else {
1123                         sendErrResponse(ctx, NO_USER_PERMISSION);
1124                 }
1125         }
1126
1127         private String getNamespace(String topic) {
1128                 return topic.substring(0, topic.lastIndexOf("."));
1129         }
1130
1131         private String removeTopic(String whitelist, String topicToRemove) {
1132                 List<String> topicList = new ArrayList<>();
1133                 List<String> newTopicList = new ArrayList<>();
1134
1135                 if (whitelist.contains(",")) {
1136                         topicList = Arrays.asList(whitelist.split(","));
1137
1138                 }
1139
1140                 if (topicList.contains(topicToRemove)) {
1141                         for (String topic: topicList) {
1142                                 if (!topic.equals(topicToRemove)) {
1143                                         newTopicList.add(topic);
1144                                 }
1145                         }
1146                 }
1147
1148                 String newWhitelist = StringUtils.join(newTopicList, ",");
1149
1150                 return newWhitelist;
1151         }
1152
1153         private void callPubSubForWhitelist(String randomStr, DMaaPContext ctx, InputStream inStream, String namespace) {
1154                 
1155                 try {
1156                         mirrorService.pushEvents(ctx, topic, inStream, null, null);
1157                         long startTime = System.currentTimeMillis();
1158                         String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1159
1160                         while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
1161                                         && (System.currentTimeMillis() - startTime) < timeout) {
1162                                 msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1163                         }
1164
1165                         JSONObject jsonObj;
1166                         JSONArray jsonArray;
1167                         JSONArray jsonArrayNamespace = null;
1168
1169                         if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
1170                                         && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
1171                                 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1172                                 jsonArray = new JSONArray(msgFrmSubscribe);
1173
1174                                 for (int i = 0; i < jsonArray.length(); i++) {
1175                                         jsonObj = jsonArray.getJSONObject(i);
1176                                         
1177                                         JSONObject obj = new JSONObject();
1178                                         if (jsonObj.has(MESSAGE)) {
1179                                                 obj = jsonObj.getJSONObject(MESSAGE);
1180                                         }
1181                                         if (obj.has("messageID") && obj.get("messageID").equals(randomStr) && obj.has(LISTMIRRORMAKER)) {
1182                                                 jsonArrayNamespace = obj.getJSONArray(LISTMIRRORMAKER);
1183                                         }
1184                                 }
1185                                 JSONObject finalJasonObj = new JSONObject();
1186                                 JSONArray finalJsonArray = new JSONArray();
1187
1188                                 for (int i = 0; i < jsonArrayNamespace.length(); i++) {
1189
1190                                         JSONObject mmObj;
1191                                         mmObj = jsonArrayNamespace.getJSONObject(i);
1192                                         String whitelist;
1193
1194                                         if (mmObj.has("whitelist")) {
1195                                                 whitelist = getWhitelistByNamespace(mmObj.getString("whitelist"), namespace);
1196
1197                                                 if (whitelist != null) {
1198                                                         mmObj.remove("whitelist");
1199                                                         mmObj.put("whitelist", whitelist);
1200                                                 } else {
1201                                                         mmObj.remove("whitelist");
1202                                                 }
1203                                         }
1204                                         finalJsonArray.put(mmObj);
1205                                 }
1206                                 finalJasonObj.put(LISTMIRRORMAKER, finalJsonArray);
1207
1208                                 DMaaPResponseBuilder.respondOk(ctx, finalJasonObj);
1209
1210                         } else {
1211
1212                                 JSONObject err = new JSONObject();
1213                                 err.append(ERROR, "listMirrorMaker is not available, please make sure MirrorMakerAgent is running");
1214                                 DMaaPResponseBuilder.respondOk(ctx, err);
1215                         }
1216
1217                 } catch (Exception e) {
1218                         LOGGER.error("Exception: ", e);
1219                 }
1220         }
1221
1222         private String getWhitelistByNamespace(String originalWhitelist, String namespace) {
1223
1224                 String whitelist = null;
1225                 List<String> resultList = new ArrayList<>();
1226                 List<String> whitelistList = new ArrayList<>();
1227                 whitelistList = Arrays.asList(originalWhitelist.split(","));
1228
1229                 for (String topic : whitelistList) {
1230                         if (StringUtils.isNotBlank(originalWhitelist) && getNamespace(topic).equals(namespace)) {
1231                                 resultList.add(topic);
1232                         }
1233                 }
1234                 if (!resultList.isEmpty()) {
1235                         whitelist = StringUtils.join(resultList, ",");
1236                 }
1237
1238                 return whitelist;
1239         }
1240         
1241         private JSONArray getListMirrorMaker(String msgFrmSubscribe, String randomStr) {
1242                 JSONObject jsonObj;
1243                 JSONArray jsonArray;
1244                 JSONArray listMirrorMaker = new JSONArray();
1245                 
1246                 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1247                 jsonArray = new JSONArray(msgFrmSubscribe);
1248
1249                 for (int i = 0; i < jsonArray.length(); i++) {
1250                         jsonObj = jsonArray.getJSONObject(i);
1251                         
1252                         JSONObject obj = new JSONObject();
1253                         if (jsonObj.has(MESSAGE)) {
1254                                 obj = jsonObj.getJSONObject(MESSAGE);
1255                         }
1256                         if (obj.has("messageID") && obj.get("messageID").equals(randomStr) && obj.has(LISTMIRRORMAKER)) {
1257                                 listMirrorMaker = obj.getJSONArray(LISTMIRRORMAKER);
1258                                 break;
1259                         }
1260                 }
1261                 return listMirrorMaker;         
1262         }
1263 }