Subversion Repositories SmartDukaan

Rev

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

Rev 2960 Rev 2973
Line 53... Line 53...
53
        HttpSession session = request.getSession(); // Get the existing session or create a new one
53
        HttpSession session = request.getSession(); // Get the existing session or create a new one
54
        
54
        
55
        
55
        
56
		createCookiesMap(request);
56
		createCookiesMap(request);
57
		
57
		
-
 
58
        // CreateUserInterceptor may have set the userinfo object in the request
-
 
59
        // itself. If we can get the userinfo object here, we don't need to
-
 
60
        // parse the cookies that came in with the request.
58
		UserSessionInfo userInfo = (UserSessionInfo) request.getAttribute(USER_INFO_COOKIE_NAME);
61
		UserSessionInfo userInfo = (UserSessionInfo) request.getAttribute(USER_INFO_COOKIE_NAME);
59
 
62
 
60
		userCookie = cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
63
		userCookie = cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
61
		userinfoCookie = cookiesMap.get(USER_INFO_COOKIE_NAME);
64
		userinfoCookie = cookiesMap.get(USER_INFO_COOKIE_NAME);
62
		
65
		
63
		if(userInfo == null ){
66
		if(userInfo == null ){
-
 
67
		    //Okay, we didn't get the userinfo object from the request. Time to parse the UIC cookie.
64
			if(userinfoCookie!=null){
68
			if(userinfoCookie!=null){
65
				userInfo = UserSessionInfo.getUserSessionInfoFromCookieValue(userinfoCookie.getValue());
69
				userInfo = UserSessionInfo.getUserSessionInfoFromCookieValue(userinfoCookie.getValue());
-
 
70
				if(userInfo.getUserId() == -1){
-
 
71
				    //This means that the cookie couldn't be parsed. So, we should remove the cookie.
-
 
72
				    expireUicCookie();
-
 
73
				    expireUidCookie();
-
 
74
				}
66
			}else{
75
			} else {
-
 
76
			    //No UIC cookie too. Try the old UID cookie. This method is guaranteed  to return a userinfo object, cookie or not.
67
				userInfo = createAndGetSessionFromUIDCookie(session);
77
				userInfo = createAndGetSessionFromUIDCookie(session);
68
			}
78
			}
69
		}
79
		}
70
			
-
 
71
		request.setAttribute(USER_INFO_COOKIE_NAME, userInfo);
-
 
72
		
80
		
-
 
81
		//Set the request attribute for access by other interceptors.
-
 
82
		request.setAttribute(USER_INFO_COOKIE_NAME, userInfo);
73
		
83
		
-
 
84
		//Set the userinfo object for use by actions.
74
		if (action instanceof UserAware) {
85
		if (action instanceof UserAware) {
75
        	UserAware sessionAction = (UserAware) action;
86
        	UserAware sessionAction = (UserAware) action;
76
        	sessionAction.setSession(session);
87
        	sessionAction.setSession(session);
77
        	sessionAction.setUserSessionInfo(userInfo);
88
        	sessionAction.setUserSessionInfo(userInfo);
78
        	sessionAction.setCookiesMap(cookiesMap);
89
        	sessionAction.setCookiesMap(cookiesMap);
79
        	sessionAction.setUserCookie(userCookie);
90
        	sessionAction.setUserCookie(userCookie);
80
        	sessionAction.setCookieDomainName(cookieDomain);
91
        	sessionAction.setCookieDomainName(cookieDomain);
81
        }
92
        }
82
		
93
		
-
 
94
        // Ensure that the response of the action is presented to the pre-result
-
 
95
        // listened of this interceptor. We want to add the cookies there.
83
		invocation.addPreResultListener(this);
96
		invocation.addPreResultListener(this);
84
		
97
		
85
		return invocation.invoke();
98
		return invocation.invoke();
86
	}
99
	}
87
	
100
	
Line 91... Line 104...
91
		ActionContext ac = invocation.getInvocationContext();
104
		ActionContext ac = invocation.getInvocationContext();
92
		HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE);
105
		HttpServletResponse response = (HttpServletResponse) ac.get(StrutsStatics.HTTP_RESPONSE);
93
		addCookiesToResponse(invocation.getAction(), response);
106
		addCookiesToResponse(invocation.getAction(), response);
94
	}	
107
	}	
95
 
108
 
-
 
109
	/**
-
 
110
	 * Adds cookies to the response object after the action has been executed.
-
 
111
	 * 
-
 
112
	 * @param action
-
 
113
	 * @param response
96
	
114
	 */
97
	private void addCookiesToResponse(Object action, HttpServletResponse response) {
115
	private void addCookiesToResponse(Object action, HttpServletResponse response) {
98
	    log.debug("Setting cookies in response");
116
	    log.debug("Setting cookies in response");
99
		if (action instanceof UserAware) {
117
		if (action instanceof UserAware) {
100
			List<Cookie> cookies = ((UserAware) action).getCookies();
118
			List<Cookie> cookies = ((UserAware) action).getCookies();
101
			if (cookies != null) {
119
			if (cookies != null) {
Line 105... Line 123...
105
				}
123
				}
106
			}
124
			}
107
		}
125
		}
108
	}
126
	}
109
 
127
 
-
 
128
    /**
-
 
129
     * Expires the UID cookie if the domain is not set or is set as the empty
-
 
130
     * domain. Creates a new UID cookie with the cookie domain set.
110
		  
131
     * 
-
 
132
     * This is mostly to handle legacy issue wherein we were not setting the
-
 
133
     * cookie domain explicitly to .saholic.com and different cookies were set
-
 
134
     * for saholic.com and www.saholic.com.
-
 
135
     * 
-
 
136
     * @param request
-
 
137
     */
111
	private void createCookiesMap(HttpServletRequest request) {
138
	private void createCookiesMap(HttpServletRequest request) {
112
		cookiesMap  = new HashMap<String, Cookie>();
139
		cookiesMap  = new HashMap<String, Cookie>();
113
		Cookie[] cookies = request.getCookies();
140
		Cookie[] cookies = request.getCookies();
114
		if(cookies==null)
141
		if(cookies==null)
115
			return;
142
			return;
Line 126... Line 153...
126
						newUserCookie.setDomain(cookieDomain);
153
						newUserCookie.setDomain(cookieDomain);
127
						
154
						
128
						HttpServletResponse response = ServletActionContext.getResponse();
155
						HttpServletResponse response = ServletActionContext.getResponse();
129
						response.addCookie(newUserCookie);
156
						response.addCookie(newUserCookie);
130
						response.addCookie(cookie);
157
						response.addCookie(cookie);
131
					}
-
 
132
					else {
158
					} else {
133
					    log.error("cookieDomain not set");
159
					    log.error("cookieDomain not set");
134
					}
160
					}
135
				}
161
				}
136
			}
162
			}
137
		    cookiesMap.put(cookie.getName(), cookie);
163
		    cookiesMap.put(cookie.getName(), cookie);
138
		}
164
		}
139
	}
165
	}
140
	
-
 
141
	
-
 
142
 
166
 
-
 
167
    /**
-
 
168
     * Creates and gets session information from the UID cookie. This should be
-
 
169
     * called only when the required information couldn't be had from the UIC
-
 
170
     * cookie.
-
 
171
     * 
-
 
172
     * It also expires the UID cookie if it can't parse the cookie value.
143
	
173
     * 
-
 
174
     * @param session
-
 
175
     * @return A user session info object.
-
 
176
     */
144
	private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
177
	private UserSessionInfo createAndGetSessionFromUIDCookie(HttpSession session) {
145
		userCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
178
		userCookie = (Cookie) cookiesMap.get(UserInterceptor.USER_ID_COOKIE_NAME);
146
		UserSessionInfo userInfo = null;
179
		UserSessionInfo userInfo = null;
147
		if(userCookie != null){
180
		if(userCookie != null){
148
			String uidString = userCookie.getValue();
181
			String uidString = userCookie.getValue();
Line 159... Line 192...
159
					log.error("The UID cookie contains an unparseable userID");
192
					log.error("The UID cookie contains an unparseable userID");
160
					expireUidCookie();
193
					expireUidCookie();
161
					userInfo = new UserSessionInfo();
194
					userInfo = new UserSessionInfo();
162
				}
195
				}
163
			}
196
			}
164
		}
-
 
165
		else{
197
		} else{
166
			userInfo = new UserSessionInfo();
-
 
167
			log.info("Invalid session without user cookie.");
198
		    log.info("Invalid session without user cookie.");
-
 
199
		    userInfo = new UserSessionInfo();
168
		}
200
		}
169
		return userInfo;
201
		return userInfo;
170
	}
202
	}
171
 
203
 
-
 
204
	/**
-
 
205
	 * Expires the UIC cookie.
-
 
206
	 */
-
 
207
    private void expireUicCookie() {
-
 
208
        Cookie newUserCookie = new Cookie(UserInterceptor.USER_INFO_COOKIE_NAME, "-1"); //The value here is immaterial
-
 
209
        newUserCookie.setMaxAge(0);                     // Expire this cookie now
-
 
210
        newUserCookie.setPath("/");
-
 
211
        newUserCookie.setDomain(cookieDomain);
-
 
212
        
-
 
213
        HttpServletResponse response = ServletActionContext.getResponse();
-
 
214
        response.addCookie(newUserCookie);
-
 
215
    }	
-
 
216
	
-
 
217
    /**
-
 
218
     * Expires the UID cookie.
-
 
219
     */
172
    private void expireUidCookie() {
220
    private void expireUidCookie() {
173
        Cookie newUserCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, "-1"); //The value here is immaterial
221
        Cookie newUserCookie = new Cookie(UserInterceptor.USER_ID_COOKIE_NAME, "-1"); //The value here is immaterial
174
        newUserCookie.setMaxAge(0);                     // Expire this cookie now
222
        newUserCookie.setMaxAge(0);                     // Expire this cookie now
175
        newUserCookie.setPath("/");
223
        newUserCookie.setPath("/");
176
        newUserCookie.setDomain(cookieDomain);
224
        newUserCookie.setDomain(cookieDomain);