Subversion Repositories SmartDukaan

Rev

Rev 6903 | Rev 8917 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6903 Rev 8578
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
-
 
3
import in.shop2020.model.v1.catalog.Banner;
-
 
4
import in.shop2020.model.v1.catalog.BannerMap;
-
 
5
import in.shop2020.model.v1.catalog.CatalogService.Client;
3
import in.shop2020.serving.interceptors.SourceAware;
6
import in.shop2020.serving.interceptors.SourceAware;
4
import in.shop2020.serving.interceptors.TrackingInterceptor;
7
import in.shop2020.serving.interceptors.TrackingInterceptor;
5
import in.shop2020.serving.interceptors.UserAware;
8
import in.shop2020.serving.interceptors.UserAware;
6
import in.shop2020.serving.interceptors.UserInterceptor;
9
import in.shop2020.serving.interceptors.UserInterceptor;
7
import in.shop2020.serving.services.PageLoaderHandler;
10
import in.shop2020.serving.services.PageLoaderHandler;
8
import in.shop2020.serving.services.UserSessionInfo;
11
import in.shop2020.serving.services.UserSessionInfo;
9
import in.shop2020.serving.utils.DesEncrypter;
12
import in.shop2020.serving.utils.DesEncrypter;
-
 
13
import in.shop2020.thrift.clients.CatalogClient;
10
 
14
 
11
import java.util.ArrayList;
15
import java.util.ArrayList;
12
import java.util.HashMap;
16
import java.util.HashMap;
13
import java.util.List;
17
import java.util.List;
14
import java.util.Map;
18
import java.util.Map;
Line 19... Line 23...
19
import javax.servlet.http.HttpSession;
23
import javax.servlet.http.HttpSession;
20
 
24
 
21
import org.apache.log4j.Logger;
25
import org.apache.log4j.Logger;
22
import org.apache.struts2.interceptor.ServletRequestAware;
26
import org.apache.struts2.interceptor.ServletRequestAware;
23
import org.apache.struts2.interceptor.ServletResponseAware;
27
import org.apache.struts2.interceptor.ServletResponseAware;
-
 
28
import org.apache.thrift.TException;
24
 
29
 
25
import com.opensymphony.xwork2.ValidationAwareSupport;
30
import com.opensymphony.xwork2.ValidationAwareSupport;
26
 
31
 
27
/**
32
/**
28
 * Base class for all user action handlers i.e. controllers
33
 * Base class for all user action handlers i.e. controllers
29
 * 
34
 * 
30
 * @author rajveer
35
 * @author rajveer
31
 */
36
 */
32
public abstract class BaseController extends ValidationAwareSupport implements
37
public abstract class BaseController extends ValidationAwareSupport implements
33
		ServletResponseAware, ServletRequestAware, UserAware, SourceAware {
38
ServletResponseAware, ServletRequestAware, UserAware, SourceAware {
34
	/**
39
	/**
35
	 * 
40
	 * 
36
	 */
41
	 */
37
	private static final long serialVersionUID = 1L;
42
	private static final long serialVersionUID = 1L;
38
	protected Map<String, Cookie> cookiesMap = null;
43
	protected Map<String, Cookie> cookiesMap = null;
Line 41... Line 46...
41
	protected HttpSession session;
46
	protected HttpSession session;
42
	protected String domainName;
47
	protected String domainName;
43
	protected UserSessionInfo userinfo = null;
48
	protected UserSessionInfo userinfo = null;
44
	private static Logger log = Logger.getLogger(Class.class);
49
	private static Logger log = Logger.getLogger(Class.class);
45
	private DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
50
	private DesEncrypter desEncrypter = new DesEncrypter(UserInterceptor.COOKIE_DECRYPTION_STRING);
46
	
51
 
47
	protected Cookie userCookie = null;
52
	protected Cookie userCookie = null;
48
 
53
 
49
	protected Map<String, String> htmlSnippets;
54
	protected Map<String, String> htmlSnippets;
50
 
55
 
51
	PageLoaderHandler pageLoader = null;
56
	PageLoaderHandler pageLoader = null;
52
	
57
 
53
	protected long sourceId;
58
	protected long sourceId;
-
 
59
 
-
 
60
	public static Map<String,List<Banner>> activeBanners = null;
-
 
61
	public static Map<String, List<BannerMap>> allBannersMap = null;
-
 
62
 
-
 
63
	public void setBanners(){
-
 
64
		if(activeBanners==null){
-
 
65
			try {
-
 
66
				CatalogClient catalogServiceClient = new CatalogClient();
-
 
67
				Client client = catalogServiceClient.getClient();
-
 
68
				log.info("Populating banner map"); 
-
 
69
				activeBanners = client.getActiveBanners();
-
 
70
				if(!activeBanners.isEmpty()){
-
 
71
					allBannersMap = new HashMap<String, List<BannerMap>>();
-
 
72
					for (Map.Entry<String, List<Banner>> entry :activeBanners.entrySet()){
-
 
73
						for (Banner banner : entry.getValue()){
-
 
74
							allBannersMap.put(banner.getBannerName(), client.getBannerMapDetails(banner.getBannerName()));
-
 
75
						}
-
 
76
					}
-
 
77
				}
-
 
78
			}
-
 
79
			catch (TException e) {
-
 
80
				log.error("Unable to fetch banners ",e);
-
 
81
			}
-
 
82
		}
-
 
83
	}
-
 
84
 
-
 
85
	public List<Banner> getActiveBanners() {
-
 
86
		setBanners();
-
 
87
		String uri = request.getRequestURI();
-
 
88
		return activeBanners.get(uri);
-
 
89
	}
-
 
90
 
-
 
91
	public List<BannerMap> getbannermapdetails(String bannerName) {
-
 
92
		return allBannersMap.get(bannerName);
-
 
93
	}
-
 
94
 
-
 
95
	public int activeBannerCount() {
-
 
96
		setBanners();
-
 
97
		String uri = request.getRequestURI();
-
 
98
		if (activeBanners.get(uri)==null){
-
 
99
			return 0;
-
 
100
		}
-
 
101
		return activeBanners.get(uri).size();
54
	
102
	}
-
 
103
 
55
	public BaseController() {
104
	public BaseController() {
56
		pageLoader = new PageLoaderHandler();
105
		pageLoader = new PageLoaderHandler();
57
		htmlSnippets = new HashMap<String, String>();
106
		htmlSnippets = new HashMap<String, String>();
58
	}
107
	}
59
 
108
 
Line 90... Line 139...
90
		}
139
		}
91
		this.userCookie = userCookie;
140
		this.userCookie = userCookie;
92
	}
141
	}
93
 
142
 
94
	@Override
143
	@Override
95
    public void setCookieDomainName(String domainName) {
144
	public void setCookieDomainName(String domainName) {
96
        this.domainName = domainName;
145
		this.domainName = domainName;
97
    }
146
	}
98
	
147
 
99
	@Override
148
	@Override
100
	public void setSourceId(long sourceId){
149
	public void setSourceId(long sourceId){
-
 
150
		log.info("Setting source id "+sourceId);
101
		this.sourceId = sourceId;
151
		this.sourceId = sourceId;
102
	}
152
	}
103
	
153
 
104
	public String getHeaderSnippet() {
154
	public String getHeaderSnippet() {
105
		String url = request.getQueryString();
155
		String url = request.getQueryString();
106
		log.info("Query String is: " + url);
156
		log.info("Query String is: " + url);
107
		if (url == null) {
157
		if (url == null) {
108
			url = "";
158
			url = "";
Line 110... Line 160...
110
			url = "?" + url;
160
			url = "?" + url;
111
		}
161
		}
112
		url = request.getRequestURI() + url;
162
		url = request.getRequestURI() + url;
113
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url, -1, true);
163
		return pageLoader.getHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url, -1, true);
114
	}
164
	}
115
	
165
 
116
	public String getThinHeaderSnippet() {
166
	public String getThinHeaderSnippet() {
117
        String url = request.getQueryString();
167
		String url = request.getQueryString();
118
        log.info("Query String is: " + url);
168
		log.info("Query String is: " + url);
119
        if (url == null) {
169
		if (url == null) {
120
            url = "";
170
			url = "";
121
        } else {
171
		} else {
122
            url = "?" + url;
172
			url = "?" + url;
123
        }
173
		}
124
        url = request.getRequestURI() + url;
174
		url = request.getRequestURI() + url;
125
        return pageLoader.getThinHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url, 0, true);
175
		return pageLoader.getThinHeaderHtml(userinfo.isLoggedIn(), userinfo.getEmail(), userinfo.getTotalItems(), url, 0, true);
126
    }
176
	}
127
 
177
 
128
	public String getSearchBarSnippet() {
178
	public String getSearchBarSnippet() {
129
		//FIXME From where it is called, need to pass category
179
		//FIXME From where it is called, need to pass category
130
		return pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000);
180
		return pageLoader.getSearchBarHtml(userinfo.getTotalItems(), 10000);
131
	}
181
	}
132
 
182
 
133
	public String getCartWidgetSnippet() {
183
	public String getCartWidgetSnippet() {
134
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),-1);
184
		return pageLoader.getCartWidgetSnippet(userinfo.getTotalItems(), userinfo.getTotalAmount(),-1);
135
	}
185
	}
136
 
186
 
137
	
187
 
138
	public UserSessionInfo getUserInfo(){
188
	public UserSessionInfo getUserInfo(){
139
		return this.userinfo;
189
		return this.userinfo;
140
	}
190
	}
141
	
191
 
142
	@Override
192
	@Override
143
	public List<Cookie> getCookies() {
193
	public List<Cookie> getCookies() {
144
		List<Cookie> cookies = new ArrayList<Cookie>();
194
		List<Cookie> cookies = new ArrayList<Cookie>();
145
		long userId = userinfo.getUserId();
195
		long userId = userinfo.getUserId();
146
		if(userId != -1){
196
		if(userId != -1){
Line 166... Line 216...
166
				cookies.add(userinfoCookie);
216
				cookies.add(userinfoCookie);
167
			}
217
			}
168
		}
218
		}
169
		return cookies;
219
		return cookies;
170
	}
220
	}
171
	
221
 
172
	public void clearUserCookies(){
222
	public void clearUserCookies(){
173
		Cookie uidCookie = cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
223
		Cookie uidCookie = cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
174
		uidCookie.setDomain(domainName);
224
		uidCookie.setDomain(domainName);
175
		uidCookie.setPath("/");
225
		uidCookie.setPath("/");
176
		uidCookie.setValue("");
226
		uidCookie.setValue("");
177
		uidCookie.setMaxAge(0);
227
		uidCookie.setMaxAge(0);
178
		this.response.addCookie(uidCookie);
228
		this.response.addCookie(uidCookie);
179
		
229
 
180
		Cookie uicCookie = cookiesMap.get(UserInterceptor.USER_INFO_COOKIE_NAME);
230
		Cookie uicCookie = cookiesMap.get(UserInterceptor.USER_INFO_COOKIE_NAME);
181
		uicCookie.setDomain(domainName);
231
		uicCookie.setDomain(domainName);
182
		uicCookie.setPath("/");
232
		uicCookie.setPath("/");
183
		uicCookie.setValue("");
233
		uicCookie.setValue("");
184
		uicCookie.setMaxAge(0);
234
		uicCookie.setMaxAge(0);
185
		this.response.addCookie(uicCookie);
235
		this.response.addCookie(uicCookie);
186
	}
236
	}
187
	
237
 
188
	/**
238
	/**
189
	 * Get the session_id cookie to track user session.
239
	 * Get the session_id cookie to track user session.
190
	 * It also creates the new cookie using current jsessionid if it was absent. 
240
	 * It also creates the new cookie using current jsessionid if it was absent. 
191
	 * 
241
	 * 
192
	 * @return sessionId
242
	 * @return sessionId
193
	 */
243
	 */
194
	public String getSessionId() {
244
	public String getSessionId() {
195
	    Cookie sessionIdCookie = cookiesMap.get(TrackingInterceptor.SESSION_ID_COOKIE);
245
		Cookie sessionIdCookie = cookiesMap.get(TrackingInterceptor.SESSION_ID_COOKIE);
196
	    if (sessionIdCookie == null) {
246
		if (sessionIdCookie == null) {
197
  	        //session id
247
			//session id
198
	        String sessionId = request.getSession().getId();
248
			String sessionId = request.getSession().getId();
199
	        sessionIdCookie = new Cookie(TrackingInterceptor.SESSION_ID_COOKIE, sessionId);
249
			sessionIdCookie = new Cookie(TrackingInterceptor.SESSION_ID_COOKIE, sessionId);
200
	        sessionIdCookie.setPath("/");
250
			sessionIdCookie.setPath("/");
201
	        if (!domainName.isEmpty()) {
251
			if (!domainName.isEmpty()) {
202
	            sessionIdCookie.setDomain(domainName);
252
				sessionIdCookie.setDomain(domainName);
203
	        }
253
			}
204
	        cookiesMap.put(TrackingInterceptor.SESSION_ID_COOKIE, sessionIdCookie);
254
			cookiesMap.put(TrackingInterceptor.SESSION_ID_COOKIE, sessionIdCookie);
205
	        response.addCookie(sessionIdCookie);
255
			response.addCookie(sessionIdCookie);
206
	    }
256
		}
207
        return sessionIdCookie.getValue();
257
		return sessionIdCookie.getValue();
208
	}
258
	}
209
	
259
 
210
	public String getCookie(String cookieName, boolean isEncripted, String encriptionString) {
260
	public String getCookie(String cookieName, boolean isEncripted, String encriptionString) {
211
	    Cookie cookie = (Cookie) cookiesMap.get(cookieName);
261
		Cookie cookie = (Cookie) cookiesMap.get(cookieName);
212
	    String cookieVal = null;
262
		String cookieVal = null;
213
        if (cookie != null) {
263
		if (cookie != null) {
214
            cookieVal = cookie.getValue();
264
			cookieVal = cookie.getValue();
215
            if (isEncripted) {
265
			if (isEncripted) {
216
                DesEncrypter desEncrypter = new DesEncrypter(encriptionString);
266
				DesEncrypter desEncrypter = new DesEncrypter(encriptionString);
217
                cookieVal = desEncrypter.decrypt(cookieVal);
267
				cookieVal = desEncrypter.decrypt(cookieVal);
218
            }
268
			}
219
        }
269
		}
220
        return cookieVal;
270
		return cookieVal;
221
	}
271
	}
222
 }
-
 
223
272
}
-
 
273
224
274