Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21577 ashik.ali 1
package com.spice.profitmandi.web.util;
2
 
3
import javax.servlet.http.Cookie;
4
import javax.servlet.http.HttpServletRequest;
22069 ashik.ali 5
import javax.servlet.http.HttpServletResponse;
21577 ashik.ali 6
 
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import org.springframework.stereotype.Component;
10
 
11
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
12
import com.spice.profitmandi.common.model.ProfitMandiConstants;
13
import com.spice.profitmandi.web.model.FofoDetails;
14
 
15
@Component
22069 ashik.ali 16
public class CookiesProcessor {
21577 ashik.ali 17
 
22069 ashik.ali 18
	private static final Logger LOGGER = LoggerFactory.getLogger(CookiesProcessor.class);
21577 ashik.ali 19
 
20
	public FofoDetails getCookiesObject(HttpServletRequest request) throws ProfitMandiBusinessException{
21
		Cookie[] cookies = request.getCookies();
21626 kshitij.so 22
		if (cookies == null){
21598 ashik.ali 23
			throw new ProfitMandiBusinessException("", "", "");
24
		}
21583 ashik.ali 25
		String fofoIdFound = null, emailIdFound = null;
26
		for(Cookie cookie : cookies){
22069 ashik.ali 27
			LOGGER.info("Requested Cookie {}", cookie.getName());
21583 ashik.ali 28
			if(cookie.getName().equals(ProfitMandiConstants.FOFO_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
29
				fofoIdFound = cookie.getValue();
30
			}
21626 kshitij.so 31
			if(cookie.getName().equals(ProfitMandiConstants.EMAIL_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
21583 ashik.ali 32
				emailIdFound = cookie.getValue();
33
			}
34
			if(fofoIdFound != null && emailIdFound != null){
35
				break;
36
			}
37
		}
38
 
39
		if(fofoIdFound == null || emailIdFound == null){
21577 ashik.ali 40
			LOGGER.error("Requested session is not valid");
41
			throw new ProfitMandiBusinessException("", "", "");
42
		}else{
43
			FofoDetails fofoDetails = new FofoDetails();
21583 ashik.ali 44
			fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
45
			fofoDetails.setEmailId(emailIdFound);
21577 ashik.ali 46
			return fofoDetails;
47
		}
21583 ashik.ali 48
 
21577 ashik.ali 49
	}
22069 ashik.ali 50
 
51
	public void removeCookies(HttpServletRequest request, HttpServletResponse response) throws ProfitMandiBusinessException{
52
		Cookie[] cookies = request.getCookies();
53
		if (cookies == null){
54
			throw new ProfitMandiBusinessException("", "", "");
55
		}
56
 
57
		Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, "");
58
		cookieFofoId.setMaxAge(0);
22093 amit.gupta 59
		cookieFofoId.setPath(request.getContextPath());
22094 amit.gupta 60
		cookieFofoId.setDomain(request.getServerName());
61
 
62
		Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, "");
22069 ashik.ali 63
		cookieEmailId.setMaxAge(0);
22094 amit.gupta 64
		cookieEmailId.setDomain(request.getServerName());
22093 amit.gupta 65
		cookieEmailId.setPath(request.getContextPath());
22094 amit.gupta 66
 
22069 ashik.ali 67
		response.addCookie(cookieFofoId);
68
		response.addCookie(cookieEmailId);
69
	}
21577 ashik.ali 70
}