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);
507 rajveer 112
 
419 rajveer 113
			}
114
			else{
115
				if(foundSessionIdCookie){
116
					userinfo = new UserSessionInfo(sessionId, true);	
117
				}else{
118
					userinfo = new UserSessionInfo();
119
				    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
120
				        Cookie cookie1 = cookies[loopIndex];
121
				        if (cookie1.getName().equals("userid")) {
122
				        	cookie1.setMaxAge(0);
123
				        	//cookie1.setPath(cookie1.getPath());
124
							//cookie1.setDomain(cookie1.getDomain());
125
				        	tempCookie = cookie1;
126
				        }
127
					}
128
				}
129
			}
130
		}			
131
		else{  
132
			if(foundSessionIdCookie){
133
				userinfo = new UserSessionInfo(sessionId, true);			
134
			}
135
			else{
136
				userinfo = new UserSessionInfo();
137
				Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");
138
		    	tempCookie = cookie1;
139
			}
140
		}
416 rajveer 141
	}
419 rajveer 142
 
424 rajveer 143
	public String getEmail(){
144
		return userinfo.getEmail();
145
	}
419 rajveer 146
 
424 rajveer 147
	public String getNameOfUser(){
148
		return userinfo.getNameOfUser();
149
	}
150
 
151
}
152
	/*
416 rajveer 153
 
419 rajveer 154
//	public String getEmail(){
155
//		if(this.session.getAttribute("email") != null){
156
//			return this.session.getAttribute("email").toString();
157
//		}else{
158
//			return "";
159
//		}
160
//	}
161
 
162
 
163
	public String getEmail(){
164
		return userinfo.getEmail();
165
	}
166
 
167
 
168
//	public long getNumberOfCartItems(){
169
//		if(this.session.getAttribute("totalitems") != null ){
170
//			return Long.parseLong(this.session.getAttribute("totalitems").toString());
171
//		}else{
172
//			return 0;
173
//		}
174
//	}
175
 
176
 
177
	public int getNumberOfCartItems(){
178
		return this.userinfo.getTotalItems();
179
	}
180
 
181
 
416 rajveer 182
	public void removeSessionOnLogout(){
183
		if(this.session.getAttribute("loggedin") != null ){
184
			this.session.removeAttribute("loggedin");
185
		}
186
		if(this.session.getAttribute("userid") != null){ 
187
			this.session.removeAttribute("userid");
188
		}
189
		if( this.session.getAttribute("issessionid") != null){
190
			this.session.removeAttribute("issessionid");
191
		}
419 rajveer 192
 
416 rajveer 193
	}
194
 
419 rajveer 195
 
416 rajveer 196
	public void setSessionAndCookies(){
424 rajveer 197
		 *Following steps will be executed on each of the requests
416 rajveer 198
		 * 		Check Session: 
199
		 * 			IF user is loggedin 
200
		 * 				- OK. Nothing to do. Allow access to the resources
201
		 * 			ELSE check cookies 
202
		 *  			IF UserID is set
203
		 *  				IF User is logged into backend. 
204
		 *  					Set user as loogedin in Session.
205
		 *  					Set SessionID in session
206
		 *  				ELSE
207
		 *  					Remove UserID from cookies.
208
		 *  			ELSE 
209
		 *  				IF SessionID is set	
210
		 *  					OK....set session id in session also
211
		 *  				ELSE Create SessionID and Set SessionID in cookies and Session
212
		 * 			ELSE IF SessionID is set in session
213
		 * 				- OK.
214
		 *  	
424 rajveer 215
		 *
416 rajveer 216
 
217
		String loggedIn = null;
218
		String userId = null;
219
		String isSessionId = null;
220
 
221
 
222
		if(this.session.getAttribute("loggedin") != null ){
223
			loggedIn = this.session.getAttribute("loggedin").toString();
224
		}
225
		if(this.session.getAttribute("userid") != null){ 
226
			userId = this.session.getAttribute("userid").toString();
227
		}
228
		if( this.session.getAttribute("issessionid") != null){
229
				isSessionId = this.session.getAttribute("issessionid").toString();
230
		}
231
 
232
 
233
 
234
 
235
		if(loggedIn != null && isSessionId != null && userId != null ){
236
			if(loggedIn.equals("true")){
237
				if(isSessionId.equals("false")){
238
					System.out.println("Logged on user id is   :" + userId );	
239
				}else{
240
					// if we reached here something is wrong.
241
					System.out.println("Something went wrong... Dude !!!");
242
				}
243
			}else{ 
244
				if(isSessionId.equals("true")){
245
					System.out.println("Session is already set with sessionid   :" + userId );
246
				}else{
247
					// if we reached here something is wrong.
248
					System.out.println("Something went wrong... Dude !!!");
249
				}
250
			}
251
		}
252
		else if(!getLoginSessionInfoFromCookies()){
253
			System.out.println("Cookies are not set. Lookes like new user. Setting all the things.");
254
		}
255
//		else if(isSessionId != null && userId != null){
256
//			System.out.println("Session is already set with sessionid   :" + userId );
257
//		}
258
 
259
	}
260
 
419 rajveer 261
 
262
	String getEmailId(long userId){
263
		UserContextServiceClient userContextServiceClient = null;
264
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
265
		String email = "";
266
 
267
		try {
268
			userContextServiceClient = new UserContextServiceClient();
269
			userClient = userContextServiceClient.getClient();
270
			email = userClient.getPrimaryInfo(userId, false).getEmail();
271
		} catch (UserContextException e) {
272
			// TODO Auto-generated catch block
273
			e.printStackTrace();
274
		} catch (TException e) {
275
			// TODO Auto-generated catch block
276
			e.printStackTrace();
277
		} catch (Exception e) {
278
			// TODO Auto-generated catch block
279
			e.printStackTrace();
280
		}
281
 
282
		return email; 
283
	}
284
 
285
 
416 rajveer 286
	public boolean isUserLoggedIn(long userId){
287
		UserContextServiceClient userContextServiceClient = null;
288
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
289
		boolean isLoggedIn = false;
290
 
291
		try {
292
			userContextServiceClient = new UserContextServiceClient();
293
			userClient = userContextServiceClient.getClient();
419 rajveer 294
			isLoggedIn = userClient.getState(userId, false).isIsLoggedIn();
416 rajveer 295
		} catch (UserContextException e) {
296
			// TODO Auto-generated catch block
297
			e.printStackTrace();
298
		} catch (TException e) {
299
			// TODO Auto-generated catch block
300
			e.printStackTrace();
301
		} catch (Exception e) {
302
			// TODO Auto-generated catch block
303
			e.printStackTrace();
304
		}
305
 
306
		return isLoggedIn; 
307
	}
308
 
309
 
310
	public long getNewUserSessionId(){
311
		// I feel we dont need to create the context right now. need to discuss
312
//		UserContextServiceClient userContextServiceClient = null;
313
//		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
314
//	
315
//		userContextServiceClient = new UserContextServiceClient();
316
//		userClient = userContextServiceClient.getClient();
317
//		
318
//		UserContext context = new UserContext();
319
//		context.setSessionid(1000);
320
//		context = userClient.createContext(context, false);
321
//		
322
		long sessionId = 12;
323
//		sessionId = context.getSessionid();
324
		return sessionId; 
325
	}
326
 
327
	public boolean getLoginSessionInfoFromCookies(){
328
		Cookie[] cookies = this.request.getCookies();
329
		boolean foundUserIdCookie = false;
330
		boolean foundSessionIdCookie = false;
331
 
332
		long sessionId = 0;
333
		long userId = 0;
334
 
335
		if(cookies != null){
336
		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
337
		        Cookie cookie1 = cookies[loopIndex];
338
		        if (cookie1.getName().equals("userid")) {
339
		            System.out.println("User Id is = " + cookie1.getValue());
340
		            userId = Long.parseLong(cookie1.getValue());
341
		            foundUserIdCookie = true;
342
		        }
343
		        if (cookie1.getName().equals("sessionid")) {
344
		            System.out.println("Session Id is = " + cookie1.getValue());
345
		            sessionId = Long.parseLong(cookie1.getValue());
346
		            foundSessionIdCookie = true;
347
		        }
348
	    	}
349
		}
350
 
351
		if(foundUserIdCookie){
352
			if(isUserLoggedIn(userId)){
353
				this.session.setAttribute("userid", userId+"");
354
				this.session.setAttribute("loggedin", "true");
355
				this.session.setAttribute("issessionid", "false");
419 rajveer 356
 
357
				this.session.setAttribute("email", getEmailId(userId));
358
 
359
				this.session.setAttribute("totalitems", getNumberOfCartItems());
416 rajveer 360
			}
361
			else{
362
				/* 	may be something different can be done as commented
363
				 *	this.session.setAttribute("userid", userId+"");
364
				 *	this.session.setAttribute("loggedin", "false");
365
				 *	this.session.setAttribute("issessionid", "false");		
424 rajveer 366
				 *
419 rajveer 367
				if(!foundSessionIdCookie){
368
					sessionId = getNewUserSessionId();
369
				}
370
 
371
				this.session.setAttribute("userid", sessionId+"");
372
				this.session.setAttribute("loggedin", "false");
373
				this.session.setAttribute("issessionid", "true");
374
 
416 rajveer 375
			    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
376
			        Cookie cookie1 = cookies[loopIndex];
377
			        if (cookie1.getName().equals("userid")) {
378
			        	cookie1.setMaxAge(0);
379
			        	//cookie1.setPath(cookie1.getPath());
380
						//cookie1.setDomain(cookie1.getDomain());
381
			        	tempCookie = cookie1;
382
			        }
383
				}
384
			}
385
		}
386
		else{  
387
			if(foundSessionIdCookie){
388
				this.session.setAttribute("userid", sessionId+"");
389
				this.session.setAttribute("loggedin", "false");
419 rajveer 390
				this.session.setAttribute("issessionid", "true");
391
				this.session.setAttribute("totalitems", "0");
416 rajveer 392
			}
393
			else{
394
				sessionId = getNewUserSessionId();
395
				this.session.setAttribute("userid", sessionId+"");
396
				this.session.setAttribute("loggedin", "false");
397
				this.session.setAttribute("issessionid", "true");			
419 rajveer 398
				this.session.setAttribute("totalitems", "0");
416 rajveer 399
 
400
				Cookie cookie1 = new Cookie("sessionid", sessionId+"");
401
		    	tempCookie = cookie1;
402
 
403
			}
404
		}
405
	return false;		
406
	}
407
 
408
 
317 ashish 409
}
416 rajveer 410
 
411
 
412
 
413
			}	
414
		}
415
 
416
	else if(getCookiesMap() != null){
417
			Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
418
			log.info("login cookie name is " + loginCookie.getName() );
419
			log.info("login cookie value is " + loginCookie.getValue());
420
		}
421
 
422
 
423
 
424
		if(loginInfo != null && loginInfo.equals("true")){
425
			System.out.println("user id is   :" + this.session.getAttribute("userid").toString());
426
		}
427
 
428
 
429
 
430
			this.session.setAttribute("loggedin", "true");
431
			this.session.setAttribute("userid", "rajveer");
432
			System.out.println("setting user as logged in ");
433
		}
434
	*/
435
 
436
 
437