| 3561 |
rajveer |
1 |
package in.shop2020.serving.interceptors;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.catalog.InventoryService.Client;
|
|
|
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();
|
|
|
67 |
String sourceIdString = updateSourceIdCookie();
|
|
|
68 |
|
|
|
69 |
long sourceId = -1;
|
|
|
70 |
if(sourceIdString != null){
|
|
|
71 |
try{
|
|
|
72 |
sourceId = Long.parseLong(sourceIdString);
|
|
|
73 |
}catch (NumberFormatException e) {
|
|
|
74 |
log.error("Invalid source cookie. Clear the cookie.");
|
|
|
75 |
//FIXME Clear the cookie, although it will work without it
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if (action instanceof SourceAware) {
|
|
|
80 |
SourceAware sessionAction = (SourceAware) action;
|
|
|
81 |
sessionAction.setSourceId(sourceId);
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
return invocation.invoke();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Update source id cookie
|
|
|
90 |
*
|
|
|
91 |
*/
|
|
|
92 |
private String updateSourceIdCookie() {
|
|
|
93 |
String sourceId = getSourceId();
|
|
|
94 |
if(sourceId == null){
|
|
|
95 |
return sourceId;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
|
|
|
99 |
String sessionSourceCookieVal = des.encrypt(sourceId);
|
|
|
100 |
|
|
|
101 |
//session source
|
|
|
102 |
sourceIdCookie = new Cookie(SOURCE_ID_COOKIE,
|
|
|
103 |
sessionSourceCookieVal);
|
|
|
104 |
sourceIdCookie.setPath("/");
|
|
|
105 |
sourceIdCookie.setMaxAge(SECONDS_IN_DAY);
|
|
|
106 |
if (!cookieDomain.isEmpty()) {
|
|
|
107 |
sourceIdCookie.setDomain(cookieDomain);
|
|
|
108 |
}
|
|
|
109 |
cookiesMap.put(SOURCE_ID_COOKIE, sourceIdCookie);
|
|
|
110 |
response.addCookie(sourceIdCookie);
|
|
|
111 |
return sourceId;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private void createCookiesMap() {
|
|
|
115 |
cookiesMap = new HashMap<String, Cookie>();
|
|
|
116 |
Cookie[] cookies = request.getCookies();
|
|
|
117 |
|
|
|
118 |
if (cookies == null)
|
|
|
119 |
return;
|
|
|
120 |
for (Cookie cookie : cookies) {
|
|
|
121 |
cookiesMap.put(cookie.getName(), cookie);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Check if referer is not saholic or null, try to match with all sources. return source id if matches else return null.
|
|
|
127 |
* If referer is saholic, return sourceId from cookie if set, else return null.
|
|
|
128 |
*/
|
|
|
129 |
private String getSourceId() {
|
|
|
130 |
String sourceId = null;
|
|
|
131 |
String referer = request.getHeader("referer");
|
|
|
132 |
if (referer != null && !referer.isEmpty() && !referer.contains("saholic.com" ) && !referer.contains("shop2020.in" )) {
|
|
|
133 |
for(Source source: allSources){
|
|
|
134 |
if(referer.matches(source.getIdentifier())){
|
|
|
135 |
return source.getId()+"";
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
}else{
|
|
|
139 |
sourceIdCookie = (Cookie) cookiesMap.get(SOURCE_ID_COOKIE);
|
|
|
140 |
if (sourceIdCookie != null) {
|
|
|
141 |
DesEncrypter des = new DesEncrypter(ENCRIPTION_STRING);
|
|
|
142 |
sourceId = des.decrypt(sourceIdCookie.getValue());
|
|
|
143 |
return sourceId;
|
|
|
144 |
}
|
|
|
145 |
}
|
|
|
146 |
return sourceId;
|
|
|
147 |
}
|
|
|
148 |
}
|