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