df01728820422f33e3402155b4b8a5a02dc89e81
[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.IOException;
41 import java.io.PrintWriter;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.http.HttpSession;
49
50 import org.json.JSONArray;
51 import org.json.JSONObject;
52 import org.onap.portalsdk.core.command.UserRowBean;
53 import org.onap.portalsdk.core.controller.RestrictedBaseController;
54 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
55 import org.onap.portalsdk.core.util.UsageUtils;
56 import org.onap.portalsdk.core.web.support.JsonMessage;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RequestMethod;
60 import org.springframework.web.servlet.ModelAndView;
61
62 @Controller
63 @RequestMapping("/")
64 public class UsageListController extends RestrictedBaseController {
65
66         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UsageListController.class);
67
68         @SuppressWarnings({ "unchecked", "rawtypes" })
69         @RequestMapping(value = { "/usage_list" }, method = RequestMethod.GET)
70         public ModelAndView usageList(HttpServletRequest request) {
71                 Map<String, Object> model = new HashMap<>();
72
73                 HttpSession httpSession = request.getSession();
74                 HashMap activeUsers = (HashMap) httpSession.getServletContext().getAttribute("activeUsers");
75                 if (activeUsers.size() == 0) {
76                         activeUsers.put(httpSession.getId(), httpSession);
77                         httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
78                 }
79                 List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
80                 JSONArray ja = new JSONArray();
81                 try {
82                         for (UserRowBean userRowBean : rows) {
83                                 JSONObject jo = new JSONObject();
84                                 jo.put("id", userRowBean.getId());
85                                 jo.put("lastName", userRowBean.getLastName());
86                                 jo.put("email", userRowBean.getEmail());
87                                 jo.put("lastAccess", userRowBean.getLastAccess());
88                                 jo.put("remaining", userRowBean.getRemaining());
89                                 jo.put("sessionId", userRowBean.getSessionId());
90                                 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
91                                         jo.put("delete", "yes");
92                                 } else {
93                                         jo.put("delete", "no");
94                                 }
95                                 ja.put(jo);
96                         }
97                 } catch (Exception e) {
98                         logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
99                 }
100
101                 model.put("model", ja);
102
103                 return new ModelAndView(getViewName(), model);
104         }
105
106         @SuppressWarnings({ "unchecked", "rawtypes" })
107         @RequestMapping(value = { "/get_usage_list" }, method = RequestMethod.GET)
108         public void getUsageList(HttpServletRequest request, HttpServletResponse response) {
109
110                 HttpSession httpSession = request.getSession();
111                 HashMap activeUsers = (HashMap) httpSession.getServletContext().getAttribute("activeUsers");
112                 if (activeUsers.size() == 0) {
113                         activeUsers.put(httpSession.getId(), httpSession);
114                         httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
115                 }
116                 List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
117                 JSONArray ja = new JSONArray();
118                 try {
119                         for (UserRowBean userRowBean : rows) {
120                                 JSONObject jo = new JSONObject();
121                                 jo.put("id", userRowBean.getId());
122                                 jo.put("lastName", userRowBean.getLastName());
123                                 jo.put("email", userRowBean.getEmail());
124                                 jo.put("lastAccess", userRowBean.getLastAccess());
125                                 jo.put("remaining", userRowBean.getRemaining());
126                                 jo.put("sessionId", userRowBean.getSessionId());
127                                 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
128                                         jo.put("delete", "yes");
129                                 } else {
130                                         jo.put("delete", "no");
131                                 }
132                                 ja.put(jo);
133                         }
134                 } catch (Exception e) {
135                         logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
136                 }
137                 JsonMessage msg;
138                 try {
139                         msg = new JsonMessage(ja.toString());
140                         JSONObject j = new JSONObject(msg);
141                         response.getWriter().write(j.toString());
142                 } catch (Exception e) {
143                         logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed to serialize", e);
144                 }
145
146         }
147
148         @SuppressWarnings("rawtypes")
149         @RequestMapping(value = { "/usage_list/removeSession" }, method = RequestMethod.GET)
150         public void removeSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
151                 HashMap activeUsers = (HashMap) request.getSession().getServletContext().getAttribute("activeUsers");
152                 UserRowBean data = new UserRowBean();
153                 data.setSessionId(request.getParameter("deleteSessionId"));
154                 UsageUtils.getActiveUsersAfterDelete(activeUsers, data);
155
156                 HttpSession httpSession = request.getSession();
157                 List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
158                 JSONArray ja = new JSONArray();
159                 try {
160                         for (UserRowBean userRowBean : rows) {
161                                 JSONObject jo = new JSONObject();
162                                 jo.put("id", userRowBean.getId());
163                                 jo.put("lastName", userRowBean.getLastName());
164                                 jo.put("email", userRowBean.getEmail());
165                                 jo.put("lastAccess", userRowBean.getLastAccess());
166                                 jo.put("remaining", userRowBean.getRemaining());
167                                 jo.put("sessionId", userRowBean.getSessionId());
168                                 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
169                                         jo.put("delete", "yes");
170                                 } else {
171                                         jo.put("delete", "no");
172                                 }
173                                 ja.put(jo);
174                         }
175                 } catch (Exception e) {
176                         logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed", e);
177                 }
178
179                 response.setContentType("application/json");
180                 PrintWriter out = response.getWriter();
181                 out.write(ja.toString());
182         }
183
184 }