Subversion Repositories SmartDukaan

Rev

Rev 27238 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.util;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.web.model.LoginDetails;

@Component
public class CookiesProcessor {
        
        private static final Logger LOGGER = LogManager.getLogger(CookiesProcessor.class);
        
        public LoginDetails getCookiesObject(HttpServletRequest request) throws ProfitMandiBusinessException{
                boolean readOnly = false;
                Cookie[] cookies = request.getCookies();
                if (cookies == null){
                        throw new ProfitMandiBusinessException("cookies", "", "GE_1008");
                }
                String fofoIdFound = null, emailIdFound = null, roleIdsString = null, readOnlyString = null;
                for(Cookie cookie : cookies){
                        //LOGGER.info("Requested Cookie {}={}", cookie.getName(), cookie.getValue());
                        if(cookie.getName().equals(ProfitMandiConstants.FOFO_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
                                fofoIdFound = cookie.getValue();
                        }
                        if(cookie.getName().equals(ProfitMandiConstants.EMAIL_ID) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
                                emailIdFound = cookie.getValue();
                        }
                        if(cookie.getName().equals(ProfitMandiConstants.ROLE_IDS) && cookie.getValue() != null){
                                roleIdsString = cookie.getValue();
                                //LOGGER.info("roleNameString is {}", roleNamesString);
                        }
                        if(cookie.getName().equals(ProfitMandiConstants.READONLY_KEY) && cookie.getValue() != null){
                                readOnlyString = cookie.getValue();
                                try {
                                        readOnly = Boolean.parseBoolean(readOnlyString); 
                                } catch(Exception e) {
                                        
                                }
                        }
                        if(fofoIdFound != null && emailIdFound != null && roleIdsString != null && readOnlyString != null){
                                break;
                        }
                }
                
                if(fofoIdFound == null || emailIdFound == null || roleIdsString == null){
                        LOGGER.error("Requested session is not valid");
                        throw new ProfitMandiBusinessException("cookies", "", "GE_1008");
                }else {
                        LoginDetails fofoDetails = new LoginDetails();
                        fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
                        fofoDetails.setEmailId(emailIdFound);
                        LOGGER.info("Session validated for - {}", fofoDetails);
                        String[] roleIdStrings = StringUtils.split(roleIdsString, "-");
                        if(roleIdStrings.length==1) {
                                roleIdStrings = StringUtils.split(roleIdsString, ",");
                        }
                        Set<Integer> roleIds = new HashSet<>();
                        for(String roleId : roleIdStrings){
                                roleIds.add(Integer.valueOf(roleId));
                        }
                        fofoDetails.setRoleIds(roleIds);
                        fofoDetails.setReadOnly(readOnly);
                        return fofoDetails;
                }
                
        }
        
        public void removeCookies(HttpServletRequest request, HttpServletResponse response) throws ProfitMandiBusinessException{
                Cookie[] cookies = request.getCookies();
                if (cookies == null){
                        throw new ProfitMandiBusinessException("", "", "");
                }
                
                Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, "");
                cookieFofoId.setMaxAge(0);
                cookieFofoId.setPath(request.getContextPath());
                cookieFofoId.setDomain(request.getServerName());
                
                Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, "");
                cookieEmailId.setMaxAge(0);
                cookieEmailId.setDomain(request.getServerName());
                cookieEmailId.setPath(request.getContextPath());

                Cookie cookieRoleNames = new Cookie(ProfitMandiConstants.ROLE_IDS, "");
                cookieRoleNames.setMaxAge(0);
                cookieRoleNames.setDomain(request.getServerName());
                cookieRoleNames.setPath(request.getContextPath());

                Cookie cookieFofoFlag = new Cookie(ProfitMandiConstants.FOFO_FLAG, "");
                cookieFofoFlag.setMaxAge(0);
                cookieFofoFlag.setDomain(request.getServerName());
                cookieFofoFlag.setPath(request.getContextPath());
                
                Cookie cookieReadOnly = new Cookie(ProfitMandiConstants.READONLY_KEY, "");
                cookieReadOnly.setMaxAge(0);
                cookieReadOnly.setDomain(request.getServerName());
                cookieReadOnly.setPath(request.getContextPath());
                
                response.addCookie(cookieFofoId);
                response.addCookie(cookieEmailId);
                response.addCookie(cookieRoleNames);
                response.addCookie(cookieFofoFlag);
                response.addCookie(cookieReadOnly);
        }
}