| 781 |
vikas |
1 |
package in.shop2020.serving.interceptors;
|
|
|
2 |
|
|
|
3 |
import java.util.HashMap;
|
|
|
4 |
import java.util.Map;
|
|
|
5 |
|
|
|
6 |
import in.shop2020.serving.services.UserSessionInfo;
|
|
|
7 |
import in.shop2020.serving.utils.DesEncrypter;
|
|
|
8 |
|
|
|
9 |
import javax.servlet.http.Cookie;
|
|
|
10 |
import javax.servlet.http.HttpServletRequest;
|
| 837 |
vikas |
11 |
import javax.servlet.http.HttpServletResponse;
|
| 781 |
vikas |
12 |
import javax.servlet.http.HttpSession;
|
|
|
13 |
|
| 1044 |
chandransh |
14 |
import org.apache.log4j.Logger;
|
| 781 |
vikas |
15 |
import org.apache.struts2.ServletActionContext;
|
|
|
16 |
|
|
|
17 |
import com.opensymphony.xwork2.ActionInvocation;
|
|
|
18 |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
|
|
19 |
|
|
|
20 |
public class UserInterceptor extends AbstractInterceptor {
|
|
|
21 |
|
| 837 |
vikas |
22 |
private static final int SECONDS_IN_YEAR = 60*60*24*365;
|
|
|
23 |
|
| 781 |
vikas |
24 |
private static final long serialVersionUID = -4125815700236506235L;
|
| 1044 |
chandransh |
25 |
private static Logger log = Logger.getLogger(UserInterceptor.class);
|
| 781 |
vikas |
26 |
|
|
|
27 |
public static final String USER_INFO = "userinfo";
|
|
|
28 |
|
|
|
29 |
private Map<String, Cookie> cookiesMap = null;
|
|
|
30 |
private Cookie userCookie = null;
|
|
|
31 |
private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
|
|
|
32 |
|
|
|
33 |
@Override
|
|
|
34 |
public String intercept(ActionInvocation invocation) throws Exception {
|
|
|
35 |
final Object action = invocation.getAction();
|
|
|
36 |
|
|
|
37 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
38 |
HttpSession session = request.getSession(); // Get the existing session or create a new one
|
|
|
39 |
|
|
|
40 |
//getCookiesMap(request);
|
|
|
41 |
String requestedSessionId = request.getRequestedSessionId();
|
|
|
42 |
|
|
|
43 |
UserSessionInfo userInfo = null;
|
|
|
44 |
createCookiesMap(request);
|
|
|
45 |
|
|
|
46 |
// Check if this is a brand new request with no prior cookies set; OR
|
|
|
47 |
// If the request is for an active session.
|
|
|
48 |
if(requestedSessionId == null || request.isRequestedSessionIdValid()){
|
|
|
49 |
log.info("Request received for valid session: " + requestedSessionId);
|
|
|
50 |
// Set the userinfo and the uid cookie if they're not already set.
|
|
|
51 |
userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
|
|
|
52 |
if(userInfo == null || userInfo.getUserId() == -1) {
|
|
|
53 |
userInfo = new UserSessionInfo(session.getId());
|
| 817 |
vikas |
54 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
55 |
}
|
|
|
56 |
createUserCookie(userInfo.getUserId(), false);
|
|
|
57 |
} else {
|
|
|
58 |
log.info("Request received for invalid session: " + requestedSessionId);
|
|
|
59 |
// If the requested session is inactive, do the following:
|
|
|
60 |
// 1. Retrieve the user for the requested session from the user cookie
|
|
|
61 |
// 2. Add the retrieved user to the newly created session above.
|
|
|
62 |
// 3. Update the uid cookie to ensure that a valid user is set in the session
|
|
|
63 |
userInfo = createAndGetSessionFromUIDCookie(session);
|
| 801 |
rajveer |
64 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
65 |
createUserCookie(userInfo.getUserId(), true);
|
|
|
66 |
}
|
|
|
67 |
if (action instanceof UserAware) {
|
|
|
68 |
UserAware sessionAction = (UserAware) action;
|
|
|
69 |
sessionAction.setSession(session);
|
|
|
70 |
sessionAction.setUserSessionInfo(userInfo);
|
|
|
71 |
sessionAction.setCookiesMap(cookiesMap);
|
|
|
72 |
sessionAction.setUserCookie(userCookie);
|
|
|
73 |
}
|
| 831 |
vikas |
74 |
|
| 781 |
vikas |
75 |
return invocation.invoke();
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
private void createCookiesMap(HttpServletRequest request) {
|
|
|
79 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
80 |
Cookie[] cookies = request.getCookies();
|
|
|
81 |
// This check is necessary for the first request when no cookies are
|
|
|
82 |
// sent.
|
|
|
83 |
if(cookies==null)
|
|
|
84 |
return;
|
|
|
85 |
for (Cookie cookie : cookies)
|
|
|
86 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
private void createUserCookie(long userId, boolean force) {
|
|
|
90 |
userCookie = (Cookie) cookiesMap.get("uid");
|
| 817 |
vikas |
91 |
String encryptedUserId = desEncrypter.encrypt(userId + "");
|
|
|
92 |
if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
|
| 781 |
vikas |
93 |
userCookie = new Cookie("uid", encryptedUserId);
|
| 837 |
vikas |
94 |
userCookie.setMaxAge(SECONDS_IN_YEAR); // one year
|
|
|
95 |
userCookie.setPath("/");
|
|
|
96 |
log.info("Created new cookie.");
|
| 824 |
vikas |
97 |
cookiesMap.put("uid", userCookie);
|
| 837 |
vikas |
98 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
99 |
response.addCookie(userCookie);
|
| 781 |
vikas |
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
|
|
|
104 |
userCookie = (Cookie) cookiesMap.get("uid");
|
|
|
105 |
UserSessionInfo userInfo = null;
|
|
|
106 |
if(userCookie != null){
|
|
|
107 |
String uidString = userCookie.getValue();
|
|
|
108 |
if(uidString != null){
|
|
|
109 |
try {
|
|
|
110 |
Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
|
| 828 |
rajveer |
111 |
log.info("Invalid session with user cookie : " + receivedUID);
|
| 781 |
vikas |
112 |
userInfo = new UserSessionInfo(receivedUID, session.getId());
|
| 817 |
vikas |
113 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
114 |
} catch (NumberFormatException nfe) {
|
|
|
115 |
log.error("The UID cookie contains an unparseable userID");
|
|
|
116 |
}
|
|
|
117 |
}
|
| 830 |
vikas |
118 |
}
|
|
|
119 |
else{
|
|
|
120 |
userInfo = new UserSessionInfo(session.getId());
|
| 828 |
rajveer |
121 |
session.setAttribute(USER_INFO, userInfo);
|
| 830 |
vikas |
122 |
log.info("Invalid session without user cookie.");
|
| 781 |
vikas |
123 |
}
|
|
|
124 |
return userInfo;
|
|
|
125 |
}
|
|
|
126 |
}
|