Subversion Repositories SmartDukaan

Rev

Rev 7825 | Rev 11986 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
781 vikas 1
package in.shop2020.serving.interceptors;
2
 
3185 vikas 3
import in.shop2020.serving.services.UserSessionInfo;
4
import in.shop2020.serving.utils.DesEncrypter;
5
 
781 vikas 6
import java.util.HashMap;
2907 rajveer 7
import java.util.List;
781 vikas 8
import java.util.Map;
9
 
10
import javax.servlet.http.Cookie;
11
import javax.servlet.http.HttpServletRequest;
837 vikas 12
import javax.servlet.http.HttpServletResponse;
781 vikas 13
import javax.servlet.http.HttpSession;
14
 
1044 chandransh 15
import org.apache.log4j.Logger;
781 vikas 16
import org.apache.struts2.ServletActionContext;
2907 rajveer 17
import org.apache.struts2.StrutsStatics;
781 vikas 18
 
2907 rajveer 19
import com.opensymphony.xwork2.ActionContext;
781 vikas 20
import com.opensymphony.xwork2.ActionInvocation;
21
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
2907 rajveer 22
import com.opensymphony.xwork2.interceptor.PreResultListener;
781 vikas 23
 
2907 rajveer 24
public class UserInterceptor extends AbstractInterceptor implements PreResultListener{
781 vikas 25
 
2907 rajveer 26
	public static final int SECONDS_IN_YEAR = 60*60*24*365; 
7825 amar.kumar 27
	public static final int FACEBOOK_USER_COOKIE_EXPIRY_TIME = 60*60*24*60; 
837 vikas 28
 
781 vikas 29
	private static final long serialVersionUID = -4125815700236506235L;
1044 chandransh 30
	private static Logger log = Logger.getLogger(UserInterceptor.class);
781 vikas 31
 
2907 rajveer 32
	public static final String USER_INFO_COOKIE_NAME = "uic";
33
	public static final String USER_ID_COOKIE_NAME = "uid";
11980 amit.gupta 34
	public static final String DEAL_COUPON_REMOVED = "dcr";
2998 rajveer 35
	public static final String COOKIE_DECRYPTION_STRING = "shop2020";
781 vikas 36
 
2998 rajveer 37
	private DesEncrypter desEncrypter = new DesEncrypter(COOKIE_DECRYPTION_STRING);
781 vikas 38
 
1658 vikas 39
	private String cookieDomain = "";
40
 
41
	public void setCookieDomain(String cookieDomain) {
42
		this.cookieDomain = cookieDomain;
43
	}
44
 
781 vikas 45
	@Override
46
	public String intercept(ActionInvocation invocation) throws Exception {
47
		final Object action = invocation.getAction();
48
 
1658 vikas 49
		log.debug("inside user intercepror");
4388 rajveer 50
 
51
		HttpServletRequest request = ServletActionContext.getRequest();
3185 vikas 52
        HttpSession session = request.getSession(); // Do not remove it, session id is used for session tracking.
781 vikas 53
 
4388 rajveer 54
        Map<String, Cookie> cookiesMap = createCookiesMap(request);
781 vikas 55
 
2973 chandransh 56
        // CreateUserInterceptor may have set the userinfo object in the request
57
        // itself. If we can get the userinfo object here, we don't need to
58
        // parse the cookies that came in with the request.
2907 rajveer 59
		UserSessionInfo userInfo = (UserSessionInfo) request.getAttribute(USER_INFO_COOKIE_NAME);
60
 
4388 rajveer 61
		Cookie userCookie = cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
62
		Cookie userinfoCookie = cookiesMap.get(USER_INFO_COOKIE_NAME);
1354 vikas 63
 
2907 rajveer 64
		if(userInfo == null ){
2973 chandransh 65
		    //Okay, we didn't get the userinfo object from the request. Time to parse the UIC cookie.
2907 rajveer 66
			if(userinfoCookie!=null){
67
				userInfo = UserSessionInfo.getUserSessionInfoFromCookieValue(userinfoCookie.getValue());
2973 chandransh 68
				if(userInfo.getUserId() == -1){
69
				    //This means that the cookie couldn't be parsed. So, we should remove the cookie.
70
				    expireUicCookie();
71
				    expireUidCookie();
72
				}
73
			} else {
74
			    //No UIC cookie too. Try the old UID cookie. This method is guaranteed  to return a userinfo object, cookie or not.
4388 rajveer 75
				userInfo = createAndGetSessionFromUIDCookie(session, cookiesMap, userCookie);
1614 rajveer 76
			}
1354 vikas 77
		}
2973 chandransh 78
 
79
		//Set the request attribute for access by other interceptors.
2907 rajveer 80
		request.setAttribute(USER_INFO_COOKIE_NAME, userInfo);
81
 
2973 chandransh 82
		//Set the userinfo object for use by actions.
781 vikas 83
		if (action instanceof UserAware) {
84
        	UserAware sessionAction = (UserAware) action;
85
        	sessionAction.setSession(session);
86
        	sessionAction.setUserSessionInfo(userInfo);
87
        	sessionAction.setCookiesMap(cookiesMap);
88
        	sessionAction.setUserCookie(userCookie);
1713 vikas 89
        	sessionAction.setCookieDomainName(cookieDomain);
781 vikas 90
        }
2907 rajveer 91
 
2973 chandransh 92
        // Ensure that the response of the action is presented to the pre-result
93
        // listened of this interceptor. We want to add the cookies there.
2907 rajveer 94
		invocation.addPreResultListener(this);
95
 
781 vikas 96
		return invocation.invoke();
97
	}
98
 
1614 rajveer 99
 
2907 rajveer 100
	@Override
101
	public void beforeResult(ActionInvocation invocation, String resultCode) {
102
		ActionContext ac = invocation.getInvocationContext();
103
		HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE);
104
		addCookiesToResponse(invocation.getAction(), response);
105
	}	
106
 
2973 chandransh 107
	/**
108
	 * Adds cookies to the response object after the action has been executed.
109
	 * 
110
	 * @param action
111
	 * @param response
112
	 */
2907 rajveer 113
	private void addCookiesToResponse(Object action, HttpServletResponse response) {
2960 chandransh 114
	    log.debug("Setting cookies in response");
2907 rajveer 115
		if (action instanceof UserAware) {
116
			List<Cookie> cookies = ((UserAware) action).getCookies();
117
			if (cookies != null) {
118
				for (Cookie cookie : cookies) {
2960 chandransh 119
				    log.debug("Adding cookie " + cookie.getName() + " to the response");
2907 rajveer 120
					response.addCookie(cookie);
121
				}
122
			}
123
		}
124
	}
125
 
2973 chandransh 126
    /**
127
     * Expires the UID cookie if the domain is not set or is set as the empty
128
     * domain. Creates a new UID cookie with the cookie domain set.
129
     * 
130
     * This is mostly to handle legacy issue wherein we were not setting the
131
     * cookie domain explicitly to .saholic.com and different cookies were set
132
     * for saholic.com and www.saholic.com.
133
     * 
134
     * @param request
135
     */
4388 rajveer 136
	private Map<String, Cookie> createCookiesMap(HttpServletRequest request) {
137
		Map<String, Cookie> cookiesMap  = new HashMap<String, Cookie>();
781 vikas 138
		Cookie[] cookies = request.getCookies();
139
		if(cookies==null)
4388 rajveer 140
			return cookiesMap;
1658 vikas 141
		for (Cookie cookie : cookies) {
2907 rajveer 142
			if (cookie.getName().equals(UserInterceptor.USER_ID_COOKIE_NAME)) {
1658 vikas 143
				if (cookie.getDomain() == null || cookie.getDomain().isEmpty()
144
						|| !cookie.getDomain().equals(this.cookieDomain)) 
145
				{
146
					if (!cookieDomain.isEmpty()) {
147
						cookie.setMaxAge(0);
2907 rajveer 148
						Cookie newUserCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, cookie.getValue());
1658 vikas 149
						newUserCookie.setMaxAge(SECONDS_IN_YEAR); // one year
150
						newUserCookie.setPath("/");
151
						newUserCookie.setDomain(cookieDomain);
152
 
153
						HttpServletResponse response = ServletActionContext.getResponse();
154
						response.addCookie(newUserCookie);
155
						response.addCookie(cookie);
2973 chandransh 156
					} else {
1722 vikas 157
					    log.error("cookieDomain not set");
158
					}
1658 vikas 159
				}
160
			}
161
		    cookiesMap.put(cookie.getName(), cookie);
162
		}
4388 rajveer 163
		return cookiesMap;
781 vikas 164
	}
1614 rajveer 165
 
2973 chandransh 166
    /**
167
     * Creates and gets session information from the UID cookie. This should be
168
     * called only when the required information couldn't be had from the UIC
169
     * cookie.
170
     * 
171
     * It also expires the UID cookie if it can't parse the cookie value.
172
     * 
173
     * @param session
174
     * @return A user session info object.
175
     */
4388 rajveer 176
	private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session, Map<String, Cookie> cookiesMap, Cookie userCookie) {
781 vikas 177
		UserSessionInfo userInfo = null;
178
		if(userCookie != null){
179
			String uidString = userCookie.getValue();
180
			if(uidString != null){
181
				try {
182
					Long receivedUID = Long.parseLong(desEncrypter.decrypt(uidString));
828 rajveer 183
                    log.info("Invalid session with user cookie : " + receivedUID);
781 vikas 184
					userInfo = new UserSessionInfo(receivedUID, session.getId());
2935 chandransh 185
					if(userInfo.getUserId() == -1){
186
					    log.error("The User for the UID cookie has been deleted in our database. So cleaning up the UID cookie.");
187
					    expireUidCookie();
188
					}
781 vikas 189
				} catch (NumberFormatException nfe) {
190
					log.error("The UID cookie contains an unparseable userID");
2935 chandransh 191
					expireUidCookie();
2473 chandransh 192
					userInfo = new UserSessionInfo();
781 vikas 193
				}
194
			}
2973 chandransh 195
		} else{
196
		    log.info("Invalid session without user cookie.");
197
		    userInfo = new UserSessionInfo();
830 vikas 198
		}
781 vikas 199
		return userInfo;
200
	}
2907 rajveer 201
 
2973 chandransh 202
	/**
203
	 * Expires the UIC cookie.
204
	 */
205
    private void expireUicCookie() {
206
        Cookie newUserCookie = new Cookie(UserInterceptor.USER_INFO_COOKIE_NAME, "-1"); //The value here is immaterial
207
        newUserCookie.setMaxAge(0);                     // Expire this cookie now
208
        newUserCookie.setPath("/");
209
        newUserCookie.setDomain(cookieDomain);
210
 
211
        HttpServletResponse response = ServletActionContext.getResponse();
212
        response.addCookie(newUserCookie);
213
    }	
214
 
215
    /**
216
     * Expires the UID cookie.
217
     */
2935 chandransh 218
    private void expireUidCookie() {
219
        Cookie newUserCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, "-1"); //The value here is immaterial
220
        newUserCookie.setMaxAge(0);                     // Expire this cookie now
221
        newUserCookie.setPath("/");
222
        newUserCookie.setDomain(cookieDomain);
223
 
224
        HttpServletResponse response = ServletActionContext.getResponse();
225
        response.addCookie(newUserCookie);
226
    }
227
 
781 vikas 228
}