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