Subversion Repositories SmartDukaan

Rev

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

Rev 410 Rev 416
Line 3... Line 3...
3
 */
3
 */
4
 
4
 
5
 
5
 
6
package in.shop2020.serving.controllers;
6
package in.shop2020.serving.controllers;
7
 
7
 
-
 
8
import in.shop2020.model.v1.user.UserContext;
-
 
9
import in.shop2020.model.v1.user.UserContextException;
-
 
10
import in.shop2020.thrift.clients.ShoppingCartClient;
-
 
11
import in.shop2020.thrift.clients.UserContextServiceClient;
-
 
12
 
8
import java.util.Map;
13
import java.util.Map;
9
 
14
 
-
 
15
import javax.servlet.http.Cookie;
-
 
16
import javax.servlet.http.HttpServletRequest;
10
import javax.servlet.http.HttpServletResponse;
17
import javax.servlet.http.HttpServletResponse;
-
 
18
import javax.servlet.http.HttpSession;
11
 
19
 
-
 
20
import org.apache.juli.logging.Log;
-
 
21
import org.apache.juli.logging.LogFactory;
12
import org.apache.struts2.interceptor.CookiesAware;
22
import org.apache.struts2.interceptor.CookiesAware;
-
 
23
import org.apache.struts2.interceptor.ServletRequestAware;
13
import org.apache.struts2.interceptor.ServletResponseAware;
24
import org.apache.struts2.interceptor.ServletResponseAware;
-
 
25
import org.apache.struts2.interceptor.SessionAware;
-
 
26
import org.apache.thrift.TException;
14
 
27
 
15
/**
28
/**
16
 * Base class for all user action handlers i.e. controllers
29
 * Base class for all user action handlers i.e. controllers
17
 * 
30
 * 
18
 * @author naveen
31
 * @author naveen
19
 *
32
 *
20
 */
33
 */
21
public abstract class BaseController implements CookiesAware, ServletResponseAware {
34
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
22
	private Map cookiesMap;
35
	private Map cookiesMap;
23
    private HttpServletResponse response;
36
    protected HttpServletResponse response;
-
 
37
    protected HttpServletRequest request;
-
 
38
    protected HttpSession session;
-
 
39
//    private boolean isLoggedIn = ;
-
 
40
//    private boolean isSessionId = true;
-
 
41
//    long userId = 0;
-
 
42
    Cookie tempCookie = null;
-
 
43
    
-
 
44
	private static Log log = LogFactory.getLog(BaseController.class);
24
 
45
	
-
 
46
	public BaseController() {
-
 
47
		//setSessionAndCookies();
-
 
48
		// TODO Auto-generated constructor stub
-
 
49
	}
25
	public Map getCookiesMap() {
50
	public Map getCookiesMap() {
26
		return cookiesMap;
51
		return cookiesMap;
27
	}
52
	}
28
	
53
	
29
	@Override
54
	@Override
Line 33... Line 58...
33
	
58
	
34
	@Override
59
	@Override
35
	public void setServletResponse(HttpServletResponse response)
60
	public void setServletResponse(HttpServletResponse response)
36
	{
61
	{
37
		this.response = response;
62
		this.response = response;
-
 
63
		if(tempCookie != null){
-
 
64
			this.response.addCookie(tempCookie);
-
 
65
		}
-
 
66
		//removeSessionOnLogout();
-
 
67
	}
-
 
68
	
-
 
69
	@Override
-
 
70
	public void setServletRequest(HttpServletRequest request){
-
 
71
		this.request = request;
-
 
72
		this.session = request.getSession();
-
 
73
		
-
 
74
		setSessionAndCookies();
-
 
75
	}
-
 
76
	
-
 
77
	public void removeSessionOnLogout(){
-
 
78
		if(this.session.getAttribute("loggedin") != null ){
-
 
79
			this.session.removeAttribute("loggedin");
-
 
80
		}
-
 
81
		if(this.session.getAttribute("userid") != null){ 
-
 
82
			this.session.removeAttribute("userid");
-
 
83
		}
-
 
84
		if( this.session.getAttribute("issessionid") != null){
-
 
85
			this.session.removeAttribute("issessionid");
-
 
86
		}
-
 
87
	}
-
 
88
	
-
 
89
	public void setSessionAndCookies(){
-
 
90
		
-
 
91
		/*Following steps will be executed on each of the requests
-
 
92
		 * 		Check Session: 
-
 
93
		 * 			IF user is loggedin 
-
 
94
		 * 				- OK. Nothing to do. Allow access to the resources
-
 
95
		 * 			ELSE check cookies 
-
 
96
		 *  			IF UserID is set
-
 
97
		 *  				IF User is logged into backend. 
-
 
98
		 *  					Set user as loogedin in Session.
-
 
99
		 *  					Set SessionID in session
-
 
100
		 *  				ELSE
-
 
101
		 *  					Remove UserID from cookies.
-
 
102
		 *  			ELSE 
-
 
103
		 *  				IF SessionID is set	
-
 
104
		 *  					OK....set session id in session also
-
 
105
		 *  				ELSE Create SessionID and Set SessionID in cookies and Session
-
 
106
		 * 			ELSE IF SessionID is set in session
-
 
107
		 * 				- OK.
-
 
108
		 *  	
-
 
109
		 */
-
 
110
		
-
 
111
		String loggedIn = null;
-
 
112
		String userId = null;
-
 
113
		String isSessionId = null;
-
 
114
		
-
 
115
		
-
 
116
		if(this.session.getAttribute("loggedin") != null ){
-
 
117
			loggedIn = this.session.getAttribute("loggedin").toString();
-
 
118
		}
-
 
119
		if(this.session.getAttribute("userid") != null){ 
-
 
120
			userId = this.session.getAttribute("userid").toString();
-
 
121
		}
-
 
122
		if( this.session.getAttribute("issessionid") != null){
-
 
123
				isSessionId = this.session.getAttribute("issessionid").toString();
-
 
124
		}
-
 
125
	
-
 
126
				
-
 
127
			
-
 
128
		
-
 
129
		if(loggedIn != null && isSessionId != null && userId != null ){
-
 
130
			if(loggedIn.equals("true")){
-
 
131
				if(isSessionId.equals("false")){
-
 
132
					System.out.println("Logged on user id is   :" + userId );	
-
 
133
				}else{
-
 
134
					// if we reached here something is wrong.
-
 
135
					System.out.println("Something went wrong... Dude !!!");
-
 
136
				}
-
 
137
			}else{ 
-
 
138
				if(isSessionId.equals("true")){
-
 
139
					System.out.println("Session is already set with sessionid   :" + userId );
-
 
140
				}else{
-
 
141
					// if we reached here something is wrong.
-
 
142
					System.out.println("Something went wrong... Dude !!!");
-
 
143
				}
-
 
144
			}
-
 
145
		}
-
 
146
		else if(!getLoginSessionInfoFromCookies()){
-
 
147
			System.out.println("Cookies are not set. Lookes like new user. Setting all the things.");
-
 
148
		}
-
 
149
//		else if(isSessionId != null && userId != null){
-
 
150
//			System.out.println("Session is already set with sessionid   :" + userId );
-
 
151
//		}
-
 
152
		
38
	}
153
	}
-
 
154
 
-
 
155
	public boolean isUserLoggedIn(long userId){
-
 
156
		UserContextServiceClient userContextServiceClient = null;
-
 
157
		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
-
 
158
		boolean isLoggedIn = false;
-
 
159
		
-
 
160
		try {
-
 
161
			userContextServiceClient = new UserContextServiceClient();
-
 
162
			userClient = userContextServiceClient.getClient();
-
 
163
			userClient.getState(userId, false).isIsLoggedIn();
-
 
164
		} catch (UserContextException e) {
-
 
165
			// TODO Auto-generated catch block
-
 
166
			e.printStackTrace();
-
 
167
		} catch (TException e) {
-
 
168
			// TODO Auto-generated catch block
-
 
169
			e.printStackTrace();
-
 
170
		} catch (Exception e) {
-
 
171
			// TODO Auto-generated catch block
-
 
172
			e.printStackTrace();
-
 
173
		}
-
 
174
		
-
 
175
		return isLoggedIn; 
-
 
176
	}
-
 
177
	
-
 
178
	
-
 
179
	public long getNewUserSessionId(){
-
 
180
		// I feel we dont need to create the context right now. need to discuss
-
 
181
//		UserContextServiceClient userContextServiceClient = null;
-
 
182
//		in.shop2020.model.v1.user.UserContextService.Client userClient = null;
-
 
183
//	
-
 
184
//		userContextServiceClient = new UserContextServiceClient();
-
 
185
//		userClient = userContextServiceClient.getClient();
-
 
186
//		
-
 
187
//		UserContext context = new UserContext();
-
 
188
//		context.setSessionid(1000);
-
 
189
//		context = userClient.createContext(context, false);
-
 
190
//		
-
 
191
		long sessionId = 12;
-
 
192
//		sessionId = context.getSessionid();
-
 
193
		return sessionId; 
-
 
194
	}
-
 
195
	
-
 
196
	public boolean getLoginSessionInfoFromCookies(){
-
 
197
		Cookie[] cookies = this.request.getCookies();
-
 
198
		boolean foundUserIdCookie = false;
-
 
199
		boolean foundSessionIdCookie = false;
-
 
200
		
-
 
201
		long sessionId = 0;
-
 
202
		long userId = 0;
-
 
203
		
-
 
204
		if(cookies != null){
-
 
205
		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
-
 
206
		        Cookie cookie1 = cookies[loopIndex];
-
 
207
		        if (cookie1.getName().equals("userid")) {
-
 
208
		            System.out.println("User Id is = " + cookie1.getValue());
-
 
209
		            userId = Long.parseLong(cookie1.getValue());
-
 
210
		            foundUserIdCookie = true;
-
 
211
		        }
-
 
212
		        if (cookie1.getName().equals("sessionid")) {
-
 
213
		            System.out.println("Session Id is = " + cookie1.getValue());
-
 
214
		            sessionId = Long.parseLong(cookie1.getValue());
-
 
215
		            foundSessionIdCookie = true;
-
 
216
		        }
-
 
217
	    	}
-
 
218
		}
-
 
219
	
-
 
220
		if(foundUserIdCookie){
-
 
221
			if(isUserLoggedIn(userId)){
-
 
222
				this.session.setAttribute("userid", userId+"");
-
 
223
				this.session.setAttribute("loggedin", "true");
-
 
224
				this.session.setAttribute("issessionid", "false");
-
 
225
			}
-
 
226
			else{
-
 
227
				/* 	may be something different can be done as commented
-
 
228
				 *	this.session.setAttribute("userid", userId+"");
-
 
229
				 *	this.session.setAttribute("loggedin", "false");
-
 
230
				 *	this.session.setAttribute("issessionid", "false");		
-
 
231
				 */
-
 
232
			    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
-
 
233
			        Cookie cookie1 = cookies[loopIndex];
-
 
234
			        if (cookie1.getName().equals("userid")) {
-
 
235
			        	cookie1.setMaxAge(0);
-
 
236
			        	//cookie1.setPath(cookie1.getPath());
-
 
237
						//cookie1.setDomain(cookie1.getDomain());
-
 
238
			        	tempCookie = cookie1;
-
 
239
			        }
-
 
240
				}
-
 
241
			}
-
 
242
		}
-
 
243
		else{  
-
 
244
			if(foundSessionIdCookie){
-
 
245
				this.session.setAttribute("userid", sessionId+"");
-
 
246
				this.session.setAttribute("loggedin", "false");
-
 
247
				this.session.setAttribute("issessionid", "true");			
-
 
248
			}
-
 
249
			else{
-
 
250
				sessionId = getNewUserSessionId();
-
 
251
				this.session.setAttribute("userid", sessionId+"");
-
 
252
				this.session.setAttribute("loggedin", "false");
-
 
253
				this.session.setAttribute("issessionid", "true");			
-
 
254
				
-
 
255
				Cookie cookie1 = new Cookie("sessionid", sessionId+"");
-
 
256
		    	tempCookie = cookie1;
-
 
257
				
-
 
258
			}
-
 
259
		}
-
 
260
	return false;		
-
 
261
	}
-
 
262
	
-
 
263
 
39
}
264
}
-
 
265
 
-
 
266
 
-
 
267
 
-
 
268
	/*
-
 
269
			}	
-
 
270
		}
-
 
271
	
-
 
272
	else if(getCookiesMap() != null){
-
 
273
			Cookie loginCookie = (Cookie)getCookiesMap().get("USER_ID");
-
 
274
			log.info("login cookie name is " + loginCookie.getName() );
-
 
275
			log.info("login cookie value is " + loginCookie.getValue());
-
 
276
		}
-
 
277
	
-
 
278
		
-
 
279
 
-
 
280
		if(loginInfo != null && loginInfo.equals("true")){
-
 
281
			System.out.println("user id is   :" + this.session.getAttribute("userid").toString());
-
 
282
		}
-
 
283
		
-
 
284
		
-
 
285
        	    	
-
 
286
			this.session.setAttribute("loggedin", "true");
-
 
287
			this.session.setAttribute("userid", "rajveer");
-
 
288
			System.out.println("setting user as logged in ");
-
 
289
		}
-
 
290
	*/
-
 
291
	
-
 
292
	
-
 
293