Subversion Repositories SmartDukaan

Rev

Rev 22149 | Rev 22217 | Go to most recent revision | 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
import com.spice.profitmandi.web.model.LoginDetails;

@Component
public class CookiesProcessor {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(CookiesProcessor.class);
        
        public LoginDetails getCookiesObject(HttpServletRequest request) throws ProfitMandiBusinessException{
                Cookie[] cookies = request.getCookies();
                if (cookies == null){
                        throw new ProfitMandiBusinessException("", "", "");
                }
                String fofoIdFound = null, emailIdFound = null, roleNamesString = null, fofoFlagFound = 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_NAMES) && cookie.getValue() != null){
                                roleNamesString = cookie.getValue();
                                LOGGER.info("roleNameString is {}", roleNamesString);
                        }
                        if(cookie.getName().equals(ProfitMandiConstants.FOFO_FLAG) && cookie.getValue() != null && !cookie.getValue().isEmpty()){
                                fofoFlagFound = cookie.getValue();
                        }
                        if(fofoIdFound != null && emailIdFound != null && roleNamesString != null && fofoFlagFound != null){
                                break;
                        }
                }
                
                if(fofoIdFound == null || emailIdFound == null || roleNamesString == null || fofoFlagFound == null){
                        LOGGER.info("roleNameString is {}", roleNamesString);
                        LOGGER.info("fofoIdFound is {}", roleNamesString);
                        LOGGER.info("emailIdFound is {}", roleNamesString);
                        LOGGER.info("fofoFlagFound is {}", roleNamesString);
                        LOGGER.error("Requested session is not valid");
                        throw new ProfitMandiBusinessException("", "", "");
                }else{
                        LoginDetails fofoDetails = new LoginDetails();
                        fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
                        fofoDetails.setEmailId(emailIdFound);
                        String[] roleNames = StringUtils.split(roleNamesString, ",");
                        Set<RoleType> roleTypes = new HashSet<>();
                        for(String roleName : roleNames){
                                RoleType roleType = RoleType.valueOf(roleName);
                                roleTypes.add(roleType);
                        }
                        fofoDetails.setRoleTypes(roleTypes);
                        fofoDetails.setFofo(Boolean.parseBoolean(fofoFlagFound));
                        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_NAMES, "");
                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());
                
                response.addCookie(cookieFofoId);
                response.addCookie(cookieEmailId);
                response.addCookie(cookieRoleNames);
                response.addCookie(cookieFofoFlag);
        }
}