Rev 5625 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.interceptors;import in.shop2020.model.v1.catalog.CatalogService.Client;import in.shop2020.model.v1.catalog.Source;import in.shop2020.serving.utils.DesEncrypter;import in.shop2020.thrift.clients.CatalogClient;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;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 org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class SourceInterceptor extends AbstractInterceptor {private static final long serialVersionUID = 1L;private static Logger log = Logger.getLogger(SourceInterceptor.class);private static final int SECONDS_IN_DAY = 60 * 60 * 24;private String cookieDomain = "";public static final String SOURCE_ID_COOKIE = "src_idc";private Cookie sourceIdCookie = null;public static final String ENCRIPTION_STRING = "Saholic";public static List<Source> allSources = new ArrayList<Source>();private Map<String, Cookie> cookiesMap = null;private HttpServletRequest request;private HttpServletResponse response;static{try {Client client = (new CatalogClient()).getClient();allSources = client.getAllSources();} catch (TTransportException e) {log.error("Unable to get all sources from thrift.", e);} catch (TException e) {log.error("Unable to get all sources from thrift.", e);}}public void setCookieDomain(String cookieDomain) {this.cookieDomain = cookieDomain;}@Overridepublic String intercept(ActionInvocation invocation) throws Exception {final Object action = invocation.getAction();request = ServletActionContext.getRequest();response = ServletActionContext.getResponse();createCookiesMap();sourceIdCookie = cookiesMap.get(SOURCE_ID_COOKIE);String sourceIdString = updateSourceIdCookie();long sourceId = -1;if(sourceIdString != null){try{sourceId = Long.parseLong(sourceIdString);}catch (NumberFormatException e) {log.error("Invalid source cookie. Clear the cookie.");expireSourceIdCookie();}}if (action instanceof SourceAware) {SourceAware sessionAction = (SourceAware) action;sessionAction.setSourceId(sourceId);}return invocation.invoke();}/*** Update source id cookie**/private String updateSourceIdCookie() {String sourceId = getSourceId();if(sourceId == null){return sourceId;}DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);String sourceIdCookieVal = des.encrypt(sourceId);//session sourcesourceIdCookie = new Cookie(SOURCE_ID_COOKIE,sourceIdCookieVal);sourceIdCookie.setPath("/");sourceIdCookie.setMaxAge(SECONDS_IN_DAY);if (!cookieDomain.isEmpty()) {sourceIdCookie.setDomain(cookieDomain);}cookiesMap.put(SOURCE_ID_COOKIE, sourceIdCookie);response.addCookie(sourceIdCookie);return sourceId;}private void createCookiesMap() {cookiesMap = new HashMap<String, Cookie>();Cookie[] cookies = request.getCookies();if (cookies == null)return;for (Cookie cookie : cookies) {cookiesMap.put(cookie.getName(), cookie);}}/*** Check if referer is not saholic or null, try to match with all sources. return source id if matches else return null and clear source cookie if exists.* If referer is saholic, return sourceId from cookie if set, else return null.*/private String getSourceId() {String sourceId = null;String referer = request.getHeader("referer");if (referer != null && !referer.isEmpty() && !referer.contains("saholic.com" ) && !referer.contains("shop2020.in" ) && !referer.contains("hotspot.com" )) {for(Source source: allSources){if(referer.matches(source.getIdentifier())){return source.getId()+"";}}// If previously we had source cookie, but now she has come from other source which is not listed.// Now we should clear the cookie.if (sourceIdCookie != null) {expireSourceIdCookie();}}else{if (sourceIdCookie != null) {DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);sourceId = des.decrypt(sourceIdCookie.getValue());return sourceId;}}return sourceId;}/*** Expires the Source ID cookie.*/private void expireSourceIdCookie() {Cookie sourceIdCookie = new Cookie(SOURCE_ID_COOKIE, "-1");sourceIdCookie.setMaxAge(0); // Expire this cookie nowsourceIdCookie.setPath("/");sourceIdCookie.setDomain(cookieDomain);HttpServletResponse response = ServletActionContext.getResponse();response.addCookie(sourceIdCookie);}}