2 * ================================================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ================================================================================
20 package org.openecomp.portalapp.controller.core;
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 import java.util.HashMap;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSession;
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33 import org.openecomp.portalsdk.core.command.UserRowBean;
34 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.openecomp.portalsdk.core.util.UsageUtils;
37 import org.openecomp.portalsdk.core.web.support.JsonMessage;
38 import org.springframework.stereotype.Controller;
39 import org.springframework.web.bind.annotation.RequestMapping;
40 import org.springframework.web.bind.annotation.RequestMethod;
41 import org.springframework.web.servlet.ModelAndView;
45 public class UsageListController extends RestrictedBaseController {
47 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UsageListController.class);
49 @SuppressWarnings({ "unchecked", "rawtypes" })
50 @RequestMapping(value = { "/usage_list" }, method = RequestMethod.GET)
51 public ModelAndView usageList(HttpServletRequest request) {
52 Map<String, Object> model = new HashMap<String, Object>();
54 HttpSession httpSession = request.getSession();
55 HashMap activeUsers = (HashMap) httpSession.getServletContext().getAttribute("activeUsers");
56 if (activeUsers.size() == 0) {
57 activeUsers.put(httpSession.getId(), httpSession);
58 httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
60 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
61 JSONArray ja = new JSONArray();
63 for (UserRowBean userRowBean : rows) {
64 JSONObject jo = new JSONObject();
65 jo.put("id", userRowBean.getId());
66 jo.put("lastName", userRowBean.getLastName());
67 jo.put("email", userRowBean.getEmail());
68 jo.put("lastAccess", userRowBean.getLastAccess());
69 jo.put("remaining", userRowBean.getRemaining());
70 jo.put("sessionId", userRowBean.getSessionId());
71 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
72 jo.put("delete", "yes");
74 jo.put("delete", "no");
78 } catch (Exception e) {
79 logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
82 model.put("model", ja);
84 return new ModelAndView(getViewName(), model);
87 @SuppressWarnings({ "unchecked", "rawtypes" })
88 @RequestMapping(value = { "/get_usage_list" }, method = RequestMethod.GET)
89 public void getUsageList(HttpServletRequest request, HttpServletResponse response) {
91 HttpSession httpSession = request.getSession();
92 HashMap activeUsers = (HashMap) httpSession.getServletContext().getAttribute("activeUsers");
93 if (activeUsers.size() == 0) {
94 activeUsers.put(httpSession.getId(), httpSession);
95 httpSession.getServletContext().setAttribute("activeUsers", activeUsers);
97 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
98 JSONArray ja = new JSONArray();
100 for (UserRowBean userRowBean : rows) {
101 JSONObject jo = new JSONObject();
102 jo.put("id", userRowBean.getId());
103 jo.put("lastName", userRowBean.getLastName());
104 jo.put("email", userRowBean.getEmail());
105 jo.put("lastAccess", userRowBean.getLastAccess());
106 jo.put("remaining", userRowBean.getRemaining());
107 jo.put("sessionId", userRowBean.getSessionId());
108 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
109 jo.put("delete", "yes");
111 jo.put("delete", "no");
115 } catch (Exception e) {
116 logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
120 msg = new JsonMessage(ja.toString());
121 JSONObject j = new JSONObject(msg);
122 response.getWriter().write(j.toString());
123 } catch (Exception e) {
124 logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed to serialize", e);
129 @SuppressWarnings("rawtypes")
130 @RequestMapping(value = { "/usage_list/removeSession" }, method = RequestMethod.GET)
131 public void removeSession(HttpServletRequest request, HttpServletResponse response) throws Exception {
132 HashMap activeUsers = (HashMap) request.getSession().getServletContext().getAttribute("activeUsers");
133 UserRowBean data = new UserRowBean();
134 data.setSessionId(request.getParameter("deleteSessionId"));
135 UsageUtils.getActiveUsersAfterDelete(activeUsers, data);
137 HttpSession httpSession = request.getSession();
138 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
139 JSONArray ja = new JSONArray();
141 for (UserRowBean userRowBean : rows) {
142 JSONObject jo = new JSONObject();
143 jo.put("id", userRowBean.getId());
144 jo.put("lastName", userRowBean.getLastName());
145 jo.put("email", userRowBean.getEmail());
146 jo.put("lastAccess", userRowBean.getLastAccess());
147 jo.put("remaining", userRowBean.getRemaining());
148 jo.put("sessionId", userRowBean.getSessionId());
149 if (!(httpSession.getId().equals(userRowBean.getSessionId()))) {
150 jo.put("delete", "yes");
152 jo.put("delete", "no");
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());