2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.controller.core;
40 import java.io.IOException;
41 import java.io.PrintWriter;
42 import java.util.HashMap;
43 import java.util.List;
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.http.HttpSession;
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;
64 public class UsageListController extends RestrictedBaseController {
66 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UsageListController.class);
68 private void addUsers2jsonArray(JSONArray ja,HashMap activeUsers,String httpSessionId)
70 List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
71 for (UserRowBean userRowBean : rows)
72 ja.put(userRowBean2json(userRowBean,httpSessionId));
75 private JSONObject userRowBean2json(UserRowBean userRowBean,String httpSessionId)
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");
87 jo.put("delete", "no");
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<>();
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);
105 JSONArray ja = new JSONArray();
107 addUsers2jsonArray(ja,activeUsers,httpSession.getId());
108 } catch (Exception e) {
109 logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
112 model.put("model", ja);
114 return new ModelAndView(getViewName(), model);
117 @SuppressWarnings({ "unchecked", "rawtypes" })
118 @RequestMapping(value = { "/get_usage_list" }, method = RequestMethod.GET)
119 public void getUsageList(HttpServletRequest request, HttpServletResponse response) {
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);
127 JSONArray ja = new JSONArray();
129 addUsers2jsonArray(ja,activeUsers,httpSession.getId());
130 } catch (Exception e) {
131 logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
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);
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);
152 HttpSession httpSession = request.getSession();
153 JSONArray ja = new JSONArray();
155 addUsers2jsonArray(ja,activeUsers,httpSession.getId());
156 } catch (Exception e) {
157 logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed", e);
160 response.setContentType("application/json");
161 PrintWriter out = response.getWriter();
162 out.write(ja.toString());