Subversion Repositories SmartDukaan

Rev

Rev 22094 | Rev 22139 | 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
 
22111 ashik.ali 3
import java.util.HashSet;
4
import java.util.Set;
5
 
21577 ashik.ali 6
import javax.servlet.http.Cookie;
7
import javax.servlet.http.HttpServletRequest;
22069 ashik.ali 8
import javax.servlet.http.HttpServletResponse;
21577 ashik.ali 9
 
10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12
import org.springframework.stereotype.Component;
13
 
14
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
15
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22111 ashik.ali 16
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
21577 ashik.ali 17
import com.spice.profitmandi.web.model.FofoDetails;
18
 
19
@Component
22069 ashik.ali 20
public class CookiesProcessor {
21577 ashik.ali 21
 
22069 ashik.ali 22
	private static final Logger LOGGER = LoggerFactory.getLogger(CookiesProcessor.class);
21577 ashik.ali 23
 
24
	public FofoDetails getCookiesObject(HttpServletRequest request) throws ProfitMandiBusinessException{
25
		Cookie[] cookies = request.getCookies();
21626 kshitij.so 26
		if (cookies == null){
21598 ashik.ali 27
			throw new ProfitMandiBusinessException("", "", "");
28
		}
22111 ashik.ali 29
		String fofoIdFound = null, emailIdFound = null, roleNamesString = null;
21583 ashik.ali 30
		for(Cookie cookie : cookies){
22111 ashik.ali 31
			LOGGER.info("Requested Cookie {}={}", cookie.getName(), cookie.getValue());
21583 ashik.ali 32
			if(cookie.getName().equals(ProfitMandiConstants.FOFO_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
33
				fofoIdFound = cookie.getValue();
34
			}
21626 kshitij.so 35
			if(cookie.getName().equals(ProfitMandiConstants.EMAIL_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
21583 ashik.ali 36
				emailIdFound = cookie.getValue();
37
			}
22111 ashik.ali 38
			if(cookie.getName().equals(ProfitMandiConstants.ROLE_NAMES) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
39
				roleNamesString = cookie.getValue();
40
			}
41
			if(fofoIdFound != null && emailIdFound != null && roleNamesString != null){
21583 ashik.ali 42
				break;
43
			}
44
		}
45
 
22111 ashik.ali 46
		if(fofoIdFound == null || emailIdFound == null || roleNamesString == null){
21577 ashik.ali 47
			LOGGER.error("Requested session is not valid");
48
			throw new ProfitMandiBusinessException("", "", "");
49
		}else{
50
			FofoDetails fofoDetails = new FofoDetails();
21583 ashik.ali 51
			fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
52
			fofoDetails.setEmailId(emailIdFound);
22111 ashik.ali 53
			String[] roleNames = roleNamesString.split(",");
54
			Set<RoleType> roleTypes = new HashSet<>();
55
			for(String roleName : roleNames){
56
				RoleType roleType = RoleType.valueOf(roleName);
57
				roleTypes.add(roleType);
58
			}
59
			fofoDetails.setRoleTypes(roleTypes);
60
 			return fofoDetails;
21577 ashik.ali 61
		}
21583 ashik.ali 62
 
21577 ashik.ali 63
	}
22069 ashik.ali 64
 
65
	public void removeCookies(HttpServletRequest request, HttpServletResponse response) throws ProfitMandiBusinessException{
66
		Cookie[] cookies = request.getCookies();
67
		if (cookies == null){
68
			throw new ProfitMandiBusinessException("", "", "");
69
		}
70
 
71
		Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, "");
72
		cookieFofoId.setMaxAge(0);
22093 amit.gupta 73
		cookieFofoId.setPath(request.getContextPath());
22094 amit.gupta 74
		cookieFofoId.setDomain(request.getServerName());
75
 
76
		Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, "");
22069 ashik.ali 77
		cookieEmailId.setMaxAge(0);
22094 amit.gupta 78
		cookieEmailId.setDomain(request.getServerName());
22093 amit.gupta 79
		cookieEmailId.setPath(request.getContextPath());
22094 amit.gupta 80
 
22111 ashik.ali 81
		Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_NAMES, "");
82
		cookieRoleNames.setMaxAge(0);
83
		cookieRoleNames.setDomain(request.getServerName());
84
		cookieRoleNames.setPath(request.getContextPath());
85
 
22069 ashik.ali 86
		response.addCookie(cookieFofoId);
87
		response.addCookie(cookieEmailId);
22111 ashik.ali 88
		response.addCookie(cookieRoleNames);
22069 ashik.ali 89
	}
21577 ashik.ali 90
}