Subversion Repositories SmartDukaan

Rev

Rev 26743 | Rev 27235 | 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;
23784 ashik.ali 11
import org.apache.logging.log4j.LogManager;
23568 govind 12
import org.apache.logging.log4j.Logger;
21577 ashik.ali 13
import org.springframework.stereotype.Component;
14
 
15
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
16
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22139 amit.gupta 17
import com.spice.profitmandi.web.model.LoginDetails;
21577 ashik.ali 18
 
19
@Component
22069 ashik.ali 20
public class CookiesProcessor {
21577 ashik.ali 21
 
23568 govind 22
	private static final Logger LOGGER = LogManager.getLogger(CookiesProcessor.class);
21577 ashik.ali 23
 
22139 amit.gupta 24
	public LoginDetails getCookiesObject(HttpServletRequest request) throws ProfitMandiBusinessException{
27229 amit.gupta 25
		boolean readOnly = false;
21577 ashik.ali 26
		Cookie[] cookies = request.getCookies();
21626 kshitij.so 27
		if (cookies == null){
23784 ashik.ali 28
			throw new ProfitMandiBusinessException("cookies", "", "GE_1008");
21598 ashik.ali 29
		}
23784 ashik.ali 30
		String fofoIdFound = null, emailIdFound = null, roleIdsString = 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
			}
23784 ashik.ali 39
			if(cookie.getName().equals(ProfitMandiConstants.ROLE_IDS) && cookie.getValue() != null){
40
				roleIdsString = cookie.getValue();
22217 ashik.ali 41
				//LOGGER.info("roleNameString is {}", roleNamesString);
22111 ashik.ali 42
			}
27229 amit.gupta 43
			if(cookie.getName().equals(ProfitMandiConstants.READONLY_KEY) && cookie.getValue() != null){
44
				readOnly = true;
45
				//LOGGER.info("roleNameString is {}", roleNamesString);
46
			}
23784 ashik.ali 47
			if(fofoIdFound != null && emailIdFound != null && roleIdsString != null){
21583 ashik.ali 48
				break;
49
			}
50
		}
51
 
23784 ashik.ali 52
		if(fofoIdFound == null || emailIdFound == null || roleIdsString == null){
53
			LOGGER.info("roleIdString is {}", roleIdsString);
22217 ashik.ali 54
			LOGGER.info("fofoIdFound is {}", fofoIdFound);
55
			LOGGER.info("emailIdFound is {}", emailIdFound);
21577 ashik.ali 56
			LOGGER.error("Requested session is not valid");
23784 ashik.ali 57
			throw new ProfitMandiBusinessException("cookies", "", "GE_1008");
23510 amit.gupta 58
		}else {
22139 amit.gupta 59
			LoginDetails fofoDetails = new LoginDetails();
21583 ashik.ali 60
			fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
61
			fofoDetails.setEmailId(emailIdFound);
26743 amit.gupta 62
			String[] roleIdStrings = StringUtils.split(roleIdsString, "-");
63
			if(roleIdStrings.length==1) {
64
				roleIdStrings = StringUtils.split(roleIdsString, ",");
65
			}
23784 ashik.ali 66
			Set<Integer> roleIds = new HashSet<>();
67
			for(String roleId : roleIdStrings){
68
				roleIds.add(Integer.valueOf(roleId));
22111 ashik.ali 69
			}
23784 ashik.ali 70
			fofoDetails.setRoleIds(roleIds);
27229 amit.gupta 71
			fofoDetails.setReadOnly(readOnly);
23506 amit.gupta 72
			return fofoDetails;
21577 ashik.ali 73
		}
21583 ashik.ali 74
 
21577 ashik.ali 75
	}
22069 ashik.ali 76
 
77
	public void removeCookies(HttpServletRequest request, HttpServletResponse response) throws ProfitMandiBusinessException{
78
		Cookie[] cookies = request.getCookies();
79
		if (cookies == null){
80
			throw new ProfitMandiBusinessException("", "", "");
81
		}
82
 
83
		Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, "");
84
		cookieFofoId.setMaxAge(0);
22093 amit.gupta 85
		cookieFofoId.setPath(request.getContextPath());
22094 amit.gupta 86
		cookieFofoId.setDomain(request.getServerName());
87
 
88
		Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, "");
22069 ashik.ali 89
		cookieEmailId.setMaxAge(0);
22094 amit.gupta 90
		cookieEmailId.setDomain(request.getServerName());
22093 amit.gupta 91
		cookieEmailId.setPath(request.getContextPath());
22094 amit.gupta 92
 
23784 ashik.ali 93
		Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_IDS, "");
22111 ashik.ali 94
		cookieRoleNames.setMaxAge(0);
95
		cookieRoleNames.setDomain(request.getServerName());
96
		cookieRoleNames.setPath(request.getContextPath());
22139 amit.gupta 97
 
98
		Cookie cookieFofoFlag = new Cookie(ProfitMandiConstants.FOFO_FLAG, "");
99
		cookieFofoFlag.setMaxAge(0);
100
		cookieFofoFlag.setDomain(request.getServerName());
101
		cookieFofoFlag.setPath(request.getContextPath());
22111 ashik.ali 102
 
27229 amit.gupta 103
		Cookie cookieReadOnly = new Cookie(ProfitMandiConstants.READONLY_KEY, "");
104
		cookieFofoFlag.setMaxAge(0);
105
		cookieFofoFlag.setDomain(request.getServerName());
106
		cookieFofoFlag.setPath(request.getContextPath());
107
 
22069 ashik.ali 108
		response.addCookie(cookieFofoId);
109
		response.addCookie(cookieEmailId);
22111 ashik.ali 110
		response.addCookie(cookieRoleNames);
22139 amit.gupta 111
		response.addCookie(cookieFofoFlag);
27229 amit.gupta 112
		response.addCookie(cookieReadOnly);
22069 ashik.ali 113
	}
21577 ashik.ali 114
}