Subversion Repositories SmartDukaan

Rev

Rev 22069 | Rev 22073 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21561 ashik.ali 1
package com.spice.profitmandi.web.controller;
21555 kshitij.so 2
 
22069 ashik.ali 3
import java.util.Properties;
4
 
21577 ashik.ali 5
import javax.servlet.http.Cookie;
21561 ashik.ali 6
import javax.servlet.http.HttpServletRequest;
21577 ashik.ali 7
import javax.servlet.http.HttpServletResponse;
21561 ashik.ali 8
 
21568 ashik.ali 9
import org.slf4j.Logger;
10
import org.slf4j.LoggerFactory;
21561 ashik.ali 11
import org.springframework.beans.factory.annotation.Autowired;
22069 ashik.ali 12
import org.springframework.core.io.Resource;
21555 kshitij.so 13
import org.springframework.stereotype.Controller;
21987 kshitij.so 14
import org.springframework.transaction.annotation.Transactional;
21615 kshitij.so 15
import org.springframework.ui.Model;
21555 kshitij.so 16
import org.springframework.ui.ModelMap;
17
import org.springframework.web.bind.annotation.ModelAttribute;
18
import org.springframework.web.bind.annotation.RequestMapping;
19
import org.springframework.web.bind.annotation.RequestMethod;
21561 ashik.ali 20
import org.springframework.web.bind.annotation.RequestParam;
21555 kshitij.so 21
 
21561 ashik.ali 22
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
23
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22069 ashik.ali 24
import com.spice.profitmandi.web.config.AppConfig;
21561 ashik.ali 25
import com.spice.profitmandi.web.model.FofoDetails;
22069 ashik.ali 26
import com.spice.profitmandi.web.util.CookiesProcessor;
21561 ashik.ali 27
import com.spice.profitmandi.web.util.GoogleLoginUtil;
21574 ashik.ali 28
import com.spice.profitmandi.web.util.MVCResponseSender;
21561 ashik.ali 29
 
21555 kshitij.so 30
@Controller
22037 amit.gupta 31
@Transactional(rollbackFor=Throwable.class)
21555 kshitij.so 32
public class LoginController {
33
 
21568 ashik.ali 34
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
35
 
21561 ashik.ali 36
	@Autowired
37
	GoogleLoginUtil googleLoginUtil;
38
 
21574 ashik.ali 39
	@Autowired
40
	MVCResponseSender mvcResponseSender;
41
 
21578 ashik.ali 42
	@Autowired
22069 ashik.ali 43
	CookiesProcessor cookiesProcessor;
21578 ashik.ali 44
 
21555 kshitij.so 45
	@RequestMapping(value = "/login", method = RequestMethod.GET)
22072 ashik.ali 46
	public String loginPage(HttpServletRequest request, Model model) throws Exception{
21578 ashik.ali 47
		try{
22069 ashik.ali 48
			cookiesProcessor.getCookiesObject(request);
21578 ashik.ali 49
			LOGGER.info("Request session is already exist, should be redirect to /dashboard");
21598 ashik.ali 50
			return "redirect:/dashboard";
21615 kshitij.so 51
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
22069 ashik.ali 52
			Resource resource = AppConfig.getResource();
53
			Properties properties = new Properties();
54
			properties.load(resource.getInputStream());
22072 ashik.ali 55
			String googleApiKey = properties.getProperty("google.api.key");
56
			LOGGER.info("Google Api Key {}", googleApiKey);
57
			model.addAttribute("googleApiKey", googleApiKey);
21577 ashik.ali 58
			return "login";
21574 ashik.ali 59
		}
21555 kshitij.so 60
	}
61
 
62
	@RequestMapping(value = "/login", method = RequestMethod.POST)
21615 kshitij.so 63
	public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{
21561 ashik.ali 64
		try{
65
			FofoDetails fofoDetails = googleLoginUtil.getFofoDetail(token);
21577 ashik.ali 66
			Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
22069 ashik.ali 67
			cookieFofoId.setDomain(request.getServerName());
21615 kshitij.so 68
			cookieFofoId.setPath(request.getContextPath());
21578 ashik.ali 69
			Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
22069 ashik.ali 70
			cookieEmailId.setDomain(request.getServerName());
21615 kshitij.so 71
			cookieEmailId.setPath(request.getContextPath());
21577 ashik.ali 72
			response.addCookie(cookieFofoId);
73
			response.addCookie(cookieEmailId);
21574 ashik.ali 74
			LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");
21615 kshitij.so 75
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard"));
21578 ashik.ali 76
			return "response";
21615 kshitij.so 77
//			return mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard");
21561 ashik.ali 78
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
21568 ashik.ali 79
			LOGGER.error("Error : ", profitMandiBusinessException);
21578 ashik.ali 80
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
81
			return "response";
21561 ashik.ali 82
		}
21555 kshitij.so 83
	}
21574 ashik.ali 84
 
22069 ashik.ali 85
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
86
	public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{
87
		try{
88
			cookiesProcessor.removeCookies(request, response);
89
			LOGGER.info("Logout is successfull, should be redirect to /login");
90
			return "login";
91
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
92
			LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
93
			return "login";
94
		}
95
	}
96
 
97
 
98
 
21555 kshitij.so 99
}