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