Subversion Repositories SmartDukaan

Rev

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