Subversion Repositories SmartDukaan

Rev

Rev 21626 | Rev 22093 | 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 javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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.web.model.FofoDetails;

@Component
public class CookiesProcessor {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(CookiesProcessor.class);
        
        public FofoDetails getCookiesObject(HttpServletRequest request) throws ProfitMandiBusinessException{
                Cookie[] cookies = request.getCookies();
                if (cookies == null){
                        throw new ProfitMandiBusinessException("", "", "");
                }
                String fofoIdFound = null, emailIdFound = null;
                for(Cookie cookie : cookies){
                        LOGGER.info("Requested Cookie {}", cookie.getName());
                        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(fofoIdFound != null && emailIdFound != null){
                                break;
                        }
                }
                
                if(fofoIdFound == null || emailIdFound == null){
                        LOGGER.error("Requested session is not valid");
                        throw new ProfitMandiBusinessException("", "", "");
                }else{
                        FofoDetails fofoDetails = new FofoDetails();
                        fofoDetails.setFofoId(Integer.parseInt(fofoIdFound));
                        fofoDetails.setEmailId(emailIdFound);
                        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, "");
                Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, "");
                cookieFofoId.setMaxAge(0);
                cookieEmailId.setMaxAge(0);
                response.addCookie(cookieFofoId);
                response.addCookie(cookieEmailId);
        }
}