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.ActionContext;
18
import com.opensymphony.xwork2.ActionInvocation;
19
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
20
 
21
public class UserInterceptor extends AbstractInterceptor {
22
 
23
	/**
24
	 * 
25
	 */
26
	private static final long serialVersionUID = -4125815700236506235L;
27
	private static Log log = LogFactory.getLog(UserInterceptor.class);
28
 
29
	public static final String USER_INFO = "userinfo";
30
 
31
	private Map<String, Cookie> cookiesMap = null;
32
	private Cookie userCookie = null;
33
	private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
34
 
35
	@Override
36
	public String intercept(ActionInvocation invocation) throws Exception {
37
		final Object action = invocation.getAction();
38
 
39
        HttpServletRequest request = ServletActionContext.getRequest();
40
        HttpSession session = request.getSession(); // Get the existing session or create a new one
41
 
42
        //getCookiesMap(request);
43
		String requestedSessionId = request.getRequestedSessionId();
44
 
45
		UserSessionInfo userInfo = null;
46
		createCookiesMap(request);
47
 
48
		// Check if this is a brand new request with no prior cookies set; OR
49
		// If the request is for an active session.
50
		if(requestedSessionId == null || request.isRequestedSessionIdValid()){
51
			log.info("Request received for valid session: " + requestedSessionId);
52
			// Set the userinfo and the uid cookie if they're not already set.
53
			userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
54
			if(userInfo == null || userInfo.getUserId() == -1) {
55
				userInfo = new UserSessionInfo(session.getId());
56
				session.setAttribute("userinfo", userInfo);
57
			}
58
			createUserCookie(userInfo.getUserId(), false);
59
		} else {
60
			log.info("Request received for invalid session: " + requestedSessionId);
61
			// If the requested session is inactive, do the following:
62
			// 1. Retrieve the user for the requested session from the user cookie
63
			// 2. Add the retrieved user to the newly created session above.
64
			// 3. Update the uid cookie to ensure that a valid user is set in the session
65
			userInfo = createAndGetSessionFromUIDCookie(session);
801 rajveer 66
			session.setAttribute(USER_INFO, userInfo);
781 vikas 67
			createUserCookie(userInfo.getUserId(), true);
68
		}
69
		if (action instanceof UserAware) {
70
        	UserAware sessionAction = (UserAware) action;
71
        	sessionAction.setSession(session);
72
        	sessionAction.setUserSessionInfo(userInfo);
73
        	sessionAction.setCookiesMap(cookiesMap);
74
        	sessionAction.setUserCookie(userCookie);
75
        }
76
 
77
		return invocation.invoke();
78
	}
79
 
80
	private void createCookiesMap(HttpServletRequest request) {
81
		cookiesMap  = new HashMap<String, Cookie>();
82
		Cookie[] cookies = request.getCookies();
83
		// This check is necessary for the first request when no cookies are
84
		// sent.
85
		if(cookies==null)
86
			return;
87
		for (Cookie cookie : cookies)
88
			cookiesMap.put(cookie.getName(), cookie);
89
	}
90
 
91
	private void createUserCookie(long userId, boolean force) {
92
		userCookie = (Cookie) cookiesMap.get("uid");
93
		if(force || userCookie == null || !(userId + "").equals(userCookie.getValue())){
94
			String encryptedUserId = desEncrypter.encrypt(userId + "");  
95
			userCookie = new Cookie("uid", encryptedUserId);
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));
107
					userInfo = new UserSessionInfo(receivedUID, session.getId());
108
					session.setAttribute("userinfo", userInfo);
109
				} catch (NumberFormatException nfe) {
110
					log.error("The UID cookie contains an unparseable userID");
111
				}
112
			}
113
		}
114
		return userInfo;
115
	}
116
}