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                                 ioe.printStackTrace();
278                                 LOGGER.error("IOException: ", ioe);
279                         }
280
281                 } else {
282
283                         sendErrResponse(getDmaapContext(), NO_ADMIN_PERMISSION);
284                 }
285         }
286
287         @POST
288         @Produces("application/json")
289         @Path("/update")
290         public void callUpdateMirrorMaker(InputStream msg) throws CambriaApiException {
291
292                 DMaaPContext ctx = getDmaapContext();
293                 if (checkMirrorMakerPermission(ctx,
294                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERADMIN))) {
295
296                         loadProperty();
297                         String input = null;
298                         String randomStr = getRandomNum();
299
300                         InputStream inStream = null;
301                         Gson gson = new Gson();
302                         UpdateMirrorMaker updateMirrorMaker = new UpdateMirrorMaker();
303
304                         try {
305                                 input = IOUtils.toString(msg, UTF_8);
306
307                                 if (input != null && input.length() > 0) {
308                                         input = removeExtraChar(input);
309                                 }
310
311                                 // Check if the request has UpdateMirrorMaker
312                                 try {
313                                         updateMirrorMaker = gson.fromJson(input, UpdateMirrorMaker.class);
314
315                                 } catch (JsonSyntaxException ex) {
316
317                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
318                                         LOGGER.error("JsonSyntaxException: ", ex);
319
320                                 }
321                                 String name = updateMirrorMaker.getUpdateMirrorMaker().getName();
322
323                                 // send error message if it is not a UpdateMirrorMaker request.
324                                 if (updateMirrorMaker.getUpdateMirrorMaker() == null) {
325                                         sendErrResponse(ctx, "This is not a UpdateMirrorMaker request. Please try again.");
326                                 }
327
328                                 // MirrorMaker whitelist and status should not be passed
329                                 else if (updateMirrorMaker.getUpdateMirrorMaker().getWhitelist() != null
330                                                 || updateMirrorMaker.getUpdateMirrorMaker().getStatus() != null) {
331                                         sendErrResponse(ctx, "This is not a UpdateMirrorMaker request. Please try again.");
332                                 }
333                                 
334                                 // if empty, blank name is entered
335                                 else if (StringUtils.isBlank(name)) {
336                                         sendErrResponse(ctx, "Name can not be empty or blank.");
337                                 }
338
339                                 // Check if the name contains only Alpha Numeric
340                                 else if (!isAlphaNumeric(name)) {
341                                         sendErrResponse(ctx, NAME_DOES_NOT_MEET_REQUIREMENT);
342
343                                 }
344
345                                 // Validate the IP and Port
346                                 else if (!StringUtils.isBlank(updateMirrorMaker.getUpdateMirrorMaker().getConsumer())
347                                                 && !StringUtils.isBlank(updateMirrorMaker.getUpdateMirrorMaker().getProducer())
348                                                 && !validateIPPort(updateMirrorMaker.getUpdateMirrorMaker().getConsumer())
349                                                 || !validateIPPort(updateMirrorMaker.getUpdateMirrorMaker().getProducer())) {
350                                         sendErrResponse(ctx, INVALID_IPPORT);
351
352                                 }
353                                 // Set a random number as messageID, convert Json Object to
354                                 // InputStream and finally call publisher and subscriber
355                                 else if (isAlphaNumeric(name) && validateIPPort(updateMirrorMaker.getUpdateMirrorMaker().getConsumer())
356                                                 && validateIPPort(updateMirrorMaker.getUpdateMirrorMaker().getProducer())) {
357
358                                         updateMirrorMaker.setMessageID(randomStr);
359                                         inStream = IOUtils.toInputStream(gson.toJson(updateMirrorMaker), UTF_8);
360                                         callPubSub(randomStr, ctx, inStream);
361                                 }
362
363                         } catch (IOException e) {
364
365                                 LOGGER.error("IOException: ", e);
366                         }
367                 }
368                 // Send error response if user does not provide Authorization
369                 else {
370                         sendErrResponse(ctx, NO_ADMIN_PERMISSION);
371                 }
372         }
373
374         @POST
375         @Produces("application/json")
376         @Path("/delete")
377         public void callDeleteMirrorMaker(InputStream msg) throws CambriaApiException {
378                 DMaaPContext ctx = getDmaapContext();
379
380                 if (checkMirrorMakerPermission(ctx,
381                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERADMIN))) {
382
383                         loadProperty();
384
385                         String input = null;
386
387                         try {
388                                 input = IOUtils.toString(msg, UTF_8);
389
390                                 if (input != null && input.length() > 0) {
391                                         input = removeExtraChar(input);
392                                 }
393
394                                 String randomStr = getRandomNum();
395                                 JSONObject jsonOb = null;
396
397                                 try {
398                                         jsonOb = new JSONObject(input);
399
400                                 } catch (JSONException ex) {
401
402                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
403                                         LOGGER.error("JSONException: ", ex);
404                                 }
405
406                                 // Check if request has DeleteMirrorMaker and
407                                 // DeleteMirrorMaker has MirrorMaker object with name variable
408                                 // and check if the name contain only alpha numeric
409                                 if ((jsonOb != null) && (jsonOb.has("deleteMirrorMaker") 
410                                                 && jsonOb.getJSONObject("deleteMirrorMaker").length() == 1
411                                                 && jsonOb.getJSONObject("deleteMirrorMaker").has("name") 
412                                                 && !StringUtils.isBlank(jsonOb.getJSONObject("deleteMirrorMaker").getString("name"))
413                                                 && isAlphaNumeric(jsonOb.getJSONObject("deleteMirrorMaker").getString("name")))) {
414
415                                         jsonOb.put("messageID", randomStr);
416                                         InputStream inStream = null;
417
418                                         try {
419                                                 inStream = IOUtils.toInputStream(jsonOb.toString(), UTF_8);
420
421                                         } catch (IOException ioe) {
422                                                 ioe.printStackTrace();
423                                                 LOGGER.error("IOException: ", ioe);
424                                         }
425
426                                         callPubSub(randomStr, ctx, inStream);
427
428                                 } else {
429
430                                         sendErrResponse(ctx, "This is not a DeleteMirrorMaker request. Please try again.");
431                                 }
432
433                         } catch (IOException ioe) {
434
435                                 ioe.printStackTrace();
436                                 LOGGER.error("IOException: ", ioe);
437                         }
438
439                 } else {
440
441                         sendErrResponse(getDmaapContext(), NO_ADMIN_PERMISSION);
442                 }
443         }
444
445         private boolean isListMirrorMaker(String msg, String messageID) {
446                 String topicmsg = msg;
447                 topicmsg = removeExtraChar(topicmsg);
448
449                 JSONObject jObj;
450                 JSONArray jArray;
451                 boolean exist = false;
452
453                 if (!StringUtils.isBlank(topicmsg) && topicmsg.length() > 2) {
454                         jArray = new JSONArray(topicmsg);
455
456                         for (int i = 0; i < jArray.length(); i++) {
457                                 jObj = jArray.getJSONObject(i);
458                                 
459                                 JSONObject obj = new JSONObject();
460                                 if (jObj.has(MESSAGE)) {
461                                         obj = jObj.getJSONObject(MESSAGE);
462                                 }
463                                 if (obj.has("messageID") && obj.get("messageID").equals(messageID) && obj.has(LISTMIRRORMAKER)) {
464                                         exist = true;
465                                         break;
466                                 }
467                         }
468                 }
469                 return exist;
470         }
471
472         private void loadProperty() {
473
474                 this.timeout = Integer.parseInt(
475                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.timeout").trim());
476                 this.topic = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.topic").trim();
477                 this.consumergroup = AJSCPropertiesMap
478                                 .getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumergroup").trim();
479                 this.consumerid = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, "msgRtr.mirrormaker.consumerid")
480                                 .trim();
481         }
482
483         private String removeExtraChar(String message) {
484                 String str = message;
485                 str = checkJsonFormate(str);
486
487                 if (str != null && str.length() > 0) {
488                         str = str.replace("\\", "");
489                         str = str.replace("\"{", "{");
490                         str = str.replace("}\"", "}");
491                 }
492                 return str;
493         }
494
495         private String getRandomNum() {
496                 long random = Math.round(Math.random() * 89999) + 10000;
497                 String strLong = Long.toString(random);
498                 return strLong;
499         }
500
501         private boolean isAlphaNumeric(String name) {
502                 String pattern = "^[a-zA-Z0-9]*$";
503                 if (name.matches(pattern)) {
504                         return true;
505                 }
506                 return false;
507         }
508
509         // This method validate IPv4
510         private boolean validateIPPort(String ipPort) {
511                 String pattern = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
512                                 + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5]):"
513                                 + "([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])$";
514                 if (ipPort.matches(pattern)) {
515                         return true;
516                 }
517                 return false;
518         }
519
520         private String checkJsonFormate(String jsonStr) {
521
522                 String json = jsonStr;
523                 if (jsonStr != null && jsonStr.length() > 0 && jsonStr.startsWith("[") && !jsonStr.endsWith("]")) {
524                         json = json + "]";
525                 }
526                 return json;
527         }
528
529         private boolean checkMirrorMakerPermission(DMaaPContext ctx, String permission) {
530
531                 boolean hasPermission = false;
532
533                 DMaaPAAFAuthenticator aaf = new DMaaPAAFAuthenticatorImpl();
534
535                 if (aaf.aafAuthentication(ctx.getRequest(), permission)) {
536                         hasPermission = true;
537                 }
538                 return hasPermission;
539         }
540
541         private void callPubSub(String randomstr, DMaaPContext ctx, InputStream inStream) {
542                 try {
543                         mirrorService.pushEvents(ctx, topic, inStream, null, null);
544                         long startTime = System.currentTimeMillis();
545                         String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
546
547                         while (!isListMirrorMaker(msgFrmSubscribe, randomstr)
548                                         && (System.currentTimeMillis() - startTime) < timeout) {
549                                 msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
550                         }
551
552                         JSONObject jsonObj;
553                         JSONObject finalJsonObj = new JSONObject();
554                         JSONArray jsonArray;
555
556                         if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
557                                         && isListMirrorMaker(msgFrmSubscribe, randomstr)) {
558                                 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
559                                 jsonArray = new JSONArray(msgFrmSubscribe);
560
561                                 for (int i = 0; i < jsonArray.length(); i++) {
562                                         jsonObj = jsonArray.getJSONObject(i);
563                                         
564                                         JSONObject obj = new JSONObject();
565                                         if (jsonObj.has(MESSAGE)) {
566                                                 obj = jsonObj.getJSONObject(MESSAGE);
567                                         }
568                                         if (obj.has("messageID") && obj.get("messageID").equals(randomstr) && obj.has(LISTMIRRORMAKER)) {
569                                                 finalJsonObj.put(LISTMIRRORMAKER, obj.get(LISTMIRRORMAKER));
570                                                 break;
571                                         }
572                                 }
573
574                                 DMaaPResponseBuilder.respondOk(ctx, finalJsonObj);
575
576                         } else {
577
578                                 JSONObject err = new JSONObject();
579                                 err.append(ERROR, "listMirrorMaker is not available, please make sure MirrorMakerAgent is running");
580                                 DMaaPResponseBuilder.respondOk(ctx, err);
581                         }
582
583                 } catch (Exception e) {
584                         e.printStackTrace();
585                         LOGGER.error("Exception: ", e);                 
586                 }
587         }
588
589         private void sendErrResponse(DMaaPContext ctx, String errMsg) {
590                 JSONObject err = new JSONObject();
591                 err.append(ERROR, errMsg);
592
593                 try {
594                         DMaaPResponseBuilder.respondOk(ctx, err);
595                         LOGGER.error(errMsg);
596
597                 } catch (JSONException | IOException e) {
598                         LOGGER.error(errMsg);
599                 }
600         }
601
602         @SuppressWarnings("unchecked")
603         @POST
604         @Produces("application/json")
605         @Path("/listallwhitelist")
606         public void listWhiteList(InputStream msg) {
607
608                 DMaaPContext ctx = getDmaapContext();
609                 if (checkMirrorMakerPermission(ctx,
610                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERUSER))) {
611
612                         loadProperty();
613                         String input = null;
614
615                         try {
616                                 input = IOUtils.toString(msg, UTF_8);
617
618                                 if (input != null && input.length() > 0) {
619                                         input = removeExtraChar(input);
620                                 }
621
622                                 // Check if it is correct Json object
623                                 JSONObject jsonOb = null;
624
625                                 try {
626                                         jsonOb = new JSONObject(input);
627
628                                 } catch (JSONException ex) {
629
630                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
631                                         LOGGER.error("JSONException: ", ex);
632                                 }
633
634                                 // Check if the request has name and name contains only alpha
635                                 // numeric
636                                 // and check if the request has namespace and namespace contains
637                                 // only alpha numeric
638                                 if (jsonOb != null 
639                                                 && jsonOb.length() == 2 && jsonOb.has("name")
640                                                 && !StringUtils.isBlank(jsonOb.getString("name"))
641                                                 && isAlphaNumeric(jsonOb.getString("name")) && jsonOb.has(NAMESPACE)
642                                                 && !StringUtils.isBlank(jsonOb.getString(NAMESPACE))) {
643
644                                         String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
645                                                         "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString(NAMESPACE) + "|create";
646
647                                         // Check if the user have create permission for the
648                                         // namespace
649                                         if (checkMirrorMakerPermission(ctx, permission)) {
650
651                                                 JSONObject listAll = new JSONObject();
652                                                 JSONObject emptyObject = new JSONObject();
653
654                                                 // Create a listAllMirrorMaker Json object
655                                                 try {
656                                                         listAll.put("listAllMirrorMaker", emptyObject);
657
658                                                 } catch (JSONException e) {
659
660                                                         LOGGER.error("JSONException: ", e);
661                                                 }
662
663                                                 // set a random number as messageID
664                                                 String randomStr = getRandomNum();
665                                                 listAll.put("messageID", randomStr);
666                                                 InputStream inStream = null;
667
668                                                 // convert listAll Json object to InputStream object
669                                                 try {
670                                                         inStream = IOUtils.toInputStream(listAll.toString(), UTF_8);
671
672                                                 } catch (IOException ioe) {
673                                                         ioe.printStackTrace();
674                                                         LOGGER.error("IOException: ", ioe);
675                                                 }
676                                                 // call listAllMirrorMaker
677                                                 mirrorService.pushEvents(ctx, topic, inStream, null, null);
678
679                                                 // subscribe for listMirrorMaker
680                                                 long startTime = System.currentTimeMillis();
681                                                 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
682
683                                                 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
684                                                                 && (System.currentTimeMillis() - startTime) < timeout) {
685                                                         msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
686                                                 }
687
688                                                 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
689                                                                 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
690                                                         
691                                                         JSONArray listMirrorMaker;
692                                                         listMirrorMaker = getListMirrorMaker(msgFrmSubscribe, randomStr);
693
694                                                         String whitelist = null;
695                                                         for (int i = 0; i < listMirrorMaker.length(); i++) {
696
697                                                                 JSONObject mm;
698                                                                 mm = listMirrorMaker.getJSONObject(i);
699                                                                 String name = mm.getString("name");
700
701                                                                 if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
702                                                                         whitelist = mm.getString("whitelist");
703                                                                         break;
704                                                                 }
705                                                         }
706
707                                                         if (!StringUtils.isBlank(whitelist)) {
708
709                                                                 List<String> topicList = new ArrayList<>();
710                                                                 List<String> finalTopicList = new ArrayList<>();
711                                                                 topicList = Arrays.asList(whitelist.split(","));
712
713                                                                 for (String topic : topicList) {
714                                                                         if (topic != null && !topic.equals("null")
715                                                                                         && getNamespace(topic).equals(jsonOb.getString(NAMESPACE))) {
716
717                                                                                 finalTopicList.add(topic);
718                                                                         }
719                                                                 }
720
721                                                                 String topicNames = "";
722
723                                                                 if (!finalTopicList.isEmpty()) {
724                                                                         topicNames = StringUtils.join(finalTopicList, ",");
725                                                                 }
726
727                                                                 JSONObject listAllWhiteList = new JSONObject();
728                                                                 listAllWhiteList.put("name", jsonOb.getString("name"));
729                                                                 listAllWhiteList.put("whitelist", topicNames);
730
731                                                                 DMaaPResponseBuilder.respondOk(ctx, listAllWhiteList);
732                                                         }
733
734                                                 } else {
735
736                                                         JSONObject err = new JSONObject();
737                                                         err.append(ERROR,
738                                                                         "listWhiteList is not available, please make sure MirrorMakerAgent is running");
739                                                         DMaaPResponseBuilder.respondOk(ctx, err);
740                                                 }
741
742                                         } else {
743                                                 sendErrResponse(ctx, NO_USER_CREATE_PERMISSION);
744                                         }
745
746                                 } else {
747
748                                         sendErrResponse(ctx, "This is not a ListAllWhitelist request. Please try again.");
749                                 }
750
751                         } catch (IOException | CambriaApiException | ConfigDbException | AccessDeniedException
752                                         | TopicExistsException | missingReqdSetting | UnavailableException e) {
753
754                                 LOGGER.error("IOException: ", e);
755                         }
756                 } else {
757                         sendErrResponse(ctx, NO_USER_PERMISSION);
758                 }
759         }
760
761         @SuppressWarnings("unchecked")
762         @POST
763         @Produces("application/json")
764         @Path("/createwhitelist")
765         public void createWhiteList(InputStream msg) {
766
767                 DMaaPContext ctx = getDmaapContext();
768                 if (checkMirrorMakerPermission(ctx,
769                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERUSER))) {
770
771                         loadProperty();
772                         String input = null;
773
774                         try {
775                                 input = IOUtils.toString(msg, UTF_8);
776
777                                 if (input != null && input.length() > 0) {
778                                         input = removeExtraChar(input);
779                                 }
780
781                                 // Check if it is correct Json object
782                                 JSONObject jsonOb = null;
783
784                                 try {
785                                         jsonOb = new JSONObject(input);
786
787                                 } catch (JSONException ex) {
788
789                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
790                                         LOGGER.error("JSONException: ", ex);
791                                 }
792
793                                 // Check if the request has name and name contains only alpha numeric,
794                                 // check if the request has namespace and
795                                 // check if the request has whitelistTopicName
796                                 // check if the topic name contains only alpha numeric
797                                 if (jsonOb != null && jsonOb.length() == 3 && jsonOb.has("name")
798                                                 && !StringUtils.isBlank(jsonOb.getString("name")) 
799                                                 && isAlphaNumeric(jsonOb.getString("name")) 
800                                                 && jsonOb.has(NAMESPACE) && !StringUtils.isBlank(jsonOb.getString(NAMESPACE))
801                                                 && jsonOb.has("whitelistTopicName") && !StringUtils.isBlank(jsonOb.getString("whitelistTopicName"))
802                                                 && isAlphaNumeric(jsonOb.getString("whitelistTopicName").substring(jsonOb.getString("whitelistTopicName").lastIndexOf(".")+1, 
803                                                                 jsonOb.getString("whitelistTopicName").length()))) {
804
805                                         String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
806                                                         "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString(NAMESPACE) + "|create";
807
808                                         // Check if the user have create permission for the
809                                         // namespace
810                                         if (checkMirrorMakerPermission(ctx, permission)) {
811
812                                                 JSONObject listAll = new JSONObject();
813                                                 JSONObject emptyObject = new JSONObject();
814
815                                                 // Create a listAllMirrorMaker Json object
816                                                 try {
817                                                         listAll.put("listAllMirrorMaker", emptyObject);
818
819                                                 } catch (JSONException e) {
820
821                                                         LOGGER.error("JSONException: ", e);
822                                                 }
823
824                                                 // set a random number as messageID
825                                                 String randomStr = getRandomNum();
826                                                 listAll.put("messageID", randomStr);
827                                                 InputStream inStream = null;
828
829                                                 // convert listAll Json object to InputStream object
830                                                 try {
831                                                         inStream = IOUtils.toInputStream(listAll.toString(), UTF_8);
832
833                                                 } catch (IOException ioe) {
834                                                         ioe.printStackTrace();
835                                                         LOGGER.error("IOException: ", ioe);
836                                                 }
837                                                 // call listAllMirrorMaker
838                                                 mirrorService.pushEvents(ctx, topic, inStream, null, null);
839
840                                                 // subscribe for listMirrorMaker
841                                                 long startTime = System.currentTimeMillis();
842                                                 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
843
844                                                 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
845                                                                 && (System.currentTimeMillis() - startTime) < timeout) {
846                                                         msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
847                                                 }
848
849                                                 JSONArray listMirrorMaker;
850
851                                                 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
852                                                                 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
853                                                         
854                                                         listMirrorMaker = getListMirrorMaker(msgFrmSubscribe, randomStr);
855                                                         String whitelist = null;
856
857                                                         for (int i = 0; i < listMirrorMaker.length(); i++) {
858                                                                 JSONObject mm;
859                                                                 mm = listMirrorMaker.getJSONObject(i);
860                                                                 String name = mm.getString("name");
861
862                                                                 if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
863                                                                         whitelist = mm.getString("whitelist");
864                                                                         break;
865                                                                 }
866                                                         }
867
868                                                         List<String> topicList = new ArrayList<>();
869                                                         List<String> finalTopicList = new ArrayList<>();
870
871                                                         if (whitelist != null) {
872                                                                 topicList = Arrays.asList(whitelist.split(","));
873                                                         }
874
875                                                         for (String st : topicList) {
876                                                                 if (!StringUtils.isBlank(st)) {
877                                                                         finalTopicList.add(st);
878                                                                 }
879                                                         }
880
881                                                         String newTopic = jsonOb.getString("whitelistTopicName");
882
883                                                         if (!topicList.contains(newTopic)
884                                                                         && getNamespace(newTopic).equals(jsonOb.getString(NAMESPACE))) {
885
886                                                                 UpdateWhiteList updateWhiteList = new UpdateWhiteList();
887                                                                 MirrorMaker mirrorMaker = new MirrorMaker();
888                                                                 mirrorMaker.setName(jsonOb.getString("name"));
889                                                                 finalTopicList.add(newTopic);
890                                                                 String newWhitelist = "";
891
892                                                                 if (!finalTopicList.isEmpty()) {
893                                                                         newWhitelist = StringUtils.join(finalTopicList, ",");
894                                                                 }
895
896                                                                 mirrorMaker.setWhitelist(newWhitelist);
897
898                                                                 String newRandom = getRandomNum();
899                                                                 updateWhiteList.setMessageID(newRandom);
900                                                                 updateWhiteList.setUpdateWhiteList(mirrorMaker);
901
902                                                                 Gson g = new Gson();
903                                                                 g.toJson(updateWhiteList);
904                                                                 InputStream inputStream;
905                                                                 inputStream = IOUtils.toInputStream(g.toJson(updateWhiteList), UTF_8);
906                                                                 // callPubSub(newRandom, ctx, inputStream);
907                                                                 callPubSubForWhitelist(newRandom, ctx, inputStream, jsonOb.getString(NAMESPACE));
908
909                                                         } else if (topicList.contains(newTopic)) {
910                                                                 sendErrResponse(ctx, "The topic already exist.");
911
912                                                         } else if (!getNamespace(newTopic).equals(jsonOb.getString(NAMESPACE))) {
913                                                                 sendErrResponse(ctx,
914                                                                                 "The namespace of the topic does not match with the namespace you provided.");
915                                                         }
916                                                 } else {
917
918                                                         JSONObject err = new JSONObject();
919                                                         err.append(ERROR,
920                                                                         "listWhiteList is not available, please make sure MirrorMakerAgent is running");
921                                                         DMaaPResponseBuilder.respondOk(ctx, err);
922                                                 }
923
924                                         } else {
925                                                 sendErrResponse(ctx, NO_USER_CREATE_PERMISSION);
926                                         }
927
928                                 } else {
929
930                                         sendErrResponse(ctx, "This is not a createWhitelist request. Please try again.");
931                                 }
932
933                         } catch (IOException | CambriaApiException | ConfigDbException | AccessDeniedException
934                                         | TopicExistsException | missingReqdSetting | UnavailableException e) {
935
936                                 LOGGER.error("IOException: ", e);
937                         }
938                 }
939                 // Send error response if user does not provide Authorization
940                 else {
941                         sendErrResponse(ctx, NO_USER_PERMISSION);
942                 }
943         }
944
945         @SuppressWarnings("unchecked")
946         @POST
947         @Produces("application/json")
948         @Path("/deletewhitelist")
949         public void deleteWhiteList(InputStream msg) {
950
951                 DMaaPContext ctx = getDmaapContext();
952                 if (checkMirrorMakerPermission(ctx,
953                                 AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, MIRROR_MAKERUSER))) {
954
955                         loadProperty();
956                         String input = null;
957
958                         try {
959                                 input = IOUtils.toString(msg, UTF_8);
960
961                                 if (input != null && input.length() > 0) {
962                                         input = removeExtraChar(input);
963                                 }
964
965                                 // Check if it is correct Json object
966                                 JSONObject jsonOb = null;
967
968                                 try {
969                                         jsonOb = new JSONObject(input);
970
971                                 } catch (JSONException ex) {
972
973                                         sendErrResponse(ctx, errorMessages.getIncorrectJson());
974                                         LOGGER.error("JSONException: ", ex);
975                                 }
976
977                                 // Check if the request has name and name contains only alpha numeric,
978                                 // check if the request has namespace and
979                                 // check if the request has whitelistTopicName
980                                 if (jsonOb != null && jsonOb.length() == 3 && jsonOb.has("name") && isAlphaNumeric(jsonOb.getString("name"))
981                                                 && jsonOb.has(NAMESPACE) && jsonOb.has("whitelistTopicName") 
982                                                 && isAlphaNumeric(jsonOb.getString("whitelistTopicName").substring(jsonOb.getString("whitelistTopicName").lastIndexOf(".")+1, 
983                                                                 jsonOb.getString("whitelistTopicName").length()))) {
984
985                                         String permission = AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,
986                                                         "msgRtr.mirrormakeruser.aaf.create") + jsonOb.getString(NAMESPACE) + "|create";
987
988                                         // Check if the user have create permission for the
989                                         // namespace
990                                         if (checkMirrorMakerPermission(ctx, permission)) {
991
992                                                 JSONObject listAll = new JSONObject();
993                                                 JSONObject emptyObject = new JSONObject();
994
995                                                 // Create a listAllMirrorMaker Json object
996                                                 try {
997                                                         listAll.put("listAllMirrorMaker", emptyObject);
998
999                                                 } catch (JSONException e) {
1000
1001                                                         LOGGER.error("JSONException: ", e);
1002                                                 }
1003
1004                                                 // set a random number as messageID
1005                                                 String randomStr = getRandomNum();
1006                                                 listAll.put("messageID", randomStr);
1007                                                 InputStream inStream = null;
1008
1009                                                 // convert listAll Json object to InputStream object
1010                                                 try {
1011                                                         inStream = IOUtils.toInputStream(listAll.toString(), UTF_8);
1012
1013                                                 } catch (IOException ioe) {
1014                                                         ioe.printStackTrace();
1015                                                         LOGGER.error("IOException: ", ioe);
1016                                                 }
1017                                                 // call listAllMirrorMaker
1018                                                 mirrorService.pushEvents(ctx, topic, inStream, null, null);
1019
1020                                                 // subscribe for listMirrorMaker
1021                                                 long startTime = System.currentTimeMillis();
1022                                                 String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1023
1024                                                 while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
1025                                                                 && (System.currentTimeMillis() - startTime) < timeout) {
1026                                                         msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1027                                                 }
1028
1029                                                 JSONObject jsonObj;
1030                                                 JSONArray jsonArray;
1031                                                 JSONArray listMirrorMaker = null;
1032
1033                                                 if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
1034                                                                 && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
1035                                                         msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1036                                                         jsonArray = new JSONArray(msgFrmSubscribe);
1037
1038                                                         for (int i = 0; i < jsonArray.length(); i++) {
1039                                                                 jsonObj = jsonArray.getJSONObject(i);
1040                                                                 
1041                                                                 JSONObject obj = new JSONObject();
1042                                                                 if (jsonObj.has(MESSAGE)) {
1043                                                                         obj = jsonObj.getJSONObject(MESSAGE);
1044                                                                 }
1045                                                                 if (obj.has("messageID") && obj.get("messageID").equals(randomStr) && obj.has(LISTMIRRORMAKER)) {
1046                                                                         listMirrorMaker = obj.getJSONArray(LISTMIRRORMAKER);
1047                                                                         break;
1048                                                                 }
1049                                                         }
1050                                                         String whitelist = null;
1051                                                         if (listMirrorMaker != null) {
1052                                                                 for (int i = 0; i < listMirrorMaker.length(); i++) {
1053
1054                                                                         JSONObject mm = new JSONObject();
1055                                                                         mm = listMirrorMaker.getJSONObject(i);
1056                                                                         String name = mm.getString("name");
1057
1058                                                                         if (name.equals(jsonOb.getString("name")) && mm.has("whitelist")) {
1059                                                                                 whitelist = mm.getString("whitelist");
1060                                                                                 break;
1061                                                                         }
1062                                                                 }
1063                                                         }
1064
1065                                                         List<String> topicList = new ArrayList<>();
1066
1067                                                         if (whitelist != null) {
1068                                                                 topicList = Arrays.asList(whitelist.split(","));
1069                                                         }
1070                                                         boolean removeTopic = false;
1071                                                         String topicToRemove = jsonOb.getString("whitelistTopicName");
1072
1073                                                         if (topicList.contains(topicToRemove)) {
1074                                                                 removeTopic = true;
1075                                                         } else {
1076                                                                 sendErrResponse(ctx, "The topic does not exist.");
1077                                                         }
1078
1079
1080                                                         if (removeTopic) {
1081                                                                 UpdateWhiteList updateWhiteList = new UpdateWhiteList();
1082                                                                 MirrorMaker mirrorMaker = new MirrorMaker();
1083                                                                 
1084                                                                 mirrorMaker.setName(jsonOb.getString("name"));
1085                                                                 mirrorMaker.setWhitelist(removeTopic(whitelist, topicToRemove));
1086                                                         
1087                                                                 String newRandom = getRandomNum();
1088                                                                 
1089                                                                 updateWhiteList.setMessageID(newRandom);
1090                                                                 updateWhiteList.setUpdateWhiteList(mirrorMaker);
1091                                                                 
1092                                                                 Gson g = new Gson();
1093                                                                 g.toJson(updateWhiteList);
1094                                                                 
1095                                                                 InputStream inputStream;
1096                                                                 inputStream = IOUtils.toInputStream(g.toJson(updateWhiteList), UTF_8);
1097                                                                 callPubSubForWhitelist(newRandom, ctx, inputStream, getNamespace(topicToRemove));
1098                                                         }
1099
1100                                                 } else {
1101
1102                                                         JSONObject err = new JSONObject();
1103                                                         err.append(ERROR,
1104                                                                         "listWhiteList is not available, please make sure MirrorMakerAgent is running");
1105                                                         DMaaPResponseBuilder.respondOk(ctx, err);
1106                                                 }
1107
1108                                         } else {
1109                                                 sendErrResponse(ctx, NO_USER_CREATE_PERMISSION);
1110                                         }
1111
1112                                 } else {
1113
1114                                         sendErrResponse(ctx, "This is not a DeleteAllWhitelist request. Please try again.");
1115                                 }
1116
1117                         } catch (IOException | CambriaApiException | ConfigDbException | AccessDeniedException
1118                                         | TopicExistsException | missingReqdSetting | UnavailableException e) {
1119
1120                                 LOGGER.error("IOException: ", e);
1121                         }
1122                 }
1123                 // Send error response if user does not provide Authorization
1124                 else {
1125                         sendErrResponse(ctx, NO_USER_PERMISSION);
1126                 }
1127         }
1128
1129         private String getNamespace(String topic) {
1130                 return topic.substring(0, topic.lastIndexOf("."));
1131         }
1132
1133         private String removeTopic(String whitelist, String topicToRemove) {
1134                 List<String> topicList = new ArrayList<>();
1135                 List<String> newTopicList = new ArrayList<>();
1136
1137                 if (whitelist.contains(",")) {
1138                         topicList = Arrays.asList(whitelist.split(","));
1139
1140                 }
1141
1142                 if (topicList.contains(topicToRemove)) {
1143                         for (String topic: topicList) {
1144                                 if (!topic.equals(topicToRemove)) {
1145                                         newTopicList.add(topic);
1146                                 }
1147                         }
1148                 }
1149
1150                 String newWhitelist = StringUtils.join(newTopicList, ",");
1151
1152                 return newWhitelist;
1153         }
1154
1155         private void callPubSubForWhitelist(String randomStr, DMaaPContext ctx, InputStream inStream, String namespace) {
1156                 
1157                 try {
1158                         mirrorService.pushEvents(ctx, topic, inStream, null, null);
1159                         long startTime = System.currentTimeMillis();
1160                         String msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1161
1162                         while (!isListMirrorMaker(msgFrmSubscribe, randomStr)
1163                                         && (System.currentTimeMillis() - startTime) < timeout) {
1164                                 msgFrmSubscribe = mirrorService.subscribe(ctx, topic, consumergroup, consumerid);
1165                         }
1166
1167                         JSONObject jsonObj;
1168                         JSONArray jsonArray;
1169                         JSONArray jsonArrayNamespace = null;
1170
1171                         if (msgFrmSubscribe != null && msgFrmSubscribe.length() > 0
1172                                         && isListMirrorMaker(msgFrmSubscribe, randomStr)) {
1173                                 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1174                                 jsonArray = new JSONArray(msgFrmSubscribe);
1175
1176                                 for (int i = 0; i < jsonArray.length(); i++) {
1177                                         jsonObj = jsonArray.getJSONObject(i);
1178                                         
1179                                         JSONObject obj = new JSONObject();
1180                                         if (jsonObj.has(MESSAGE)) {
1181                                                 obj = jsonObj.getJSONObject(MESSAGE);
1182                                         }
1183                                         if (obj.has("messageID") && obj.get("messageID").equals(randomStr) && obj.has(LISTMIRRORMAKER)) {
1184                                                 jsonArrayNamespace = obj.getJSONArray(LISTMIRRORMAKER);
1185                                         }
1186                                 }
1187                                 JSONObject finalJasonObj = new JSONObject();
1188                                 JSONArray finalJsonArray = new JSONArray();
1189
1190                                 for (int i = 0; i < jsonArrayNamespace.length(); i++) {
1191
1192                                         JSONObject mmObj;
1193                                         mmObj = jsonArrayNamespace.getJSONObject(i);
1194                                         String whitelist;
1195
1196                                         if (mmObj.has("whitelist")) {
1197                                                 whitelist = getWhitelistByNamespace(mmObj.getString("whitelist"), namespace);
1198
1199                                                 if (whitelist != null) {
1200                                                         mmObj.remove("whitelist");
1201                                                         mmObj.put("whitelist", whitelist);
1202                                                 } else {
1203                                                         mmObj.remove("whitelist");
1204                                                 }
1205                                         }
1206                                         finalJsonArray.put(mmObj);
1207                                 }
1208                                 finalJasonObj.put(LISTMIRRORMAKER, finalJsonArray);
1209
1210                                 DMaaPResponseBuilder.respondOk(ctx, finalJasonObj);
1211
1212                         } else {
1213
1214                                 JSONObject err = new JSONObject();
1215                                 err.append(ERROR, "listMirrorMaker is not available, please make sure MirrorMakerAgent is running");
1216                                 DMaaPResponseBuilder.respondOk(ctx, err);
1217                         }
1218
1219                 } catch (Exception e) {
1220                         LOGGER.error("Exception: ", e);
1221                 }
1222         }
1223
1224         private String getWhitelistByNamespace(String originalWhitelist, String namespace) {
1225
1226                 String whitelist = null;
1227                 List<String> resultList = new ArrayList<>();
1228                 List<String> whitelistList = new ArrayList<>();
1229                 whitelistList = Arrays.asList(originalWhitelist.split(","));
1230
1231                 for (String topic : whitelistList) {
1232                         if (StringUtils.isNotBlank(originalWhitelist) && getNamespace(topic).equals(namespace)) {
1233                                 resultList.add(topic);
1234                         }
1235                 }
1236                 if (!resultList.isEmpty()) {
1237                         whitelist = StringUtils.join(resultList, ",");
1238                 }
1239
1240                 return whitelist;
1241         }
1242         
1243         private JSONArray getListMirrorMaker(String msgFrmSubscribe, String randomStr) {
1244                 JSONObject jsonObj;
1245                 JSONArray jsonArray;
1246                 JSONArray listMirrorMaker = new JSONArray();
1247                 
1248                 msgFrmSubscribe = removeExtraChar(msgFrmSubscribe);
1249                 jsonArray = new JSONArray(msgFrmSubscribe);
1250
1251                 for (int i = 0; i < jsonArray.length(); i++) {
1252                         jsonObj = jsonArray.getJSONObject(i);
1253                         
1254                         JSONObject obj = new JSONObject();
1255                         if (jsonObj.has(MESSAGE)) {
1256                                 obj = jsonObj.getJSONObject(MESSAGE);
1257                         }
1258                         if (obj.has("messageID") && obj.get("messageID").equals(randomStr) && obj.has(LISTMIRRORMAKER)) {
1259                                 listMirrorMaker = obj.getJSONArray(LISTMIRRORMAKER);
1260                                 break;
1261                         }
1262                 }
1263                 return listMirrorMaker;         
1264         }
1265 }