Subversion Repositories SmartDukaan

Rev

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

Rev 1614 Rev 1658
Line 9... Line 9...
9
import javax.servlet.http.Cookie;
9
import javax.servlet.http.Cookie;
10
import javax.servlet.http.HttpServletRequest;
10
import javax.servlet.http.HttpServletRequest;
11
import javax.servlet.http.HttpServletResponse;
11
import javax.servlet.http.HttpServletResponse;
12
import javax.servlet.http.HttpSession;
12
import javax.servlet.http.HttpSession;
13
 
13
 
14
import nl.bitwalker.useragentutils.BrowserType;
-
 
15
import nl.bitwalker.useragentutils.UserAgent;
-
 
16
 
-
 
17
import org.apache.log4j.Logger;
14
import org.apache.log4j.Logger;
18
import org.apache.struts2.ServletActionContext;
15
import org.apache.struts2.ServletActionContext;
19
 
16
 
20
import com.opensymphony.xwork2.ActionInvocation;
17
import com.opensymphony.xwork2.ActionInvocation;
21
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
18
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
Line 31... Line 28...
31
	
28
	
32
	private Map<String, Cookie> cookiesMap = null;
29
	private Map<String, Cookie> cookiesMap = null;
33
	private Cookie userCookie = null;
30
	private Cookie userCookie = null;
34
	private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
31
	private DesEncrypter desEncrypter = new DesEncrypter("shop2020");
35
	
32
	
-
 
33
	private String cookieDomain = "";
-
 
34
	
-
 
35
	public void setCookieDomain(String cookieDomain) {
-
 
36
		this.cookieDomain = cookieDomain;
-
 
37
	}
-
 
38
	
36
	@Override
39
	@Override
37
	public String intercept(ActionInvocation invocation) throws Exception {
40
	public String intercept(ActionInvocation invocation) throws Exception {
38
		final Object action = invocation.getAction();
41
		final Object action = invocation.getAction();
39
        
42
        
40
		log.info("inside user intercepror");
43
		log.debug("inside user intercepror");
41
		
44
		
42
        HttpServletRequest request = ServletActionContext.getRequest();
45
        HttpServletRequest request = ServletActionContext.getRequest();
43
        log.info("url " + request.getRequestURL());
-
 
44
        log.info("uri " + request.getRequestURI());
-
 
45
        HttpSession session = request.getSession(); // Get the existing session or create a new one
46
        HttpSession session = request.getSession(); // Get the existing session or create a new one
46
        
47
        
47
        //getCookiesMap(request);
48
        //getCookiesMap(request);
48
		createCookiesMap(request);
49
		createCookiesMap(request);
49
		
50
		
Line 79... Line 80...
79
		Cookie[] cookies = request.getCookies();
80
		Cookie[] cookies = request.getCookies();
80
		// This check is necessary for the first request when no cookies are
81
		// This check is necessary for the first request when no cookies are
81
		// sent.
82
		// sent.
82
		if(cookies==null)
83
		if(cookies==null)
83
			return;
84
			return;
84
		for (Cookie cookie : cookies)
85
		for (Cookie cookie : cookies) {
-
 
86
			if (cookie.getName().equals("uid")) {
-
 
87
				if (cookie.getDomain() == null || cookie.getDomain().isEmpty()
-
 
88
						|| !cookie.getDomain().equals(this.cookieDomain)) 
-
 
89
				{
-
 
90
					if (!cookieDomain.isEmpty()) {
-
 
91
						cookie.setMaxAge(0);
-
 
92
						Cookie newUserCookie = new Cookie("uid", cookie.getValue());
-
 
93
						newUserCookie.setMaxAge(SECONDS_IN_YEAR); // one year
-
 
94
						newUserCookie.setPath("/");
-
 
95
						newUserCookie.setDomain(cookieDomain);
-
 
96
						
-
 
97
						HttpServletResponse response = ServletActionContext.getResponse();
-
 
98
						response.addCookie(newUserCookie);
-
 
99
						response.addCookie(cookie);
-
 
100
					}
-
 
101
				}
-
 
102
			}
85
			cookiesMap.put(cookie.getName(), cookie);
103
		    cookiesMap.put(cookie.getName(), cookie);
-
 
104
		}
86
	}
105
	}
87
	
106
	
88
	private void createUserCookie(long userId, boolean force) {
107
	private void createUserCookie(long userId, boolean force) {
89
		userCookie = (Cookie) cookiesMap.get("uid");
108
		userCookie = (Cookie) cookiesMap.get("uid");
90
		String encryptedUserId = desEncrypter.encrypt(userId + "");
109
		String encryptedUserId = desEncrypter.encrypt(userId + "");
91
		if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
110
		if(force || userCookie == null || !(encryptedUserId + "").equals(userCookie.getValue())){
92
			userCookie = new Cookie("uid", encryptedUserId);
111
			userCookie = new Cookie("uid", encryptedUserId);
93
			userCookie.setMaxAge(SECONDS_IN_YEAR); // one year
112
			userCookie.setMaxAge(SECONDS_IN_YEAR); // one year
94
			userCookie.setPath("/");
113
			userCookie.setPath("/");
-
 
114
			if(!cookieDomain.isEmpty()) {
-
 
115
				userCookie.setDomain(cookieDomain);
-
 
116
			}
95
			log.info("Created new cookie.");
117
			log.info("Created new cookie.");
96
			cookiesMap.put("uid", userCookie);
118
			cookiesMap.put("uid", userCookie);
97
			HttpServletResponse response = ServletActionContext.getResponse();
119
			HttpServletResponse response = ServletActionContext.getResponse();
98
	        response.addCookie(userCookie);
120
	        response.addCookie(userCookie);
99
		}
121
		}