Subversion Repositories SmartDukaan

Rev

Rev 27238 | 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
		}
27235 amit.gupta 30
		String fofoIdFound = null, emailIdFound = null, roleIdsString = null, readOnlyString = null;
21583 ashik.ali 31
		for(Cookie cookie : cookies){
33070 amit.gupta 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){
27235 amit.gupta 44
				readOnlyString = cookie.getValue();
27236 amit.gupta 45
				try {
46
					readOnly = Boolean.parseBoolean(readOnlyString); 
47
				} catch(Exception e) {
48
 
49
				}
27229 amit.gupta 50
			}
27235 amit.gupta 51
			if(fofoIdFound != null && emailIdFound != null && roleIdsString != null && readOnlyString != null){
21583 ashik.ali 52
				break;
53
			}
54
		}
55
 
23784 ashik.ali 56
		if(fofoIdFound == null || emailIdFound == null || roleIdsString == null){
21577 ashik.ali 57
			LOGGER.error("Requested session is not valid");
23784 ashik.ali 58
			throw new ProfitMandiBusinessException("cookies", "", "GE_1008");
23510 amit.gupta 59
		}else {
22139 amit.gupta 60
			LoginDetails fofoDetails = new LoginDetails();
21583 ashik.ali 61
			fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
62
			fofoDetails.setEmailId(emailIdFound);
33070 amit.gupta 63
			LOGGER.info("Session validated for - {}", fofoDetails);
26743 amit.gupta 64
			String[] roleIdStrings = StringUtils.split(roleIdsString, "-");
65
			if(roleIdStrings.length==1) {
66
				roleIdStrings = StringUtils.split(roleIdsString, ",");
67
			}
23784 ashik.ali 68
			Set<Integer> roleIds = new HashSet<>();
69
			for(String roleId : roleIdStrings){
70
				roleIds.add(Integer.valueOf(roleId));
22111 ashik.ali 71
			}
23784 ashik.ali 72
			fofoDetails.setRoleIds(roleIds);
27229 amit.gupta 73
			fofoDetails.setReadOnly(readOnly);
23506 amit.gupta 74
			return fofoDetails;
21577 ashik.ali 75
		}
21583 ashik.ali 76
 
21577 ashik.ali 77
	}
22069 ashik.ali 78
 
79
	public void removeCookies(HttpServletRequest request, HttpServletResponse response) throws ProfitMandiBusinessException{
80
		Cookie[] cookies = request.getCookies();
81
		if (cookies == null){
82
			throw new ProfitMandiBusinessException("", "", "");
83
		}
84
 
85
		Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, "");
86
		cookieFofoId.setMaxAge(0);
22093 amit.gupta 87
		cookieFofoId.setPath(request.getContextPath());
22094 amit.gupta 88
		cookieFofoId.setDomain(request.getServerName());
89
 
90
		Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, "");
22069 ashik.ali 91
		cookieEmailId.setMaxAge(0);
22094 amit.gupta 92
		cookieEmailId.setDomain(request.getServerName());
22093 amit.gupta 93
		cookieEmailId.setPath(request.getContextPath());
22094 amit.gupta 94
 
23784 ashik.ali 95
		Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_IDS, "");
22111 ashik.ali 96
		cookieRoleNames.setMaxAge(0);
97
		cookieRoleNames.setDomain(request.getServerName());
98
		cookieRoleNames.setPath(request.getContextPath());
22139 amit.gupta 99
 
100
		Cookie cookieFofoFlag = new Cookie(ProfitMandiConstants.FOFO_FLAG, "");
101
		cookieFofoFlag.setMaxAge(0);
102
		cookieFofoFlag.setDomain(request.getServerName());
103
		cookieFofoFlag.setPath(request.getContextPath());
22111 ashik.ali 104
 
27229 amit.gupta 105
		Cookie cookieReadOnly = new Cookie(ProfitMandiConstants.READONLY_KEY, "");
27238 amit.gupta 106
		cookieReadOnly.setMaxAge(0);
107
		cookieReadOnly.setDomain(request.getServerName());
108
		cookieReadOnly.setPath(request.getContextPath());
27229 amit.gupta 109
 
22069 ashik.ali 110
		response.addCookie(cookieFofoId);
111
		response.addCookie(cookieEmailId);
22111 ashik.ali 112
		response.addCookie(cookieRoleNames);
22139 amit.gupta 113
		response.addCookie(cookieFofoFlag);
27229 amit.gupta 114
		response.addCookie(cookieReadOnly);
22069 ashik.ali 115
	}
21577 ashik.ali 116
}