// Path: src/main/java/gluu/scim2/client/exception/ScimInitializationException.java
// public class ScimInitializationException extends RuntimeException {
// 
//     private static final long serialVersionUID = -6376075805135656133L;
// 
//     public ScimInitializationException(String message) {
//         super(message);
//     }
// 
//     public ScimInitializationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
// }


import java.util.List;
import javax.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gluu.oxauth.model.common.AuthenticationMethod;
import org.gluu.oxauth.model.common.GrantType;
import org.gluu.oxauth.model.crypto.OxAuthCryptoProvider;
import org.gluu.oxauth.model.token.ClientAssertionType;
import org.gluu.oxauth.model.uma.UmaMetadata;
import org.gluu.oxauth.model.uma.UmaTokenResponse;
import org.gluu.util.StringHelper;
import org.gluu.oxauth.client.TokenRequest;
import org.gluu.oxauth.client.uma.UmaClientFactory;
import org.gluu.oxauth.client.uma.UmaTokenService;
import gluu.scim2.client.exception.ScimInitializationException;
    /**
     * Recomputes a new RPT according to UMA workflow if the response passed as parameter has status code 401 (unauthorized).
     * @param response A Response object corresponding to the request obtained in the previous call to a service method
     * @return If the parameter passed has a status code different to 401, it returns false. Otherwise it returns the success
     * of the attempt made to get a new RPT
     */
    @Override
    boolean authorize(Response response) {

        boolean value = false;

        if (response.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()) {

            try {
                String permissionTicketResponse = response.getHeaderString("WWW-Authenticate");
                String permissionTicket = null;
                String asUri = null;

                String[] headerKeyValues = StringHelper.split(permissionTicketResponse, ",");
                for (String headerKeyValue : headerKeyValues) {
                    if (headerKeyValue.startsWith("ticket=")) {
                        permissionTicket = headerKeyValue.substring(7);
                    }
                    if (headerKeyValue.startsWith("as_uri=")) {
                        asUri = headerKeyValue.substring(7);
                    }
                }
                value= StringHelper.isNotEmpty(asUri) && StringHelper.isNotEmpty(permissionTicket)
                        && obtainAuthorizedRpt(asUri, permissionTicket);
            } catch (Exception e) {
// Path: src/main/java/gluu/scim2/client/exception/ScimInitializationException.java
// public class ScimInitializationException extends RuntimeException {
// 
//     private static final long serialVersionUID = -6376075805135656133L;
// 
//     public ScimInitializationException(String message) {
//         super(message);
//     }
// 
//     public ScimInitializationException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
// }


// Path: src/main/java/gluu/scim2/client/UmaScimClient.java
import java.util.List;
import javax.ws.rs.core.Response;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.gluu.oxauth.model.common.AuthenticationMethod;
import org.gluu.oxauth.model.common.GrantType;
import org.gluu.oxauth.model.crypto.OxAuthCryptoProvider;
import org.gluu.oxauth.model.token.ClientAssertionType;
import org.gluu.oxauth.model.uma.UmaMetadata;
import org.gluu.oxauth.model.uma.UmaTokenResponse;
import org.gluu.util.StringHelper;
import org.gluu.oxauth.client.TokenRequest;
import org.gluu.oxauth.client.uma.UmaClientFactory;
import org.gluu.oxauth.client.uma.UmaTokenService;
import gluu.scim2.client.exception.ScimInitializationException;

    /**
     * Recomputes a new RPT according to UMA workflow if the response passed as parameter has status code 401 (unauthorized).
     * @param response A Response object corresponding to the request obtained in the previous call to a service method
     * @return If the parameter passed has a status code different to 401, it returns false. Otherwise it returns the success
     * of the attempt made to get a new RPT
     */
    @Override
    boolean authorize(Response response) {

        boolean value = false;

        if (response.getStatus() == Response.Status.UNAUTHORIZED.getStatusCode()) {

            try {
                String permissionTicketResponse = response.getHeaderString("WWW-Authenticate");
                String permissionTicket = null;
                String asUri = null;

                String[] headerKeyValues = StringHelper.split(permissionTicketResponse, ",");
                for (String headerKeyValue : headerKeyValues) {
                    if (headerKeyValue.startsWith("ticket=")) {
                        permissionTicket = headerKeyValue.substring(7);
                    }
                    if (headerKeyValue.startsWith("as_uri=")) {
                        asUri = headerKeyValue.substring(7);
                    }
                }
                value= StringHelper.isNotEmpty(asUri) && StringHelper.isNotEmpty(permissionTicket)
                        && obtainAuthorizedRpt(asUri, permissionTicket);
            } catch (Exception e) {
                throw new ScimInitializationException(e.getMessage(), e);