| 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";
|
| 1999 |
vikas |
32 |
public static final String AFF_COOKIE = "uafc";
|
| 1862 |
vikas |
33 |
|
|
|
34 |
private String cookieDomain = "";
|
|
|
35 |
private Cookie trackerCookie = null;
|
| 1999 |
vikas |
36 |
private Cookie affCookie = null;
|
| 1862 |
vikas |
37 |
private Map<String, Cookie> cookiesMap = null;
|
|
|
38 |
|
|
|
39 |
public void setCookieDomain(String cookieDomain) {
|
|
|
40 |
this.cookieDomain = cookieDomain;
|
|
|
41 |
}
|
| 1999 |
vikas |
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Logic to update trakcer.
|
|
|
45 |
*
|
|
|
46 |
* 1. AFFILIATE: If there is valid afid parameter passed and is different than the
|
|
|
47 |
* afid of current tracker, create new tracker with new afid.
|
|
|
48 |
* 2. REFERER FROM SEARCH ENGINE SAHOLIC IN QUERY: Create a direct traffic tracker
|
|
|
49 |
* if there is no tracker assigned yet.
|
|
|
50 |
* 3. REFERER FROM SEARCH ENGINE NO SAHOLIC IN QUERY: Create a tracker
|
|
|
51 |
*/
|
| 1862 |
vikas |
52 |
@Override
|
|
|
53 |
public String intercept(ActionInvocation invocation) throws Exception {
|
|
|
54 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
55 |
|
|
|
56 |
String affId = request.getParameter("afid");
|
|
|
57 |
|
| 1999 |
vikas |
58 |
cleanTrackerCookie(request);
|
|
|
59 |
|
| 1862 |
vikas |
60 |
if(affId != null && !affId.isEmpty()) {
|
|
|
61 |
createCookiesMap(request);
|
| 1999 |
vikas |
62 |
updateAffCookie(affId);
|
| 1862 |
vikas |
63 |
}
|
|
|
64 |
|
|
|
65 |
return invocation.invoke();
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
private void createCookiesMap(HttpServletRequest request) {
|
|
|
69 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
70 |
Cookie[] cookies = request.getCookies();
|
|
|
71 |
// This check is necessary for the first request when no cookies are
|
|
|
72 |
// sent.
|
|
|
73 |
if(cookies==null)
|
|
|
74 |
return;
|
|
|
75 |
for (Cookie cookie : cookies) {
|
|
|
76 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
77 |
}
|
|
|
78 |
}
|
| 1999 |
vikas |
79 |
|
|
|
80 |
private void cleanTrackerCookie(HttpServletRequest request) {
|
|
|
81 |
createCookiesMap(request);
|
|
|
82 |
trackerCookie = (Cookie) cookiesMap.get(TRACKER);
|
|
|
83 |
affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
|
|
|
84 |
|
|
|
85 |
if (trackerCookie != null) {
|
|
|
86 |
if (affCookie == null) {
|
|
|
87 |
try {
|
|
|
88 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
89 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
90 |
Tracker tracker = userClient.getTrackerById(Long.parseLong(trackerCookie.getValue()));
|
|
|
91 |
affCookie = new Cookie(AFF_COOKIE, String.valueOf(tracker.getAffiliateId()));
|
|
|
92 |
affCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);
|
|
|
93 |
affCookie.setPath("/");
|
|
|
94 |
if (!cookieDomain.isEmpty()) {
|
|
|
95 |
affCookie.setDomain(cookieDomain);
|
|
|
96 |
}
|
|
|
97 |
cookiesMap.put(AFF_COOKIE, trackerCookie);
|
|
|
98 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
99 |
response.addCookie(affCookie);
|
|
|
100 |
} catch (Exception e) {
|
|
|
101 |
// TODO Auto-generated catch block
|
|
|
102 |
e.printStackTrace();
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
if (!cookieDomain.isEmpty()) {
|
|
|
107 |
trackerCookie.setDomain(cookieDomain);
|
|
|
108 |
}
|
|
|
109 |
trackerCookie.setPath("/");
|
|
|
110 |
trackerCookie.setValue("");
|
|
|
111 |
trackerCookie.setMaxAge(0);
|
|
|
112 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
113 |
response.addCookie(trackerCookie);
|
|
|
114 |
}
|
|
|
115 |
}
|
| 1862 |
vikas |
116 |
|
| 1999 |
vikas |
117 |
private void updateAffCookie(String affId) {
|
| 1862 |
vikas |
118 |
try {
|
|
|
119 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
120 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
|
|
|
121 |
.getClient();
|
|
|
122 |
long affiliateId = Long.parseLong(affId);
|
|
|
123 |
Affiliate affiliate = userClient.getAffiliateById(affiliateId);
|
|
|
124 |
if (affiliate != null) {
|
| 1999 |
vikas |
125 |
affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
|
|
|
126 |
if (affCookie != null) {
|
|
|
127 |
Long.parseLong(affCookie.getValue());
|
|
|
128 |
if (affiliateId == Long.parseLong(affCookie.getValue())) {
|
| 1862 |
vikas |
129 |
return;
|
|
|
130 |
}
|
|
|
131 |
}
|
| 1999 |
vikas |
132 |
// create new aff cookie if affCookie ==null or affiliateId() != affCookie.
|
|
|
133 |
Cookie newAffCookie = new Cookie(AFF_COOKIE, String.valueOf(affiliateId));
|
|
|
134 |
newAffCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);
|
|
|
135 |
newAffCookie.setPath("/");
|
| 1862 |
vikas |
136 |
if (!cookieDomain.isEmpty()) {
|
| 1999 |
vikas |
137 |
newAffCookie.setDomain(cookieDomain);
|
| 1862 |
vikas |
138 |
}
|
| 1999 |
vikas |
139 |
cookiesMap.put(AFF_COOKIE, newAffCookie);
|
| 1862 |
vikas |
140 |
HttpServletResponse response = ServletActionContext.getResponse();
|
| 1999 |
vikas |
141 |
response.addCookie(newAffCookie);
|
| 1862 |
vikas |
142 |
}
|
|
|
143 |
} catch (Exception e) {
|
|
|
144 |
log.error(e);
|
|
|
145 |
}
|
|
|
146 |
}
|
| 1877 |
vikas |
147 |
}
|