package org.jenkinsci.plugins.cas.protocols;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jasig.cas.client.validation.Cas30ProxyTicketValidator;
import org.jasig.cas.client.validation.Cas30ServiceTicketValidator;
import org.jasig.cas.client.validation.ProxyList;
import org.jasig.cas.client.validation.TicketValidator;
import org.jasig.cas.client.validation.json.Cas30JsonProxyTicketValidator;
import org.jasig.cas.client.validation.json.Cas30JsonServiceTicketValidator;
import org.jenkinsci.plugins.cas.CasProtocol;
import org.kohsuke.stapler.DataBoundConstructor;

import hudson.Extension;
import hudson.model.Descriptor;

/**
 * CAS 3.0 protocol support.
 * 
 * @author Fabien Crespel
 */
public class Cas30Protocol extends CasProtocol {

	public final Boolean proxyEnabled;
	public final Boolean proxyAllowAny;
	public final String proxyAllowList;
	public final Boolean jsonEnabled;

	@Deprecated
	public Cas30Protocol(String authoritiesAttribute, String fullNameAttribute, String emailAttribute, Boolean proxyEnabled, Boolean proxyAllowAny, String proxyAllowList) {
		this(authoritiesAttribute, fullNameAttribute, emailAttribute, proxyEnabled, proxyAllowAny, proxyAllowList, false);
	}

	@Deprecated
	public Cas30Protocol(String authoritiesAttribute, String fullNameAttribute, String emailAttribute, Boolean proxyEnabled, Boolean proxyAllowAny, String proxyAllowList, Boolean jsonEnabled) {
		this(authoritiesAttribute, fullNameAttribute, emailAttribute, null, proxyEnabled, proxyAllowAny, proxyAllowList, jsonEnabled);
	}

	@DataBoundConstructor
	public Cas30Protocol(String authoritiesAttribute, String fullNameAttribute, String emailAttribute, String customValidationParams, Boolean proxyEnabled, Boolean proxyAllowAny, String proxyAllowList, Boolean jsonEnabled) {
		super(authoritiesAttribute, fullNameAttribute, emailAttribute, customValidationParams);
		this.proxyEnabled = proxyEnabled;
		this.proxyAllowAny = proxyAllowAny;
		this.proxyAllowList = proxyAllowList;
		this.jsonEnabled = jsonEnabled;
	}

	@Override
	public TicketValidator createTicketValidator(String casServerUrl) {
		if (this.proxyEnabled != null && this.proxyEnabled) {
			Cas30ProxyTicketValidator ptv;
			if (Boolean.TRUE.equals(this.jsonEnabled)) {
				ptv = new Cas30JsonProxyTicketValidator(casServerUrl);
			} else {
				ptv = new Cas30ProxyTicketValidator(casServerUrl);
			}
			ptv.setCustomParameters(getCustomValidationParamsMap());
			ptv.setAcceptAnyProxy(this.proxyAllowAny);
			String[] proxyChain = StringUtils.split(this.proxyAllowList, '\n');
			if (proxyChain != null && proxyChain.length > 0) {
				List<String[]> proxyList = new ArrayList<String[]>(1);
				proxyList.add(proxyChain);
				ptv.setAllowedProxyChains(new ProxyList(proxyList));
			}
			return ptv;
		} else {
			Cas30ServiceTicketValidator stv;
			if (Boolean.TRUE.equals(this.jsonEnabled)) {
				stv = new Cas30JsonServiceTicketValidator(casServerUrl);
			} else {
				stv = new Cas30ServiceTicketValidator(casServerUrl);
			}
			stv.setCustomParameters(getCustomValidationParamsMap());
			return stv;
		}
	}

	@Extension
	public static final class DescriptorImpl extends Descriptor<CasProtocol> {
		@Override
		public String getDisplayName() {
			return "CAS 3.0";
		}
	}

}

@@ -84,20 +84,68 @@
 va.util.
-List
+HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -2200,16 +2200,125 @@
 rUrl) %7B%0A
+%09%09Map%3CString, String%3E customParams = new HashMap%3C%3E();%0A%09%09customParams.putAll(getCustomValidationParamsMap());%0A
 %09%09if (th
@@ -2365,16 +2365,16 @@
 bled) %7B%0A
-
 %09%09%09Cas30
@@ -2498,32 +2498,72 @@
 (casServerUrl);%0A
+%09%09%09%09customParams.put(%22format%22, %22JSON%22);%0A
 %09%09%09%7D else %7B%0A%09%09%09%09
@@ -2649,38 +2649,20 @@
 ers(
-getC
+c
 ustom
-Validation
 Params
-Map()
 );%0A%09
@@ -3150,24 +3150,64 @@
 ServerUrl);%0A
+%09%09%09%09customParams.put(%22format%22, %22JSON%22);%0A
 %09%09%09%7D else %7B%0A
@@ -3299,38 +3299,20 @@
 ers(
-getC
+c
 ustom
-Validation
 Params
-Map()
 );%0A%09

Fix custom params with JSON format