13a59f12c9ae3df0bcbeefdea2987f25f9b94f32
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the “License”);
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller.core;
39
40 import java.io.PrintWriter;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.Map;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47 import javax.servlet.http.HttpSession;
48
49 import org.json.JSONArray;
50 import org.json.JSONObject;
51 import org.onap.portalsdk.core.command.UserRowBean;
52 import org.onap.portalsdk.core.controller.RestrictedBaseController;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.core.util.UsageUtils;
55 import org.onap.portalsdk.core.web.support.JsonMessage;
56 import org.springframework.stereotype.Controller;
57 import org.springframework.web.bind.annotation.RequestMapping;
58 import org.springframework.web.bind.annotation.RequestMethod;
59 import org.springframework.web.servlet.ModelAndView;
60
61 @Controller
62 @RequestMapping("/")
63 public class UsageListController extends RestrictedBaseController {
64
65         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UsageListController.class);
66
67         @SuppressWarnings({ "unchecked", "rawtypes" })
68         @RequestMapping(value = { "/usage_list" }, method = RequestMethod.GET)
69         public ModelAndView usageList(HttpServletRequest request) {
70                 Map<String, Object> model = new HashMap<String, Object>();
71
72                 HttpSession httpSession = request.getSession();
73                 HashMap activeUsers = (HashMap) httpSession.getServletContext().getAttribute("activeUsers");
74                 if (activeUsers.size() == 0) {
75                         activeUsers.put(httpSession.getId(), httpSession);
76                         httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
77                 }
78                 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
79                 JSONArray ja = new JSONArray();
80                 try {
81                         for (UserRowBean userRowBean : rows) {
82                                 JSONObject jo = new JSONObject();
83                                 jo.put("id", userRowBean.getId());
84                                 jo.put("lastName", userRowBean.getLastName());
85                                 jo.put("email", userRowBean.getEmail());
86                                 jo.put("lastAccess", userRowBean.getLastAccess());
87                                 jo.put("remaining", userRowBean.getRemaining());
88                                 jo.put("sessionId", userRowBean.getSessionId());
89                                 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
90                                         jo.put("delete", "yes");
91                                 } else {
92                                         jo.put("delete", "no");
93                                 }
94                                 ja.put(jo);
95                         }
96                 } catch (Exception e) {
97                         logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
98                 }
99
100                 model.put("model", ja);
101
102                 return new ModelAndView(getViewName(), model);
103         }
104
105         @SuppressWarnings({ "unchecked", "rawtypes" })
106         @RequestMapping(value = { "/get_usage_list" }, method = RequestMethod.GET)
107         public void getUsageList(HttpServletRequest request, HttpServletResponse response) {
108
109                 HttpSession httpSession = request.getSession();
110                 HashMap activeUsers = (HashMap) httpSession.getServletContext().getAttribute("activeUsers");
111                 if (activeUsers.size() == 0) {
112                         activeUsers.put(httpSession.getId(), httpSession);
113                         httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
114                 }
115                 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
116                 JSONArray ja = new JSONArray();
117                 try {
118                         for (UserRowBean userRowBean : rows) {
119                                 JSONObject jo = new JSONObject();
120                                 jo.put("id", userRowBean.getId());
121                                 jo.put("lastName", userRowBean.getLastName());
122                                 jo.put("email", userRowBean.getEmail());
123                                 jo.put("lastAccess", userRowBean.getLastAccess());
124                                 jo.put("remaining", userRowBean.getRemaining());
125                                 jo.put("sessionId", userRowBean.getSessionId());
126                                 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
127                                         jo.put("delete", "yes");
128                                 } else {
129                                         jo.put("delete", "no");
130                                 }
131                                 ja.put(jo);
132                         }
133                 } catch (Exception e) {
134                         logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
135                 }
136                 JsonMessage msg;
137                 try {
138                         msg = new JsonMessage(ja.toString());
139                         JSONObject j = new JSONObject(msg);
140                         response.getWriter().write(j.toString());
141                 } catch (Exception e) {
142                         logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed to serialize", e);
143                 }
144
145         }
146
147         @SuppressWarnings("rawtypes")
148         @RequestMapping(value = { "/usage_list/removeSession" }, method = RequestMethod.GET)
149         public void removeSession(HttpServletRequest request, HttpServletResponse response) throws Exception {
150                 HashMap activeUsers = (HashMap) request.getSession().getServletContext().getAttribute("activeUsers");
151                 UserRowBean data = new UserRowBean();
152                 data.setSessionId(request.getParameter("deleteSessionId"));
153                 UsageUtils.getActiveUsersAfterDelete(activeUsers, data);
154
155                 HttpSession httpSession = request.getSession();
156                 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
157                 JSONArray ja = new JSONArray();
158                 try {
159                         for (UserRowBean userRowBean : rows) {
160                                 JSONObject jo = new JSONObject();
161                                 jo.put("id", userRowBean.getId());
162                                 jo.put("lastName", userRowBean.getLastName());
163                                 jo.put("email", userRowBean.getEmail());
164                                 jo.put("lastAccess", userRowBean.getLastAccess());
165                                 jo.put("remaining", userRowBean.getRemaining());
166                                 jo.put("sessionId", userRowBean.getSessionId());
167                                 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
168                                         jo.put("delete", "yes");
169                                 } else {
170                                         jo.put("delete", "no");
171                                 }
172                                 ja.put(jo);
173                         }
174                 } catch (Exception e) {
175                         logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed", e);
176                 }
177
178                 response.setContentType("application/json");
179                 PrintWriter out = response.getWriter();
180                 out.write(ja.toString());
181         }
182
183 }