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