Subversion Repositories SmartDukaan

Rev

Rev 22037 | Rev 22072 | 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.io.InputStream;
4
import java.util.Properties;
5
 
21577 ashik.ali 6
import javax.servlet.http.Cookie;
21561 ashik.ali 7
import javax.servlet.http.HttpServletRequest;
21577 ashik.ali 8
import javax.servlet.http.HttpServletResponse;
21561 ashik.ali 9
 
21568 ashik.ali 10
import org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
21561 ashik.ali 12
import org.springframework.beans.factory.annotation.Autowired;
22069 ashik.ali 13
import org.springframework.context.annotation.PropertySource;
14
import org.springframework.core.io.Resource;
21555 kshitij.so 15
import org.springframework.stereotype.Controller;
21987 kshitij.so 16
import org.springframework.transaction.annotation.Transactional;
21615 kshitij.so 17
import org.springframework.ui.Model;
21555 kshitij.so 18
import org.springframework.ui.ModelMap;
19
import org.springframework.web.bind.annotation.ModelAttribute;
20
import org.springframework.web.bind.annotation.RequestMapping;
21
import org.springframework.web.bind.annotation.RequestMethod;
21561 ashik.ali 22
import org.springframework.web.bind.annotation.RequestParam;
21555 kshitij.so 23
 
21561 ashik.ali 24
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
25
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22069 ashik.ali 26
import com.spice.profitmandi.web.config.AppConfig;
21561 ashik.ali 27
import com.spice.profitmandi.web.model.FofoDetails;
22069 ashik.ali 28
import com.spice.profitmandi.web.util.CookiesProcessor;
21561 ashik.ali 29
import com.spice.profitmandi.web.util.GoogleLoginUtil;
21574 ashik.ali 30
import com.spice.profitmandi.web.util.MVCResponseSender;
21561 ashik.ali 31
 
21555 kshitij.so 32
@Controller
22037 amit.gupta 33
@Transactional(rollbackFor=Throwable.class)
21555 kshitij.so 34
public class LoginController {
35
 
21568 ashik.ali 36
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
37
 
21561 ashik.ali 38
	@Autowired
39
	GoogleLoginUtil googleLoginUtil;
40
 
21574 ashik.ali 41
	@Autowired
42
	MVCResponseSender mvcResponseSender;
43
 
21578 ashik.ali 44
	@Autowired
22069 ashik.ali 45
	CookiesProcessor cookiesProcessor;
21578 ashik.ali 46
 
21555 kshitij.so 47
	@RequestMapping(value = "/login", method = RequestMethod.GET)
21574 ashik.ali 48
	public String loginPage(HttpServletRequest request, @ModelAttribute("model") ModelMap model) throws Exception{
21578 ashik.ali 49
		try{
22069 ashik.ali 50
			cookiesProcessor.getCookiesObject(request);
21578 ashik.ali 51
			LOGGER.info("Request session is already exist, should be redirect to /dashboard");
21598 ashik.ali 52
			return "redirect:/dashboard";
21615 kshitij.so 53
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
22069 ashik.ali 54
			Resource resource = AppConfig.getResource();
55
			Properties properties = new Properties();
56
			properties.load(resource.getInputStream());
57
			model.addAttribute("googleApiKey", properties.getProperty("google.api.key"));
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
}