| 781 |
vikas |
1 |
package in.shop2020.serving.interceptors;
|
|
|
2 |
|
|
|
3 |
import java.util.HashMap;
|
|
|
4 |
import java.util.Map;
|
|
|
5 |
|
| 831 |
vikas |
6 |
import in.shop2020.serving.controllers.LoginController;
|
| 781 |
vikas |
7 |
import in.shop2020.serving.services.UserSessionInfo;
|
|
|
8 |
import in.shop2020.serving.utils.DesEncrypter;
|
|
|
9 |
|
|
|
10 |
import javax.servlet.http.Cookie;
|
|
|
11 |
import javax.servlet.http.HttpServletRequest;
|
| 837 |
vikas |
12 |
import javax.servlet.http.HttpServletResponse;
|
| 781 |
vikas |
13 |
import javax.servlet.http.HttpSession;
|
|
|
14 |
|
|
|
15 |
import org.apache.juli.logging.Log;
|
|
|
16 |
import org.apache.juli.logging.LogFactory;
|
|
|
17 |
import org.apache.struts2.ServletActionContext;
|
|
|
18 |
|
|
|
19 |
import com.opensymphony.xwork2.ActionInvocation;
|
|
|
20 |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
|
|
21 |
|
|
|
22 |
public class UserInterceptor extends AbstractInterceptor {
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
*
|
|
|
26 |
*/
|
| 837 |
vikas |
27 |
private static final int SECONDS_IN_YEAR = 60*60*24*365;
|
|
|
28 |
|
| 781 |
vikas |
29 |
private static final long serialVersionUID = -4125815700236506235L;
|
|
|
30 |
private static Log log = LogFactory.getLog(UserInterceptor.class);
|
|
|
31 |
|
|
|
32 |
public static final String USER_INFO = "userinfo";
|
|
|
33 |
|
|
|
34 |
private Map<String, Cookie> cookiesMap = null;
|
|
|
35 |
private Cookie userCookie = null;
|
|
|
36 |
private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
|
|
|
37 |
|
|
|
38 |
@Override
|
|
|
39 |
public String intercept(ActionInvocation invocation) throws Exception {
|
|
|
40 |
final Object action = invocation.getAction();
|
|
|
41 |
|
|
|
42 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
43 |
HttpSession session = request.getSession(); // Get the existing session or create a new one
|
|
|
44 |
|
|
|
45 |
//getCookiesMap(request);
|
|
|
46 |
String requestedSessionId = request.getRequestedSessionId();
|
|
|
47 |
|
|
|
48 |
UserSessionInfo userInfo = null;
|
|
|
49 |
createCookiesMap(request);
|
|
|
50 |
|
|
|
51 |
// Check if this is a brand new request with no prior cookies set; OR
|
|
|
52 |
// If the request is for an active session.
|
|
|
53 |
if(requestedSessionId == null || request.isRequestedSessionIdValid()){
|
|
|
54 |
log.info("Request received for valid session: " + requestedSessionId);
|
|
|
55 |
// Set the userinfo and the uid cookie if they're not already set.
|
|
|
56 |
userInfo = (UserSessionInfo) session.getAttribute(USER_INFO);
|
|
|
57 |
if(userInfo == null || userInfo.getUserId() == -1) {
|
|
|
58 |
userInfo = new UserSessionInfo(session.getId());
|
| 817 |
vikas |
59 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
60 |
}
|
|
|
61 |
createUserCookie(userInfo.getUserId(), false);
|
|
|
62 |
} else {
|
|
|
63 |
log.info("Request received for invalid session: " + requestedSessionId);
|
|
|
64 |
// If the requested session is inactive, do the following:
|
|
|
65 |
// 1. Retrieve the user for the requested session from the user cookie
|
|
|
66 |
// 2. Add the retrieved user to the newly created session above.
|
|
|
67 |
// 3. Update the uid cookie to ensure that a valid user is set in the session
|
|
|
68 |
userInfo = createAndGetSessionFromUIDCookie(session);
|
| 801 |
rajveer |
69 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
70 |
createUserCookie(userInfo.getUserId(), true);
|
|
|
71 |
}
|
|
|
72 |
if (action instanceof UserAware) {
|
|
|
73 |
UserAware sessionAction = (UserAware) action;
|
|
|
74 |
sessionAction.setSession(session);
|
|
|
75 |
sessionAction.setUserSessionInfo(userInfo);
|
|
|
76 |
sessionAction.setCookiesMap(cookiesMap);
|
|
|
77 |
sessionAction.setUserCookie(userCookie);
|
|
|
78 |
}
|
| 831 |
vikas |
79 |
|
|
|
80 |
if (!(action instanceof LoginController)) {
|
|
|
81 |
setRedirectUrl(request);
|
|
|
82 |
}
|
| 781 |
vikas |
83 |
|
|
|
84 |
return invocation.invoke();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
private void createCookiesMap(HttpServletRequest request) {
|
|
|
88 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
89 |
Cookie[] cookies = request.getCookies();
|
|
|
90 |
// This check is necessary for the first request when no cookies are
|
|
|
91 |
// sent.
|
|
|
92 |
if(cookies==null)
|
|
|
93 |
return;
|
|
|
94 |
for (Cookie cookie : cookies)
|
|
|
95 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
private void createUserCookie(long userId, boolean force) {
|
|
|
99 |
userCookie = (Cookie) cookiesMap.get("uid");
|
| 817 |
vikas |
100 |
String encryptedUserId = desEncrypter.encrypt(userId + "");
|
|
|
101 |
if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
|
| 781 |
vikas |
102 |
userCookie = new Cookie("uid", encryptedUserId);
|
| 837 |
vikas |
103 |
userCookie.setMaxAge(SECONDS_IN_YEAR); // one year
|
|
|
104 |
userCookie.setPath("/");
|
|
|
105 |
log.info("Created new cookie.");
|
| 824 |
vikas |
106 |
cookiesMap.put("uid", userCookie);
|
| 837 |
vikas |
107 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
108 |
response.addCookie(userCookie);
|
| 781 |
vikas |
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
|
|
|
113 |
userCookie = (Cookie) cookiesMap.get("uid");
|
|
|
114 |
UserSessionInfo userInfo = null;
|
|
|
115 |
if(userCookie != null){
|
|
|
116 |
String uidString = userCookie.getValue();
|
|
|
117 |
if(uidString != null){
|
|
|
118 |
try {
|
|
|
119 |
Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
|
| 828 |
rajveer |
120 |
log.info("Invalid session with user cookie : " + receivedUID);
|
| 781 |
vikas |
121 |
userInfo = new UserSessionInfo(receivedUID, session.getId());
|
| 817 |
vikas |
122 |
session.setAttribute(USER_INFO, userInfo);
|
| 781 |
vikas |
123 |
} catch (NumberFormatException nfe) {
|
|
|
124 |
log.error("The UID cookie contains an unparseable userID");
|
|
|
125 |
}
|
|
|
126 |
}
|
| 830 |
vikas |
127 |
}
|
|
|
128 |
else{
|
|
|
129 |
userInfo = new UserSessionInfo(session.getId());
|
| 828 |
rajveer |
130 |
session.setAttribute(USER_INFO, userInfo);
|
| 830 |
vikas |
131 |
log.info("Invalid session without user cookie.");
|
| 781 |
vikas |
132 |
}
|
|
|
133 |
return userInfo;
|
|
|
134 |
}
|
| 831 |
vikas |
135 |
|
|
|
136 |
public void setRedirectUrl(HttpServletRequest request) {
|
|
|
137 |
String queryString = request.getQueryString();
|
|
|
138 |
log.info("Query String is: " + queryString);
|
|
|
139 |
if (queryString == null) {
|
|
|
140 |
queryString = "";
|
|
|
141 |
} else {
|
|
|
142 |
queryString = "?" + queryString;
|
|
|
143 |
}
|
|
|
144 |
log.info("Redirect Url : " + request.getRequestURI() + queryString);
|
|
|
145 |
request.getSession().setAttribute(LoginInterceptor.REDIRECT_URL,
|
|
|
146 |
request.getRequestURI() + queryString);
|
|
|
147 |
}
|
| 781 |
vikas |
148 |
}
|