Search in sources :

Example 16 with CharonException

use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.

the class RoleResourceManager method doPatchRole.

private Role doPatchRole(Role oldRole, SCIMResourceTypeSchema roleSchema, String patchRequest) throws CharonException, BadRequestException, NotImplementedException, InternalErrorException {
    // Make a copy of the original group.
    Role originalRole = (Role) CopyUtil.deepCopy(oldRole);
    Role copyOfOldRole = (Role) CopyUtil.deepCopy(oldRole);
    Role patchedRole = null;
    List<PatchOperation> opList = getDecoder().decodeRequest(patchRequest);
    for (PatchOperation operation : opList) {
        switch(operation.getOperation()) {
            case SCIMConstants.OperationalConstants.ADD:
                if (patchedRole == null) {
                    patchedRole = (Role) PatchOperationUtil.doPatchAdd(operation, getDecoder(), oldRole, copyOfOldRole, roleSchema);
                } else {
                    patchedRole = (Role) PatchOperationUtil.doPatchAdd(operation, getDecoder(), patchedRole, copyOfOldRole, roleSchema);
                }
                copyOfOldRole = (Role) CopyUtil.deepCopy(patchedRole);
                break;
            case SCIMConstants.OperationalConstants.REMOVE:
                if (patchedRole == null) {
                    patchedRole = (Role) PatchOperationUtil.doPatchRemove(operation, oldRole, copyOfOldRole, roleSchema);
                } else {
                    patchedRole = (Role) PatchOperationUtil.doPatchRemove(operation, patchedRole, copyOfOldRole, roleSchema);
                }
                copyOfOldRole = (Role) CopyUtil.deepCopy(patchedRole);
                break;
            case SCIMConstants.OperationalConstants.REPLACE:
                if (patchedRole == null) {
                    patchedRole = (Role) PatchOperationUtil.doPatchReplace(operation, getDecoder(), oldRole, copyOfOldRole, roleSchema);
                } else {
                    patchedRole = (Role) PatchOperationUtil.doPatchReplace(operation, getDecoder(), patchedRole, copyOfOldRole, roleSchema);
                }
                copyOfOldRole = (Role) CopyUtil.deepCopy(patchedRole);
                break;
            default:
                throw new BadRequestException("Unknown operation.", ResponseCodeConstants.INVALID_SYNTAX);
        }
    }
    return (Role) ServerSideValidator.validateUpdatedSCIMObject(originalRole, patchedRole, roleSchema);
}
Also used : Role(org.wso2.charon3.core.objects.Role) PatchOperation(org.wso2.charon3.core.utils.codeutils.PatchOperation) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Example 17 with CharonException

use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.

the class RoleResourceManager method updateWithPUTRole.

@Override
public SCIMResponse updateWithPUTRole(String id, String putRequest, RoleManager roleManager) {
    try {
        if (roleManager == null) {
            String error = "Provided role manager is null.";
            throw new InternalErrorException(error);
        }
        JSONEncoder encoder = getEncoder();
        JSONDecoder decoder = getDecoder();
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getRoleResourceSchema();
        Map<String, Boolean> requestAttributes = ResourceManagerUtil.getAllAttributeURIs(schema);
        Role role = decoder.decodeResource(putRequest, schema, new Role());
        Role updatedRole;
        // Retrieve the old object.
        Role oldRole = roleManager.getRole(id, requestAttributes);
        if (oldRole != null) {
            Role newRole = (Role) ServerSideValidator.validateUpdatedSCIMObject(oldRole, role, schema);
            updatedRole = roleManager.updateRole(oldRole, newRole);
        } else {
            String error = "No role exists with the given id: " + id;
            throw new NotFoundException(error);
        }
        return getScimResponse(encoder, updatedRole);
    } catch (NotFoundException | BadRequestException | CharonException | ConflictException | InternalErrorException | NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : ConflictException(org.wso2.charon3.core.exceptions.ConflictException) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) Role(org.wso2.charon3.core.objects.Role) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException)

Example 18 with CharonException

use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.

the class RoleResourceManager method getScimResponse.

private SCIMResponse getScimResponse(JSONEncoder encoder, Role updatedRole) throws CharonException, NotFoundException, InternalErrorException {
    String encodedRole;
    Map<String, String> httpHeaders = new HashMap<>();
    if (updatedRole != null) {
        // Create a deep copy of the user object since we are going to change it.
        Role copiedRole = (Role) CopyUtil.deepCopy(updatedRole);
        encodedRole = encoder.encodeSCIMObject(copiedRole);
        // Add location header
        httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.ROLE_ENDPOINT) + "/" + updatedRole.getId());
        httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
    } else {
        String error = "Updated Role resource is null.";
        throw new InternalErrorException(error);
    }
    return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedRole, httpHeaders);
}
Also used : Role(org.wso2.charon3.core.objects.Role) HashMap(java.util.HashMap) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 19 with CharonException

use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.

the class ServiceProviderConfigResourceManager method getServiceProviderConfig.

private SCIMResponse getServiceProviderConfig() {
    JSONEncoder encoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder
        JSONDecoder decoder = getDecoder();
        // get the service provider config schema
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getServiceProviderConfigResourceSchema();
        // create a string in json format with relevant values
        String scimObjectString = encoder.buildServiceProviderConfigJsonBody(CharonConfiguration.getInstance().getConfig());
        // decode the SCIM service provider config object, encoded in the submitted payload.
        AbstractSCIMObject serviceProviderConfigObject = (AbstractSCIMObject) decoder.decodeResource(scimObjectString, schema, new AbstractSCIMObject());
        // encode the newly created SCIM service provider config object and add id attribute to Location header.
        String encodedObject;
        Map<String, String> responseHeaders = new HashMap<String, String>();
        if (serviceProviderConfigObject != null) {
            // create a deep copy of the service provider config object since we are going to change it.
            AbstractSCIMObject copiedObject = (AbstractSCIMObject) CopyUtil.deepCopy(serviceProviderConfigObject);
            encodedObject = encoder.encodeSCIMObject(copiedObject);
            // add location header
            responseHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.SERVICE_PROVIDER_CONFIG_ENDPOINT));
            responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String error = "Newly created User resource is null.";
            throw new InternalErrorException(error);
        }
        // put the uri of the service provider config object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedObject, responseHeaders);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (JSONException e) {
        return null;
    }
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) HashMap(java.util.HashMap) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) JSONException(org.json.JSONException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 20 with CharonException

use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.

the class AbstractValidator method validateSCIMObjectForRequiredAttributes.

/*
     * Validate SCIMObject for required attributes given the object and the corresponding schema.
     *
     * @param scimObject
     * @param resourceSchema
     */
public static void validateSCIMObjectForRequiredAttributes(AbstractSCIMObject scimObject, ResourceTypeSchema resourceSchema) throws BadRequestException, CharonException {
    // get attributes from schema.
    List<AttributeSchema> attributeSchemaList = resourceSchema.getAttributesList();
    // get attribute list from scim object.
    Map<String, Attribute> attributeList = scimObject.getAttributeList();
    for (AttributeSchema attributeSchema : attributeSchemaList) {
        // check for required attributes.
        if (attributeSchema.getRequired()) {
            if (!attributeList.containsKey(attributeSchema.getName())) {
                String error = "Required attribute " + attributeSchema.getName() + " is missing in the SCIM " + "Object.";
                throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
            }
        }
        // check for required sub attributes.
        AbstractAttribute attribute = (AbstractAttribute) attributeList.get(attributeSchema.getName());
        validateSCIMObjectForRequiredSubAttributes(attribute, attributeSchema, scimObject);
    }
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute)

Aggregations

CharonException (org.wso2.charon3.core.exceptions.CharonException)153 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)137 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)120 User (org.wso2.charon3.core.objects.User)114 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)109 Test (org.testng.annotations.Test)106 JSONObject (org.json.JSONObject)88 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)80 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)73 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)63 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)59 HashMap (java.util.HashMap)55 Group (org.wso2.charon3.core.objects.Group)55 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)52 DataProvider (org.testng.annotations.DataProvider)51 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)49 Attribute (org.wso2.charon3.core.attributes.Attribute)46 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)46 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)42 ArrayList (java.util.ArrayList)37