Subversion Repositories SmartDukaan

Rev

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