| 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 |
|
| 1511 |
rajveer |
14 |
import nl.bitwalker.useragentutils.BrowserType;
|
|
|
15 |
import nl.bitwalker.useragentutils.UserAgent;
|
|
|
16 |
|
| 1044 |
chandransh |
17 |
import org.apache.log4j.Logger;
|
| 781 |
vikas |
18 |
import org.apache.struts2.ServletActionContext;
|
|
|
19 |
|
|
|
20 |
import com.opensymphony.xwork2.ActionInvocation;
|
|
|
21 |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
|
|
22 |
|
|
|
23 |
public class UserInterceptor extends AbstractInterceptor {
|
|
|
24 |
|
| 837 |
vikas |
25 |
private static final int SECONDS_IN_YEAR = 60*60*24*365;
|
|
|
26 |
|
| 781 |
vikas |
27 |
private static final long serialVersionUID = -4125815700236506235L;
|
| 1044 |
chandransh |
28 |
private static Logger log = Logger.getLogger(UserInterceptor.class);
|
| 781 |
vikas |
29 |
|
|
|
30 |
public static final String USER_INFO = "userinfo";
|
|
|
31 |
|
|
|
32 |
private Map<String, Cookie> cookiesMap = null;
|
|
|
33 |
private Cookie userCookie = null;
|
|
|
34 |
private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
|
|
|
35 |
|
|
|
36 |
@Override
|
|
|
37 |
public String intercept(ActionInvocation invocation) throws Exception {
|
|
|
38 |
final Object action = invocation.getAction();
|
|
|
39 |
|
|
|
40 |
HttpServletRequest request = ServletActionContext.getRequest();
|
| 1511 |
rajveer |
41 |
|
|
|
42 |
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
|
|
|
43 |
log.info("User browser is:" + userAgent.getBrowser() + " OS is:" + userAgent.getOperatingSystem());
|
|
|
44 |
|
|
|
45 |
|
| 781 |
vikas |
46 |
HttpSession session = request.getSession(); // Get the existing session or create a new one
|
|
|
47 |
|
|
|
48 |
//getCookiesMap(request);
|
|
|
49 |
createCookiesMap(request);
|
|
|
50 |
|
|
|
51 |
// If the request is for an active session.
|
| 1354 |
vikas |
52 |
UserSessionInfo userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
|
|
|
53 |
|
|
|
54 |
// Set the userinfo and the uid cookie if they're not already set.
|
|
|
55 |
if (userInfo == null) {
|
| 1553 |
rajveer |
56 |
/* Code for bitwalked. No more used.
|
| 1511 |
rajveer |
57 |
if(userAgent.getBrowser().getBrowserType() == BrowserType.WEB_BROWSER){
|
|
|
58 |
userInfo = createAndGetSessionFromUIDCookie(session);
|
|
|
59 |
}else{
|
|
|
60 |
userInfo = new UserSessionInfo();
|
|
|
61 |
}
|
| 1553 |
rajveer |
62 |
*/
|
|
|
63 |
userInfo = createAndGetSessionFromUIDCookie(session);
|
| 801 |
rajveer |
64 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
65 |
}
|
| 1354 |
vikas |
66 |
else {
|
|
|
67 |
// Update user cookie in case of new registration and login.
|
|
|
68 |
createUserCookie(userInfo.getUserId(), false);
|
|
|
69 |
}
|
|
|
70 |
|
| 781 |
vikas |
71 |
if (action instanceof UserAware) {
|
|
|
72 |
UserAware sessionAction = (UserAware) action;
|
|
|
73 |
sessionAction.setSession(session);
|
|
|
74 |
sessionAction.setUserSessionInfo(userInfo);
|
|
|
75 |
sessionAction.setCookiesMap(cookiesMap);
|
|
|
76 |
sessionAction.setUserCookie(userCookie);
|
|
|
77 |
}
|
| 831 |
vikas |
78 |
|
| 781 |
vikas |
79 |
return invocation.invoke();
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
private void createCookiesMap(HttpServletRequest request) {
|
|
|
83 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
84 |
Cookie[] cookies = request.getCookies();
|
|
|
85 |
// This check is necessary for the first request when no cookies are
|
|
|
86 |
// sent.
|
|
|
87 |
if(cookies==null)
|
|
|
88 |
return;
|
|
|
89 |
for (Cookie cookie : cookies)
|
|
|
90 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
private void createUserCookie(long userId, boolean force) {
|
|
|
94 |
userCookie = (Cookie) cookiesMap.get("uid");
|
| 817 |
vikas |
95 |
String encryptedUserId = desEncrypter.encrypt(userId + "");
|
|
|
96 |
if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
|
| 781 |
vikas |
97 |
userCookie = new Cookie("uid", encryptedUserId);
|
| 837 |
vikas |
98 |
userCookie.setMaxAge(SECONDS_IN_YEAR); // one year
|
|
|
99 |
userCookie.setPath("/");
|
|
|
100 |
log.info("Created new cookie.");
|
| 824 |
vikas |
101 |
cookiesMap.put("uid", userCookie);
|
| 837 |
vikas |
102 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
103 |
response.addCookie(userCookie);
|
| 781 |
vikas |
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
|
|
|
108 |
userCookie = (Cookie) cookiesMap.get("uid");
|
|
|
109 |
UserSessionInfo userInfo = null;
|
|
|
110 |
if(userCookie != null){
|
|
|
111 |
String uidString = userCookie.getValue();
|
|
|
112 |
if(uidString != null){
|
|
|
113 |
try {
|
|
|
114 |
Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
|
| 828 |
rajveer |
115 |
log.info("Invalid session with user cookie : " + receivedUID);
|
| 781 |
vikas |
116 |
userInfo = new UserSessionInfo(receivedUID, session.getId());
|
| 817 |
vikas |
117 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
118 |
} catch (NumberFormatException nfe) {
|
|
|
119 |
log.error("The UID cookie contains an unparseable userID");
|
|
|
120 |
}
|
|
|
121 |
}
|
| 830 |
vikas |
122 |
}
|
|
|
123 |
else{
|
|
|
124 |
userInfo = new UserSessionInfo(session.getId());
|
| 828 |
rajveer |
125 |
session.setAttribute(USER_INFO, userInfo);
|
| 830 |
vikas |
126 |
log.info("Invalid session without user cookie.");
|
| 1354 |
vikas |
127 |
createUserCookie(userInfo.getUserId(), true);
|
| 781 |
vikas |
128 |
}
|
|
|
129 |
return userInfo;
|
|
|
130 |
}
|
|
|
131 |
}
|