| 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;
|
| 719 |
rajveer |
78 |
log.info("REQUESTED URL: " + request.getRequestURL().toString());
|
|
|
79 |
log.info("Remote host "+ request.getRemoteHost());
|
|
|
80 |
log.info("requested Session Id "+ request.getRequestedSessionId());
|
|
|
81 |
log.info("Session Id "+ request.getSession().getId());
|
|
|
82 |
log.info("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.info(name + ": " + value);
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
for(Object param: request.getParameterMap().keySet()){
|
|
|
97 |
log.info("PARAMS: " + param + " = "+ request.getParameter((String)param));
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
|
| 555 |
chandransh |
101 |
this.session = request.getSession(); // Get the existing session or create a new one
|
|
|
102 |
getCookiesMap(request);
|
|
|
103 |
String requestedSessionId = request.getRequestedSessionId();
|
|
|
104 |
|
|
|
105 |
// Check if this is a brand new request with no prior cookies set; OR
|
|
|
106 |
// If the request is for an active session.
|
|
|
107 |
if(requestedSessionId == null || request.isRequestedSessionIdValid()){
|
|
|
108 |
log.info("Request received for valid session: " + requestedSessionId);
|
|
|
109 |
// Set the userinfo and the uid cookie if they're not already set.
|
|
|
110 |
this.session = request.getSession();
|
|
|
111 |
setUserSessionInfo(this.session.getId());
|
|
|
112 |
createUserCookie(this.userinfo.getUserId(), false);
|
|
|
113 |
} else {
|
|
|
114 |
log.info("Request received for invalid session: " + requestedSessionId);
|
|
|
115 |
// If the requested session is inactive, do the following:
|
|
|
116 |
// 1. Retrieve the user for the requested session from the user cookie
|
|
|
117 |
// 2. Add the retrieved user to the newly created session above.
|
|
|
118 |
// 3. Update the uid cookie to ensure that a valid user is set in the session
|
|
|
119 |
recreateSessionFromUIDCookie(this.session.getId());
|
|
|
120 |
createUserCookie(this.userinfo.getUserId(), true);
|
|
|
121 |
}
|
| 419 |
rajveer |
122 |
}
|
| 555 |
chandransh |
123 |
|
|
|
124 |
private void getCookiesMap(HttpServletRequest request) {
|
|
|
125 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
126 |
Cookie[] cookies = request.getCookies();
|
|
|
127 |
// This check is necessary for the first request when no cookies are
|
|
|
128 |
// sent.
|
|
|
129 |
if(cookies==null)
|
|
|
130 |
return;
|
|
|
131 |
for (Cookie cookie : cookies)
|
|
|
132 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
private void setUserSessionInfo(String jsessionid){
|
| 719 |
rajveer |
136 |
log.info("Inside SetUserSessionInfo 1");
|
| 555 |
chandransh |
137 |
this.userinfo = (UserSessionInfo) this.session.getAttribute("userinfo");
|
| 572 |
chandransh |
138 |
if(this.userinfo == null || this.userinfo.getUserId() == -1){
|
| 719 |
rajveer |
139 |
log.info("Inside SetUserSessionInfo 2");
|
| 555 |
chandransh |
140 |
this.userinfo = new UserSessionInfo(jsessionid);
|
|
|
141 |
this.session.setAttribute("userinfo", this.userinfo);
|
| 419 |
rajveer |
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
| 555 |
chandransh |
145 |
protected void createUserCookie(long userId, boolean force) {
|
| 719 |
rajveer |
146 |
log.info("Inside CreateUserCookie 1");
|
| 555 |
chandransh |
147 |
userCookie = (Cookie) cookiesMap.get("uid");
|
|
|
148 |
if(force || userCookie == null || !(userId + "").equals(userCookie.getValue())){
|
| 719 |
rajveer |
149 |
log.info("Inside CreateUserCookie 2");
|
| 555 |
chandransh |
150 |
String encryptedUserId = desEncrypter.encrypt(userId + "");
|
|
|
151 |
userCookie = new Cookie("uid", encryptedUserId);
|
| 419 |
rajveer |
152 |
}
|
|
|
153 |
}
|
|
|
154 |
|
| 555 |
chandransh |
155 |
private void recreateSessionFromUIDCookie(String jsessionid) {
|
|
|
156 |
Cookie userCookie = (Cookie) cookiesMap.get("uid");
|
|
|
157 |
if(userCookie != null){
|
|
|
158 |
String uidString = userCookie.getValue();
|
|
|
159 |
if(uidString != null){
|
|
|
160 |
try {
|
|
|
161 |
Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
|
|
|
162 |
this.userinfo = new UserSessionInfo(receivedUID, jsessionid);
|
|
|
163 |
this.session.setAttribute("userinfo", this.userinfo);
|
|
|
164 |
} catch (NumberFormatException nfe) {
|
|
|
165 |
log.error("The UID cookie contains an unparseable userID");
|
| 419 |
rajveer |
166 |
}
|
|
|
167 |
}
|
|
|
168 |
}
|
| 555 |
chandransh |
169 |
if(this.userinfo==null)
|
|
|
170 |
setUserSessionInfo(jsessionid);
|
| 416 |
rajveer |
171 |
}
|
| 555 |
chandransh |
172 |
|
|
|
173 |
// private void processCookiesInfo(){
|
|
|
174 |
// Cookie[] cookies = this.request.getCookies();
|
|
|
175 |
// boolean foundUserIdCookie = false;
|
|
|
176 |
// boolean foundSessionIdCookie = false;
|
|
|
177 |
// long userId = 0 ;
|
|
|
178 |
// long sessionId = 0;
|
|
|
179 |
//
|
|
|
180 |
// if(cookies != null){
|
|
|
181 |
// for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {
|
|
|
182 |
// Cookie cookie1 = cookies[loopIndex];
|
|
|
183 |
// if (cookie1.getName().equals("userid")) {
|
|
|
184 |
// System.out.println("User Id is = " + cookie1.getValue());
|
|
|
185 |
// userId = Long.parseLong(cookie1.getValue());
|
|
|
186 |
// foundUserIdCookie = true;
|
|
|
187 |
// }
|
|
|
188 |
// if (cookie1.getName().equals("sessionid")) {
|
|
|
189 |
// System.out.println("Session Id is = " + cookie1.getValue());
|
|
|
190 |
// sessionId = Long.parseLong(cookie1.getValue());
|
|
|
191 |
// foundSessionIdCookie = true;
|
|
|
192 |
// }
|
|
|
193 |
// }
|
|
|
194 |
// }
|
|
|
195 |
//
|
|
|
196 |
// if(foundUserIdCookie){
|
|
|
197 |
// if(Utils.isUserLoggedIn(userId)){
|
|
|
198 |
// userinfo = new UserSessionInfo(userId, false);
|
|
|
199 |
//
|
|
|
200 |
// }
|
|
|
201 |
// else{
|
|
|
202 |
// if(foundSessionIdCookie){
|
|
|
203 |
// userinfo = new UserSessionInfo(sessionId, true);
|
|
|
204 |
// }else{
|
|
|
205 |
// userinfo = new UserSessionInfo();
|
|
|
206 |
// for(int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {
|
|
|
207 |
// Cookie cookie1 = cookies[loopIndex];
|
|
|
208 |
// if (cookie1.getName().equals("userid")) {
|
|
|
209 |
// cookie1.setMaxAge(0);
|
|
|
210 |
// //cookie1.setPath(cookie1.getPath());
|
|
|
211 |
// //cookie1.setDomain(cookie1.getDomain());
|
|
|
212 |
// tempCookie = cookie1;
|
|
|
213 |
// }
|
|
|
214 |
// }
|
|
|
215 |
// }
|
|
|
216 |
// }
|
|
|
217 |
// }
|
|
|
218 |
// else{
|
|
|
219 |
// if(foundSessionIdCookie){
|
|
|
220 |
// userinfo = new UserSessionInfo(sessionId, true);
|
|
|
221 |
// }
|
|
|
222 |
// else{
|
|
|
223 |
// userinfo = new UserSessionInfo();
|
|
|
224 |
// //Cookie cookie1 = new Cookie("sessionid", userinfo.getSessionId()+"");
|
|
|
225 |
// //tempCookie = cookie1;
|
|
|
226 |
// }
|
|
|
227 |
// }
|
|
|
228 |
// }
|
| 627 |
rajveer |
229 |
|
|
|
230 |
public UserSessionInfo getUserInfo(){
|
|
|
231 |
return this.userinfo;
|
| 424 |
rajveer |
232 |
}
|
|
|
233 |
|
| 637 |
rajveer |
234 |
public String getHeaderSnippet(){
|
|
|
235 |
return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getNameOfUser());
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
public String getMainMenuSnippet(){
|
|
|
239 |
return pageLoader.getMainMenuHtml();
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
public String getSearchBarSnippet(){
|
|
|
243 |
return pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
public String getCustomerServiceSnippet(){
|
|
|
247 |
return pageLoader.getCustomerServiceHtml();
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
public String getMyResearchSnippet(){
|
|
|
251 |
return pageLoader.getMyResearchHtml(userinfo.getUserId(), userinfo.isLoggedIn());
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
public String getBrowseHistorySnippet(){
|
|
|
255 |
return pageLoader.getBrowseHistoryHtml(userinfo.getUserId(), userinfo.isLoggedIn());
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
public String getFooterSnippet(){
|
|
|
259 |
return pageLoader.getFooterHtml();
|
|
|
260 |
}
|
| 650 |
rajveer |
261 |
|
|
|
262 |
public String getRedirectUrl(){
|
|
|
263 |
return (String)this.request.getSession().getAttribute("REDIRECT_URL");
|
|
|
264 |
}
|
| 637 |
rajveer |
265 |
|
| 650 |
rajveer |
266 |
public void setRedirectUrl(){
|
| 719 |
rajveer |
267 |
String queryString = this.request.getQueryString();
|
|
|
268 |
log.info("Query String is: "+queryString);
|
|
|
269 |
if(queryString==null){
|
|
|
270 |
queryString="";
|
|
|
271 |
}else{
|
|
|
272 |
queryString = "?" + queryString;
|
|
|
273 |
}
|
|
|
274 |
this.request.getSession().setAttribute("REDIRECT_URL", this.request.getRequestURI() + queryString);
|
| 650 |
rajveer |
275 |
}
|
|
|
276 |
|
|
|
277 |
public void resetRedirectUrl(){
|
|
|
278 |
this.request.getSession().removeAttribute("REDIRECT_URL");
|
|
|
279 |
}
|
|
|
280 |
|
| 424 |
rajveer |
281 |
}
|
| 419 |
rajveer |
282 |
|