| Line 27... |
Line 27... |
| 27 |
private static final long serialVersionUID = 1L;
|
27 |
private static final long serialVersionUID = 1L;
|
| 28 |
private static Logger log = Logger.getLogger(TrackingInterceptor.class);
|
28 |
private static Logger log = Logger.getLogger(TrackingInterceptor.class);
|
| 29 |
|
29 |
|
| 30 |
private static final int SECONDS_IN_TWO_MONTHS = 60*60*24*60;
|
30 |
private static final int SECONDS_IN_TWO_MONTHS = 60*60*24*60;
|
| 31 |
public static final String TRACKER = "tracker";
|
31 |
public static final String TRACKER = "tracker";
|
| - |
|
32 |
public static final String AFF_COOKIE = "uafc";
|
| 32 |
|
33 |
|
| 33 |
private String cookieDomain = "";
|
34 |
private String cookieDomain = "";
|
| 34 |
private Cookie trackerCookie = null;
|
35 |
private Cookie trackerCookie = null;
|
| - |
|
36 |
private Cookie affCookie = null;
|
| 35 |
private Map<String, Cookie> cookiesMap = null;
|
37 |
private Map<String, Cookie> cookiesMap = null;
|
| 36 |
|
38 |
|
| 37 |
public void setCookieDomain(String cookieDomain) {
|
39 |
public void setCookieDomain(String cookieDomain) {
|
| 38 |
this.cookieDomain = cookieDomain;
|
40 |
this.cookieDomain = cookieDomain;
|
| 39 |
}
|
41 |
}
|
| 40 |
|
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 |
*/
|
| 41 |
@Override
|
52 |
@Override
|
| 42 |
public String intercept(ActionInvocation invocation) throws Exception {
|
53 |
public String intercept(ActionInvocation invocation) throws Exception {
|
| 43 |
HttpServletRequest request = ServletActionContext.getRequest();
|
54 |
HttpServletRequest request = ServletActionContext.getRequest();
|
| 44 |
|
55 |
|
| 45 |
String affId = request.getParameter("afid");
|
56 |
String affId = request.getParameter("afid");
|
| 46 |
|
57 |
|
| - |
|
58 |
cleanTrackerCookie(request);
|
| - |
|
59 |
|
| 47 |
if(affId != null && !affId.isEmpty()) {
|
60 |
if(affId != null && !affId.isEmpty()) {
|
| 48 |
createCookiesMap(request);
|
61 |
createCookiesMap(request);
|
| 49 |
updateTrackerCookie(affId);
|
62 |
updateAffCookie(affId);
|
| 50 |
}
|
63 |
}
|
| 51 |
|
64 |
|
| 52 |
return invocation.invoke();
|
65 |
return invocation.invoke();
|
| 53 |
}
|
66 |
}
|
| 54 |
|
67 |
|
| Line 61... |
Line 74... |
| 61 |
return;
|
74 |
return;
|
| 62 |
for (Cookie cookie : cookies) {
|
75 |
for (Cookie cookie : cookies) {
|
| 63 |
cookiesMap.put(cookie.getName(), cookie);
|
76 |
cookiesMap.put(cookie.getName(), cookie);
|
| 64 |
}
|
77 |
}
|
| 65 |
}
|
78 |
}
|
| - |
|
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 |
}
|
| 66 |
|
116 |
|
| 67 |
private void updateTrackerCookie(String affId) {
|
117 |
private void updateAffCookie(String affId) {
|
| 68 |
try {
|
118 |
try {
|
| 69 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
119 |
UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
|
| 70 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
|
120 |
in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
|
| 71 |
.getClient();
|
121 |
.getClient();
|
| 72 |
long affiliateId = Long.parseLong(affId);
|
122 |
long affiliateId = Long.parseLong(affId);
|
| 73 |
Affiliate affiliate = userClient.getAffiliateById(affiliateId);
|
123 |
Affiliate affiliate = userClient.getAffiliateById(affiliateId);
|
| 74 |
if (affiliate != null) {
|
124 |
if (affiliate != null) {
|
| 75 |
trackerCookie = (Cookie) cookiesMap.get(TRACKER);
|
125 |
affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
|
| 76 |
if (trackerCookie != null) {
|
126 |
if (affCookie != null) {
|
| 77 |
Tracker tracker = userClient.getTrackerById(Long.parseLong(trackerCookie.getValue()));
|
127 |
Long.parseLong(affCookie.getValue());
|
| 78 |
if (tracker.getAffiliateId() == affiliateId) {
|
128 |
if (affiliateId == Long.parseLong(affCookie.getValue())) {
|
| 79 |
return;
|
129 |
return;
|
| 80 |
}
|
130 |
}
|
| 81 |
}
|
131 |
}
|
| 82 |
// create new tracker and cookie if trackerCookie ==null or tracker.getAffiliateId() != affiliateId.
|
132 |
// create new aff cookie if affCookie ==null or affiliateId() != affCookie.
|
| 83 |
Tracker newTracker = userClient.createTracker(affiliateId, (new Date()).getTime());
|
- |
|
| 84 |
trackerCookie = new Cookie(TRACKER, String.valueOf(newTracker.getId()));
|
133 |
Cookie newAffCookie = new Cookie(AFF_COOKIE, String.valueOf(affiliateId));
|
| 85 |
trackerCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);
|
134 |
newAffCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);
|
| 86 |
trackerCookie.setPath("/");
|
135 |
newAffCookie.setPath("/");
|
| 87 |
if (!cookieDomain.isEmpty()) {
|
136 |
if (!cookieDomain.isEmpty()) {
|
| 88 |
trackerCookie.setDomain(cookieDomain);
|
137 |
newAffCookie.setDomain(cookieDomain);
|
| 89 |
}
|
138 |
}
|
| 90 |
cookiesMap.put(TRACKER, trackerCookie);
|
139 |
cookiesMap.put(AFF_COOKIE, newAffCookie);
|
| 91 |
HttpServletResponse response = ServletActionContext.getResponse();
|
140 |
HttpServletResponse response = ServletActionContext.getResponse();
|
| 92 |
response.addCookie(trackerCookie);
|
141 |
response.addCookie(newAffCookie);
|
| 93 |
}
|
142 |
}
|
| 94 |
} catch (Exception e) {
|
143 |
} catch (Exception e) {
|
| 95 |
log.error(e);
|
144 |
log.error(e);
|
| 96 |
}
|
145 |
}
|
| 97 |
}
|
146 |
}
|