| 8842 |
anupam.sin |
1 |
package in.shop2020.serving.interceptors;
|
|
|
2 |
import java.util.HashMap;
|
|
|
3 |
import java.util.Map;
|
|
|
4 |
|
|
|
5 |
import in.shop2020.model.v1.user.User;
|
|
|
6 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
|
|
7 |
import in.shop2020.serving.services.UserSessionInfo;
|
|
|
8 |
import in.shop2020.serving.utils.DesEncrypter;
|
|
|
9 |
import in.shop2020.thrift.clients.UserClient;
|
|
|
10 |
|
|
|
11 |
import javax.servlet.http.Cookie;
|
|
|
12 |
import javax.servlet.http.HttpServletRequest;
|
|
|
13 |
import javax.servlet.http.HttpServletResponse;
|
|
|
14 |
import javax.servlet.http.HttpSession;
|
|
|
15 |
|
|
|
16 |
import org.apache.log4j.Logger;
|
|
|
17 |
import org.apache.struts2.ServletActionContext;
|
|
|
18 |
|
|
|
19 |
import com.opensymphony.xwork2.ActionInvocation;
|
|
|
20 |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
|
|
21 |
|
|
|
22 |
public class CreateUserInterceptor extends AbstractInterceptor {
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
private static final long serialVersionUID = -4125815700236506235L;
|
|
|
26 |
private static Logger log = Logger.getLogger(CreateUserInterceptor.class);
|
|
|
27 |
private Map<String, Cookie> cookiesMap = null;
|
|
|
28 |
private Cookie userCookie = null;
|
|
|
29 |
private String cookieDomain = "";
|
|
|
30 |
private DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
|
|
|
31 |
|
|
|
32 |
public void setCookieDomain(String cookieDomain) {
|
|
|
33 |
this.cookieDomain = cookieDomain;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
@Override
|
|
|
37 |
public String intercept(ActionInvocation invocation) throws Exception {
|
|
|
38 |
log.info("inside create user interceprot");
|
|
|
39 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
40 |
HttpSession session = request.getSession(); // Get the existing session or create a new one
|
|
|
41 |
|
|
|
42 |
createCookiesMap(request);
|
|
|
43 |
|
|
|
44 |
UserSessionInfo userInfo = null;
|
|
|
45 |
Cookie userinfoCookie = cookiesMap.get(UserInterceptor.USER_INFO_COOKIE_NAME);
|
|
|
46 |
//FIXME Remove the if part once we are done with solving the side effected of deleting anonymous users.
|
|
|
47 |
if(userinfoCookie!=null){
|
|
|
48 |
userInfo = UserSessionInfo.getUserSessionInfoFromCookieValue(userinfoCookie.getValue());
|
|
|
49 |
UserClient ucsc = new UserClient();
|
|
|
50 |
Client userClient = ucsc.getClient();
|
|
|
51 |
User existingUser = userClient.getUserById(userInfo.getUserId());
|
|
|
52 |
if(existingUser == null || existingUser.getUserId() == -1){
|
|
|
53 |
userInfo = new UserSessionInfo(session.getId());
|
|
|
54 |
request.setAttribute(UserInterceptor.USER_INFO_COOKIE_NAME, userInfo);
|
|
|
55 |
createUserCookie(userInfo.getUserId(), true);
|
|
|
56 |
}
|
|
|
57 |
}else{
|
|
|
58 |
userInfo = new UserSessionInfo(session.getId());
|
|
|
59 |
request.setAttribute(UserInterceptor.USER_INFO_COOKIE_NAME, userInfo);
|
|
|
60 |
createUserCookie(userInfo.getUserId(), true);
|
|
|
61 |
}
|
|
|
62 |
return invocation.invoke();
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
private void createUserCookie(long userId, boolean force) {
|
|
|
67 |
userCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
|
|
|
68 |
String encryptedUserId = desEncrypter.encrypt(userId + "");
|
|
|
69 |
if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
|
|
|
70 |
userCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, encryptedUserId);
|
|
|
71 |
userCookie.setMaxAge(UserInterceptor.SECONDS_IN_YEAR); // one year
|
|
|
72 |
userCookie.setPath("/");
|
|
|
73 |
if(!cookieDomain.isEmpty()) {
|
|
|
74 |
userCookie.setDomain(cookieDomain);
|
|
|
75 |
}
|
|
|
76 |
log.info("Created new cookie.");
|
|
|
77 |
cookiesMap.put(UserInterceptor.USER_ID_COOKIE_NAME, userCookie);
|
|
|
78 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
79 |
response.addCookie(userCookie);
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
private void createCookiesMap(HttpServletRequest request) {
|
|
|
84 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
85 |
Cookie[] cookies = request.getCookies();
|
|
|
86 |
if(cookies==null)
|
|
|
87 |
return;
|
|
|
88 |
for (Cookie cookie : cookies) {
|
|
|
89 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
}
|