package ca.corefacility.bioinformatics.irida.ria.security;

import java.io.IOException;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.web.servlet.LocaleResolver;

import ca.corefacility.bioinformatics.irida.model.user.User;
import ca.corefacility.bioinformatics.irida.repositories.user.UserRepository;

/**
 * Handles actions for when a user is successfully logged in.
 */
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
	private static final Logger logger = LoggerFactory.getLogger(LoginSuccessHandler.class);

	private final UserRepository userRepository;
	private final LocaleResolver localeResolver;

	public LoginSuccessHandler(UserRepository userRepository, LocaleResolver localeResolver) {
		this.userRepository = userRepository;
		this.localeResolver = localeResolver;
	}

	@Override
	public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			Authentication authentication) throws IOException, ServletException {

		HttpSession session = httpServletRequest.getSession();
		User user = (User) authentication.getPrincipal();

		//set the user's selected locale
		try {

			/*
			Get the context path, used by the client JS for requests
			 */
			String contextPath = httpServletRequest.getContextPath();
			String cp = contextPath;
			if(!cp.endsWith("/")) {
				// If the context path is set to anything other the "/" it will look like "/foobar"
				// the UI needs the final slash.
				cp = cp + "/";
			}
			Cookie pathCookie = new Cookie("cp", cp);
			pathCookie.setPath(contextPath);
			httpServletResponse.addCookie(pathCookie);


			/*
			Get and store the locale
			 */
			Locale locale = Locale.forLanguageTag(user.getLocale());
			localeResolver.setLocale(httpServletRequest, httpServletResponse, locale);
			Cookie localeCookie = new Cookie("locale", locale.toLanguageTag());
			localeCookie.setPath(contextPath);
			httpServletResponse.addCookie(localeCookie);


			/*
			Add the session expiration time.
			 */
			Cookie timeCookie = new Cookie("expiration_time", String.valueOf(session.getMaxInactiveInterval()));
			timeCookie.setPath(contextPath);
			httpServletResponse.addCookie(timeCookie);

			/**
			 * Add the user
			 */
			session.setAttribute("user", user);
		} catch (NullPointerException ex) {
			logger.warn("Locale cannot be resolved for " + user.getLocale() + ".  Setting system default locale.");
			localeResolver.setLocale(httpServletRequest, httpServletResponse, Locale.getDefault());
		}

		userRepository.updateLogin(user, new Date());
		super.onAuthenticationSuccess(httpServletRequest, httpServletResponse, authentication);
	}
}
@@ -173,42 +173,8 @@
 on;%0A
-import javax.servlet.http.Cookie;%0A
 impo
@@ -1010,16 +1010,17 @@
 sitory;%0A
+%0A
 %09private
@@ -1434,16 +1434,106 @@
 eption %7B
+%0A%09%09super.onAuthenticationSuccess(httpServletRequest, httpServletResponse, authentication);
 %0A%0A%09%09Http
@@ -1683,1119 +1683,143 @@
 y %7B%0A
-%0A%09%09%09/*%0A%09%09%09Get the context path, used by the client JS for requests%0A%09%09%09 */%0A%09%09%09String contextPath = httpServletRequest.getContextPath();%0A%09%09%09String cp = contextPath;%0A%09%09%09if(!cp.endsWith(%22/%22)) %7B%0A%09%09%09%09// If the context path is set to anything other the %22/%22 it will look like %22/foobar%22%0A%09%09%09%09// the UI needs the final slash.%0A%09%09%09%09cp = cp + %22/%22;%0A%09%09%09%7D%0A%09%09%09Cookie pathCookie = new Cookie(%22cp%22, cp);%0A%09%09%09pathCookie.setPath(contextPath);%0A%09%09%09httpServletResponse.addCookie(pathCookie);%0A%0A%0A%09%09%09/*%0A%09%09%09Get and store the locale%0A%09%09%09 */%0A%09%09%09Locale locale = Locale.forLanguageTag(user.getLocale());%0A%09%09%09localeResolver.setLocale(httpServletRequest, httpServletResponse, locale);%0A%09%09%09Cookie localeCookie = new Cookie(%22locale%22, locale.toLanguageTag());%0A%09%09%09localeCookie.setPath(contextPath);%0A%09%09%09httpServletResponse.addCookie(localeCookie);%0A%0A%0A%09%09%09/*%0A%09%09%09Add the session expiration time.%0A%09%09%09 */%0A%09%09%09Cookie timeCookie = new Cookie(%22expiration_time%22, String.valueOf(session.getMaxInactiveInterval()));%0A%09%09%09timeCookie.setPath(contextPath);%0A%09%09%09httpServletResponse.addCookie(timeCookie);%0A%0A%09%09%09/**%0A%09%09%09 * Add the user%0A%09%09%09 */%0A%09%09%09session.setAttribute(%22user%22, user
+%09%09%09Locale locale = Locale.forLanguageTag(user.getLocale());%0A%09%09%09localeResolver.setLocale(httpServletRequest, httpServletResponse, locale
 );%0A%09
@@ -2064,141 +2064,125 @@
 %0A%0A%09%09
-userRepository.updateLogin(
+// Add the user into the session%0A%09%09session.setAttribute(%22
 user
+%22
 , 
-new Date()
+user
 );%0A
+%0A
 %09%09
-super.onAuthenticationSuccess(httpServletRequest, httpServletRespon
+userRepository.updateLogin(u
 se
+r
 , 
-authentication
+new Date()
 );%0A%09

Reset to original, this should not have been modified here.