Subversion Repositories SmartDukaan

Rev

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

Rev 416 Rev 419
Line 5... Line 5...
5
 
5
 
6
package in.shop2020.serving.controllers;
6
package in.shop2020.serving.controllers;
7
 
7
 
8
import in.shop2020.model.v1.user.UserContext;
8
import in.shop2020.model.v1.user.UserContext;
9
import in.shop2020.model.v1.user.UserContextException;
9
import in.shop2020.model.v1.user.UserContextException;
-
 
10
import in.shop2020.model.v1.user.UserInternalInfo;
-
 
11
import in.shop2020.serving.services.UserSessionInfo;
-
 
12
import in.shop2020.serving.utils.Utils;
10
import in.shop2020.thrift.clients.ShoppingCartClient;
13
import in.shop2020.thrift.clients.ShoppingCartClient;
11
import in.shop2020.thrift.clients.UserContextServiceClient;
14
import in.shop2020.thrift.clients.UserContextServiceClient;
12
 
15
 
13
import java.util.Map;
16
import java.util.Map;
14
 
17
 
Line 34... Line 37...
34
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
37
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
35
	private Map cookiesMap;
38
	private Map cookiesMap;
36
    protected HttpServletResponse response;
39
    protected HttpServletResponse response;
37
    protected HttpServletRequest request;
40
    protected HttpServletRequest request;
38
    protected HttpSession session;
41
    protected HttpSession session;
-
 
42
    
-
 
43
    UserSessionInfo userinfo = null;
39
//    private boolean isLoggedIn = ;
44
//    public boolean isLoggedIn = false;
40
//    private boolean isSessionId = true;
45
//    public boolean isSessionId = true;
41
//    long userId = 0;
46
//    public long userId = 0;
-
 
47
    //private String email = "";
-
 
48
    
42
    Cookie tempCookie = null;
49
    Cookie tempCookie = null;
43
    
50
    
44
	private static Log log = LogFactory.getLog(BaseController.class);
51
	private static Log log = LogFactory.getLog(BaseController.class);
45
	
52
	
46
	public BaseController() {
53
	public BaseController() {
Line 68... Line 75...
68
	
75
	
69
	@Override
76
	@Override
70
	public void setServletRequest(HttpServletRequest request){
77
	public void setServletRequest(HttpServletRequest request){
71
		this.request = request;
78
		this.request = request;
72
		this.session = request.getSession();
79
		this.session = request.getSession();
-
 
80
		//setSessionAndCookies();
-
 
81
		setUserSessionInfo();
-
 
82
	}
-
 
83
	
-
 
84
 
-
 
85
	
-
 
86
	public void removeUserSessionInfo(){
-
 
87
		if(this.session.getAttribute("userinfo") != null ){
-
 
88
			this.session.removeAttribute("userinfo");
-
 
89
		}
-
 
90
	}
-
 
91
 
-
 
92
	
-
 
93
	public void setUserSessionInfo(){
-
 
94
		if(this.session.getAttribute("userinfo") != null ){
-
 
95
			userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
-
 
96
		}else{
-
 
97
			processCookiesInfo();
-
 
98
			//userinfo = new UserSessionInfo();
-
 
99
		}
-
 
100
		this.session.setAttribute("userinfo",userinfo);
-
 
101
	}
-
 
102
	
-
 
103
	public void processCookiesInfo(){
-
 
104
		Cookie[] cookies = this.request.getCookies();
-
 
105
		boolean foundUserIdCookie = false;
-
 
106
		boolean foundSessionIdCookie = false;
-
 
107
		long userId = 0 ;
-
 
108
		long sessionId = 0;
73
		
109
		
-
 
110
		if(cookies != null){
-
 
111
		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
-
 
112
		        Cookie cookie1 = cookies[loopIndex];
-
 
113
		        if (cookie1.getName().equals("userid")) {
-
 
114
		            System.out.println("User Id is = " + cookie1.getValue());
-
 
115
		            userId = Long.parseLong(cookie1.getValue());
-
 
116
		            foundUserIdCookie = true;
-
 
117
		        }
-
 
118
		        if (cookie1.getName().equals("sessionid")) {
-
 
119
		            System.out.println("Session Id is = " + cookie1.getValue());
-
 
120
		            sessionId = Long.parseLong(cookie1.getValue());
-
 
121
		            foundSessionIdCookie = true;
-
 
122
		        }
-
 
123
	    	}
-
 
124
		}
-
 
125
	
-
 
126
		if(foundUserIdCookie){
-
 
127
			if(Utils.isUserLoggedIn(userId)){
-
 
128
				userinfo = new UserSessionInfo(userId, false);
-
 
129
			}
-
 
130
			else{
-
 
131
				if(foundSessionIdCookie){
-
 
132
					userinfo = new UserSessionInfo(sessionId, true);	
-
 
133
				}else{
-
 
134
					userinfo = new UserSessionInfo();
-
 
135
				    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
-
 
136
				        Cookie cookie1 = cookies[loopIndex];
-
 
137
				        if (cookie1.getName().equals("userid")) {
-
 
138
				        	cookie1.setMaxAge(0);
-
 
139
				        	//cookie1.setPath(cookie1.getPath());
-
 
140
							//cookie1.setDomain(cookie1.getDomain());
-
 
141
				        	tempCookie = cookie1;
-
 
142
				        }
-
 
143
					}
-
 
144
				}
-
 
145
			}
-
 
146
		}			
-
 
147
		else{  
74
		setSessionAndCookies();
148
			if(foundSessionIdCookie){
-
 
149
				userinfo = new UserSessionInfo(sessionId, true);			
-
 
150
			}
-
 
151
			else{
-
 
152
				userinfo = new UserSessionInfo();
-
 
153
				Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");
-
 
154
		    	tempCookie = cookie1;
-
 
155
			}
-
 
156
		}
75
	}
157
	}
-
 
158
 
-
 
159
 
-
 
160
	
-
 
161
//	public String getEmail(){
-
 
162
//		if(this.session.getAttribute("email") != null){
-
 
163
//			return this.session.getAttribute("email").toString();
-
 
164
//		}else{
-
 
165
//			return "";
-
 
166
//		}
-
 
167
//	}
-
 
168
 
-
 
169
	
-
 
170
	public String getEmail(){
-
 
171
		return userinfo.getEmail();
-
 
172
	}
-
 
173
 
-
 
174
	
-
 
175
//	public long getNumberOfCartItems(){
-
 
176
//		if(this.session.getAttribute("totalitems") != null ){
-
 
177
//			return Long.parseLong(this.session.getAttribute("totalitems").toString());
-
 
178
//		}else{
-
 
179
//			return 0;
-
 
180
//		}
-
 
181
//	}
-
 
182
 
-
 
183
	
-
 
184
	public int getNumberOfCartItems(){
-
 
185
		return this.userinfo.getTotalItems();
-
 
186
	}
-
 
187
	
76
	
188
	
77
	public void removeSessionOnLogout(){
189
	public void removeSessionOnLogout(){
78
		if(this.session.getAttribute("loggedin") != null ){
190
		if(this.session.getAttribute("loggedin") != null ){
79
			this.session.removeAttribute("loggedin");
191
			this.session.removeAttribute("loggedin");
80
		}
192
		}
Line 82... Line 194...
82
			this.session.removeAttribute("userid");
194
			this.session.removeAttribute("userid");
83
		}
195
		}
84
		if( this.session.getAttribute("issessionid") != null){
196
		if( this.session.getAttribute("issessionid") != null){
85
			this.session.removeAttribute("issessionid");
197
			this.session.removeAttribute("issessionid");
86
		}
198
		}
-
 
199
 
87
	}
200
	}
88
	
201
	
-
 
202
	
89
	public void setSessionAndCookies(){
203
	public void setSessionAndCookies(){
90
		
-
 
91
		/*Following steps will be executed on each of the requests
204
		/*Following steps will be executed on each of the requests
92
		 * 		Check Session: 
205
		 * 		Check Session: 
93
		 * 			IF user is loggedin 
206
		 * 			IF user is loggedin 
94
		 * 				- OK. Nothing to do. Allow access to the resources
207
		 * 				- OK. Nothing to do. Allow access to the resources
95
		 * 			ELSE check cookies 
208
		 * 			ELSE check cookies 
Line 150... Line 263...
150
//			System.out.println("Session is already set with sessionid   :" + userId );
263
//			System.out.println("Session is already set with sessionid   :" + userId );
151
//		}
264
//		}
152
		
265
		
153
	}
266
	}
154
 
267
 
-
 
268
	
-
 
269
	String getEmailId(long userId){
-
 
270
		UserContextServiceClient userContextServiceClient = null;
-
 
271
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
-
 
272
		String email = "";
-
 
273
		
-
 
274
		try {
-
 
275
			userContextServiceClient = new UserContextServiceClient();
-
 
276
			userClient = userContextServiceClient.getClient();
-
 
277
			email = userClient.getPrimaryInfo(userId, false).getEmail();
-
 
278
		} catch (UserContextException e) {
-
 
279
			// TODO Auto-generated catch block
-
 
280
			e.printStackTrace();
-
 
281
		} catch (TException e) {
-
 
282
			// TODO Auto-generated catch block
-
 
283
			e.printStackTrace();
-
 
284
		} catch (Exception e) {
-
 
285
			// TODO Auto-generated catch block
-
 
286
			e.printStackTrace();
-
 
287
		}
-
 
288
		
-
 
289
		return email; 
-
 
290
	}
-
 
291
	
-
 
292
	
155
	public boolean isUserLoggedIn(long userId){
293
	public boolean isUserLoggedIn(long userId){
156
		UserContextServiceClient userContextServiceClient = null;
294
		UserContextServiceClient userContextServiceClient = null;
157
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
295
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
158
		boolean isLoggedIn = false;
296
		boolean isLoggedIn = false;
159
		
297
		
160
		try {
298
		try {
161
			userContextServiceClient = new UserContextServiceClient();
299
			userContextServiceClient = new UserContextServiceClient();
162
			userClient = userContextServiceClient.getClient();
300
			userClient = userContextServiceClient.getClient();
163
			userClient.getState(userId, false).isIsLoggedIn();
301
			isLoggedIn = userClient.getState(userId, false).isIsLoggedIn();
164
		} catch (UserContextException e) {
302
		} catch (UserContextException e) {
165
			// TODO Auto-generated catch block
303
			// TODO Auto-generated catch block
166
			e.printStackTrace();
304
			e.printStackTrace();
167
		} catch (TException e) {
305
		} catch (TException e) {
168
			// TODO Auto-generated catch block
306
			// TODO Auto-generated catch block
Line 220... Line 358...
220
		if(foundUserIdCookie){
358
		if(foundUserIdCookie){
221
			if(isUserLoggedIn(userId)){
359
			if(isUserLoggedIn(userId)){
222
				this.session.setAttribute("userid", userId+"");
360
				this.session.setAttribute("userid", userId+"");
223
				this.session.setAttribute("loggedin", "true");
361
				this.session.setAttribute("loggedin", "true");
224
				this.session.setAttribute("issessionid", "false");
362
				this.session.setAttribute("issessionid", "false");
-
 
363
				
-
 
364
				this.session.setAttribute("email", getEmailId(userId));
-
 
365
				
-
 
366
				this.session.setAttribute("totalitems", getNumberOfCartItems());
225
			}
367
			}
226
			else{
368
			else{
227
				/* 	may be something different can be done as commented
369
				/* 	may be something different can be done as commented
228
				 *	this.session.setAttribute("userid", userId+"");
370
				 *	this.session.setAttribute("userid", userId+"");
229
				 *	this.session.setAttribute("loggedin", "false");
371
				 *	this.session.setAttribute("loggedin", "false");
230
				 *	this.session.setAttribute("issessionid", "false");		
372
				 *	this.session.setAttribute("issessionid", "false");		
231
				 */
373
				 */
-
 
374
				if(!foundSessionIdCookie){
-
 
375
					sessionId = getNewUserSessionId();
-
 
376
				}
-
 
377
				
-
 
378
				this.session.setAttribute("userid", sessionId+"");
-
 
379
				this.session.setAttribute("loggedin", "false");
-
 
380
				this.session.setAttribute("issessionid", "true");
-
 
381
 
232
			    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
382
			    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
233
			        Cookie cookie1 = cookies[loopIndex];
383
			        Cookie cookie1 = cookies[loopIndex];
234
			        if (cookie1.getName().equals("userid")) {
384
			        if (cookie1.getName().equals("userid")) {
235
			        	cookie1.setMaxAge(0);
385
			        	cookie1.setMaxAge(0);
236
			        	//cookie1.setPath(cookie1.getPath());
386
			        	//cookie1.setPath(cookie1.getPath());
Line 242... Line 392...
242
		}
392
		}
243
		else{  
393
		else{  
244
			if(foundSessionIdCookie){
394
			if(foundSessionIdCookie){
245
				this.session.setAttribute("userid", sessionId+"");
395
				this.session.setAttribute("userid", sessionId+"");
246
				this.session.setAttribute("loggedin", "false");
396
				this.session.setAttribute("loggedin", "false");
247
				this.session.setAttribute("issessionid", "true");			
397
				this.session.setAttribute("issessionid", "true");
-
 
398
				this.session.setAttribute("totalitems", "0");
248
			}
399
			}
249
			else{
400
			else{
250
				sessionId = getNewUserSessionId();
401
				sessionId = getNewUserSessionId();
251
				this.session.setAttribute("userid", sessionId+"");
402
				this.session.setAttribute("userid", sessionId+"");
252
				this.session.setAttribute("loggedin", "false");
403
				this.session.setAttribute("loggedin", "false");
253
				this.session.setAttribute("issessionid", "true");			
404
				this.session.setAttribute("issessionid", "true");			
-
 
405
				this.session.setAttribute("totalitems", "0");
254
				
406
				
255
				Cookie cookie1 = new Cookie("sessionid", sessionId+"");
407
				Cookie cookie1 = new Cookie("sessionid", sessionId+"");
256
		    	tempCookie = cookie1;
408
		    	tempCookie = cookie1;
257
				
409
				
258
			}
410
			}