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;
650 rajveer 46
 
47
    protected Map<String,String> htmlSnippets;
48
 
637 rajveer 49
    PageLoaderHandler pageLoader = null;
416 rajveer 50
	public BaseController() {
637 rajveer 51
		pageLoader = new PageLoaderHandler();
650 rajveer 52
		htmlSnippets = new HashMap<String, String>();
416 rajveer 53
	}
545 rajveer 54
 
410 rajveer 55
	public Map getCookiesMap() {
56
		return cookiesMap;
57
	}
58
 
59
	@Override
60
	public void setCookiesMap(Map cookiesMap) {
555 chandransh 61
		log.info("Received cookiesMap and it is " + cookiesMap);
410 rajveer 62
		this.cookiesMap = cookiesMap;
63
	}
64
 
65
	@Override
66
	public void setServletResponse(HttpServletResponse response)
67
	{
68
		this.response = response;
555 chandransh 69
		if(userCookie!=null)
70
			response.addCookie(userCookie);
410 rajveer 71
	}
416 rajveer 72
 
73
	@Override
74
	public void setServletRequest(HttpServletRequest request){
75
		this.request = request;
555 chandransh 76
		this.session = request.getSession();	// Get the existing session or create a new one
77
		getCookiesMap(request);
78
		String requestedSessionId = request.getRequestedSessionId();
79
 
80
		// Check if this is a brand new request with no prior cookies set; OR
81
		// If the request is for an active session.
82
		if(requestedSessionId == null || request.isRequestedSessionIdValid()){
83
			log.info("Request received for valid session: " + requestedSessionId);
84
			// Set the userinfo and the uid cookie if they're not already set.
85
			this.session = request.getSession();
86
			setUserSessionInfo(this.session.getId());
87
			createUserCookie(this.userinfo.getUserId(), false);
88
		} else {
89
			log.info("Request received for invalid session: " + requestedSessionId);
90
			// If the requested session is inactive, do the following:
91
			// 1. Retrieve the user for the requested session from the user cookie
92
			// 2. Add the retrieved user to the newly created session above.
93
			// 3. Update the uid cookie to ensure that a valid user is set in the session
94
			recreateSessionFromUIDCookie(this.session.getId());
95
			createUserCookie(this.userinfo.getUserId(), true);
96
		}
419 rajveer 97
	}
555 chandransh 98
 
99
	private void getCookiesMap(HttpServletRequest request) {
100
		cookiesMap  = new HashMap<String, Cookie>();
101
		Cookie[] cookies = request.getCookies();
102
		// This check is necessary for the first request when no cookies are
103
		// sent.
104
		if(cookies==null)
105
			return;
106
		for (Cookie cookie : cookies)
107
			cookiesMap.put(cookie.getName(), cookie);
108
	}
109
 
110
	private void setUserSessionInfo(String jsessionid){
111
		this.userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
572 chandransh 112
		if(this.userinfo == null || this.userinfo.getUserId() == -1){
555 chandransh 113
			this.userinfo = new UserSessionInfo(jsessionid);
114
			this.session.setAttribute("userinfo", this.userinfo);
419 rajveer 115
		}
116
	}
117
 
555 chandransh 118
	protected void createUserCookie(long userId, boolean force) {
119
		userCookie = (Cookie) cookiesMap.get("uid");
120
		if(force || userCookie == null || !(userId + "").equals(userCookie.getValue())){
121
			String encryptedUserId = desEncrypter.encrypt(userId + "");  
122
			userCookie = new Cookie("uid", encryptedUserId);
419 rajveer 123
		}
124
	}
125
 
555 chandransh 126
	private void recreateSessionFromUIDCookie(String jsessionid) {
127
		Cookie userCookie = (Cookie) cookiesMap.get("uid");
128
		if(userCookie != null){
129
			String uidString = userCookie.getValue();
130
			if(uidString != null){
131
				try {
132
					Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
133
					this.userinfo = new UserSessionInfo(receivedUID, jsessionid);
134
					this.session.setAttribute("userinfo", this.userinfo);
135
				} catch (NumberFormatException nfe) {
136
					log.error("The UID cookie contains an unparseable userID");
419 rajveer 137
				}
138
			}
139
		}
555 chandransh 140
		if(this.userinfo==null)
141
			setUserSessionInfo(jsessionid);
416 rajveer 142
	}
555 chandransh 143
 
144
//	private void processCookiesInfo(){
145
//		Cookie[] cookies = this.request.getCookies();
146
//		boolean foundUserIdCookie = false;
147
//		boolean foundSessionIdCookie = false;
148
//		long userId = 0 ;
149
//		long sessionId = 0;
150
//		
151
//		if(cookies != null){
152
//		    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
153
//		        Cookie cookie1 = cookies[loopIndex];
154
//		        if (cookie1.getName().equals("userid")) {
155
//		            System.out.println("User Id is = " + cookie1.getValue());
156
//		            userId = Long.parseLong(cookie1.getValue());
157
//		            foundUserIdCookie = true;
158
//		        }
159
//		        if (cookie1.getName().equals("sessionid")) {
160
//		            System.out.println("Session Id is = " + cookie1.getValue());
161
//		            sessionId = Long.parseLong(cookie1.getValue());
162
//		            foundSessionIdCookie = true;
163
//		        }
164
//	    	}
165
//		}
166
//	
167
//		if(foundUserIdCookie){
168
//			if(Utils.isUserLoggedIn(userId)){
169
//				userinfo = new UserSessionInfo(userId, false);
170
//				
171
//			}
172
//			else{
173
//				if(foundSessionIdCookie){
174
//					userinfo = new UserSessionInfo(sessionId, true);	
175
//				}else{
176
//					userinfo = new UserSessionInfo();
177
//				    for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) { 
178
//				        Cookie cookie1 = cookies[loopIndex];
179
//				        if (cookie1.getName().equals("userid")) {
180
//				        	cookie1.setMaxAge(0);
181
//				        	//cookie1.setPath(cookie1.getPath());
182
//							//cookie1.setDomain(cookie1.getDomain());
183
//				        	tempCookie = cookie1;
184
//				        }
185
//					}
186
//				}
187
//			}
188
//		}			
189
//		else{  
190
//			if(foundSessionIdCookie){
191
//				userinfo = new UserSessionInfo(sessionId, true);			
192
//			}
193
//			else{
194
//				userinfo = new UserSessionInfo();
195
//				//Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");
196
//		    	//tempCookie = cookie1;
197
//			}
198
//		}
199
//	}
627 rajveer 200
 
201
	public UserSessionInfo getUserInfo(){
202
		return this.userinfo;
424 rajveer 203
	}
204
 
637 rajveer 205
	public String getHeaderSnippet(){
206
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser());
207
	}
208
 
209
	public String getMainMenuSnippet(){
210
		return pageLoader.getMainMenuHtml();
211
	}
212
 
213
	public String getSearchBarSnippet(){
214
		return pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000);
215
	}
216
 
217
	public String getCustomerServiceSnippet(){
218
		return pageLoader.getCustomerServiceHtml();
219
	}
220
 
221
	public String getMyResearchSnippet(){
222
		return pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isLoggedIn());
223
	}
224
 
225
	public String getBrowseHistorySnippet(){
226
		return pageLoader.getBrowseHistoryHtml(userinfo.getUserId(), userinfo.isLoggedIn());
227
	}
228
 
229
	public String getFooterSnippet(){
230
		return pageLoader.getFooterHtml();
231
	}
650 rajveer 232
 
233
	public String getRedirectUrl(){
234
		return (String)this.request.getSession().getAttribute("REDIRECT_URL");
235
	}
637 rajveer 236
 
650 rajveer 237
	public void setRedirectUrl(){
238
		this.request.getSession().setAttribute("REDIRECT_URL", this.request.getRequestURI());
239
	}
240
 
241
	public void resetRedirectUrl(){
242
		this.request.getSession().removeAttribute("REDIRECT_URL");
243
	}
244
 
424 rajveer 245
}
419 rajveer 246