Subversion Repositories SmartDukaan

Rev

Rev 22086 | Rev 22088 | 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);
22086 amit.gupta 60
			model.addAttribute("appContextPath", request.getContextPath());
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{
68
			FofoDetails fofoDetails = googleLoginUtil.getFofoDetail(token);
21577 ashik.ali 69
			Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
22069 ashik.ali 70
			cookieFofoId.setDomain(request.getServerName());
22087 amit.gupta 71
			cookieFofoId.setPath(request.getContextPath());
21578 ashik.ali 72
			Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
22069 ashik.ali 73
			cookieEmailId.setDomain(request.getServerName());
22087 amit.gupta 74
			cookieEmailId.setPath(request.getContextPath());
21577 ashik.ali 75
			response.addCookie(cookieFofoId);
76
			response.addCookie(cookieEmailId);
21574 ashik.ali 77
			LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");
22085 amit.gupta 78
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, request.getContextPath() + "/dashboard"));
21578 ashik.ali 79
			return "response";
21615 kshitij.so 80
//			return mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard");
21561 ashik.ali 81
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
21568 ashik.ali 82
			LOGGER.error("Error : ", profitMandiBusinessException);
21578 ashik.ali 83
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
84
			return "response";
21561 ashik.ali 85
		}
21555 kshitij.so 86
	}
21574 ashik.ali 87
 
22069 ashik.ali 88
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
89
	public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{
90
		try{
91
			cookiesProcessor.removeCookies(request, response);
92
			LOGGER.info("Logout is successfull, should be redirect to /login");
22085 amit.gupta 93
			return "redirect:/login";
22069 ashik.ali 94
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
95
			LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
22085 amit.gupta 96
			return "redirect:/login";
22069 ashik.ali 97
		}
98
	}
99
 
100
 
101
 
21555 kshitij.so 102
}