Subversion Repositories SmartDukaan

Rev

Rev 2005 | Rev 2637 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2005 Rev 2021
Line 1... Line 1...
1
package in.shop2020.serving.interceptors;
1
package in.shop2020.serving.interceptors;
2
 
2
 
3
import java.util.Date;
-
 
4
import java.util.HashMap;
3
import java.util.HashMap;
5
import java.util.Map;
4
import java.util.Map;
6
 
5
 
7
import in.shop2020.model.v1.user.Affiliate;
6
import in.shop2020.model.v1.user.Affiliate;
8
import in.shop2020.model.v1.user.Tracker;
7
import in.shop2020.model.v1.user.Tracker;
-
 
8
import in.shop2020.serving.utils.DesEncrypter;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
9
import in.shop2020.thrift.clients.UserContextServiceClient;
10
 
10
 
11
import javax.servlet.http.Cookie;
11
import javax.servlet.http.Cookie;
12
import javax.servlet.http.HttpServletRequest;
12
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletResponse;
13
import javax.servlet.http.HttpServletResponse;
Line 18... Line 18...
18
import com.opensymphony.xwork2.ActionInvocation;
18
import com.opensymphony.xwork2.ActionInvocation;
19
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
19
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
20
 
20
 
21
public class TrackingInterceptor extends AbstractInterceptor {
21
public class TrackingInterceptor extends AbstractInterceptor {
22
 
22
 
23
 
-
 
24
	/**
-
 
25
	 * 
-
 
26
	 */
-
 
27
	private static final long serialVersionUID = 1L;
23
	private static final long serialVersionUID = 1L;
28
	private static Logger log = Logger.getLogger(TrackingInterceptor.class);
24
	private static Logger log = Logger.getLogger(TrackingInterceptor.class);
29
	
25
	
30
	private static final int SECONDS_IN_TWO_MONTHS = 60*60*24*60;
26
	private static final int SECONDS_IN_TWO_MONTHS = 60*60*24*60;
-
 
27
	private static final int SECONDS_IN_YEAR = 60*60*24*365;
31
	public static final String TRACKER = "tracker";
28
	public static final String TRACKER = "tracker";
32
	public static final String AFF_COOKIE = "uafc";
29
	public static final String AFF_COOKIE = "uafc";
-
 
30
	public static final String SRC_COOKIE = "usrcc";
33
	
31
	
34
	private String cookieDomain = "";
32
	private String cookieDomain = "";
35
	private Cookie trackerCookie = null;
33
	private Cookie trackerCookie = null;
36
	private Cookie affCookie = null;
34
	private Cookie affCookie = null;
-
 
35
	private Cookie firstSourceCookie = null;
37
    private Map<String, Cookie> cookiesMap = null;
36
    private Map<String, Cookie> cookiesMap = null;
38
    
37
    
39
    public void setCookieDomain(String cookieDomain) {
38
    public void setCookieDomain(String cookieDomain) {
40
        this.cookieDomain = cookieDomain;
39
        this.cookieDomain = cookieDomain;
41
    }
40
    }
42
 
41
 
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
     */
-
 
52
	@Override
42
    @Override
53
	public String intercept(ActionInvocation invocation) throws Exception {
43
	public String intercept(ActionInvocation invocation) throws Exception {
54
		HttpServletRequest request = ServletActionContext.getRequest();
44
		HttpServletRequest request = ServletActionContext.getRequest();
55
 
45
 
56
		String affId = request.getParameter("afid");
46
		String affId = request.getParameter("afid");
57
		
47
		createCookiesMap(request);
58
		cleanTrackerCookie(request);
48
		cleanTrackerCookie();
-
 
49
		updateFirstSourceCookie();
59
		
50
		
60
		if(affId != null && !affId.isEmpty()) {
51
		if(affId != null && !affId.isEmpty()) {
61
		    createCookiesMap(request);
-
 
62
		    updateAffCookie(affId);
52
		    updateAffCookie(affId);
63
		}
53
		}
64
 
54
 
65
        return invocation.invoke();
55
        return invocation.invoke();
66
	}
56
	}
67
 
57
 
-
 
58
    /**
-
 
59
     * Check afid, referer
-
 
60
     * 
-
 
61
     */
-
 
62
    private void updateFirstSourceCookie() {
-
 
63
        firstSourceCookie = (Cookie) cookiesMap.get(SRC_COOKIE);
-
 
64
        if (firstSourceCookie != null) {
-
 
65
            return;
-
 
66
        }
-
 
67
        HttpServletRequest request = ServletActionContext.getRequest();
-
 
68
        String src = "";
-
 
69
        String referer = request.getHeader("referer");
-
 
70
        if (referer == null || referer.isEmpty() || referer.contains("saholic") || referer.contains("shop2020")) {
-
 
71
            src = "DIRECT : " + "(" + request.getRequestURL() + ")" ;
-
 
72
        }
-
 
73
        else if (referer.contains("google.co")) {
-
 
74
            src = "ORGANIC : " + "(" + referer + ")";
-
 
75
        }
-
 
76
        else {
-
 
77
            src = referer;
-
 
78
        }
-
 
79
        DesEncrypter des = new DesEncrypter("Saholic");
-
 
80
        String sourceCookieVal = des.encrypt(src);
-
 
81
        
-
 
82
        firstSourceCookie = new Cookie(SRC_COOKIE, sourceCookieVal);
-
 
83
        firstSourceCookie.setMaxAge(SECONDS_IN_YEAR);
-
 
84
        firstSourceCookie.setPath("/");
-
 
85
        if (!cookieDomain.isEmpty()) {
-
 
86
            firstSourceCookie.setDomain(cookieDomain);
-
 
87
        }
-
 
88
        cookiesMap.put(SRC_COOKIE, firstSourceCookie);
-
 
89
        HttpServletResponse response = ServletActionContext.getResponse();
-
 
90
        response.addCookie(firstSourceCookie);
-
 
91
    }
-
 
92
 
68
    private void createCookiesMap(HttpServletRequest request) {
93
    private void createCookiesMap(HttpServletRequest request) {
69
        cookiesMap  = new HashMap<String, Cookie>();
94
        cookiesMap  = new HashMap<String, Cookie>();
70
        Cookie[] cookies = request.getCookies();
95
        Cookie[] cookies = request.getCookies();
71
        // This check is necessary for the first request when no cookies are
96
        // This check is necessary for the first request when no cookies are
72
        // sent.
97
        // sent.
Line 75... Line 100...
75
        for (Cookie cookie : cookies) {
100
        for (Cookie cookie : cookies) {
76
            cookiesMap.put(cookie.getName(), cookie);
101
            cookiesMap.put(cookie.getName(), cookie);
77
        }
102
        }
78
    }
103
    }
79
    
104
    
80
    private void cleanTrackerCookie(HttpServletRequest request) {
105
    private void cleanTrackerCookie() {
81
        createCookiesMap(request);
-
 
82
        trackerCookie = (Cookie) cookiesMap.get(TRACKER);
106
        trackerCookie = (Cookie) cookiesMap.get(TRACKER);
83
        affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
107
        affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
84
        
108
        
85
        if (trackerCookie != null) {
109
        if (trackerCookie != null) {
86
            if (affCookie == null) {
110
            if (affCookie == null) {