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