Subversion Repositories SmartDukaan

Rev

Rev 22072 | Rev 22075 | 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());
22073 ashik.ali 55
			model.addAttribute("googleApiKey", properties.getProperty("google.api.key"));
56
			model.addAttribute("appContextPath", properties.getProperty("app.context.path"));
21577 ashik.ali 57
			return "login";
21574 ashik.ali 58
		}
21555 kshitij.so 59
	}
60
 
61
	@RequestMapping(value = "/login", method = RequestMethod.POST)
21615 kshitij.so 62
	public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{
21561 ashik.ali 63
		try{
64
			FofoDetails fofoDetails = googleLoginUtil.getFofoDetail(token);
21577 ashik.ali 65
			Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
22069 ashik.ali 66
			cookieFofoId.setDomain(request.getServerName());
21615 kshitij.so 67
			cookieFofoId.setPath(request.getContextPath());
21578 ashik.ali 68
			Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
22069 ashik.ali 69
			cookieEmailId.setDomain(request.getServerName());
21615 kshitij.so 70
			cookieEmailId.setPath(request.getContextPath());
21577 ashik.ali 71
			response.addCookie(cookieFofoId);
72
			response.addCookie(cookieEmailId);
21574 ashik.ali 73
			LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");
21615 kshitij.so 74
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard"));
21578 ashik.ali 75
			return "response";
21615 kshitij.so 76
//			return mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard");
21561 ashik.ali 77
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
21568 ashik.ali 78
			LOGGER.error("Error : ", profitMandiBusinessException);
21578 ashik.ali 79
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
80
			return "response";
21561 ashik.ali 81
		}
21555 kshitij.so 82
	}
21574 ashik.ali 83
 
22069 ashik.ali 84
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
85
	public String logout(HttpServletRequest request, @ModelAttribute("model") ModelMap model, HttpServletResponse response) throws Exception{
86
		try{
87
			cookiesProcessor.removeCookies(request, response);
88
			LOGGER.info("Logout is successfull, should be redirect to /login");
89
			return "login";
90
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
91
			LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
92
			return "login";
93
		}
94
	}
95
 
96
 
97
 
21555 kshitij.so 98
}