Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | 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;
11
import javax.servlet.http.HttpSession;
12
 
13
import org.apache.juli.logging.Log;
14
import org.apache.juli.logging.LogFactory;
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
 
22
	/**
23
	 * 
24
	 */
25
	private static final long serialVersionUID = -4125815700236506235L;
26
	private static Log log = LogFactory.getLog(UserInterceptor.class);
27
 
28
	public static final String USER_INFO = "userinfo";
29
 
30
	private Map<String, Cookie> cookiesMap = null;
31
	private Cookie userCookie = null;
32
	private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
33
 
34
	@Override
35
	public String intercept(ActionInvocation invocation) throws Exception {
36
		final Object action = invocation.getAction();
37
 
38
        HttpServletRequest request = ServletActionContext.getRequest();
39
        HttpSession session = request.getSession(); // Get the existing session or create a new one
40
 
41
        //getCookiesMap(request);
42
		String requestedSessionId = request.getRequestedSessionId();
43
 
44
		UserSessionInfo userInfo = null;
45
		createCookiesMap(request);
46
 
47
		// Check if this is a brand new request with no prior cookies set; OR
48
		// If the request is for an active session.
49
		if(requestedSessionId == null || request.isRequestedSessionIdValid()){
50
			log.info("Request received for valid session: " + requestedSessionId);
51
			// Set the userinfo and the uid cookie if they're not already set.
52
			userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
53
			if(userInfo == null || userInfo.getUserId() == -1) {
54
				userInfo = new UserSessionInfo(session.getId());
817 vikas 55
				session.setAttribute(USER_INFO, userInfo);
781 vikas 56
			}
57
			createUserCookie(userInfo.getUserId(), false);
58
		} else {
59
			log.info("Request received for invalid session: " + requestedSessionId);
60
			// If the requested session is inactive, do the following:
61
			// 1. Retrieve the user for the requested session from the user cookie
62
			// 2. Add the retrieved user to the newly created session above.
63
			// 3. Update the uid cookie to ensure that a valid user is set in the session
64
			userInfo = createAndGetSessionFromUIDCookie(session);
801 rajveer 65
			session.setAttribute(USER_INFO, userInfo);
781 vikas 66
			createUserCookie(userInfo.getUserId(), true);
67
		}
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
        }
75
 
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);
824 vikas 95
			cookiesMap.put("uid", userCookie);
781 vikas 96
		}
97
	}
98
 
99
	private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
100
		userCookie = (Cookie) cookiesMap.get("uid");
101
		UserSessionInfo userInfo = null;
102
		if(userCookie != null){
103
			String uidString = userCookie.getValue();
104
			if(uidString != null){
105
				try {
106
					Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
828 rajveer 107
                    log.info("Invalid session with user cookie : " + receivedUID);
781 vikas 108
					userInfo = new UserSessionInfo(receivedUID, session.getId());
817 vikas 109
					session.setAttribute(USER_INFO, userInfo);
781 vikas 110
				} catch (NumberFormatException nfe) {
111
					log.error("The UID cookie contains an unparseable userID");
112
				}
113
			}
828 rajveer 114
		}else{
115
			session.setAttribute(USER_INFO, userInfo);
116
            userInfo = new UserSessionInfo(session.getId());
117
            log.info("Invalid session without user cookie.");
781 vikas 118
		}
119
		return userInfo;
120
	}
121
}