Java tutorial
/* * Copyright(C) 2014 * NEC Corporation All rights reserved. * * No permission to use, copy, modify and distribute this software * and its documentation for any purpose is granted. * This software is provided under applicable license agreement only. */ package com.nec.harvest.userdetails; import java.util.Calendar; import java.util.Date; import javax.servlet.http.HttpServletRequest; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.springframework.security.core.userdetails.User; import com.nec.core.container.ContextAwareContainer; import com.nec.harvest.cache.CacheService; import com.nec.harvest.constant.Constants; import com.nec.harvest.model.Organization; import com.nec.harvest.service.UserService; /** * This class provides the logged-in user informations * * @author <a href="mailto:sondn@nec.vn">Ngoc Son Dang</a> * @version AuthenticatedUserDetails.java * @since 2014/05/16 * */ public class AuthenticatedUserDetails extends AuthenticationInfo { private static CacheService<String, com.nec.harvest.model.User> cacheService; public AuthenticatedUserDetails(CacheService<String, com.nec.harvest.model.User> authCacheService) { cacheService = authCacheService; } /** * Get the user name of the logged in user * * @return the user name of the user */ public static String getUsername() { final Object principal = ContextAwareContainer.getInstance().getPrincipal(); if (principal != null && principal instanceof User) { return ((User) principal).getUsername(); } else { return null; } } /** * Find all the information of logged-in user * * @return */ public static com.nec.harvest.model.User getUserPrincipal() { com.nec.harvest.model.User userDetail = null; // UserPrincipal's name String username = getUsername(); if (StringUtils.isNotEmpty(username)) { userDetail = cacheService.get(username); if (userDetail == null) { userDetail = ContextAwareContainer.getInstance().getComponent(UserService.class) .findByUsrCode(username); // cacheService.put(username, userDetail); } } return userDetail; } public static void removeUserPrincipal() { final Object principal = ContextAwareContainer.getInstance().getPrincipal(); if (principal != null && principal instanceof User) { String username = ((User) principal).getUsername(); if (StringUtils.isNotEmpty(username)) { cacheService.remove(username); } } } /** * Get organization for the logged-in user * * @return */ public static Organization getOrganization() { final com.nec.harvest.model.User userPrincipal = getUserPrincipal(); if (userPrincipal == null) { return null; } return userPrincipal.getOrganization(); } /** * Get last time user logged on system * * @return A date time string */ @Override public Calendar getLastLogIn() { final HttpServletRequest request = ContextAwareContainer.getInstance().getRequest(); Calendar lastTime = (Calendar) request.getSession().getAttribute(Constants.USER_LOGGED_IN_LASTTIME); if (lastTime == null) { lastTime = Calendar.getInstance(); lastTime.setTime(new Date()); } return lastTime; } /** * Get user's avatar encode into base64 string * * @return The base64 string */ @Override public String getUserAvatarBase64() { final com.nec.harvest.model.User user = getUserPrincipal(); if (user != null) { byte[] bytes = user.getAvatarUrl(); if (bytes != null && bytes.length > 0) { // Encodes the byte array into base64 string return Base64.encodeBase64String(bytes); } } return null; } }