Subversion Repositories SmartDukaan

Rev

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