| 1862 |
vikas |
1 |
package in.shop2020.serving.interceptors;
|
|
|
2 |
|
| 2637 |
vikas |
3 |
import in.shop2020.datalogger.EventType;
|
| 1862 |
vikas |
4 |
import in.shop2020.model.v1.user.Affiliate;
|
|
|
5 |
import in.shop2020.model.v1.user.Tracker;
|
| 2637 |
vikas |
6 |
import in.shop2020.serving.services.UserSessionInfo;
|
| 2021 |
vikas |
7 |
import in.shop2020.serving.utils.DesEncrypter;
|
| 1862 |
vikas |
8 |
import in.shop2020.thrift.clients.UserContextServiceClient;
|
| 2637 |
vikas |
9 |
import in.shop2020.utils.DataLogger;
|
| 1862 |
vikas |
10 |
|
| 2817 |
vikas |
11 |
import java.util.Date;
|
| 2637 |
vikas |
12 |
import java.util.HashMap;
|
|
|
13 |
import java.util.Map;
|
|
|
14 |
|
| 1862 |
vikas |
15 |
import javax.servlet.http.Cookie;
|
|
|
16 |
import javax.servlet.http.HttpServletRequest;
|
|
|
17 |
import javax.servlet.http.HttpServletResponse;
|
|
|
18 |
|
|
|
19 |
import org.apache.log4j.Logger;
|
|
|
20 |
import org.apache.struts2.ServletActionContext;
|
|
|
21 |
|
|
|
22 |
import com.opensymphony.xwork2.ActionInvocation;
|
|
|
23 |
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
|
|
24 |
|
|
|
25 |
public class TrackingInterceptor extends AbstractInterceptor {
|
|
|
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;
|
| 2021 |
vikas |
31 |
private static final int SECONDS_IN_YEAR = 60*60*24*365;
|
| 2936 |
vikas |
32 |
private static final int SECONDS_IN_DAY = 60*60*24;
|
| 1866 |
vikas |
33 |
public static final String TRACKER = "tracker";
|
| 1999 |
vikas |
34 |
public static final String AFF_COOKIE = "uafc";
|
| 2021 |
vikas |
35 |
public static final String SRC_COOKIE = "usrcc";
|
| 2637 |
vikas |
36 |
public static final String SESSION_SRC_COOKIE = "sess_srcc";
|
| 2817 |
vikas |
37 |
public static final String SRC_TIME_COOKIE = "usrctc";
|
|
|
38 |
public static final String SESSION_SRC_TIME_COOKIE = "sess_srctc";
|
|
|
39 |
public static final String ENCRIPTION_STRING = "Saholic";
|
| 1862 |
vikas |
40 |
|
|
|
41 |
private String cookieDomain = "";
|
|
|
42 |
private Cookie trackerCookie = null;
|
| 1999 |
vikas |
43 |
private Cookie affCookie = null;
|
| 2021 |
vikas |
44 |
private Cookie firstSourceCookie = null;
|
| 2637 |
vikas |
45 |
private Cookie sessionSourceCookie = null;
|
| 2817 |
vikas |
46 |
private Cookie firstSourceTimeCookie = null;
|
|
|
47 |
private Cookie sessionSourceTimeCookie = null;
|
| 1862 |
vikas |
48 |
private Map<String, Cookie> cookiesMap = null;
|
|
|
49 |
|
|
|
50 |
public void setCookieDomain(String cookieDomain) {
|
|
|
51 |
this.cookieDomain = cookieDomain;
|
|
|
52 |
}
|
| 1999 |
vikas |
53 |
|
| 2021 |
vikas |
54 |
@Override
|
| 1862 |
vikas |
55 |
public String intercept(ActionInvocation invocation) throws Exception {
|
|
|
56 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
57 |
|
|
|
58 |
String affId = request.getParameter("afid");
|
| 2021 |
vikas |
59 |
createCookiesMap(request);
|
|
|
60 |
cleanTrackerCookie();
|
|
|
61 |
updateFirstSourceCookie();
|
| 2637 |
vikas |
62 |
updateSessionSourceCookie();
|
| 1862 |
vikas |
63 |
|
|
|
64 |
if(affId != null && !affId.isEmpty()) {
|
| 1999 |
vikas |
65 |
updateAffCookie(affId);
|
| 1862 |
vikas |
66 |
}
|
|
|
67 |
|
|
|
68 |
return invocation.invoke();
|
|
|
69 |
}
|
|
|
70 |
|
| 2021 |
vikas |
71 |
/**
|
| 2637 |
vikas |
72 |
* Update first source cookie
|
| 2021 |
vikas |
73 |
*
|
|
|
74 |
*/
|
|
|
75 |
private void updateFirstSourceCookie() {
|
|
|
76 |
firstSourceCookie = (Cookie) cookiesMap.get(SRC_COOKIE);
|
|
|
77 |
if (firstSourceCookie != null) {
|
|
|
78 |
return;
|
|
|
79 |
}
|
|
|
80 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
81 |
String src = "";
|
|
|
82 |
String referer = request.getHeader("referer");
|
|
|
83 |
if (referer == null || referer.isEmpty() || referer.contains("saholic") || referer.contains("shop2020")) {
|
|
|
84 |
src = "DIRECT : " + "(" + request.getRequestURL() + ")" ;
|
|
|
85 |
}
|
|
|
86 |
else if (referer.contains("google.co")) {
|
|
|
87 |
src = "ORGANIC : " + "(" + referer + ")";
|
|
|
88 |
}
|
|
|
89 |
else {
|
|
|
90 |
src = referer;
|
|
|
91 |
}
|
| 2817 |
vikas |
92 |
DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
|
| 2021 |
vikas |
93 |
String sourceCookieVal = des.encrypt(src);
|
|
|
94 |
|
|
|
95 |
firstSourceCookie = new Cookie(SRC_COOKIE, sourceCookieVal);
|
|
|
96 |
firstSourceCookie.setMaxAge(SECONDS_IN_YEAR);
|
|
|
97 |
firstSourceCookie.setPath("/");
|
|
|
98 |
if (!cookieDomain.isEmpty()) {
|
|
|
99 |
firstSourceCookie.setDomain(cookieDomain);
|
|
|
100 |
}
|
|
|
101 |
cookiesMap.put(SRC_COOKIE, firstSourceCookie);
|
| 2817 |
vikas |
102 |
|
|
|
103 |
firstSourceTimeCookie = new Cookie(SRC_TIME_COOKIE, Long.toString((new Date()).getTime()));
|
|
|
104 |
firstSourceTimeCookie.setMaxAge(SECONDS_IN_YEAR);
|
|
|
105 |
firstSourceTimeCookie.setPath("/");
|
|
|
106 |
if (!cookieDomain.isEmpty()) {
|
|
|
107 |
firstSourceTimeCookie.setDomain(cookieDomain);
|
|
|
108 |
}
|
|
|
109 |
cookiesMap.put(SRC_TIME_COOKIE, firstSourceTimeCookie);
|
|
|
110 |
|
| 2021 |
vikas |
111 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
112 |
response.addCookie(firstSourceCookie);
|
| 2817 |
vikas |
113 |
response.addCookie(firstSourceTimeCookie);
|
| 2021 |
vikas |
114 |
}
|
| 2637 |
vikas |
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Update first source cookie
|
|
|
118 |
*
|
|
|
119 |
*/
|
|
|
120 |
private void updateSessionSourceCookie() {
|
|
|
121 |
sessionSourceCookie = (Cookie) cookiesMap.get(SESSION_SRC_COOKIE);
|
|
|
122 |
if (sessionSourceCookie != null) {
|
|
|
123 |
return;
|
|
|
124 |
}
|
|
|
125 |
HttpServletRequest request = ServletActionContext.getRequest();
|
|
|
126 |
String src = "";
|
|
|
127 |
String referer = request.getHeader("referer");
|
|
|
128 |
if (referer == null || referer.isEmpty() || referer.contains("saholic") || referer.contains("shop2020")) {
|
|
|
129 |
src = "DIRECT : " + "(" + request.getRequestURL() + ")" ;
|
|
|
130 |
}
|
|
|
131 |
else if (referer.contains("google.co")) {
|
|
|
132 |
src = "ORGANIC : " + "(" + referer + ")";
|
|
|
133 |
}
|
|
|
134 |
else {
|
|
|
135 |
src = referer;
|
|
|
136 |
}
|
| 2817 |
vikas |
137 |
DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
|
| 2637 |
vikas |
138 |
String sessionSourceCookieVal = des.encrypt(src);
|
|
|
139 |
|
|
|
140 |
sessionSourceCookie = new Cookie(SESSION_SRC_COOKIE, sessionSourceCookieVal);
|
|
|
141 |
sessionSourceCookie.setPath("/");
|
|
|
142 |
if (!cookieDomain.isEmpty()) {
|
|
|
143 |
sessionSourceCookie.setDomain(cookieDomain);
|
|
|
144 |
}
|
|
|
145 |
cookiesMap.put(SESSION_SRC_COOKIE, sessionSourceCookie);
|
| 2817 |
vikas |
146 |
|
|
|
147 |
sessionSourceTimeCookie = new Cookie(SESSION_SRC_TIME_COOKIE, Long.toString((new Date()).getTime()));
|
|
|
148 |
sessionSourceTimeCookie.setPath("/");
|
|
|
149 |
if (!cookieDomain.isEmpty()) {
|
|
|
150 |
sessionSourceTimeCookie.setDomain(cookieDomain);
|
|
|
151 |
}
|
|
|
152 |
cookiesMap.put(SESSION_SRC_TIME_COOKIE, sessionSourceTimeCookie);
|
|
|
153 |
|
| 2637 |
vikas |
154 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
155 |
response.addCookie(sessionSourceCookie);
|
| 2817 |
vikas |
156 |
response.addCookie(sessionSourceTimeCookie);
|
|
|
157 |
|
| 2907 |
rajveer |
158 |
UserSessionInfo userInfo = (UserSessionInfo) request.getAttribute(UserInterceptor.USER_INFO_COOKIE_NAME);
|
|
|
159 |
DataLogger.logData(EventType.NEW_SESSION, request.getSession().getId(), userInfo.getUserId(), userInfo.getEmail(), src);
|
| 2637 |
vikas |
160 |
}
|
| 2021 |
vikas |
161 |
|
| 1862 |
vikas |
162 |
private void createCookiesMap(HttpServletRequest request) {
|
|
|
163 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
164 |
Cookie[] cookies = request.getCookies();
|
|
|
165 |
// This check is necessary for the first request when no cookies are
|
|
|
166 |
// sent.
|
|
|
167 |
if(cookies==null)
|
|
|
168 |
return;
|
|
|
169 |
for (Cookie cookie : cookies) {
|
|
|
170 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
171 |
}
|
|
|
172 |
}
|
| 1999 |
vikas |
173 |
|
| 2021 |
vikas |
174 |
private void cleanTrackerCookie() {
|
| 1999 |
vikas |
175 |
trackerCookie = (Cookie) cookiesMap.get(TRACKER);
|
|
|
176 |
affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
|
|
|
177 |
|
|
|
178 |
if (trackerCookie != null) {
|
|
|
179 |
if (affCookie == null) {
|
|
|
180 |
try {
|
|
|
181 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
182 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
|
|
|
183 |
Tracker tracker = userClient.getTrackerById(Long.parseLong(trackerCookie.getValue()));
|
|
|
184 |
affCookie = new Cookie(AFF_COOKIE, String.valueOf(tracker.getAffiliateId()));
|
|
|
185 |
affCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);
|
|
|
186 |
affCookie.setPath("/");
|
|
|
187 |
if (!cookieDomain.isEmpty()) {
|
|
|
188 |
affCookie.setDomain(cookieDomain);
|
|
|
189 |
}
|
|
|
190 |
cookiesMap.put(AFF_COOKIE, trackerCookie);
|
|
|
191 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
192 |
response.addCookie(affCookie);
|
|
|
193 |
} catch (Exception e) {
|
| 2942 |
chandransh |
194 |
log.error("Unable to clear tracker cookie", e);
|
| 1999 |
vikas |
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
if (!cookieDomain.isEmpty()) {
|
|
|
199 |
trackerCookie.setDomain(cookieDomain);
|
|
|
200 |
}
|
|
|
201 |
trackerCookie.setPath("/");
|
|
|
202 |
trackerCookie.setMaxAge(0);
|
|
|
203 |
HttpServletResponse response = ServletActionContext.getResponse();
|
|
|
204 |
response.addCookie(trackerCookie);
|
|
|
205 |
}
|
|
|
206 |
}
|
| 1862 |
vikas |
207 |
|
| 1999 |
vikas |
208 |
private void updateAffCookie(String affId) {
|
| 1862 |
vikas |
209 |
try {
|
|
|
210 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
|
|
211 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
|
|
|
212 |
.getClient();
|
|
|
213 |
long affiliateId = Long.parseLong(affId);
|
|
|
214 |
Affiliate affiliate = userClient.getAffiliateById(affiliateId);
|
|
|
215 |
if (affiliate != null) {
|
| 1999 |
vikas |
216 |
affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
|
|
|
217 |
if (affCookie != null) {
|
|
|
218 |
Long.parseLong(affCookie.getValue());
|
|
|
219 |
if (affiliateId == Long.parseLong(affCookie.getValue())) {
|
| 1862 |
vikas |
220 |
return;
|
|
|
221 |
}
|
|
|
222 |
}
|
| 1999 |
vikas |
223 |
// create new aff cookie if affCookie ==null or affiliateId() != affCookie.
|
|
|
224 |
Cookie newAffCookie = new Cookie(AFF_COOKIE, String.valueOf(affiliateId));
|
| 2936 |
vikas |
225 |
newAffCookie.setMaxAge(SECONDS_IN_DAY * 5);
|
| 1999 |
vikas |
226 |
newAffCookie.setPath("/");
|
| 1862 |
vikas |
227 |
if (!cookieDomain.isEmpty()) {
|
| 1999 |
vikas |
228 |
newAffCookie.setDomain(cookieDomain);
|
| 1862 |
vikas |
229 |
}
|
| 1999 |
vikas |
230 |
cookiesMap.put(AFF_COOKIE, newAffCookie);
|
| 1862 |
vikas |
231 |
HttpServletResponse response = ServletActionContext.getResponse();
|
| 1999 |
vikas |
232 |
response.addCookie(newAffCookie);
|
| 1862 |
vikas |
233 |
}
|
|
|
234 |
} catch (Exception e) {
|
|
|
235 |
log.error(e);
|
|
|
236 |
}
|
|
|
237 |
}
|
| 1877 |
vikas |
238 |
}
|