Subversion Repositories SmartDukaan

Rev

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