Subversion Repositories SmartDukaan

Rev

Rev 5625 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3561 rajveer 1
package in.shop2020.serving.interceptors;
2
 
5945 mandeep.dh 3
import in.shop2020.model.v1.catalog.CatalogService.Client;
3561 rajveer 4
import in.shop2020.model.v1.catalog.Source;
5
import in.shop2020.serving.utils.DesEncrypter;
6
import in.shop2020.thrift.clients.CatalogClient;
7
 
8
import java.util.ArrayList;
9
import java.util.HashMap;
10
import java.util.List;
11
import java.util.Map;
12
 
13
import javax.servlet.http.Cookie;
14
import javax.servlet.http.HttpServletRequest;
15
import javax.servlet.http.HttpServletResponse;
16
 
17
import org.apache.log4j.Logger;
18
import org.apache.struts2.ServletActionContext;
19
import org.apache.thrift.TException;
20
import org.apache.thrift.transport.TTransportException;
21
 
22
import com.opensymphony.xwork2.ActionInvocation;
23
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
24
 
25
public class SourceInterceptor extends AbstractInterceptor {
26
 
27
    private static final long serialVersionUID = 1L;
28
    private static Logger log = Logger.getLogger(SourceInterceptor.class);
29
 
30
    private static final int SECONDS_IN_DAY = 60 * 60 * 24;
31
 
32
    private String cookieDomain = "";
33
    public static final String SOURCE_ID_COOKIE = "src_idc";
34
    private Cookie sourceIdCookie = null;
35
 
36
    public static final String ENCRIPTION_STRING = "Saholic";
37
 
38
 
39
    public static List<Source> allSources = new ArrayList<Source>();
40
 
41
    private Map<String, Cookie> cookiesMap = null;
42
    private HttpServletRequest request;
43
    private HttpServletResponse response;
44
 
45
    static{
46
    	try {
47
			Client client = (new CatalogClient()).getClient();
48
			allSources = client.getAllSources();
49
		} catch (TTransportException e) {
50
			log.error("Unable to get all sources from thrift.", e);
51
		} catch (TException e) {
52
			log.error("Unable to get all sources from thrift.", e);
53
		}
54
    }
55
 
56
    public void setCookieDomain(String cookieDomain) {
57
        this.cookieDomain = cookieDomain;
58
    }
59
 
60
    @Override
61
    public String intercept(ActionInvocation invocation) throws Exception {
62
    	final Object action = invocation.getAction();
63
        request = ServletActionContext.getRequest();
64
        response = ServletActionContext.getResponse();
65
 
66
        createCookiesMap();
3565 rajveer 67
        sourceIdCookie = cookiesMap.get(SOURCE_ID_COOKIE);
68
 
3561 rajveer 69
        String sourceIdString = updateSourceIdCookie();
70
 
71
        long sourceId = -1;
72
        if(sourceIdString != null){
73
        	try{
74
        		sourceId = Long.parseLong(sourceIdString);
75
        	}catch (NumberFormatException e) {
76
        		log.error("Invalid source cookie. Clear the cookie.");
3565 rajveer 77
        		expireSourceIdCookie();
3561 rajveer 78
			}
79
        }
80
 
81
		if (action instanceof SourceAware) {
82
			SourceAware sessionAction = (SourceAware) action;
83
			sessionAction.setSourceId(sourceId);
84
        }
85
 
86
        return invocation.invoke();
87
    }
88
 
89
 
90
    /**
91
     * Update source id cookie
92
     * 
93
     */
94
    private String updateSourceIdCookie() {
95
    	String sourceId = getSourceId();
96
        if(sourceId == null){
97
        	return sourceId;
98
        }
99
 
100
        DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
3565 rajveer 101
        String sourceIdCookieVal = des.encrypt(sourceId);
3561 rajveer 102
 
103
        //session source
104
        sourceIdCookie = new Cookie(SOURCE_ID_COOKIE,
3565 rajveer 105
                sourceIdCookieVal);
3561 rajveer 106
        sourceIdCookie.setPath("/");
107
        sourceIdCookie.setMaxAge(SECONDS_IN_DAY);
108
        if (!cookieDomain.isEmpty()) {
109
            sourceIdCookie.setDomain(cookieDomain);
110
        }
111
        cookiesMap.put(SOURCE_ID_COOKIE, sourceIdCookie);
112
        response.addCookie(sourceIdCookie);
113
        return sourceId;
114
    }
115
 
116
    private void createCookiesMap() {
117
        cookiesMap = new HashMap<String, Cookie>();
118
        Cookie[] cookies = request.getCookies();
119
 
120
        if (cookies == null)
121
            return;
122
        for (Cookie cookie : cookies) {
123
            cookiesMap.put(cookie.getName(), cookie);
124
        }
125
    }
126
 
127
    /**
3565 rajveer 128
     * 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.
3561 rajveer 129
     * If referer is saholic, return sourceId from cookie if set, else return null.
130
     */
131
    private String getSourceId() {
132
    	String sourceId = null;
133
        String referer = request.getHeader("referer");
5625 vikram.rag 134
        if (referer != null && !referer.isEmpty() && !referer.contains("saholic.com" ) && !referer.contains("shop2020.in" ) && !referer.contains("hotspot.com" )) {
3561 rajveer 135
        	for(Source source: allSources){
136
        		if(referer.matches(source.getIdentifier())){
137
        			return source.getId()+"";
138
        		}
139
        	}
3565 rajveer 140
        	// If previously we had source cookie, but now she has come from other source which is not listed.
141
        	// Now we should clear the cookie. 
142
        	if (sourceIdCookie != null) {
143
        		expireSourceIdCookie();
144
        	}
3561 rajveer 145
        }else{
3565 rajveer 146
        	if (sourceIdCookie != null) {
3561 rajveer 147
            	DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
148
            	sourceId = des.decrypt(sourceIdCookie.getValue());
149
                return sourceId;
150
            }
151
        }
152
        return sourceId;
153
    }
3565 rajveer 154
 
155
 
156
    /**
157
     * Expires the Source ID cookie.
158
     */
159
    private void expireSourceIdCookie() {
160
        Cookie sourceIdCookie = new Cookie(SOURCE_ID_COOKIE, "-1"); 
161
        sourceIdCookie.setMaxAge(0);                     // Expire this cookie now
162
        sourceIdCookie.setPath("/");
163
        sourceIdCookie.setDomain(cookieDomain);
164
        HttpServletResponse response = ServletActionContext.getResponse();
165
        response.addCookie(sourceIdCookie);
166
    }
3561 rajveer 167
}