/**
 * Licensed to Esup-Portail under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Esup-Portail licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License.  You may obtain a
 * copy of the License at the following location:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
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.AuthenticationBuilder;
import org.jasig.cas.authentication.AuthenticationMetaDataPopulator;
import org.jasig.cas.authentication.Credential;
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 void populateAttributes(AuthenticationBuilder builder, Credential credential) {
		if(enabled) {
			String id = traceMeUniqueIdGenerator.getNewTicketId("TRACE");
			String principalId = builder.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);
		}
	}

	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;
	}
    
}

@@ -1186,16 +1186,81 @@
 ntial;%0D%0A
+import org.jasig.cas.authentication.UsernamePasswordCredential;%0D%0A
 import o
@@ -1689,21 +1689,16 @@
 plements
-%0D%0A   
  Authent
@@ -2061,16 +2061,22 @@
 ributes(
+final 
 Authenti
@@ -2097,16 +2097,22 @@
 builder,
+ final
  Credent
@@ -2147,16 +2147,77 @@
 led) %7B%0D%0A
+%09%09%09if (credential instanceof UsernamePasswordCredential) %7B%0D%0A%09
 %09%09%09Strin
@@ -2277,16 +2277,17 @@
 %22);%0D%0A%09%09%09
+%09
 String p
@@ -2335,18 +2335,20 @@
 ();%0D%0A%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09Exter
@@ -2421,16 +2421,17 @@
 ext();%0D%0A
+%09
 %09%09%09Servl
@@ -2519,19 +2519,20 @@
 xt;%0D%0A%09%09%09
- 
+%09
 %0D%0A
+%09
 %09%09%09HttpS
@@ -2620,16 +2620,17 @@
 est();%0D%0A
+%09
 %09%09%09HttpS
@@ -2725,18 +2725,20 @@
 ();%0D%0A%09%09%09
+%09
 %0D%0A
+%09
 %09%09%09this.
@@ -2795,18 +2795,23 @@
 , id);%0D%0A
+%09%09%09%09
 %0D%0A
+%09
 %09%09%09log.i
@@ -2840,16 +2840,199 @@
 alId);%0D%0A
+%09%09%09%7D else %7B%0D%0A%09%09%09%09log.debug(%22TraceMeMetaDataPopulator.populateAttributes, TraceMe cookie not supported for class %22 + credential!= null?credential.getClass().toString():%22null%22);%0D%0A%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%7D%0D
@@ -3444,13 +3444,7 @@
 %09%7D%0D%0A
-    %0D%0A
 %7D%0D%0A

Fix errors and conflicts between TraceMe MetaDataPopulator and proxy tickets of clearpass tickets