| 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 |
|
| 1614 |
rajveer |
40 |
log.info("inside user intercepror");
|
|
|
41 |
|
| 781 |
vikas |
42 |
HttpServletRequest request = ServletActionContext.getRequest();
|
| 1614 |
rajveer |
43 |
log.info("url " + request.getRequestURL());
|
|
|
44 |
log.info("uri " + request.getRequestURI());
|
| 781 |
vikas |
45 |
HttpSession session = request.getSession(); // Get the existing session or create a new one
|
|
|
46 |
|
|
|
47 |
//getCookiesMap(request);
|
|
|
48 |
createCookiesMap(request);
|
|
|
49 |
|
|
|
50 |
// If the request is for an active session.
|
| 1354 |
vikas |
51 |
UserSessionInfo userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
|
|
|
52 |
|
|
|
53 |
// Set the userinfo and the uid cookie if they're not already set.
|
|
|
54 |
if (userInfo == null) {
|
| 1553 |
rajveer |
55 |
userInfo = createAndGetSessionFromUIDCookie(session);
|
| 801 |
rajveer |
56 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
57 |
}
|
| 1354 |
vikas |
58 |
else {
|
|
|
59 |
// Update user cookie in case of new registration and login.
|
| 1614 |
rajveer |
60 |
if(userInfo.getUserId() != -1){
|
|
|
61 |
createUserCookie(userInfo.getUserId(), false);
|
|
|
62 |
}
|
| 1354 |
vikas |
63 |
}
|
|
|
64 |
|
| 781 |
vikas |
65 |
if (action instanceof UserAware) {
|
|
|
66 |
UserAware sessionAction = (UserAware) action;
|
|
|
67 |
sessionAction.setSession(session);
|
|
|
68 |
sessionAction.setUserSessionInfo(userInfo);
|
|
|
69 |
sessionAction.setCookiesMap(cookiesMap);
|
|
|
70 |
sessionAction.setUserCookie(userCookie);
|
|
|
71 |
}
|
| 831 |
vikas |
72 |
|
| 781 |
vikas |
73 |
return invocation.invoke();
|
|
|
74 |
}
|
|
|
75 |
|
| 1614 |
rajveer |
76 |
|
| 781 |
vikas |
77 |
private void createCookiesMap(HttpServletRequest request) {
|
|
|
78 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
79 |
Cookie[] cookies = request.getCookies();
|
|
|
80 |
// This check is necessary for the first request when no cookies are
|
|
|
81 |
// sent.
|
|
|
82 |
if(cookies==null)
|
|
|
83 |
return;
|
|
|
84 |
for (Cookie cookie : cookies)
|
|
|
85 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
private void createUserCookie(long userId, boolean force) {
|
|
|
89 |
userCookie = (Cookie) cookiesMap.get("uid");
|
| 817 |
vikas |
90 |
String encryptedUserId = desEncrypter.encrypt(userId + "");
|
|
|
91 |
if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
|
| 781 |
vikas |
92 |
userCookie = new Cookie("uid", encryptedUserId);
|
| 837 |
vikas |
93 |
userCookie.setMaxAge(SECONDS_IN_YEAR); // one year
|
|
|
94 |
userCookie.setPath("/");
|
|
|
95 |
log.info("Created new cookie.");
|
| 824 |
vikas |
96 |
cookiesMap.put("uid", userCookie);
|
| 837 |
vikas |
97 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
98 |
response.addCookie(userCookie);
|
| 781 |
vikas |
99 |
}
|
|
|
100 |
}
|
| 1614 |
rajveer |
101 |
|
| 781 |
vikas |
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{
|
| 1614 |
rajveer |
120 |
userInfo = new UserSessionInfo();
|
| 828 |
rajveer |
121 |
session.setAttribute(USER_INFO, userInfo);
|
| 830 |
vikas |
122 |
log.info("Invalid session without user cookie.");
|
| 1614 |
rajveer |
123 |
//createUserCookie(userInfo.getUserId(), true);
|
| 781 |
vikas |
124 |
}
|
|
|
125 |
return userInfo;
|
|
|
126 |
}
|
|
|
127 |
}
|