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