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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalapp.controller.core;
40 import java.io.PrintWriter;
41 import java.util.ArrayList;
42 import java.util.HashMap;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47 import javax.servlet.http.HttpSession;
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;
63 public class UsageListController extends RestrictedBaseController {
65 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UsageListController.class);
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>();
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);
78 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
79 JSONArray ja = new JSONArray();
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");
92 jo.put("delete", "no");
96 } catch (Exception e) {
97 logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
100 model.put("model", ja);
102 return new ModelAndView(getViewName(), model);
105 @SuppressWarnings({ "unchecked", "rawtypes" })
106 @RequestMapping(value = { "/get_usage_list" }, method = RequestMethod.GET)
107 public void getUsageList(HttpServletRequest request, HttpServletResponse response) {
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);
115 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
116 JSONArray ja = new JSONArray();
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");
129 jo.put("delete", "no");
133 } catch (Exception e) {
134 logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
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);
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);
155 HttpSession httpSession = request.getSession();
156 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
157 JSONArray ja = new JSONArray();
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");
170 jo.put("delete", "no");
174 } catch (Exception e) {
175 logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed", e);
178 response.setContentType("application/json");
179 PrintWriter out = response.getWriter();
180 out.write(ja.toString());