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