Subversion Repositories SmartDukaan

Rev

Rev 2473 | Rev 2935 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2473 Rev 2907
Line 1... Line 1...
1
package in.shop2020.serving.interceptors;
1
package in.shop2020.serving.interceptors;
2
 
2
 
3
import java.util.HashMap;
3
import java.util.HashMap;
-
 
4
import java.util.List;
4
import java.util.Map;
5
import java.util.Map;
5
 
6
 
6
import in.shop2020.serving.services.UserSessionInfo;
7
import in.shop2020.serving.services.UserSessionInfo;
7
import in.shop2020.serving.utils.DesEncrypter;
8
import in.shop2020.serving.utils.DesEncrypter;
8
 
9
 
Line 11... Line 12...
11
import javax.servlet.http.HttpServletResponse;
12
import javax.servlet.http.HttpServletResponse;
12
import javax.servlet.http.HttpSession;
13
import javax.servlet.http.HttpSession;
13
 
14
 
14
import org.apache.log4j.Logger;
15
import org.apache.log4j.Logger;
15
import org.apache.struts2.ServletActionContext;
16
import org.apache.struts2.ServletActionContext;
-
 
17
import org.apache.struts2.StrutsStatics;
16
 
18
 
-
 
19
import com.opensymphony.xwork2.ActionContext;
17
import com.opensymphony.xwork2.ActionInvocation;
20
import com.opensymphony.xwork2.ActionInvocation;
18
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
21
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
-
 
22
import com.opensymphony.xwork2.interceptor.PreResultListener;
19
 
23
 
20
public class UserInterceptor extends AbstractInterceptor {
24
public class UserInterceptor extends AbstractInterceptor implements PreResultListener{
21
 
25
 
22
	private static final int SECONDS_IN_YEAR = 60*60*24*365; 
26
	public static final int SECONDS_IN_YEAR = 60*60*24*365; 
23
	
27
	
24
	private static final long serialVersionUID = -4125815700236506235L;
28
	private static final long serialVersionUID = -4125815700236506235L;
25
	private static Logger log = Logger.getLogger(UserInterceptor.class);
29
	private static Logger log = Logger.getLogger(UserInterceptor.class);
26
	
30
	
27
	public static final String USER_INFO = "userinfo";
31
	public static final String USER_INFO_COOKIE_NAME = "uic";
-
 
32
	public static final String USER_ID_COOKIE_NAME = "uid";
28
	
33
	
29
	private Map<String, Cookie> cookiesMap = null;
34
	private Map<String, Cookie> cookiesMap = null;
30
	private Cookie userCookie = null;
35
	private Cookie userCookie = null;
31
	private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
36
	public static DesEncrypter desEncrypter = new DesEncrypter("shop2020");
-
 
37
	
-
 
38
	private Cookie userinfoCookie = null;
32
	
39
	
33
	private String cookieDomain = "";
40
	private String cookieDomain = "";
34
	
41
	
35
	public void setCookieDomain(String cookieDomain) {
42
	public void setCookieDomain(String cookieDomain) {
36
		this.cookieDomain = cookieDomain;
43
		this.cookieDomain = cookieDomain;
Line 43... Line 50...
43
		log.debug("inside user intercepror");
50
		log.debug("inside user intercepror");
44
		
51
		
45
        HttpServletRequest request = ServletActionContext.getRequest();
52
        HttpServletRequest request = ServletActionContext.getRequest();
46
        HttpSession session = request.getSession(); // Get the existing session or create a new one
53
        HttpSession session = request.getSession(); // Get the existing session or create a new one
47
        
54
        
48
        //getCookiesMap(request);
55
        
49
		createCookiesMap(request);
56
		createCookiesMap(request);
50
		
57
		
51
		// If the request is for an active session.
-
 
52
		UserSessionInfo userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
58
		UserSessionInfo userInfo = (UserSessionInfo) request.getAttribute(USER_INFO_COOKIE_NAME);
-
 
59
 
-
 
60
		userCookie = cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
-
 
61
		userinfoCookie = cookiesMap.get(USER_INFO_COOKIE_NAME);
53
		
62
		
54
		// Set the userinfo and the uid cookie if they're not already set.
-
 
55
		if (userInfo == null) {
63
		if(userInfo == null ){
56
			userInfo = createAndGetSessionFromUIDCookie(session);
64
			if(userinfoCookie!=null){
57
			session.setAttribute(USER_INFO, userInfo);
65
				userInfo = UserSessionInfo.getUserSessionInfoFromCookieValue(userinfoCookie.getValue());
58
		}
-
 
59
		else {
66
			}else{
60
			// Update user cookie in case of new registration and login.
-
 
61
			if(userInfo.getUserId() != -1){
-
 
62
				createUserCookie(userInfo.getUserId(), false);
67
				userInfo = createAndGetSessionFromUIDCookie(session);
63
			}
68
			}
64
		}
69
		}
-
 
70
			
-
 
71
		request.setAttribute(USER_INFO_COOKIE_NAME, userInfo);
-
 
72
		
65
	
73
		
66
		if (action instanceof UserAware) {
74
		if (action instanceof UserAware) {
67
        	UserAware sessionAction = (UserAware) action;
75
        	UserAware sessionAction = (UserAware) action;
68
        	sessionAction.setSession(session);
76
        	sessionAction.setSession(session);
69
        	sessionAction.setUserSessionInfo(userInfo);
77
        	sessionAction.setUserSessionInfo(userInfo);
70
        	sessionAction.setCookiesMap(cookiesMap);
78
        	sessionAction.setCookiesMap(cookiesMap);
71
        	sessionAction.setUserCookie(userCookie);
79
        	sessionAction.setUserCookie(userCookie);
72
        	sessionAction.setCookieDomainName(cookieDomain);
80
        	sessionAction.setCookieDomainName(cookieDomain);
73
        }
81
        }
-
 
82
		
-
 
83
		invocation.addPreResultListener(this);
74
 
84
		
75
		return invocation.invoke();
85
		return invocation.invoke();
76
	}
86
	}
77
	
87
	
78
	
88
	
-
 
89
	@Override
-
 
90
	public void beforeResult(ActionInvocation invocation, String resultCode) {
-
 
91
		ActionContext ac = invocation.getInvocationContext();
-
 
92
		HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE);
-
 
93
		addCookiesToResponse(invocation.getAction(), response);
-
 
94
	}	
-
 
95
 
-
 
96
	
-
 
97
	private void addCookiesToResponse(Object action, HttpServletResponse response) {
-
 
98
		if (action instanceof UserAware) {
-
 
99
			List<Cookie> cookies = ((UserAware) action).getCookies();
-
 
100
			if (cookies != null) {
-
 
101
				for (Cookie cookie : cookies) {
-
 
102
					response.addCookie(cookie);
-
 
103
				}
-
 
104
			}
-
 
105
		}
-
 
106
	}
-
 
107
 
-
 
108
		  
79
	private void createCookiesMap(HttpServletRequest request) {
109
	private void createCookiesMap(HttpServletRequest request) {
80
		cookiesMap  = new HashMap<String, Cookie>();
110
		cookiesMap  = new HashMap<String, Cookie>();
81
		Cookie[] cookies = request.getCookies();
111
		Cookie[] cookies = request.getCookies();
82
		// This check is necessary for the first request when no cookies are
-
 
83
		// sent.
-
 
84
		if(cookies==null)
112
		if(cookies==null)
85
			return;
113
			return;
86
		for (Cookie cookie : cookies) {
114
		for (Cookie cookie : cookies) {
87
			if (cookie.getName().equals("uid")) {
115
			if (cookie.getName().equals(UserInterceptor.USER_ID_COOKIE_NAME)) {
88
				if (cookie.getDomain() == null || cookie.getDomain().isEmpty()
116
				if (cookie.getDomain() == null || cookie.getDomain().isEmpty()
89
						|| !cookie.getDomain().equals(this.cookieDomain)) 
117
						|| !cookie.getDomain().equals(this.cookieDomain)) 
90
				{
118
				{
91
					if (!cookieDomain.isEmpty()) {
119
					if (!cookieDomain.isEmpty()) {
92
						cookie.setMaxAge(0);
120
						cookie.setMaxAge(0);
93
						Cookie newUserCookie = new Cookie("uid", cookie.getValue());
121
						Cookie newUserCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, cookie.getValue());
94
						newUserCookie.setMaxAge(SECONDS_IN_YEAR); // one year
122
						newUserCookie.setMaxAge(SECONDS_IN_YEAR); // one year
95
						newUserCookie.setPath("/");
123
						newUserCookie.setPath("/");
96
						newUserCookie.setDomain(cookieDomain);
124
						newUserCookie.setDomain(cookieDomain);
97
						
125
						
98
						HttpServletResponse response = ServletActionContext.getResponse();
126
						HttpServletResponse response = ServletActionContext.getResponse();
Line 106... Line 134...
106
			}
134
			}
107
		    cookiesMap.put(cookie.getName(), cookie);
135
		    cookiesMap.put(cookie.getName(), cookie);
108
		}
136
		}
109
	}
137
	}
110
	
138
	
111
	private void createUserCookie(long userId, boolean force) {
-
 
112
		userCookie = (Cookie) cookiesMap.get("uid");
-
 
113
		String encryptedUserId = desEncrypter.encrypt(userId + "");
-
 
114
		if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
-
 
115
			userCookie = new Cookie("uid", encryptedUserId);
-
 
116
			userCookie.setMaxAge(SECONDS_IN_YEAR); // one year
-
 
117
			userCookie.setPath("/");
-
 
118
			if(!cookieDomain.isEmpty()) {
-
 
119
				userCookie.setDomain(cookieDomain);
-
 
120
			}
-
 
121
			log.info("Created new cookie.");
-
 
122
			cookiesMap.put("uid", userCookie);
-
 
123
			HttpServletResponse response = ServletActionContext.getResponse();
-
 
124
	        response.addCookie(userCookie);
-
 
125
		}
-
 
126
	}
139
	
127
 
140
 
128
	
141
	
129
	private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
142
	private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
130
		userCookie = (Cookie) cookiesMap.get("uid");
143
		userCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
131
		UserSessionInfo userInfo = null;
144
		UserSessionInfo userInfo = null;
132
		if(userCookie != null){
145
		if(userCookie != null){
133
			String uidString = userCookie.getValue();
146
			String uidString = userCookie.getValue();
134
			if(uidString != null){
147
			if(uidString != null){
135
				try {
148
				try {
136
					Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
149
					Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
137
                    log.info("Invalid session with user cookie : " + receivedUID);
150
                    log.info("Invalid session with user cookie : " + receivedUID);
138
					userInfo = new UserSessionInfo(receivedUID, session.getId());
151
					userInfo = new UserSessionInfo(receivedUID, session.getId());
139
					session.setAttribute(USER_INFO, userInfo);
-
 
140
				} catch (NumberFormatException nfe) {
152
				} catch (NumberFormatException nfe) {
141
					log.error("The UID cookie contains an unparseable userID");
153
					log.error("The UID cookie contains an unparseable userID");
142
					Cookie newUserCookie = new Cookie("uid", "-1"); //The value here is immaterial
154
					Cookie newUserCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, "-1"); //The value here is immaterial
143
					newUserCookie.setMaxAge(0);                     // Expire this cookie now
155
					newUserCookie.setMaxAge(0);                     // Expire this cookie now
144
					newUserCookie.setPath("/");
156
					newUserCookie.setPath("/");
145
					newUserCookie.setDomain(cookieDomain);
157
					newUserCookie.setDomain(cookieDomain);
146
					
158
					
147
					HttpServletResponse response = ServletActionContext.getResponse();
159
					HttpServletResponse response = ServletActionContext.getResponse();
148
					response.addCookie(newUserCookie);
160
					response.addCookie(newUserCookie);
149
					
161
					
150
					userInfo = new UserSessionInfo();
162
					userInfo = new UserSessionInfo();
151
					session.setAttribute(USER_INFO, userInfo);
-
 
152
				}
163
				}
153
			}
164
			}
154
		}
165
		}
155
		else{
166
		else{
156
			userInfo = new UserSessionInfo();
167
			userInfo = new UserSessionInfo();
157
			session.setAttribute(USER_INFO, userInfo);
-
 
158
			log.info("Invalid session without user cookie.");
168
			log.info("Invalid session without user cookie.");
159
			//createUserCookie(userInfo.getUserId(), true);
-
 
160
		}
169
		}
161
		return userInfo;
170
		return userInfo;
162
	}
171
	}
-
 
172
 
163
}
173
}
164
174