Subversion Repositories SmartDukaan

Rev

Rev 545 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 545 Rev 555
Line 1... Line -...
1
/**
-
 
2
 * 
-
 
3
 */
-
 
4
 
-
 
5
 
-
 
6
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
7
 
2
 
8
import in.shop2020.serving.services.UserSessionInfo;
3
import in.shop2020.serving.services.UserSessionInfo;
-
 
4
import in.shop2020.serving.utils.DesEncrypter;
9
import in.shop2020.serving.utils.Utils;
5
import in.shop2020.serving.utils.Utils;
10
 
6
 
-
 
7
import java.util.HashMap;
11
import java.util.Map;
8
import java.util.Map;
12
 
9
 
13
import javax.servlet.http.Cookie;
10
import javax.servlet.http.Cookie;
14
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
12
import javax.servlet.http.HttpServletResponse;
Line 23... Line 20...
23
 
20
 
24
/**
21
/**
25
 * Base class for all user action handlers i.e. controllers
22
 * Base class for all user action handlers i.e. controllers
26
 * 
23
 * 
27
 * @author rajveer
24
 * @author rajveer
28
 *
-
 
29
 */
25
 */
30
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
26
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
31
	private Map cookiesMap;
27
	protected Map<String, Cookie> cookiesMap = null;
32
    protected HttpServletResponse response;
28
    protected HttpServletResponse response;
33
    protected HttpServletRequest request;
29
    protected HttpServletRequest request;
34
    protected HttpSession session;
30
    protected HttpSession session;
35
    
-
 
36
    UserSessionInfo userinfo = null;
31
    protected UserSessionInfo userinfo = null;
37
    
-
 
38
    Cookie tempCookie = null;
-
 
39
    
-
 
40
	private static Log log = LogFactory.getLog(BaseController.class);
32
	private static Log log = LogFactory.getLog(BaseController.class);
41
	
33
	
-
 
34
    private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
-
 
35
    
-
 
36
    protected Cookie userCookie = null;
-
 
37
	
42
	public BaseController() {
38
	public BaseController() {
43
	}
39
	}
44
	
40
	
45
	public Map getCookiesMap() {
41
	public Map getCookiesMap() {
46
		return cookiesMap;
42
		return cookiesMap;
47
	}
43
	}
48
	
44
	
49
	@Override
45
	@Override
50
	public void setCookiesMap(Map cookiesMap) {
46
	public void setCookiesMap(Map cookiesMap) {
-
 
47
		log.info("Received cookiesMap and it is " + cookiesMap);
51
		this.cookiesMap = cookiesMap;
48
		this.cookiesMap = cookiesMap;
52
	}
49
	}
53
	
50
	
54
	@Override
51
	@Override
55
	public void setServletResponse(HttpServletResponse response)
52
	public void setServletResponse(HttpServletResponse response)
56
	{
53
	{
57
		this.response = response;
54
		this.response = response;
58
		if(tempCookie != null){
55
		if(userCookie!=null)
59
			this.response.addCookie(tempCookie);
56
			response.addCookie(userCookie);
60
		}
-
 
61
	}
57
	}
62
	
58
	
63
	@Override
59
	@Override
64
	public void setServletRequest(HttpServletRequest request){
60
	public void setServletRequest(HttpServletRequest request){
65
		this.request = request;
61
		this.request = request;
66
		this.session = request.getSession();
62
		this.session = request.getSession();	// Get the existing session or create a new one
67
		setUserSessionInfo();
63
		getCookiesMap(request);
-
 
64
		String requestedSessionId = request.getRequestedSessionId();
68
	}
65
		
-
 
66
		// Check if this is a brand new request with no prior cookies set; OR
-
 
67
		// If the request is for an active session.
-
 
68
		if(requestedSessionId == null || request.isRequestedSessionIdValid()){
-
 
69
			log.info("Request received for valid session: " + requestedSessionId);
-
 
70
			// Set the userinfo and the uid cookie if they're not already set.
-
 
71
			this.session = request.getSession();
-
 
72
			setUserSessionInfo(this.session.getId());
-
 
73
			createUserCookie(this.userinfo.getUserId(), false);
69
	
74
		} else {
-
 
75
			log.info("Request received for invalid session: " + requestedSessionId);
70
	public void removeUserSessionInfo(){
76
			// If the requested session is inactive, do the following:
71
		if(this.session.getAttribute("userinfo") != null ){
77
			// 1. Retrieve the user for the requested session from the user cookie
-
 
78
			// 2. Add the retrieved user to the newly created session above.
-
 
79
			// 3. Update the uid cookie to ensure that a valid user is set in the session
72
			this.session.removeAttribute("userinfo");
80
			recreateSessionFromUIDCookie(this.session.getId());
-
 
81
			createUserCookie(this.userinfo.getUserId(), true);
73
		}
82
		}
74
	}
83
	}
75
 
84
 
-
 
85
	private void getCookiesMap(HttpServletRequest request) {
-
 
86
		cookiesMap  = new HashMap<String, Cookie>();
-
 
87
		Cookie[] cookies = request.getCookies();
-
 
88
		// This check is necessary for the first request when no cookies are
-
 
89
		// sent.
-
 
90
		if(cookies==null)
-
 
91
			return;
-
 
92
		for (Cookie cookie : cookies)
-
 
93
			cookiesMap.put(cookie.getName(), cookie);
76
	
94
	}
-
 
95
 
77
	public void setUserSessionInfo(){
96
	private void setUserSessionInfo(String jsessionid){
78
		if(this.session.getAttribute("userinfo") != null ){
-
 
79
			userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
97
		this.userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
80
		}else{
-
 
81
			processCookiesInfo();
98
		if(this.userinfo == null){
82
			//userinfo = new UserSessionInfo();
99
			this.userinfo = new UserSessionInfo(jsessionid);
-
 
100
			this.session.setAttribute("userinfo", this.userinfo);
83
		}
101
		}
84
		this.session.setAttribute("userinfo",userinfo);
-
 
85
	}
102
	}
86
	
103
 
87
	public void processCookiesInfo(){
104
	protected void createUserCookie(long userId, boolean force) {
88
		Cookie[] cookies = this.request.getCookies();
105
		userCookie = (Cookie) cookiesMap.get("uid");
89
		boolean foundUserIdCookie = false;
-
 
90
		boolean foundSessionIdCookie = false;
-
 
91
		long userId = 0 ;
-
 
92
		long sessionId = 0;
-
 
93
		
-
 
94
		if(cookies != null){
-
 
95
		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
-
 
96
		        Cookie cookie1 = cookies[loopIndex];
-
 
97
		        if (cookie1.getName().equals("userid")) {
-
 
98
		            System.out.println("User Id is = " + cookie1.getValue());
106
		if(force || userCookie == null || !(userId + "").equals(userCookie.getValue())){
99
		            userId = Long.parseLong(cookie1.getValue());
107
			String encryptedUserId = desEncrypter.encrypt(userId + "");  
100
		            foundUserIdCookie = true;
-
 
101
		        }
-
 
102
		        if (cookie1.getName().equals("sessionid")) {
108
			userCookie = new Cookie("uid", encryptedUserId);
103
		            System.out.println("Session Id is = " + cookie1.getValue());
-
 
104
		            sessionId = Long.parseLong(cookie1.getValue());
-
 
105
		            foundSessionIdCookie = true;
-
 
106
		        }
-
 
107
	    	}
-
 
108
		}
109
		}
-
 
110
	}
109
	
111
	
110
		if(foundUserIdCookie){
-
 
111
			if(Utils.isUserLoggedIn(userId)){
112
	private void recreateSessionFromUIDCookie(String jsessionid) {
112
				userinfo = new UserSessionInfo(userId, false);
113
		Cookie userCookie = (Cookie) cookiesMap.get("uid");
113
				
-
 
114
			}
-
 
115
			else{
114
		if(userCookie != null){
116
				if(foundSessionIdCookie){
115
			String uidString = userCookie.getValue();
117
					userinfo = new UserSessionInfo(sessionId, true);	
116
			if(uidString != null){
118
				}else{
117
				try {
119
					userinfo = new UserSessionInfo();
-
 
120
				    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
118
					Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
121
				        Cookie cookie1 = cookies[loopIndex];
119
					this.userinfo = new UserSessionInfo(receivedUID, jsessionid);
122
				        if (cookie1.getName().equals("userid")) {
120
					this.session.setAttribute("userinfo", this.userinfo);
123
				        	cookie1.setMaxAge(0);
121
				} catch (NumberFormatException nfe) {
124
				        	//cookie1.setPath(cookie1.getPath());
122
					log.error("The UID cookie contains an unparseable userID");
125
							//cookie1.setDomain(cookie1.getDomain());
-
 
126
				        	tempCookie = cookie1;
-
 
127
				        }
-
 
128
					}
-
 
129
				}
123
				}
130
			}
124
			}
131
		}			
-
 
132
		else{  
-
 
133
			if(foundSessionIdCookie){
-
 
134
				userinfo = new UserSessionInfo(sessionId, true);			
-
 
135
			}
-
 
136
			else{
-
 
137
				userinfo = new UserSessionInfo();
-
 
138
				Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");
-
 
139
		    	tempCookie = cookie1;
-
 
140
			}
-
 
141
		}
125
		}
-
 
126
		if(this.userinfo==null)
-
 
127
			setUserSessionInfo(jsessionid);
142
	}
128
	}
-
 
129
	
-
 
130
//	private void processCookiesInfo(){
-
 
131
//		Cookie[] cookies = this.request.getCookies();
-
 
132
//		boolean foundUserIdCookie = false;
-
 
133
//		boolean foundSessionIdCookie = false;
-
 
134
//		long userId = 0 ;
-
 
135
//		long sessionId = 0;
-
 
136
//		
-
 
137
//		if(cookies != null){
-
 
138
//		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
-
 
139
//		        Cookie cookie1 = cookies[loopIndex];
-
 
140
//		        if (cookie1.getName().equals("userid")) {
-
 
141
//		            System.out.println("User Id is = " + cookie1.getValue());
-
 
142
//		            userId = Long.parseLong(cookie1.getValue());
-
 
143
//		            foundUserIdCookie = true;
-
 
144
//		        }
-
 
145
//		        if (cookie1.getName().equals("sessionid")) {
-
 
146
//		            System.out.println("Session Id is = " + cookie1.getValue());
-
 
147
//		            sessionId = Long.parseLong(cookie1.getValue());
-
 
148
//		            foundSessionIdCookie = true;
-
 
149
//		        }
-
 
150
//	    	}
-
 
151
//		}
-
 
152
//	
-
 
153
//		if(foundUserIdCookie){
-
 
154
//			if(Utils.isUserLoggedIn(userId)){
-
 
155
//				userinfo = new UserSessionInfo(userId, false);
-
 
156
//				
-
 
157
//			}
-
 
158
//			else{
-
 
159
//				if(foundSessionIdCookie){
-
 
160
//					userinfo = new UserSessionInfo(sessionId, true);	
-
 
161
//				}else{
-
 
162
//					userinfo = new UserSessionInfo();
-
 
163
//				    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
-
 
164
//				        Cookie cookie1 = cookies[loopIndex];
-
 
165
//				        if (cookie1.getName().equals("userid")) {
-
 
166
//				        	cookie1.setMaxAge(0);
-
 
167
//				        	//cookie1.setPath(cookie1.getPath());
-
 
168
//							//cookie1.setDomain(cookie1.getDomain());
-
 
169
//				        	tempCookie = cookie1;
-
 
170
//				        }
-
 
171
//					}
-
 
172
//				}
-
 
173
//			}
-
 
174
//		}			
-
 
175
//		else{  
-
 
176
//			if(foundSessionIdCookie){
-
 
177
//				userinfo = new UserSessionInfo(sessionId, true);			
-
 
178
//			}
-
 
179
//			else{
-
 
180
//				userinfo = new UserSessionInfo();
-
 
181
//				//Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");
-
 
182
//		    	//tempCookie = cookie1;
-
 
183
//			}
-
 
184
//		}
-
 
185
//	}
143
 
186
 
144
	public String getEmail(){
187
	private String getEmail(){
145
		return userinfo.getEmail();
188
		return userinfo.getEmail();
146
	}
189
	}
147
 
190
 
148
	public String getNameOfUser(){
191
	private String getNameOfUser(){
149
		return userinfo.getNameOfUser();
192
		return userinfo.getNameOfUser();
150
	}
193
	}
151
 
194
 
152
}
195
}
153
	
-
 
154
 
196