04c1f2bcd1b5adfeef8cb6904dbe35105ff01e86
[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  * 
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         private void addUsers2jsonArray(JSONArray ja,HashMap activeUsers,String httpSessionId)
69   {
70     List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
71                 for (UserRowBean userRowBean : rows)
72                   ja.put(userRowBean2json(userRowBean,httpSessionId));
73   }
74
75         private JSONObject userRowBean2json(UserRowBean userRowBean,String httpSessionId)
76   {
77     JSONObject jo = new JSONObject();
78     jo.put("id", userRowBean.getId());
79     jo.put("lastName", userRowBean.getLastName());
80     jo.put("email", userRowBean.getEmail());
81     jo.put("lastAccess", userRowBean.getLastAccess());
82     jo.put("remaining", userRowBean.getRemaining());
83     jo.put("sessionId", userRowBean.getSessionId());
84     if (!httpSessionId.equals(userRowBean.getSessionId())) {
85       jo.put("delete", "yes");
86     } else {
87       jo.put("delete", "no");
88     }
89
90     return jo;
91   }
92
93         @SuppressWarnings({ "unchecked", "rawtypes" })
94         @RequestMapping(value = { "/usage_list" }, method = RequestMethod.GET)
95         public ModelAndView usageList(HttpServletRequest request) {
96                 Map<String, Object> model = new HashMap<>();
97
98                 HttpSession httpSession = request.getSession();
99                 HashMap activeUsers = (HashMap) httpSession.getServletContext().getAttribute("activeUsers");
100                 if (activeUsers.size() == 0) {
101                         activeUsers.put(httpSession.getId(), httpSession);
102                         httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
103                 }
104
105                 JSONArray ja = new JSONArray();
106                 try {
107       addUsers2jsonArray(ja,activeUsers,httpSession.getId());
108                 } catch (Exception e) {
109                         logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
110                 }
111
112                 model.put("model", ja);
113
114                 return new ModelAndView(getViewName(), model);
115         }
116
117         @SuppressWarnings({ "unchecked", "rawtypes" })
118         @RequestMapping(value = { "/get_usage_list" }, method = RequestMethod.GET)
119         public void getUsageList(HttpServletRequest request, HttpServletResponse response) {
120
121                 HttpSession httpSession = request.getSession();
122                 HashMap activeUsers = (HashMap) httpSession.getServletContext().getAttribute("activeUsers");
123                 if (activeUsers.size() == 0) {
124                         activeUsers.put(httpSession.getId(), httpSession);
125                         httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
126                 }
127                 JSONArray ja = new JSONArray();
128                 try {
129       addUsers2jsonArray(ja,activeUsers,httpSession.getId());
130                 } catch (Exception e) {
131                         logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
132                 }
133                 JsonMessage msg;
134                 try {
135                         msg = new JsonMessage(ja.toString());
136                         JSONObject j = new JSONObject(msg);
137                         response.getWriter().write(j.toString());
138                 } catch (Exception e) {
139                         logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed to serialize", e);
140                 }
141
142         }
143
144         @SuppressWarnings("rawtypes")
145         @RequestMapping(value = { "/usage_list/removeSession" }, method = RequestMethod.GET)
146         public void removeSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
147                 HashMap activeUsers = (HashMap) request.getSession().getServletContext().getAttribute("activeUsers");
148                 UserRowBean data = new UserRowBean();
149                 data.setSessionId(request.getParameter("deleteSessionId"));
150                 UsageUtils.getActiveUsersAfterDelete(activeUsers, data);
151
152                 HttpSession httpSession = request.getSession();
153                 JSONArray ja = new JSONArray();
154                 try {
155       addUsers2jsonArray(ja,activeUsers,httpSession.getId());
156                 } catch (Exception e) {
157                         logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed", e);
158                 }
159
160                 response.setContentType("application/json");
161                 PrintWriter out = response.getWriter();
162                 out.write(ja.toString());
163         }
164
165 }