Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
317 ashish 1
/**
2
 * 
3
 */
4
 
5
 
6
package in.shop2020.serving.controllers;
410 rajveer 7
 
416 rajveer 8
import in.shop2020.model.v1.user.UserContext;
9
import in.shop2020.model.v1.user.UserContextException;
419 rajveer 10
import in.shop2020.model.v1.user.UserInternalInfo;
11
import in.shop2020.serving.services.UserSessionInfo;
12
import in.shop2020.serving.utils.Utils;
416 rajveer 13
import in.shop2020.thrift.clients.ShoppingCartClient;
14
import in.shop2020.thrift.clients.UserContextServiceClient;
15
 
410 rajveer 16
import java.util.Map;
17
 
416 rajveer 18
import javax.servlet.http.Cookie;
19
import javax.servlet.http.HttpServletRequest;
410 rajveer 20
import javax.servlet.http.HttpServletResponse;
416 rajveer 21
import javax.servlet.http.HttpSession;
410 rajveer 22
 
416 rajveer 23
import org.apache.juli.logging.Log;
24
import org.apache.juli.logging.LogFactory;
410 rajveer 25
import org.apache.struts2.interceptor.CookiesAware;
416 rajveer 26
import org.apache.struts2.interceptor.ServletRequestAware;
410 rajveer 27
import org.apache.struts2.interceptor.ServletResponseAware;
416 rajveer 28
import org.apache.struts2.interceptor.SessionAware;
29
import org.apache.thrift.TException;
410 rajveer 30
 
317 ashish 31
/**
32
 * Base class for all user action handlers i.e. controllers
33
 * 
34
 * @author naveen
35
 *
36
 */
416 rajveer 37
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
410 rajveer 38
	private Map cookiesMap;
416 rajveer 39
    protected HttpServletResponse response;
40
    protected HttpServletRequest request;
41
    protected HttpSession session;
419 rajveer 42
 
43
    UserSessionInfo userinfo = null;
44
 
416 rajveer 45
    Cookie tempCookie = null;
46
 
47
	private static Log log = LogFactory.getLog(BaseController.class);
48
 
49
	public BaseController() {
50
	}
410 rajveer 51
	public Map getCookiesMap() {
52
		return cookiesMap;
53
	}
54
 
55
	@Override
56
	public void setCookiesMap(Map cookiesMap) {
57
		this.cookiesMap = cookiesMap;
58
	}
59
 
60
	@Override
61
	public void setServletResponse(HttpServletResponse response)
62
	{
63
		this.response = response;
416 rajveer 64
		if(tempCookie != null){
65
			this.response.addCookie(tempCookie);
66
		}
410 rajveer 67
	}
416 rajveer 68
 
69
	@Override
70
	public void setServletRequest(HttpServletRequest request){
71
		this.request = request;
72
		this.session = request.getSession();
419 rajveer 73
		setUserSessionInfo();
74
	}
75
 
76
	public void removeUserSessionInfo(){
77
		if(this.session.getAttribute("userinfo") != null ){
78
			this.session.removeAttribute("userinfo");
79
		}
80
	}
81
 
82
 
83
	public void setUserSessionInfo(){
84
		if(this.session.getAttribute("userinfo") != null ){
85
			userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
86
		}else{
87
			processCookiesInfo();
88
			//userinfo = new UserSessionInfo();
89
		}
90
		this.session.setAttribute("userinfo",userinfo);
91
	}
92
 
93
	public void processCookiesInfo(){
94
		Cookie[] cookies = this.request.getCookies();
95
		boolean foundUserIdCookie = false;
96
		boolean foundSessionIdCookie = false;
97
		long userId = 0 ;
98
		long sessionId = 0;
416 rajveer 99
 
419 rajveer 100
		if(cookies != null){
101
		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
102
		        Cookie cookie1 = cookies[loopIndex];
103
		        if (cookie1.getName().equals("userid")) {
104
		            System.out.println("User Id is = " + cookie1.getValue());
105
		            userId = Long.parseLong(cookie1.getValue());
106
		            foundUserIdCookie = true;
107
		        }
108
		        if (cookie1.getName().equals("sessionid")) {
109
		            System.out.println("Session Id is = " + cookie1.getValue());
110
		            sessionId = Long.parseLong(cookie1.getValue());
111
		            foundSessionIdCookie = true;
112
		        }
113
	    	}
114
		}
115
 
116
		if(foundUserIdCookie){
117
			if(Utils.isUserLoggedIn(userId)){
118
				userinfo = new UserSessionInfo(userId, false);
119
			}
120
			else{
121
				if(foundSessionIdCookie){
122
					userinfo = new UserSessionInfo(sessionId, true);	
123
				}else{
124
					userinfo = new UserSessionInfo();
125
				    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
126
				        Cookie cookie1 = cookies[loopIndex];
127
				        if (cookie1.getName().equals("userid")) {
128
				        	cookie1.setMaxAge(0);
129
				        	//cookie1.setPath(cookie1.getPath());
130
							//cookie1.setDomain(cookie1.getDomain());
131
				        	tempCookie = cookie1;
132
				        }
133
					}
134
				}
135
			}
136
		}			
137
		else{  
138
			if(foundSessionIdCookie){
139
				userinfo = new UserSessionInfo(sessionId, true);			
140
			}
141
			else{
142
				userinfo = new UserSessionInfo();
143
				Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");
144
		    	tempCookie = cookie1;
145
			}
146
		}
416 rajveer 147
	}
419 rajveer 148
 
424 rajveer 149
	public String getEmail(){
150
		return userinfo.getEmail();
151
	}
419 rajveer 152
 
424 rajveer 153
	public String getNameOfUser(){
154
		return userinfo.getNameOfUser();
155
	}
156
 
157
}
158
	/*
416 rajveer 159
 
419 rajveer 160
//	public String getEmail(){
161
//		if(this.session.getAttribute("email") != null){
162
//			return this.session.getAttribute("email").toString();
163
//		}else{
164
//			return "";
165
//		}
166
//	}
167
 
168
 
169
	public String getEmail(){
170
		return userinfo.getEmail();
171
	}
172
 
173
 
174
//	public long getNumberOfCartItems(){
175
//		if(this.session.getAttribute("totalitems") != null ){
176
//			return Long.parseLong(this.session.getAttribute("totalitems").toString());
177
//		}else{
178
//			return 0;
179
//		}
180
//	}
181
 
182
 
183
	public int getNumberOfCartItems(){
184
		return this.userinfo.getTotalItems();
185
	}
186
 
187
 
416 rajveer 188
	public void removeSessionOnLogout(){
189
		if(this.session.getAttribute("loggedin") != null ){
190
			this.session.removeAttribute("loggedin");
191
		}
192
		if(this.session.getAttribute("userid") != null){ 
193
			this.session.removeAttribute("userid");
194
		}
195
		if( this.session.getAttribute("issessionid") != null){
196
			this.session.removeAttribute("issessionid");
197
		}
419 rajveer 198
 
416 rajveer 199
	}
200
 
419 rajveer 201
 
416 rajveer 202
	public void setSessionAndCookies(){
424 rajveer 203
		 *Following steps will be executed on each of the requests
416 rajveer 204
		 * 		Check Session: 
205
		 * 			IF user is loggedin 
206
		 * 				- OK. Nothing to do. Allow access to the resources
207
		 * 			ELSE check cookies 
208
		 *  			IF UserID is set
209
		 *  				IF User is logged into backend. 
210
		 *  					Set user as loogedin in Session.
211
		 *  					Set SessionID in session
212
		 *  				ELSE
213
		 *  					Remove UserID from cookies.
214
		 *  			ELSE 
215
		 *  				IF SessionID is set	
216
		 *  					OK....set session id in session also
217
		 *  				ELSE Create SessionID and Set SessionID in cookies and Session
218
		 * 			ELSE IF SessionID is set in session
219
		 * 				- OK.
220
		 *  	
424 rajveer 221
		 *
416 rajveer 222
 
223
		String loggedIn = null;
224
		String userId = null;
225
		String isSessionId = null;
226
 
227
 
228
		if(this.session.getAttribute("loggedin") != null ){
229
			loggedIn = this.session.getAttribute("loggedin").toString();
230
		}
231
		if(this.session.getAttribute("userid") != null){ 
232
			userId = this.session.getAttribute("userid").toString();
233
		}
234
		if( this.session.getAttribute("issessionid") != null){
235
				isSessionId = this.session.getAttribute("issessionid").toString();
236
		}
237
 
238
 
239
 
240
 
241
		if(loggedIn != null && isSessionId != null && userId != null ){
242
			if(loggedIn.equals("true")){
243
				if(isSessionId.equals("false")){
244
					System.out.println("Logged on user id is   :" + userId );	
245
				}else{
246
					// if we reached here something is wrong.
247
					System.out.println("Something went wrong... Dude !!!");
248
				}
249
			}else{ 
250
				if(isSessionId.equals("true")){
251
					System.out.println("Session is already set with sessionid   :" + userId );
252
				}else{
253
					// if we reached here something is wrong.
254
					System.out.println("Something went wrong... Dude !!!");
255
				}
256
			}
257
		}
258
		else if(!getLoginSessionInfoFromCookies()){
259
			System.out.println("Cookies are not set. Lookes like new user. Setting all the things.");
260
		}
261
//		else if(isSessionId != null && userId != null){
262
//			System.out.println("Session is already set with sessionid   :" + userId );
263
//		}
264
 
265
	}
266
 
419 rajveer 267
 
268
	String getEmailId(long userId){
269
		UserContextServiceClient userContextServiceClient = null;
270
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
271
		String email = "";
272
 
273
		try {
274
			userContextServiceClient = new UserContextServiceClient();
275
			userClient = userContextServiceClient.getClient();
276
			email = userClient.getPrimaryInfo(userId, false).getEmail();
277
		} catch (UserContextException e) {
278
			// TODO Auto-generated catch block
279
			e.printStackTrace();
280
		} catch (TException e) {
281
			// TODO Auto-generated catch block
282
			e.printStackTrace();
283
		} catch (Exception e) {
284
			// TODO Auto-generated catch block
285
			e.printStackTrace();
286
		}
287
 
288
		return email; 
289
	}
290
 
291
 
416 rajveer 292
	public boolean isUserLoggedIn(long userId){
293
		UserContextServiceClient userContextServiceClient = null;
294
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
295
		boolean isLoggedIn = false;
296
 
297
		try {
298
			userContextServiceClient = new UserContextServiceClient();
299
			userClient = userContextServiceClient.getClient();
419 rajveer 300
			isLoggedIn = userClient.getState(userId, false).isIsLoggedIn();
416 rajveer 301
		} catch (UserContextException e) {
302
			// TODO Auto-generated catch block
303
			e.printStackTrace();
304
		} catch (TException e) {
305
			// TODO Auto-generated catch block
306
			e.printStackTrace();
307
		} catch (Exception e) {
308
			// TODO Auto-generated catch block
309
			e.printStackTrace();
310
		}
311
 
312
		return isLoggedIn; 
313
	}
314
 
315
 
316
	public long getNewUserSessionId(){
317
		// I feel we dont need to create the context right now. need to discuss
318
//		UserContextServiceClient userContextServiceClient = null;
319
//		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
320
//	
321
//		userContextServiceClient = new UserContextServiceClient();
322
//		userClient = userContextServiceClient.getClient();
323
//		
324
//		UserContext context = new UserContext();
325
//		context.setSessionid(1000);
326
//		context = userClient.createContext(context, false);
327
//		
328
		long sessionId = 12;
329
//		sessionId = context.getSessionid();
330
		return sessionId; 
331
	}
332
 
333
	public boolean getLoginSessionInfoFromCookies(){
334
		Cookie[] cookies = this.request.getCookies();
335
		boolean foundUserIdCookie = false;
336
		boolean foundSessionIdCookie = false;
337
 
338
		long sessionId = 0;
339
		long userId = 0;
340
 
341
		if(cookies != null){
342
		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
343
		        Cookie cookie1 = cookies[loopIndex];
344
		        if (cookie1.getName().equals("userid")) {
345
		            System.out.println("User Id is = " + cookie1.getValue());
346
		            userId = Long.parseLong(cookie1.getValue());
347
		            foundUserIdCookie = true;
348
		        }
349
		        if (cookie1.getName().equals("sessionid")) {
350
		            System.out.println("Session Id is = " + cookie1.getValue());
351
		            sessionId = Long.parseLong(cookie1.getValue());
352
		            foundSessionIdCookie = true;
353
		        }
354
	    	}
355
		}
356
 
357
		if(foundUserIdCookie){
358
			if(isUserLoggedIn(userId)){
359
				this.session.setAttribute("userid", userId+"");
360
				this.session.setAttribute("loggedin", "true");
361
				this.session.setAttribute("issessionid", "false");
419 rajveer 362
 
363
				this.session.setAttribute("email", getEmailId(userId));
364
 
365
				this.session.setAttribute("totalitems", getNumberOfCartItems());
416 rajveer 366
			}
367
			else{
368
				/* 	may be something different can be done as commented
369
				 *	this.session.setAttribute("userid", userId+"");
370
				 *	this.session.setAttribute("loggedin", "false");
371
				 *	this.session.setAttribute("issessionid", "false");		
424 rajveer 372
				 *
419 rajveer 373
				if(!foundSessionIdCookie){
374
					sessionId = getNewUserSessionId();
375
				}
376
 
377
				this.session.setAttribute("userid", sessionId+"");
378
				this.session.setAttribute("loggedin", "false");
379
				this.session.setAttribute("issessionid", "true");
380
 
416 rajveer 381
			    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
382
			        Cookie cookie1 = cookies[loopIndex];
383
			        if (cookie1.getName().equals("userid")) {
384
			        	cookie1.setMaxAge(0);
385
			        	//cookie1.setPath(cookie1.getPath());
386
						//cookie1.setDomain(cookie1.getDomain());
387
			        	tempCookie = cookie1;
388
			        }
389
				}
390
			}
391
		}
392
		else{  
393
			if(foundSessionIdCookie){
394
				this.session.setAttribute("userid", sessionId+"");
395
				this.session.setAttribute("loggedin", "false");
419 rajveer 396
				this.session.setAttribute("issessionid", "true");
397
				this.session.setAttribute("totalitems", "0");
416 rajveer 398
			}
399
			else{
400
				sessionId = getNewUserSessionId();
401
				this.session.setAttribute("userid", sessionId+"");
402
				this.session.setAttribute("loggedin", "false");
403
				this.session.setAttribute("issessionid", "true");			
419 rajveer 404
				this.session.setAttribute("totalitems", "0");
416 rajveer 405
 
406
				Cookie cookie1 = new Cookie("sessionid", sessionId+"");
407
		    	tempCookie = cookie1;
408
 
409
			}
410
		}
411
	return false;		
412
	}
413
 
414
 
317 ashish 415
}
416 rajveer 416
 
417
 
418
 
419
			}	
420
		}
421
 
422
	else if(getCookiesMap() != null){
423
			Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
424
			log.info("login cookie name is " + loginCookie.getName() );
425
			log.info("login cookie value is " + loginCookie.getValue());
426
		}
427
 
428
 
429
 
430
		if(loginInfo != null && loginInfo.equals("true")){
431
			System.out.println("user id is   :" + this.session.getAttribute("userid").toString());
432
		}
433
 
434
 
435
 
436
			this.session.setAttribute("loggedin", "true");
437
			this.session.setAttribute("userid", "rajveer");
438
			System.out.println("setting user as logged in ");
439
		}
440
	*/
441
 
442
 
443