// Path: src/main/java/org/keycloak/protocol/cas/mappers/CASAttributeMapper.java
// public interface CASAttributeMapper {
//     void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession,
//                       KeycloakSession session, ClientSessionContext clientSessionCtx);
// }
// 
// Path: src/main/java/org/keycloak/protocol/cas/representations/CASServiceResponse.java
// @XmlRootElement(name = "serviceResponse")
// public class CASServiceResponse {
//     private CASServiceResponseAuthenticationFailure authenticationFailure;
//     private CASServiceResponseAuthenticationSuccess authenticationSuccess;
// 
//     public CASServiceResponseAuthenticationFailure getAuthenticationFailure() {
//         return this.authenticationFailure;
//     }
// 
//     public void setAuthenticationFailure(final CASServiceResponseAuthenticationFailure authenticationFailure) {
//         this.authenticationFailure = authenticationFailure;
//     }
// 
//     public CASServiceResponseAuthenticationSuccess getAuthenticationSuccess() {
//         return this.authenticationSuccess;
//     }
// 
//     public void setAuthenticationSuccess(final CASServiceResponseAuthenticationSuccess authenticationSuccess) {
//         this.authenticationSuccess = authenticationSuccess;
//     }
// }
// 
// Path: src/main/java/org/keycloak/protocol/cas/utils/CASValidationException.java
// public class CASValidationException extends WebApplicationException {
//     private final CASErrorCode error;
//     private final String errorDescription;
//     private final Response.Status status;
// 
//     public CASValidationException(CASErrorCode error, String errorDescription, Response.Status status) {
//         this.error = error;
//         this.errorDescription = errorDescription;
//         this.status = status;
//     }
// 
//     public CASErrorCode getError() {
//         return error;
//     }
// 
//     public String getErrorDescription() {
//         return errorDescription;
//     }
// 
//     public Response.Status getStatus() {
//         return status;
//     }
// }
// 
// Path: src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
// public class ContentTypeHelper {
//     private final HttpRequest request;
//     private final Request restRequest;
//     private final UriInfo uriInfo;
// 
//     public ContentTypeHelper(HttpRequest request, Request restRequest, UriInfo uriInfo) {
//         this.request = request;
//         this.restRequest = restRequest;
//         this.uriInfo = uriInfo;
//     }
// 
//     public MediaType selectResponseType() {
//         String format = uriInfo.getQueryParameters().getFirst(CASLoginProtocol.FORMAT_PARAM);
//         if (format != null && !format.isEmpty()) {
//             //if parameter is set, it overrides all header values (see spec section 2.5.1)
//             request.getMutableHeaders().putSingle(HttpHeaders.ACCEPT, "application/" + format.toLowerCase());
//         }
//         try {
//             Variant variant = restRequest.selectVariant(Variant.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build());
//             return variant == null ? MediaType.APPLICATION_XML_TYPE : variant.getMediaType();
//         } catch (BadRequestException e) {
//             //the default Accept header set by java.net.HttpURLConnection is invalid (cf. RESTEASY-960)
//             return MediaType.APPLICATION_XML_TYPE;
//         }
//     }
// }
// 
// Path: src/main/java/org/keycloak/protocol/cas/utils/ServiceResponseHelper.java
// public final class ServiceResponseHelper {
//     private ServiceResponseHelper() {
//     }
// 
//     public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes) {
//         return createSuccess(username, attributes, null, null);
//     }
// 
//     public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes,
//                                                    String proxyGrantingTicket, List<String> proxies) {
//         CASServiceResponse response = new CASServiceResponse();
//         CASServiceResponseAuthenticationSuccess success = new CASServiceResponseAuthenticationSuccess();
//         success.setUser(username);
//         success.setProxies(proxies);
//         success.setProxyGrantingTicket(proxyGrantingTicket);
//         success.setAttributes(attributes);
// 
//         response.setAuthenticationSuccess(success);
// 
//         return response;
//     }
// 
//     public static CASServiceResponse createFailure(CASErrorCode errorCode, String errorDescription) {
//         CASServiceResponse response = new CASServiceResponse();
//         CASServiceResponseAuthenticationFailure failure = new CASServiceResponseAuthenticationFailure();
//         failure.setCode(errorCode == null ? CASErrorCode.INTERNAL_ERROR.name() : errorCode.name());
//         failure.setDescription(errorDescription);
//         response.setAuthenticationFailure(failure);
// 
//         return response;
//     }
// 
//     public static Response createResponse(Response.Status status, MediaType mediaType, CASServiceResponse serviceResponse) {
//         Response.ResponseBuilder builder = Response.status(status)
//                 .header(HttpHeaders.CONTENT_TYPE, mediaType.withCharset("utf-8"));
//         if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
//             return builder.entity(ServiceResponseMarshaller.marshalJson(serviceResponse)).build();
//         } else {
//             return builder.entity(ServiceResponseMarshaller.marshalXml(serviceResponse)).build();
//         }
//     }
// }


import org.keycloak.events.EventBuilder;
import org.keycloak.models.*;
import org.keycloak.protocol.ProtocolMapper;
import org.keycloak.protocol.cas.mappers.CASAttributeMapper;
import org.keycloak.protocol.cas.representations.CASServiceResponse;
import org.keycloak.protocol.cas.utils.CASValidationException;
import org.keycloak.protocol.cas.utils.ContentTypeHelper;
import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
import org.keycloak.services.managers.ClientSessionCode;
import org.keycloak.services.util.DefaultClientSessionContext;
import javax.ws.rs.core.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
package org.keycloak.protocol.cas.endpoints;



public class ServiceValidateEndpoint extends ValidateEndpoint {
    @Context
    private Request restRequest;

// Path: src/main/java/org/keycloak/protocol/cas/mappers/CASAttributeMapper.java
// public interface CASAttributeMapper {
//     void setAttribute(Map<String, Object> attributes, ProtocolMapperModel mappingModel, UserSessionModel userSession,
//                       KeycloakSession session, ClientSessionContext clientSessionCtx);
// }
// 
// Path: src/main/java/org/keycloak/protocol/cas/representations/CASServiceResponse.java
// @XmlRootElement(name = "serviceResponse")
// public class CASServiceResponse {
//     private CASServiceResponseAuthenticationFailure authenticationFailure;
//     private CASServiceResponseAuthenticationSuccess authenticationSuccess;
// 
//     public CASServiceResponseAuthenticationFailure getAuthenticationFailure() {
//         return this.authenticationFailure;
//     }
// 
//     public void setAuthenticationFailure(final CASServiceResponseAuthenticationFailure authenticationFailure) {
//         this.authenticationFailure = authenticationFailure;
//     }
// 
//     public CASServiceResponseAuthenticationSuccess getAuthenticationSuccess() {
//         return this.authenticationSuccess;
//     }
// 
//     public void setAuthenticationSuccess(final CASServiceResponseAuthenticationSuccess authenticationSuccess) {
//         this.authenticationSuccess = authenticationSuccess;
//     }
// }
// 
// Path: src/main/java/org/keycloak/protocol/cas/utils/CASValidationException.java
// public class CASValidationException extends WebApplicationException {
//     private final CASErrorCode error;
//     private final String errorDescription;
//     private final Response.Status status;
// 
//     public CASValidationException(CASErrorCode error, String errorDescription, Response.Status status) {
//         this.error = error;
//         this.errorDescription = errorDescription;
//         this.status = status;
//     }
// 
//     public CASErrorCode getError() {
//         return error;
//     }
// 
//     public String getErrorDescription() {
//         return errorDescription;
//     }
// 
//     public Response.Status getStatus() {
//         return status;
//     }
// }
// 
// Path: src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
// public class ContentTypeHelper {
//     private final HttpRequest request;
//     private final Request restRequest;
//     private final UriInfo uriInfo;
// 
//     public ContentTypeHelper(HttpRequest request, Request restRequest, UriInfo uriInfo) {
//         this.request = request;
//         this.restRequest = restRequest;
//         this.uriInfo = uriInfo;
//     }
// 
//     public MediaType selectResponseType() {
//         String format = uriInfo.getQueryParameters().getFirst(CASLoginProtocol.FORMAT_PARAM);
//         if (format != null && !format.isEmpty()) {
//             //if parameter is set, it overrides all header values (see spec section 2.5.1)
//             request.getMutableHeaders().putSingle(HttpHeaders.ACCEPT, "application/" + format.toLowerCase());
//         }
//         try {
//             Variant variant = restRequest.selectVariant(Variant.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build());
//             return variant == null ? MediaType.APPLICATION_XML_TYPE : variant.getMediaType();
//         } catch (BadRequestException e) {
//             //the default Accept header set by java.net.HttpURLConnection is invalid (cf. RESTEASY-960)
//             return MediaType.APPLICATION_XML_TYPE;
//         }
//     }
// }
// 
// Path: src/main/java/org/keycloak/protocol/cas/utils/ServiceResponseHelper.java
// public final class ServiceResponseHelper {
//     private ServiceResponseHelper() {
//     }
// 
//     public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes) {
//         return createSuccess(username, attributes, null, null);
//     }
// 
//     public static CASServiceResponse createSuccess(String username, Map<String, Object> attributes,
//                                                    String proxyGrantingTicket, List<String> proxies) {
//         CASServiceResponse response = new CASServiceResponse();
//         CASServiceResponseAuthenticationSuccess success = new CASServiceResponseAuthenticationSuccess();
//         success.setUser(username);
//         success.setProxies(proxies);
//         success.setProxyGrantingTicket(proxyGrantingTicket);
//         success.setAttributes(attributes);
// 
//         response.setAuthenticationSuccess(success);
// 
//         return response;
//     }
// 
//     public static CASServiceResponse createFailure(CASErrorCode errorCode, String errorDescription) {
//         CASServiceResponse response = new CASServiceResponse();
//         CASServiceResponseAuthenticationFailure failure = new CASServiceResponseAuthenticationFailure();
//         failure.setCode(errorCode == null ? CASErrorCode.INTERNAL_ERROR.name() : errorCode.name());
//         failure.setDescription(errorDescription);
//         response.setAuthenticationFailure(failure);
// 
//         return response;
//     }
// 
//     public static Response createResponse(Response.Status status, MediaType mediaType, CASServiceResponse serviceResponse) {
//         Response.ResponseBuilder builder = Response.status(status)
//                 .header(HttpHeaders.CONTENT_TYPE, mediaType.withCharset("utf-8"));
//         if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
//             return builder.entity(ServiceResponseMarshaller.marshalJson(serviceResponse)).build();
//         } else {
//             return builder.entity(ServiceResponseMarshaller.marshalXml(serviceResponse)).build();
//         }
//     }
// }


// Path: src/main/java/org/keycloak/protocol/cas/endpoints/ServiceValidateEndpoint.java
import org.keycloak.events.EventBuilder;
import org.keycloak.models.*;
import org.keycloak.protocol.ProtocolMapper;
import org.keycloak.protocol.cas.mappers.CASAttributeMapper;
import org.keycloak.protocol.cas.representations.CASServiceResponse;
import org.keycloak.protocol.cas.utils.CASValidationException;
import org.keycloak.protocol.cas.utils.ContentTypeHelper;
import org.keycloak.protocol.cas.utils.ServiceResponseHelper;
import org.keycloak.services.managers.ClientSessionCode;
import org.keycloak.services.util.DefaultClientSessionContext;
import javax.ws.rs.core.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

package org.keycloak.protocol.cas.endpoints;



public class ServiceValidateEndpoint extends ValidateEndpoint {
    @Context
    private Request restRequest;

    public ServiceValidateEndpoint(RealmModel realm, EventBuilder event) {