|
| 1 | +/** |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 3 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 4 | + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under |
| 5 | + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. |
| 6 | + * |
| 7 | + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS |
| 8 | + * graphic logo is a trademark of OpenMRS Inc. |
| 9 | + */ |
| 10 | + |
| 11 | +package org.openmrs.web.filter.util; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.TreeMap; |
| 18 | + |
| 19 | +import javax.servlet.ServletContext; |
| 20 | +import javax.servlet.http.HttpSession; |
| 21 | + |
| 22 | +import org.apache.commons.lang.StringUtils; |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | +import org.openmrs.User; |
| 26 | +import org.openmrs.web.WebConstants; |
| 27 | + |
| 28 | +public class CurrentUsers { |
| 29 | + |
| 30 | + private static final Log log = LogFactory.getLog(CurrentUsers.class); |
| 31 | + |
| 32 | + /** |
| 33 | + * Initialize the current users list. |
| 34 | + * |
| 35 | + * @param servletContext |
| 36 | + */ |
| 37 | + |
| 38 | + public static Map<String,String> init(ServletContext servletContext){ |
| 39 | + |
| 40 | + Map<String,String> currentUserMap = Collections.synchronizedMap(new TreeMap<String,String>()); |
| 41 | + servletContext.setAttribute(WebConstants.CURRENT_USERS,currentUserMap); |
| 42 | + return currentUserMap; |
| 43 | + |
| 44 | + } |
| 45 | + /** |
| 46 | + * Get the current list of map of users stored in the session |
| 47 | + * |
| 48 | + * @param httpSession the current session |
| 49 | + * @return map of users logged in |
| 50 | + */ |
| 51 | + @SuppressWarnings("unchecked") |
| 52 | + private static Map<String,String> getCurrentUsers(HttpSession httpSession){ |
| 53 | + Map<String,String> currentUsers = (Map<String,String>) httpSession.getServletContext().getAttribute(WebConstants.CURRENT_USERS); |
| 54 | + |
| 55 | + if(currentUsers == null) { |
| 56 | + currentUsers = init(httpSession.getServletContext()); |
| 57 | + } |
| 58 | + |
| 59 | + return currentUsers; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Add the user to the current users. |
| 64 | + * |
| 65 | + * @param httpSession |
| 66 | + * @param user the user that just logged in |
| 67 | + */ |
| 68 | + public static void addUser(HttpSession httpSession,User user) { |
| 69 | + Map<String,String> currentUsers = getCurrentUsers(httpSession); |
| 70 | + |
| 71 | + String currentUserName = user.getUsername(); |
| 72 | + //if the user name is blank then print their system id |
| 73 | + if(StringUtils.isBlank(currentUserName)) { |
| 74 | + currentUserName = "systemid:" + user.getSystemId(); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + if(log.isDebugEnabled()) { |
| 79 | + |
| 80 | + log.debug("Adding current user name" + currentUserName); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + currentUsers.put(httpSession.getId(),currentUserName); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get sorted user names list. |
| 90 | + * |
| 91 | + * @param httpSession |
| 92 | + * @return sorted user names |
| 93 | + */ |
| 94 | + public static List<String> getCurrentUsernames(HttpSession httpSession){ |
| 95 | + Map<String,String> currentUsers = getCurrentUsers(httpSession); |
| 96 | + List<String> userNames = new ArrayList<String>(); |
| 97 | + synchronized(currentUsers) { |
| 98 | + for(String value:currentUsers.values()) { |
| 99 | + userNames.add(value); |
| 100 | + } |
| 101 | + Collections.sort(userNames); |
| 102 | + |
| 103 | + } |
| 104 | + return userNames; |
| 105 | + } |
| 106 | + |
| 107 | +} |
0 commit comments