Subversion Repositories SmartDukaan

Rev

Rev 2005 | Rev 2637 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1862 vikas 1
package in.shop2020.serving.interceptors;
2
 
3
import java.util.HashMap;
4
import java.util.Map;
5
 
6
import in.shop2020.model.v1.user.Affiliate;
7
import in.shop2020.model.v1.user.Tracker;
2021 vikas 8
import in.shop2020.serving.utils.DesEncrypter;
1862 vikas 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
	private static final long serialVersionUID = 1L;
24
	private static Logger log = Logger.getLogger(TrackingInterceptor.class);
25
 
26
	private static final int SECONDS_IN_TWO_MONTHS = 60*60*24*60;
2021 vikas 27
	private static final int SECONDS_IN_YEAR = 60*60*24*365;
1866 vikas 28
	public static final String TRACKER = "tracker";
1999 vikas 29
	public static final String AFF_COOKIE = "uafc";
2021 vikas 30
	public static final String SRC_COOKIE = "usrcc";
1862 vikas 31
 
32
	private String cookieDomain = "";
33
	private Cookie trackerCookie = null;
1999 vikas 34
	private Cookie affCookie = null;
2021 vikas 35
	private Cookie firstSourceCookie = null;
1862 vikas 36
    private Map<String, Cookie> cookiesMap = null;
37
 
38
    public void setCookieDomain(String cookieDomain) {
39
        this.cookieDomain = cookieDomain;
40
    }
1999 vikas 41
 
2021 vikas 42
    @Override
1862 vikas 43
	public String intercept(ActionInvocation invocation) throws Exception {
44
		HttpServletRequest request = ServletActionContext.getRequest();
45
 
46
		String affId = request.getParameter("afid");
2021 vikas 47
		createCookiesMap(request);
48
		cleanTrackerCookie();
49
		updateFirstSourceCookie();
1862 vikas 50
 
51
		if(affId != null && !affId.isEmpty()) {
1999 vikas 52
		    updateAffCookie(affId);
1862 vikas 53
		}
54
 
55
        return invocation.invoke();
56
	}
57
 
2021 vikas 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
 
1862 vikas 93
    private void createCookiesMap(HttpServletRequest request) {
94
        cookiesMap  = new HashMap<String, Cookie>();
95
        Cookie[] cookies = request.getCookies();
96
        // This check is necessary for the first request when no cookies are
97
        // sent.
98
        if(cookies==null)
99
            return;
100
        for (Cookie cookie : cookies) {
101
            cookiesMap.put(cookie.getName(), cookie);
102
        }
103
    }
1999 vikas 104
 
2021 vikas 105
    private void cleanTrackerCookie() {
1999 vikas 106
        trackerCookie = (Cookie) cookiesMap.get(TRACKER);
107
        affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
108
 
109
        if (trackerCookie != null) {
110
            if (affCookie == null) {
111
                try {
112
                    UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
113
                    in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();
114
                    Tracker tracker = userClient.getTrackerById(Long.parseLong(trackerCookie.getValue()));
115
                    affCookie = new Cookie(AFF_COOKIE, String.valueOf(tracker.getAffiliateId()));
116
                    affCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);
117
                    affCookie.setPath("/");
118
                    if (!cookieDomain.isEmpty()) {
119
                        affCookie.setDomain(cookieDomain);
120
                    }
121
                    cookiesMap.put(AFF_COOKIE, trackerCookie);
122
                    HttpServletResponse response = ServletActionContext.getResponse();
123
                    response.addCookie(affCookie);
124
                } catch (Exception e) {
125
                    // TODO Auto-generated catch block
126
                    e.printStackTrace();
127
                }
128
            }
129
 
130
            if (!cookieDomain.isEmpty()) {
131
                trackerCookie.setDomain(cookieDomain);
132
            }
133
            trackerCookie.setPath("/");
134
            trackerCookie.setMaxAge(0);
135
            HttpServletResponse response = ServletActionContext.getResponse();
136
            response.addCookie(trackerCookie);
137
        }
138
    }
1862 vikas 139
 
1999 vikas 140
	private void updateAffCookie(String affId) {
1862 vikas 141
        try {
142
            UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
143
            in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient
144
                    .getClient();
145
            long affiliateId = Long.parseLong(affId);
146
            Affiliate affiliate = userClient.getAffiliateById(affiliateId);
147
            if (affiliate != null) {
1999 vikas 148
                affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);
149
                if (affCookie != null) {
150
                    Long.parseLong(affCookie.getValue());
151
                    if (affiliateId == Long.parseLong(affCookie.getValue())) {
1862 vikas 152
                        return;
153
                    }
154
                }
1999 vikas 155
                // create new aff cookie if affCookie ==null or affiliateId() != affCookie.
156
                Cookie newAffCookie = new Cookie(AFF_COOKIE, String.valueOf(affiliateId));
157
                newAffCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);
158
                newAffCookie.setPath("/");
1862 vikas 159
                if (!cookieDomain.isEmpty()) {
1999 vikas 160
                    newAffCookie.setDomain(cookieDomain);
1862 vikas 161
                }
1999 vikas 162
                cookiesMap.put(AFF_COOKIE, newAffCookie);
1862 vikas 163
                HttpServletResponse response = ServletActionContext.getResponse();
1999 vikas 164
                response.addCookie(newAffCookie);
1862 vikas 165
            }
166
        } catch (Exception e) {
167
            log.error(e);
168
        }
169
	}
1877 vikas 170
}