0939ea04fce1927516878bdf5bf4ba4f05938dba
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / websocketmanager / impl / src / main / java / org / opendaylight / mwtn / impl / dto / UserDto.java
1 /*
2 * Copyright (c) 2016 Wipro Ltd. and others. All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 */
8
9 package org.opendaylight.mwtn.impl.dto;
10
11 import java.util.HashMap;
12
13 import org.json.JSONArray;
14 import org.json.JSONException;
15 import org.json.JSONObject;
16 import org.opendaylight.mwtn.impl.utils.Utils;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class UserDto {
21     private static final Logger LOG = LoggerFactory.getLogger(UserDto.class);
22
23         private String userId;
24         private HashMap<String, Boolean> hmScopes;
25
26         public String getUserId() {
27                 return userId;
28         }
29
30         public void setUserId(String sessionId) {
31                 this.userId = sessionId;
32         }
33
34         public HashMap<String, Boolean> getScopes() {
35                 return hmScopes;
36         }
37
38         public void setScopes(JSONArray jsonScopes) {
39                 hmScopes = new HashMap<>();
40                 for (int i = 0; jsonScopes != null && i < jsonScopes.length(); i++) {
41                         try {
42                                 hmScopes.put(jsonScopes.getString(i), true);
43                         } catch (JSONException e) {
44                                 LOG.warn("Something wrong: {}", e);
45                         }
46                 }
47         }
48
49         public JSONObject getScopedResponse(JSONObject jsonAllValue) {
50                 JSONObject jsonResponse = new JSONObject();
51                 try {
52                         JSONArray jsonArrayRes = new JSONArray();
53                         JSONArray jsonArray = jsonAllValue.getJSONArray(Utils.MSG_KEY_DATA);
54                         for (int i = 0; jsonArray != null && i < jsonArray.length(); i++) {
55                                 JSONObject jsonObject = jsonArray.getJSONObject(i);
56                                 String msgScope = jsonObject.getString(Utils.MSG_KEY_SCOPE);
57                                 if (getScopes().get(msgScope) != null && getScopes().get(msgScope)) {
58                                         jsonArrayRes.put(jsonObject);
59                                 }
60                         }
61                         if (jsonArrayRes.length() == 0) {
62                                 return null;
63                         }
64                         jsonResponse.put(Utils.MSG_KEY_DATA, jsonArrayRes);
65                 } catch (Exception e) {
66                         LOG.warn("Something wrong: {}", e);
67                 }
68                 return jsonResponse;
69         }
70 }