Subversion Repositories SmartDukaan

Rev

Rev 21578 | Rev 21615 | 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");
21598 ashik.ali 43
			return "redirect:/dashboard";
21578 ashik.ali 44
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
21577 ashik.ali 45
			return "login";
21574 ashik.ali 46
		}
21555 kshitij.so 47
	}
48
 
49
	@RequestMapping(value = "/login", method = RequestMethod.POST)
21578 ashik.ali 50
	public String login(HttpServletRequest request, HttpServletResponse response, @RequestParam(name = ProfitMandiConstants.TOKEN) String token, @ModelAttribute ModelMap model) throws Exception{
21561 ashik.ali 51
		try{
52
			FofoDetails fofoDetails = googleLoginUtil.getFofoDetail(token);
21577 ashik.ali 53
			Cookie cookieFofoId = new Cookie(ProfitMandiConstants.FOFO_ID, String.valueOf(fofoDetails.getFofoId()));
21598 ashik.ali 54
			cookieFofoId.setDomain(request.getServerName());
21577 ashik.ali 55
			cookieFofoId.setMaxAge(30);
21578 ashik.ali 56
			Cookie cookieEmailId = new Cookie(ProfitMandiConstants.EMAIL_ID, fofoDetails.getEmailId());
21598 ashik.ali 57
			cookieEmailId.setDomain(request.getServerName());
21577 ashik.ali 58
			cookieEmailId.setMaxAge(30);
59
			response.addCookie(cookieFofoId);
60
			response.addCookie(cookieEmailId);
21574 ashik.ali 61
			LOGGER.info("Requested token email_id is valid, user login to system, shoud be redirect to /dashboard");
21578 ashik.ali 62
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString("RTLR_OK_1002", true, "/dashboard"));
63
			return "response";
21561 ashik.ali 64
		}catch(ProfitMandiBusinessException profitMandiBusinessException){
21568 ashik.ali 65
			LOGGER.error("Error : ", profitMandiBusinessException);
21578 ashik.ali 66
			model.addAttribute("loginResponse", mvcResponseSender.createResponseString(profitMandiBusinessException.getCode(), false, "/error"));
67
			return "response";
21561 ashik.ali 68
		}
21555 kshitij.so 69
	}
21574 ashik.ali 70
 
21555 kshitij.so 71
}