Rev 1511 | Rev 1658 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.interceptors;import java.util.HashMap;import java.util.Map;import in.shop2020.serving.services.UserSessionInfo;import in.shop2020.serving.utils.DesEncrypter;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import nl.bitwalker.useragentutils.BrowserType;import nl.bitwalker.useragentutils.UserAgent;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class UserInterceptor extends AbstractInterceptor {private static final int SECONDS_IN_YEAR = 60*60*24*365;private static final long serialVersionUID = -4125815700236506235L;private static Logger log = Logger.getLogger(UserInterceptor.class);public static final String USER_INFO = "userinfo";private Map<String, Cookie> cookiesMap = null;private Cookie userCookie = null;private DesEncrypter desEncrypter = new DesEncrypter("shop2020");@Overridepublic String intercept(ActionInvocation invocation) throws Exception {final Object action = invocation.getAction();HttpServletRequest request = ServletActionContext.getRequest();UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));log.info("User browser is:" + userAgent.getBrowser() + " OS is:" + userAgent.getOperatingSystem());HttpSession session = request.getSession(); // Get the existing session or create a new one//getCookiesMap(request);createCookiesMap(request);// If the request is for an active session.UserSessionInfo userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);// Set the userinfo and the uid cookie if they're not already set.if (userInfo == null) {/* Code for bitwalked. No more used.if(userAgent.getBrowser().getBrowserType() == BrowserType.WEB_BROWSER){userInfo = createAndGetSessionFromUIDCookie(session);}else{userInfo = new UserSessionInfo();}*/userInfo = createAndGetSessionFromUIDCookie(session);session.setAttribute(USER_INFO, userInfo);}else {// Update user cookie in case of new registration and login.createUserCookie(userInfo.getUserId(), false);}if (action instanceof UserAware) {UserAware sessionAction = (UserAware) action;sessionAction.setSession(session);sessionAction.setUserSessionInfo(userInfo);sessionAction.setCookiesMap(cookiesMap);sessionAction.setUserCookie(userCookie);}return invocation.invoke();}private void createCookiesMap(HttpServletRequest request) {cookiesMap = new HashMap<String, Cookie>();Cookie[] cookies = request.getCookies();// This check is necessary for the first request when no cookies are// sent.if(cookies==null)return;for (Cookie cookie : cookies)cookiesMap.put(cookie.getName(), cookie);}private void createUserCookie(long userId, boolean force) {userCookie = (Cookie) cookiesMap.get("uid");String encryptedUserId = desEncrypter.encrypt(userId + "");if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){userCookie = new Cookie("uid", encryptedUserId);userCookie.setMaxAge(SECONDS_IN_YEAR); // one yearuserCookie.setPath("/");log.info("Created new cookie.");cookiesMap.put("uid", userCookie);HttpServletResponse response = ServletActionContext.getResponse();response.addCookie(userCookie);}}private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {userCookie = (Cookie) cookiesMap.get("uid");UserSessionInfo userInfo = null;if(userCookie != null){String uidString = userCookie.getValue();if(uidString != null){try {Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));log.info("Invalid session with user cookie : " + receivedUID);userInfo = new UserSessionInfo(receivedUID, session.getId());session.setAttribute(USER_INFO, userInfo);} catch (NumberFormatException nfe) {log.error("The UID cookie contains an unparseable userID");}}}else{userInfo = new UserSessionInfo(session.getId());session.setAttribute(USER_INFO, userInfo);log.info("Invalid session without user cookie.");createUserCookie(userInfo.getUserId(), true);}return userInfo;}}