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