Subversion Repositories SmartDukaan

Rev

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