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