Subversion Repositories SmartDukaan

Rev

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

Rev 507 Rev 545
Line 22... Line 22...
22
import org.apache.struts2.interceptor.ServletResponseAware;
22
import org.apache.struts2.interceptor.ServletResponseAware;
23
 
23
 
24
/**
24
/**
25
 * Base class for all user action handlers i.e. controllers
25
 * Base class for all user action handlers i.e. controllers
26
 * 
26
 * 
27
 * @author naveen
27
 * @author rajveer
28
 *
28
 *
29
 */
29
 */
30
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
30
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
31
	private Map cookiesMap;
31
	private Map cookiesMap;
32
    protected HttpServletResponse response;
32
    protected HttpServletResponse response;
Line 39... Line 39...
39
    
39
    
40
	private static Log log = LogFactory.getLog(BaseController.class);
40
	private static Log log = LogFactory.getLog(BaseController.class);
41
	
41
	
42
	public BaseController() {
42
	public BaseController() {
43
	}
43
	}
-
 
44
	
44
	public Map getCookiesMap() {
45
	public Map getCookiesMap() {
45
		return cookiesMap;
46
		return cookiesMap;
46
	}
47
	}
47
	
48
	
48
	@Override
49
	@Override
Line 147... Line 148...
147
	public String getNameOfUser(){
148
	public String getNameOfUser(){
148
		return userinfo.getNameOfUser();
149
		return userinfo.getNameOfUser();
149
	}
150
	}
150
 
151
 
151
}
152
}
152
	/*
-
 
153
	
-
 
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
	
-
 
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
		}
-
 
192
 
-
 
193
	}
-
 
194
	
-
 
195
	
-
 
196
	public void setSessionAndCookies(){
-
 
197
		 *Following steps will be executed on each of the requests
-
 
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
		 *  	
-
 
215
		 *
-
 
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
 
-
 
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
	
-
 
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();
-
 
294
			isLoggedIn = userClient.getState(userId, false).isIsLoggedIn();
-
 
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");
-
 
356
				
-
 
357
				this.session.setAttribute("email", getEmailId(userId));
-
 
358
				
-
 
359
				this.session.setAttribute("totalitems", getNumberOfCartItems());
-
 
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");		
-
 
366
				 *
-
 
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
 
-
 
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");
-
 
390
				this.session.setAttribute("issessionid", "true");
-
 
391
				this.session.setAttribute("totalitems", "0");
-
 
392
			}
-
 
393
			else{
-
 
394
				sessionId = getNewUserSessionId();
-
 
395
				this.session.setAttribute("userid", sessionId+"");
-
 
396
				this.session.setAttribute("loggedin", "false");
-
 
397
				this.session.setAttribute("issessionid", "true");			
-
 
398
				this.session.setAttribute("totalitems", "0");
-
 
399
				
-
 
400
				Cookie cookie1 = new Cookie("sessionid", sessionId+"");
-
 
401
		    	tempCookie = cookie1;
-
 
402
				
-
 
403
			}
-
 
404
		}
-
 
405
	return false;		
-
 
406
	}
-
 
407
	
-
 
408
 
-
 
409
}
-
 
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
	
153
	
437
 
154