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