fabc06bf9e078fbda1993a03a1eb73d1dafa68da
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.controller.core;
39
40 import java.io.IOException;
41 import java.io.PrintWriter;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48 import javax.servlet.http.HttpSession;
49
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;
61
62 @Controller
63 @RequestMapping("/")
64 public class UsageListController extends RestrictedBaseController {
65
66         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UsageListController.class);
67
68         private static final String ACTIVE_USERS = "activeUsers";
69
70         private void addUsers2jsonArray(JSONArray ja,HashMap activeUsers,String httpSessionId)
71   {
72     List<UserRowBean> rows = UsageUtils.getActiveUsers(activeUsers);
73                 for (UserRowBean userRowBean : rows)
74                   ja.put(userRowBean2json(userRowBean,httpSessionId));
75   }
76
77         private JSONObject userRowBean2json(UserRowBean userRowBean,String httpSessionId)
78   {
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");
88     } else {
89       jo.put("delete", "no");
90     }
91
92     return jo;
93   }
94
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<>();
99
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);
105                 }
106
107                 JSONArray ja = new JSONArray();
108                 try {
109       addUsers2jsonArray(ja,activeUsers,httpSession.getId());
110                 } catch (Exception e) {
111                         logger.error(EELFLoggerDelegate.errorLogger, "usageList failed", e);
112                 }
113
114                 model.put("model", ja);
115
116                 return new ModelAndView(getViewName(), model);
117         }
118
119         @SuppressWarnings({ "unchecked", "rawtypes" })
120         @RequestMapping(value = { "/get_usage_list" }, method = RequestMethod.GET)
121         public void getUsageList(HttpServletRequest request, HttpServletResponse response) {
122
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);
128                 }
129                 JSONArray ja = new JSONArray();
130                 try {
131       addUsers2jsonArray(ja,activeUsers,httpSession.getId());
132                 } catch (Exception e) {
133                         logger.error(EELFLoggerDelegate.errorLogger, "getUsageList failed", e);
134                 }
135                 JsonMessage msg;
136                 try {
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);
142                 }
143
144         }
145
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);
153
154                 HttpSession httpSession = request.getSession();
155                 JSONArray ja = new JSONArray();
156                 try {
157       addUsers2jsonArray(ja,activeUsers,httpSession.getId());
158                 } catch (Exception e) {
159                         logger.error(EELFLoggerDelegate.errorLogger, "removeSession failed", e);
160                 }
161
162                 response.setContentType("application/json");
163                 PrintWriter out = response.getWriter();
164                 out.write(ja.toString());
165         }
166
167 }