| Line 1... |
Line 1... |
| 1 |
package in.shop2020.serving.interceptors;
|
1 |
package in.shop2020.serving.interceptors;
|
| 2 |
|
2 |
|
| - |
|
3 |
import java.util.Collection;
|
| 3 |
import java.util.HashMap;
|
4 |
import java.util.HashMap;
|
| 4 |
import java.util.Map;
|
5 |
import java.util.Map;
|
| 5 |
|
6 |
|
| 6 |
import in.shop2020.serving.services.UserSessionInfo;
|
7 |
import in.shop2020.serving.services.UserSessionInfo;
|
| 7 |
import in.shop2020.serving.utils.DesEncrypter;
|
8 |
import in.shop2020.serving.utils.DesEncrypter;
|
| Line 14... |
Line 15... |
| 14 |
import org.apache.juli.logging.LogFactory;
|
15 |
import org.apache.juli.logging.LogFactory;
|
| 15 |
import org.apache.struts2.ServletActionContext;
|
16 |
import org.apache.struts2.ServletActionContext;
|
| 16 |
|
17 |
|
| 17 |
import com.opensymphony.xwork2.ActionContext;
|
18 |
import com.opensymphony.xwork2.ActionContext;
|
| 18 |
import com.opensymphony.xwork2.ActionInvocation;
|
19 |
import com.opensymphony.xwork2.ActionInvocation;
|
| - |
|
20 |
import com.opensymphony.xwork2.ValidationAwareSupport;
|
| 19 |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
21 |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
| 20 |
|
22 |
|
| 21 |
public class UserInterceptor extends AbstractInterceptor {
|
23 |
public class UserInterceptor extends AbstractInterceptor {
|
| 22 |
|
24 |
|
| 23 |
/**
|
25 |
/**
|
| Line 51... |
Line 53... |
| 51 |
log.info("Request received for valid session: " + requestedSessionId);
|
53 |
log.info("Request received for valid session: " + requestedSessionId);
|
| 52 |
// Set the userinfo and the uid cookie if they're not already set.
|
54 |
// Set the userinfo and the uid cookie if they're not already set.
|
| 53 |
userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
|
55 |
userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
|
| 54 |
if(userInfo == null || userInfo.getUserId() == -1) {
|
56 |
if(userInfo == null || userInfo.getUserId() == -1) {
|
| 55 |
userInfo = new UserSessionInfo(session.getId());
|
57 |
userInfo = new UserSessionInfo(session.getId());
|
| 56 |
session.setAttribute("userinfo", userInfo);
|
58 |
session.setAttribute(USER_INFO, userInfo);
|
| 57 |
}
|
59 |
}
|
| 58 |
createUserCookie(userInfo.getUserId(), false);
|
60 |
createUserCookie(userInfo.getUserId(), false);
|
| 59 |
} else {
|
61 |
} else {
|
| 60 |
log.info("Request received for invalid session: " + requestedSessionId);
|
62 |
log.info("Request received for invalid session: " + requestedSessionId);
|
| 61 |
// If the requested session is inactive, do the following:
|
63 |
// If the requested session is inactive, do the following:
|
| Line 88... |
Line 90... |
| 88 |
cookiesMap.put(cookie.getName(), cookie);
|
90 |
cookiesMap.put(cookie.getName(), cookie);
|
| 89 |
}
|
91 |
}
|
| 90 |
|
92 |
|
| 91 |
private void createUserCookie(long userId, boolean force) {
|
93 |
private void createUserCookie(long userId, boolean force) {
|
| 92 |
userCookie = (Cookie) cookiesMap.get("uid");
|
94 |
userCookie = (Cookie) cookiesMap.get("uid");
|
| 93 |
if(force || userCookie == null || !(userId + "").equals(userCookie.getValue())){
|
- |
|
| 94 |
String encryptedUserId = desEncrypter.encrypt(userId + "");
|
95 |
String encryptedUserId = desEncrypter.encrypt(userId + "");
|
| - |
|
96 |
if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
|
| 95 |
userCookie = new Cookie("uid", encryptedUserId);
|
97 |
userCookie = new Cookie("uid", encryptedUserId);
|
| 96 |
}
|
98 |
}
|
| 97 |
}
|
99 |
}
|
| 98 |
|
100 |
|
| 99 |
private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
|
101 |
private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
|
| Line 103... |
Line 105... |
| 103 |
String uidString = userCookie.getValue();
|
105 |
String uidString = userCookie.getValue();
|
| 104 |
if(uidString != null){
|
106 |
if(uidString != null){
|
| 105 |
try {
|
107 |
try {
|
| 106 |
Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
|
108 |
Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
|
| 107 |
userInfo = new UserSessionInfo(receivedUID, session.getId());
|
109 |
userInfo = new UserSessionInfo(receivedUID, session.getId());
|
| 108 |
session.setAttribute("userinfo", userInfo);
|
110 |
session.setAttribute(USER_INFO, userInfo);
|
| 109 |
} catch (NumberFormatException nfe) {
|
111 |
} catch (NumberFormatException nfe) {
|
| 110 |
log.error("The UID cookie contains an unparseable userID");
|
112 |
log.error("The UID cookie contains an unparseable userID");
|
| 111 |
}
|
113 |
}
|
| 112 |
}
|
114 |
}
|
| 113 |
}
|
115 |
}
|