Subversion Repositories SmartDukaan

Rev

Rev 21615 | Rev 22037 | 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
 
21577 ashik.ali 3
import javax.servlet.http.Cookie;
21561 ashik.ali 4
import javax.servlet.http.HttpServletRequest;
21577 ashik.ali 5
import javax.servlet.http.HttpServletResponse;
21561 ashik.ali 6
 
21568 ashik.ali 7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
21561 ashik.ali 9
import org.springframework.beans.factory.annotation.Autowired;
21555 kshitij.so 10
import org.springframework.stereotype.Controller;
21987 kshitij.so 11
import org.springframework.transaction.annotation.Transactional;
21615 kshitij.so 12
import org.springframework.ui.Model;
21555 kshitij.so 13
import org.springframework.ui.ModelMap;
14
import org.springframework.web.bind.annotation.ModelAttribute;
15
import org.springframework.web.bind.annotation.RequestMapping;
16
import org.springframework.web.bind.annotation.RequestMethod;
21561 ashik.ali 17
import org.springframework.web.bind.annotation.RequestParam;
21555 kshitij.so 18
 
21561 ashik.ali 19
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
20
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21
import com.spice.profitmandi.web.model.FofoDetails;
21578 ashik.ali 22
import com.spice.profitmandi.web.util.CookiesFetcher;
21561 ashik.ali 23
import com.spice.profitmandi.web.util.GoogleLoginUtil;
21574 ashik.ali 24
import com.spice.profitmandi.web.util.MVCResponseSender;
21561 ashik.ali 25
 
21555 kshitij.so 26
@Controller
21987 kshitij.so 27
@Transactional
21555 kshitij.so 28
public class LoginController {
29
 
21568 ashik.ali 30
	private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);
31
 
21561 ashik.ali 32
	@Autowired
33
	GoogleLoginUtil googleLoginUtil;
34
 
21574 ashik.ali 35
	@Autowired
36
	MVCResponseSender mvcResponseSender;
37
 
21578 ashik.ali 38
	@Autowired
39
	CookiesFetcher cookiesFetcher;
40
 
21555 kshitij.so 41
	@RequestMapping(value = "/login", method = RequestMethod.GET)
21574 ashik.ali 42
	public String loginPage(HttpServletRequest request, @ModelAttribute("model") ModelMap model) throws Exception{
21578 ashik.ali 43
		try{
44
			cookiesFetcher.getCookiesObject(request);
45
			LOGGER.info("Request session is already exist, should be redirect to /dashboard");
21598 ashik.ali 46
			return "redirect:/dashboard";
21615 kshitij.so 47
		}catch(Exception | ProfitMandiBusinessException profitMandiBusinessException){
21577 ashik.ali 48
			return "login";
21574 ashik.ali 49
		}
21555 kshitij.so 50
	}
51
 
52
	@RequestMapping(value = "/login", method = RequestMethod.POST)
21615 kshitij.so 53
	public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, Model model) throws Exception{
21561 ashik.ali 54
		try{
55
			FofoDetails fofoDetails = googleLoginUtil.getFofoDetail(token);
21577 ashik.ali 56
			Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
21615 kshitij.so 57
			cookieFofoId.setDomain("localhost");
58
			cookieFofoId.setPath(request.getContextPath());
21578 ashik.ali 59
			Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
21615 kshitij.so 60
			cookieEmailId.setDomain("localhost");
61
			cookieEmailId.setPath(request.getContextPath());
21577 ashik.ali 62
			response.addCookie(cookieFofoId);
63
			response.addCookie(cookieEmailId);
21574 ashik.ali 64
			LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");
21615 kshitij.so 65
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard"));
21578 ashik.ali 66
			return "response";
21615 kshitij.so 67
//			return mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/profitmandi-fofo/dashboard");
21561 ashik.ali 68
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
21568 ashik.ali 69
			LOGGER.error("Error : ", profitMandiBusinessException);
21578 ashik.ali 70
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
71
			return "response";
21561 ashik.ali 72
		}
21555 kshitij.so 73
	}
21574 ashik.ali 74
 
21555 kshitij.so 75
}