Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
317 ashish 1
package in.shop2020.serving.controllers;
410 rajveer 2
 
419 rajveer 3
import in.shop2020.serving.services.UserSessionInfo;
555 chandransh 4
import in.shop2020.serving.utils.DesEncrypter;
419 rajveer 5
import in.shop2020.serving.utils.Utils;
416 rajveer 6
 
555 chandransh 7
import java.util.HashMap;
410 rajveer 8
import java.util.Map;
9
 
416 rajveer 10
import javax.servlet.http.Cookie;
11
import javax.servlet.http.HttpServletRequest;
410 rajveer 12
import javax.servlet.http.HttpServletResponse;
416 rajveer 13
import javax.servlet.http.HttpSession;
410 rajveer 14
 
416 rajveer 15
import org.apache.juli.logging.Log;
16
import org.apache.juli.logging.LogFactory;
410 rajveer 17
import org.apache.struts2.interceptor.CookiesAware;
416 rajveer 18
import org.apache.struts2.interceptor.ServletRequestAware;
410 rajveer 19
import org.apache.struts2.interceptor.ServletResponseAware;
20
 
317 ashish 21
/**
22
 * Base class for all user action handlers i.e. controllers
23
 * 
545 rajveer 24
 * @author rajveer
317 ashish 25
 */
416 rajveer 26
public abstract class BaseController implements  CookiesAware, ServletResponseAware, ServletRequestAware {
555 chandransh 27
	protected Map<String, Cookie> cookiesMap = null;
416 rajveer 28
    protected HttpServletResponse response;
29
    protected HttpServletRequest request;
30
    protected HttpSession session;
555 chandransh 31
    protected UserSessionInfo userinfo = null;
416 rajveer 32
	private static Log log = LogFactory.getLog(BaseController.class);
33
 
555 chandransh 34
    private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
35
 
36
    protected Cookie userCookie = null;
37
 
416 rajveer 38
	public BaseController() {
39
	}
545 rajveer 40
 
410 rajveer 41
	public Map getCookiesMap() {
42
		return cookiesMap;
43
	}
44
 
45
	@Override
46
	public void setCookiesMap(Map cookiesMap) {
555 chandransh 47
		log.info("Received cookiesMap and it is " + cookiesMap);
410 rajveer 48
		this.cookiesMap = cookiesMap;
49
	}
50
 
51
	@Override
52
	public void setServletResponse(HttpServletResponse response)
53
	{
54
		this.response = response;
555 chandransh 55
		if(userCookie!=null)
56
			response.addCookie(userCookie);
410 rajveer 57
	}
416 rajveer 58
 
59
	@Override
60
	public void setServletRequest(HttpServletRequest request){
61
		this.request = request;
555 chandransh 62
		this.session = request.getSession();	// Get the existing session or create a new one
63
		getCookiesMap(request);
64
		String requestedSessionId = request.getRequestedSessionId();
65
 
66
		// Check if this is a brand new request with no prior cookies set; OR
67
		// If the request is for an active session.
68
		if(requestedSessionId == null || request.isRequestedSessionIdValid()){
69
			log.info("Request received for valid session: " + requestedSessionId);
70
			// Set the userinfo and the uid cookie if they're not already set.
71
			this.session = request.getSession();
72
			setUserSessionInfo(this.session.getId());
73
			createUserCookie(this.userinfo.getUserId(), false);
74
		} else {
75
			log.info("Request received for invalid session: " + requestedSessionId);
76
			// If the requested session is inactive, do the following:
77
			// 1. Retrieve the user for the requested session from the user cookie
78
			// 2. Add the retrieved user to the newly created session above.
79
			// 3. Update the uid cookie to ensure that a valid user is set in the session
80
			recreateSessionFromUIDCookie(this.session.getId());
81
			createUserCookie(this.userinfo.getUserId(), true);
82
		}
419 rajveer 83
	}
555 chandransh 84
 
85
	private void getCookiesMap(HttpServletRequest request) {
86
		cookiesMap  = new HashMap<String, Cookie>();
87
		Cookie[] cookies = request.getCookies();
88
		// This check is necessary for the first request when no cookies are
89
		// sent.
90
		if(cookies==null)
91
			return;
92
		for (Cookie cookie : cookies)
93
			cookiesMap.put(cookie.getName(), cookie);
94
	}
95
 
96
	private void setUserSessionInfo(String jsessionid){
97
		this.userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
98
		if(this.userinfo == null){
99
			this.userinfo = new UserSessionInfo(jsessionid);
100
			this.session.setAttribute("userinfo", this.userinfo);
419 rajveer 101
		}
102
	}
103
 
555 chandransh 104
	protected void createUserCookie(long userId, boolean force) {
105
		userCookie = (Cookie) cookiesMap.get("uid");
106
		if(force || userCookie == null || !(userId + "").equals(userCookie.getValue())){
107
			String encryptedUserId = desEncrypter.encrypt(userId + "");  
108
			userCookie = new Cookie("uid", encryptedUserId);
419 rajveer 109
		}
110
	}
111
 
555 chandransh 112
	private void recreateSessionFromUIDCookie(String jsessionid) {
113
		Cookie userCookie = (Cookie) cookiesMap.get("uid");
114
		if(userCookie != null){
115
			String uidString = userCookie.getValue();
116
			if(uidString != null){
117
				try {
118
					Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
119
					this.userinfo = new UserSessionInfo(receivedUID, jsessionid);
120
					this.session.setAttribute("userinfo", this.userinfo);
121
				} catch (NumberFormatException nfe) {
122
					log.error("The UID cookie contains an unparseable userID");
419 rajveer 123
				}
124
			}
125
		}
555 chandransh 126
		if(this.userinfo==null)
127
			setUserSessionInfo(jsessionid);
416 rajveer 128
	}
555 chandransh 129
 
130
//	private void processCookiesInfo(){
131
//		Cookie[] cookies = this.request.getCookies();
132
//		boolean foundUserIdCookie = false;
133
//		boolean foundSessionIdCookie = false;
134
//		long userId = 0 ;
135
//		long sessionId = 0;
136
//		
137
//		if(cookies != null){
138
//		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
139
//		        Cookie cookie1 = cookies[loopIndex];
140
//		        if (cookie1.getName().equals("userid")) {
141
//		            System.out.println("User Id is = " + cookie1.getValue());
142
//		            userId = Long.parseLong(cookie1.getValue());
143
//		            foundUserIdCookie = true;
144
//		        }
145
//		        if (cookie1.getName().equals("sessionid")) {
146
//		            System.out.println("Session Id is = " + cookie1.getValue());
147
//		            sessionId = Long.parseLong(cookie1.getValue());
148
//		            foundSessionIdCookie = true;
149
//		        }
150
//	    	}
151
//		}
152
//	
153
//		if(foundUserIdCookie){
154
//			if(Utils.isUserLoggedIn(userId)){
155
//				userinfo = new UserSessionInfo(userId, false);
156
//				
157
//			}
158
//			else{
159
//				if(foundSessionIdCookie){
160
//					userinfo = new UserSessionInfo(sessionId, true);	
161
//				}else{
162
//					userinfo = new UserSessionInfo();
163
//				    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
164
//				        Cookie cookie1 = cookies[loopIndex];
165
//				        if (cookie1.getName().equals("userid")) {
166
//				        	cookie1.setMaxAge(0);
167
//				        	//cookie1.setPath(cookie1.getPath());
168
//							//cookie1.setDomain(cookie1.getDomain());
169
//				        	tempCookie = cookie1;
170
//				        }
171
//					}
172
//				}
173
//			}
174
//		}			
175
//		else{  
176
//			if(foundSessionIdCookie){
177
//				userinfo = new UserSessionInfo(sessionId, true);			
178
//			}
179
//			else{
180
//				userinfo = new UserSessionInfo();
181
//				//Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");
182
//		    	//tempCookie = cookie1;
183
//			}
184
//		}
185
//	}
419 rajveer 186
 
555 chandransh 187
	private String getEmail(){
424 rajveer 188
		return userinfo.getEmail();
189
	}
419 rajveer 190
 
555 chandransh 191
	private String getNameOfUser(){
424 rajveer 192
		return userinfo.getNameOfUser();
193
	}
194
 
195
}
419 rajveer 196