package org.esupportail.cas.services;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import org.jasig.cas.authentication.Authentication;
import org.jasig.cas.authentication.AuthenticationMetaDataPopulator;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.util.UniqueTicketIdGenerator;
import org.jasig.cas.web.support.CookieRetrievingCookieGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.webflow.context.ExternalContext;
import org.springframework.webflow.context.ExternalContextHolder;
import org.springframework.webflow.context.servlet.ServletExternalContext;

public final class TraceMeMetaDataPopulator implements
    AuthenticationMetaDataPopulator {

	protected final static Logger log = LoggerFactory.getLogger(TraceMeMetaDataPopulator.class);
	
	@NotNull
	private CookieRetrievingCookieGenerator traceMeCookieGenerator;
	
	@NotNull
	private UniqueTicketIdGenerator traceMeUniqueIdGenerator;
	
	private boolean enabled = false;
	
	@Override
	public Authentication populateAttributes(Authentication authentication, Credentials credentials) {
		if(enabled) {
			String id = traceMeUniqueIdGenerator.getNewTicketId("TRACE");
			String principalId = authentication.getPrincipal().getId();
			
			ExternalContext externalContext = ExternalContextHolder.getExternalContext();
			ServletExternalContext servletExternalContext = (ServletExternalContext) externalContext;
			 
			HttpServletRequest request = (HttpServletRequest) servletExternalContext.getNativeRequest();
			HttpServletResponse response = (HttpServletResponse) servletExternalContext.getNativeResponse();
			
			this.traceMeCookieGenerator.addCookie(request, response, id);

			log.info(id + ":" + principalId);
		}
		return authentication;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public void setTraceMeCookieGenerator(final CookieRetrievingCookieGenerator traceMeCookieGenerator) {
		this.traceMeCookieGenerator= traceMeCookieGenerator;
	}

	public void setTraceMeUniqueIdGenerator(final UniqueTicketIdGenerator uniqueTicketIdGenerator) {
		this.traceMeUniqueIdGenerator = uniqueTicketIdGenerator;
	}
    
}

@@ -227,19 +227,26 @@
 tication
+Builder
 ;%0D%0A
-
 import o
@@ -347,18 +347,8 @@
 ion.
-principal.
 Cred
@@ -353,17 +353,16 @@
 edential
-s
 ;%0D%0Aimpor
@@ -1147,30 +1147,20 @@
 %09public 
-Authentication
+void
  populat
@@ -1185,31 +1185,31 @@
 tication
- authentication
+Builder builder
 , Creden
@@ -1212,17 +1212,16 @@
 edential
-s
  credent
@@ -1223,17 +1223,16 @@
 edential
-s
 ) %7B%0D%0A%09%09i
@@ -1335,30 +1335,23 @@
 palId = 
-authentication
+builder
 .getPrin
@@ -1868,39 +1868,13 @@
 );%0D%0A
+
 %09%09%7D%0D%0A
-%09%09return authentication;%0D%0A
 %09%7D%0D%0A

Fix TraceMetaDataPopulator