Subversion Repositories SmartDukaan

Rev

Rev 1354 | Rev 1553 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
781 vikas 1
package in.shop2020.serving.interceptors;
2
 
3
import java.util.HashMap;
4
import java.util.Map;
5
 
6
import in.shop2020.serving.services.UserSessionInfo;
7
import in.shop2020.serving.utils.DesEncrypter;
8
 
9
import javax.servlet.http.Cookie;
10
import javax.servlet.http.HttpServletRequest;
837 vikas 11
import javax.servlet.http.HttpServletResponse;
781 vikas 12
import javax.servlet.http.HttpSession;
13
 
1511 rajveer 14
import nl.bitwalker.useragentutils.BrowserType;
15
import nl.bitwalker.useragentutils.UserAgent;
16
 
1044 chandransh 17
import org.apache.log4j.Logger;
781 vikas 18
import org.apache.struts2.ServletActionContext;
19
 
20
import com.opensymphony.xwork2.ActionInvocation;
21
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
22
 
23
public class UserInterceptor extends AbstractInterceptor {
24
 
837 vikas 25
	private static final int SECONDS_IN_YEAR = 60*60*24*365; 
26
 
781 vikas 27
	private static final long serialVersionUID = -4125815700236506235L;
1044 chandransh 28
	private static Logger log = Logger.getLogger(UserInterceptor.class);
781 vikas 29
 
30
	public static final String USER_INFO = "userinfo";
31
 
32
	private Map<String, Cookie> cookiesMap = null;
33
	private Cookie userCookie = null;
34
	private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
35
 
36
	@Override
37
	public String intercept(ActionInvocation invocation) throws Exception {
38
		final Object action = invocation.getAction();
39
 
40
        HttpServletRequest request = ServletActionContext.getRequest();
1511 rajveer 41
 
42
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
43
        log.info("User browser is:" + userAgent.getBrowser() + " OS is:" + userAgent.getOperatingSystem());
44
 
45
 
781 vikas 46
        HttpSession session = request.getSession(); // Get the existing session or create a new one
47
 
48
        //getCookiesMap(request);
49
		createCookiesMap(request);
50
 
51
		// If the request is for an active session.
1354 vikas 52
		UserSessionInfo userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
53
 
54
		// Set the userinfo and the uid cookie if they're not already set.
55
		if (userInfo == null) {
1511 rajveer 56
			if(userAgent.getBrowser().getBrowserType() == BrowserType.WEB_BROWSER){
57
				userInfo = createAndGetSessionFromUIDCookie(session);
58
			}else{
59
				userInfo = new UserSessionInfo();
60
			}
801 rajveer 61
			session.setAttribute(USER_INFO, userInfo);
781 vikas 62
		}
1354 vikas 63
		else {
64
			// Update user cookie in case of new registration and login.
65
		    createUserCookie(userInfo.getUserId(), false);
66
		}
67
 
781 vikas 68
		if (action instanceof UserAware) {
69
        	UserAware sessionAction = (UserAware) action;
70
        	sessionAction.setSession(session);
71
        	sessionAction.setUserSessionInfo(userInfo);
72
        	sessionAction.setCookiesMap(cookiesMap);
73
        	sessionAction.setUserCookie(userCookie);
74
        }
831 vikas 75
 
781 vikas 76
		return invocation.invoke();
77
	}
78
 
79
	private void createCookiesMap(HttpServletRequest request) {
80
		cookiesMap  = new HashMap<String, Cookie>();
81
		Cookie[] cookies = request.getCookies();
82
		// This check is necessary for the first request when no cookies are
83
		// sent.
84
		if(cookies==null)
85
			return;
86
		for (Cookie cookie : cookies)
87
			cookiesMap.put(cookie.getName(), cookie);
88
	}
89
 
90
	private void createUserCookie(long userId, boolean force) {
91
		userCookie = (Cookie) cookiesMap.get("uid");
817 vikas 92
		String encryptedUserId = desEncrypter.encrypt(userId + "");
93
		if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
781 vikas 94
			userCookie = new Cookie("uid", encryptedUserId);
837 vikas 95
			userCookie.setMaxAge(SECONDS_IN_YEAR); // one year
96
			userCookie.setPath("/");
97
			log.info("Created new cookie.");
824 vikas 98
			cookiesMap.put("uid", userCookie);
837 vikas 99
			HttpServletResponse response = ServletActionContext.getResponse();
100
	        response.addCookie(userCookie);
781 vikas 101
		}
102
	}
103
 
104
	private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
105
		userCookie = (Cookie) cookiesMap.get("uid");
106
		UserSessionInfo userInfo = null;
107
		if(userCookie != null){
108
			String uidString = userCookie.getValue();
109
			if(uidString != null){
110
				try {
111
					Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
828 rajveer 112
                    log.info("Invalid session with user cookie : " + receivedUID);
781 vikas 113
					userInfo = new UserSessionInfo(receivedUID, session.getId());
817 vikas 114
					session.setAttribute(USER_INFO, userInfo);
781 vikas 115
				} catch (NumberFormatException nfe) {
116
					log.error("The UID cookie contains an unparseable userID");
117
				}
118
			}
830 vikas 119
		}
120
		else{
121
			userInfo = new UserSessionInfo(session.getId());
828 rajveer 122
			session.setAttribute(USER_INFO, userInfo);
830 vikas 123
			log.info("Invalid session without user cookie.");
1354 vikas 124
			createUserCookie(userInfo.getUserId(), true);
781 vikas 125
		}
126
		return userInfo;
127
	}
128
}