Subversion Repositories SmartDukaan

Rev

Rev 22073 | Rev 22079 | 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{
22075 ashik.ali 64
			Resource resource = AppConfig.getResource();
65
			Properties properties = new Properties();
66
			properties.load(resource.getInputStream());
67
 
21561 ashik.ali 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());
22075 ashik.ali 71
			cookieFofoId.setPath(properties.getProperty("app.context.path"));
21578 ashik.ali 72
			Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
22069 ashik.ali 73
			cookieEmailId.setDomain(request.getServerName());
22075 ashik.ali 74
			cookieEmailId.setPath(properties.getProperty("app.context.path"));
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");
21615 kshitij.so 78
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/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");
93
			return "login";
94
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
95
			LOGGER.info("Error occured while removing requested cookies, should be redirect to /login");
96
			return "login";
97
		}
98
	}
99
 
100
 
101
 
21555 kshitij.so 102
}