| 1862 |
vikas |
1 |
package in.shop2020.serving.interceptors;
|
|
|
2 |
|
|
|
3 |
import java.util.Date;
|
|
|
4 |
import java.util.HashMap;
|
|
|
5 |
import java.util.Map;
|
|
|
6 |
|
|
|
7 |
import in.shop2020.model.v1.user.Affiliate;
|
|
|
8 |
import in.shop2020.model.v1.user.Tracker;
|
|
|
9 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
|
|
10 |
|
|
|
11 |
import javax.servlet.http.Cookie;
|
|
|
12 |
import javax.servlet.http.HttpServletRequest;
|
|
|
13 |
import javax.servlet.http.HttpServletResponse;
|
|
|
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 TrackingInterceptor extends AbstractInterceptor {
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
*
|
|
|
26 |
*/
|
|
|
27 |
private static final long serialVersionUID = 1L;
|
|
|
28 |
private static Logger log = Logger.getLogger(TrackingInterceptor.class);
|
|
|
29 |
|
|
|
30 |
private static final int SECONDS_IN_TWO_MONTHS = 60*60*24*60;
|
| 1866 |
vikas |
31 |
public static final String TRACKER = "tracker";
|
| 1862 |
vikas |
32 |
|
|
|
33 |
private String cookieDomain = "";
|
|
|
34 |
private Cookie trackerCookie = null;
|
|
|
35 |
private Map<String, Cookie> cookiesMap = null;
|
|
|
36 |
|
|
|
37 |
public void setCookieDomain(String cookieDomain) {
|
|
|
38 |
this.cookieDomain = cookieDomain;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
@Override
|
|
|
42 |
public String intercept(ActionInvocation invocation) throws Exception {
|
|
|
43 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
44 |
|
|
|
45 |
String affId = request.getParameter("afid");
|
|
|
46 |
|
|
|
47 |
if(affId != null && !affId.isEmpty()) {
|
|
|
48 |
createCookiesMap(request);
|
|
|
49 |
updateTrackerCookie(affId);
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
return invocation.invoke();
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
private void createCookiesMap(HttpServletRequest request) {
|
|
|
56 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
57 |
Cookie[] cookies = request.getCookies();
|
|
|
58 |
// This check is necessary for the first request when no cookies are
|
|
|
59 |
// sent.
|
|
|
60 |
if(cookies==null)
|
|
|
61 |
return;
|
|
|
62 |
for (Cookie cookie : cookies) {
|
|
|
63 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
private void updateTrackerCookie(String affId) {
|
|
|
68 |
try {
|
|
|
69 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
70 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
|
|
|
71 |
.getClient();
|
|
|
72 |
long affiliateId = Long.parseLong(affId);
|
|
|
73 |
Affiliate affiliate = userClient.getAffiliateById(affiliateId);
|
|
|
74 |
if (affiliate != null) {
|
|
|
75 |
trackerCookie = (Cookie) cookiesMap.get(TRACKER);
|
|
|
76 |
if (trackerCookie != null) {
|
|
|
77 |
Tracker tracker = userClient.getTrackerById(Long.parseLong(trackerCookie.getValue()));
|
|
|
78 |
if (tracker.getAffiliateId() == affiliateId) {
|
|
|
79 |
return;
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
// create new tracker and cookie if trackerCookie ==null or tracker.getAffiliateId() != affiliateId.
|
|
|
83 |
Tracker newTracker = userClient.createTracker(affiliateId, (new Date()).getTime());
|
|
|
84 |
trackerCookie = new Cookie(TRACKER, String.valueOf(newTracker.getId()));
|
|
|
85 |
trackerCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);
|
|
|
86 |
trackerCookie.setPath("/");
|
|
|
87 |
if (!cookieDomain.isEmpty()) {
|
|
|
88 |
trackerCookie.setDomain(cookieDomain);
|
|
|
89 |
}
|
|
|
90 |
cookiesMap.put(TRACKER, trackerCookie);
|
|
|
91 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
92 |
response.addCookie(trackerCookie);
|
|
|
93 |
}
|
|
|
94 |
} catch (Exception e) {
|
|
|
95 |
log.error(e);
|
|
|
96 |
}
|
|
|
97 |
}
|
| 1877 |
vikas |
98 |
}
|