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