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
//    public boolean isLoggedIn = false;
45
//    public boolean isSessionId = true;
46
//    public long userId = 0;
47
    //private String email = "";
48
 
416 rajveer 49
    Cookie tempCookie = null;
50
 
51
	private static Log log = LogFactory.getLog(BaseController.class);
52
 
53
	public BaseController() {
54
		//setSessionAndCookies();
55
		// TODO Auto-generated constructor stub
56
	}
410 rajveer 57
	public Map getCookiesMap() {
58
		return cookiesMap;
59
	}
60
 
61
	@Override
62
	public void setCookiesMap(Map cookiesMap) {
63
		this.cookiesMap = cookiesMap;
64
	}
65
 
66
	@Override
67
	public void setServletResponse(HttpServletResponse response)
68
	{
69
		this.response = response;
416 rajveer 70
		if(tempCookie != null){
71
			this.response.addCookie(tempCookie);
72
		}
73
		//removeSessionOnLogout();
410 rajveer 74
	}
416 rajveer 75
 
76
	@Override
77
	public void setServletRequest(HttpServletRequest request){
78
		this.request = request;
79
		this.session = request.getSession();
419 rajveer 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;
416 rajveer 109
 
419 rajveer 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{  
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
		}
416 rajveer 157
	}
419 rajveer 158
 
159
 
416 rajveer 160
 
419 rajveer 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
 
188
 
416 rajveer 189
	public void removeSessionOnLogout(){
190
		if(this.session.getAttribute("loggedin") != null ){
191
			this.session.removeAttribute("loggedin");
192
		}
193
		if(this.session.getAttribute("userid") != null){ 
194
			this.session.removeAttribute("userid");
195
		}
196
		if( this.session.getAttribute("issessionid") != null){
197
			this.session.removeAttribute("issessionid");
198
		}
419 rajveer 199
 
416 rajveer 200
	}
201
 
419 rajveer 202
 
416 rajveer 203
	public void setSessionAndCookies(){
204
		/*Following steps will be executed on each of the requests
205
		 * 		Check Session: 
206
		 * 			IF user is loggedin 
207
		 * 				- OK. Nothing to do. Allow access to the resources
208
		 * 			ELSE check cookies 
209
		 *  			IF UserID is set
210
		 *  				IF User is logged into backend. 
211
		 *  					Set user as loogedin in Session.
212
		 *  					Set SessionID in session
213
		 *  				ELSE
214
		 *  					Remove UserID from cookies.
215
		 *  			ELSE 
216
		 *  				IF SessionID is set	
217
		 *  					OK....set session id in session also
218
		 *  				ELSE Create SessionID and Set SessionID in cookies and Session
219
		 * 			ELSE IF SessionID is set in session
220
		 * 				- OK.
221
		 *  	
222
		 */
223
 
224
		String loggedIn = null;
225
		String userId = null;
226
		String isSessionId = null;
227
 
228
 
229
		if(this.session.getAttribute("loggedin") != null ){
230
			loggedIn = this.session.getAttribute("loggedin").toString();
231
		}
232
		if(this.session.getAttribute("userid") != null){ 
233
			userId = this.session.getAttribute("userid").toString();
234
		}
235
		if( this.session.getAttribute("issessionid") != null){
236
				isSessionId = this.session.getAttribute("issessionid").toString();
237
		}
238
 
239
 
240
 
241
 
242
		if(loggedIn != null && isSessionId != null && userId != null ){
243
			if(loggedIn.equals("true")){
244
				if(isSessionId.equals("false")){
245
					System.out.println("Logged on user id is   :" + userId );	
246
				}else{
247
					// if we reached here something is wrong.
248
					System.out.println("Something went wrong... Dude !!!");
249
				}
250
			}else{ 
251
				if(isSessionId.equals("true")){
252
					System.out.println("Session is already set with sessionid   :" + userId );
253
				}else{
254
					// if we reached here something is wrong.
255
					System.out.println("Something went wrong... Dude !!!");
256
				}
257
			}
258
		}
259
		else if(!getLoginSessionInfoFromCookies()){
260
			System.out.println("Cookies are not set. Lookes like new user. Setting all the things.");
261
		}
262
//		else if(isSessionId != null && userId != null){
263
//			System.out.println("Session is already set with sessionid   :" + userId );
264
//		}
265
 
266
	}
267
 
419 rajveer 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
 
416 rajveer 293
	public boolean isUserLoggedIn(long userId){
294
		UserContextServiceClient userContextServiceClient = null;
295
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
296
		boolean isLoggedIn = false;
297
 
298
		try {
299
			userContextServiceClient = new UserContextServiceClient();
300
			userClient = userContextServiceClient.getClient();
419 rajveer 301
			isLoggedIn = userClient.getState(userId, false).isIsLoggedIn();
416 rajveer 302
		} catch (UserContextException e) {
303
			// TODO Auto-generated catch block
304
			e.printStackTrace();
305
		} catch (TException e) {
306
			// TODO Auto-generated catch block
307
			e.printStackTrace();
308
		} catch (Exception e) {
309
			// TODO Auto-generated catch block
310
			e.printStackTrace();
311
		}
312
 
313
		return isLoggedIn; 
314
	}
315
 
316
 
317
	public long getNewUserSessionId(){
318
		// I feel we dont need to create the context right now. need to discuss
319
//		UserContextServiceClient userContextServiceClient = null;
320
//		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
321
//	
322
//		userContextServiceClient = new UserContextServiceClient();
323
//		userClient = userContextServiceClient.getClient();
324
//		
325
//		UserContext context = new UserContext();
326
//		context.setSessionid(1000);
327
//		context = userClient.createContext(context, false);
328
//		
329
		long sessionId = 12;
330
//		sessionId = context.getSessionid();
331
		return sessionId; 
332
	}
333
 
334
	public boolean getLoginSessionInfoFromCookies(){
335
		Cookie[] cookies = this.request.getCookies();
336
		boolean foundUserIdCookie = false;
337
		boolean foundSessionIdCookie = false;
338
 
339
		long sessionId = 0;
340
		long userId = 0;
341
 
342
		if(cookies != null){
343
		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
344
		        Cookie cookie1 = cookies[loopIndex];
345
		        if (cookie1.getName().equals("userid")) {
346
		            System.out.println("User Id is = " + cookie1.getValue());
347
		            userId = Long.parseLong(cookie1.getValue());
348
		            foundUserIdCookie = true;
349
		        }
350
		        if (cookie1.getName().equals("sessionid")) {
351
		            System.out.println("Session Id is = " + cookie1.getValue());
352
		            sessionId = Long.parseLong(cookie1.getValue());
353
		            foundSessionIdCookie = true;
354
		        }
355
	    	}
356
		}
357
 
358
		if(foundUserIdCookie){
359
			if(isUserLoggedIn(userId)){
360
				this.session.setAttribute("userid", userId+"");
361
				this.session.setAttribute("loggedin", "true");
362
				this.session.setAttribute("issessionid", "false");
419 rajveer 363
 
364
				this.session.setAttribute("email", getEmailId(userId));
365
 
366
				this.session.setAttribute("totalitems", getNumberOfCartItems());
416 rajveer 367
			}
368
			else{
369
				/* 	may be something different can be done as commented
370
				 *	this.session.setAttribute("userid", userId+"");
371
				 *	this.session.setAttribute("loggedin", "false");
372
				 *	this.session.setAttribute("issessionid", "false");		
373
				 */
419 rajveer 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
 
416 rajveer 382
			    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
383
			        Cookie cookie1 = cookies[loopIndex];
384
			        if (cookie1.getName().equals("userid")) {
385
			        	cookie1.setMaxAge(0);
386
			        	//cookie1.setPath(cookie1.getPath());
387
						//cookie1.setDomain(cookie1.getDomain());
388
			        	tempCookie = cookie1;
389
			        }
390
				}
391
			}
392
		}
393
		else{  
394
			if(foundSessionIdCookie){
395
				this.session.setAttribute("userid", sessionId+"");
396
				this.session.setAttribute("loggedin", "false");
419 rajveer 397
				this.session.setAttribute("issessionid", "true");
398
				this.session.setAttribute("totalitems", "0");
416 rajveer 399
			}
400
			else{
401
				sessionId = getNewUserSessionId();
402
				this.session.setAttribute("userid", sessionId+"");
403
				this.session.setAttribute("loggedin", "false");
404
				this.session.setAttribute("issessionid", "true");			
419 rajveer 405
				this.session.setAttribute("totalitems", "0");
416 rajveer 406
 
407
				Cookie cookie1 = new Cookie("sessionid", sessionId+"");
408
		    	tempCookie = cookie1;
409
 
410
			}
411
		}
412
	return false;		
413
	}
414
 
415
 
317 ashish 416
}
416 rajveer 417
 
418
 
419
 
420
	/*
421
			}	
422
		}
423
 
424
	else if(getCookiesMap() != null){
425
			Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
426
			log.info("login cookie name is " + loginCookie.getName() );
427
			log.info("login cookie value is " + loginCookie.getValue());
428
		}
429
 
430
 
431
 
432
		if(loginInfo != null && loginInfo.equals("true")){
433
			System.out.println("user id is   :" + this.session.getAttribute("userid").toString());
434
		}
435
 
436
 
437
 
438
			this.session.setAttribute("loggedin", "true");
439
			this.session.setAttribute("userid", "rajveer");
440
			System.out.println("setting user as logged in ");
441
		}
442
	*/
443
 
444
 
445