Rev 1999 | Rev 2637 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.interceptors;import java.util.Date;import java.util.HashMap;import java.util.Map;import in.shop2020.model.v1.user.Affiliate;import in.shop2020.model.v1.user.Tracker;import in.shop2020.thrift.clients.UserContextServiceClient;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.log4j.Logger;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class TrackingInterceptor extends AbstractInterceptor {/****/private static final long serialVersionUID = 1L;private static Logger log = Logger.getLogger(TrackingInterceptor.class);private static final int SECONDS_IN_TWO_MONTHS = 60*60*24*60;public static final String TRACKER = "tracker";public static final String AFF_COOKIE = "uafc";private String cookieDomain = "";private Cookie trackerCookie = null;private Cookie affCookie = null;private Map<String, Cookie> cookiesMap = null;public void setCookieDomain(String cookieDomain) {this.cookieDomain = cookieDomain;}/*** Logic to update trakcer.** 1. AFFILIATE: If there is valid afid parameter passed and is different than the* afid of current tracker, create new tracker with new afid.* 2. REFERER FROM SEARCH ENGINE SAHOLIC IN QUERY: Create a direct traffic tracker* if there is no tracker assigned yet.* 3. REFERER FROM SEARCH ENGINE NO SAHOLIC IN QUERY: Create a tracker*/@Overridepublic String intercept(ActionInvocation invocation) throws Exception {HttpServletRequest request = ServletActionContext.getRequest();String affId = request.getParameter("afid");cleanTrackerCookie(request);if(affId != null && !affId.isEmpty()) {createCookiesMap(request);updateAffCookie(affId);}return invocation.invoke();}private void createCookiesMap(HttpServletRequest request) {cookiesMap = new HashMap<String, Cookie>();Cookie[] cookies = request.getCookies();// This check is necessary for the first request when no cookies are// sent.if(cookies==null)return;for (Cookie cookie : cookies) {cookiesMap.put(cookie.getName(), cookie);}}private void cleanTrackerCookie(HttpServletRequest request) {createCookiesMap(request);trackerCookie = (Cookie) cookiesMap.get(TRACKER);affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);if (trackerCookie != null) {if (affCookie == null) {try {UserContextServiceClient userContextServiceClient = new UserContextServiceClient();in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();Tracker tracker = userClient.getTrackerById(Long.parseLong(trackerCookie.getValue()));affCookie = new Cookie(AFF_COOKIE, String.valueOf(tracker.getAffiliateId()));affCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);affCookie.setPath("/");if (!cookieDomain.isEmpty()) {affCookie.setDomain(cookieDomain);}cookiesMap.put(AFF_COOKIE, trackerCookie);HttpServletResponse response = ServletActionContext.getResponse();response.addCookie(affCookie);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (!cookieDomain.isEmpty()) {trackerCookie.setDomain(cookieDomain);}trackerCookie.setPath("/");trackerCookie.setMaxAge(0);HttpServletResponse response = ServletActionContext.getResponse();response.addCookie(trackerCookie);}}private void updateAffCookie(String affId) {try {UserContextServiceClient userContextServiceClient = new UserContextServiceClient();in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();long affiliateId = Long.parseLong(affId);Affiliate affiliate = userClient.getAffiliateById(affiliateId);if (affiliate != null) {affCookie = (Cookie) cookiesMap.get(AFF_COOKIE);if (affCookie != null) {Long.parseLong(affCookie.getValue());if (affiliateId == Long.parseLong(affCookie.getValue())) {return;}}// create new aff cookie if affCookie ==null or affiliateId() != affCookie.Cookie newAffCookie = new Cookie(AFF_COOKIE, String.valueOf(affiliateId));newAffCookie.setMaxAge(SECONDS_IN_TWO_MONTHS);newAffCookie.setPath("/");if (!cookieDomain.isEmpty()) {newAffCookie.setDomain(cookieDomain);}cookiesMap.put(AFF_COOKIE, newAffCookie);HttpServletResponse response = ServletActionContext.getResponse();response.addCookie(newAffCookie);}} catch (Exception e) {log.error(e);}}}