d7dfbaa504f750e22ebccac50f376024ded0b4c8
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.controller.core;
21
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSession;
30
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;
42
43 @Controller
44 @RequestMapping("/")
45 public class UsageListController extends RestrictedBaseController {
46
47         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UsageListController.class);
48
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>();
53
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);
59                 }
60                 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
61                 JSONArray ja = new JSONArray();
62                 try {
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");
73                                 } else {
74                                         jo.put("delete", "no");
75                                 }
76                                 ja.put(jo);
77                         }
78                 } catch (Exception e) {
79                         logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
80                 }
81
82                 model.put("model", ja);
83
84                 return new ModelAndView(getViewName(), model);
85         }
86
87         @SuppressWarnings({ "unchecked", "rawtypes" })
88         @RequestMapping(value = { "/get_usage_list" }, method = RequestMethod.GET)
89         public void getUsageList(HttpServletRequest request, HttpServletResponse response) {
90
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);
96                 }
97                 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
98                 JSONArray ja = new JSONArray();
99                 try {
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");
110                                 } else {
111                                         jo.put("delete", "no");
112                                 }
113                                 ja.put(jo);
114                         }
115                 } catch (Exception e) {
116                         logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
117                 }
118                 JsonMessage msg;
119                 try {
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);
125                 }
126
127         }
128
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);
136
137                 HttpSession httpSession = request.getSession();
138                 ArrayList<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
139                 JSONArray ja = new JSONArray();
140                 try {
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");
151                                 } else {
152                                         jo.put("delete", "no");
153                                 }
154                                 ja.put(jo);
155                         }
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 }