Search in sources :

Example 1 with SCIMObject

use of org.wso2.charon3.core.objects.SCIMObject in project charon by wso2.

the class ServerSideValidator method validateCreatedSCIMObject.

/*
     * Validate created SCIMObject according to the spec
     *
     * @param scimObject
     * @param resourceSchema
     * @throw CharonException
     * @throw BadRequestException
     * @throw NotFoundException
     */
public static void validateCreatedSCIMObject(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException, NotFoundException {
    if (scimObject instanceof User) {
        // set display names for complex multivalued attributes
        setDisplayNameInComplexMultiValuedAttributes(scimObject, resourceSchema);
    }
    // remove any read only attributes
    removeAnyReadOnlyAttributes(scimObject, resourceSchema);
    // add created and last modified dates
    String id = UUID.randomUUID().toString();
    scimObject.setId(id);
    Date date = new Date();
    // set the created date and time
    scimObject.setCreatedDate(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(date)));
    // creates date and the last modified are the same if not updated.
    scimObject.setLastModified(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(date)));
    // set location and resourceType
    if (resourceSchema.isSchemaAvailable(SCIMConstants.USER_CORE_SCHEMA_URI)) {
        String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.USER_ENDPOINT), scimObject.getId());
        scimObject.setLocation(location);
        scimObject.setResourceType(SCIMConstants.USER);
    } else if (resourceSchema.isSchemaAvailable(SCIMConstants.GROUP_CORE_SCHEMA_URI)) {
        String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT), scimObject.getId());
        scimObject.setLocation(location);
        scimObject.setResourceType(SCIMConstants.GROUP);
    }
    // check for required attributes
    validateSCIMObjectForRequiredAttributes(scimObject, resourceSchema);
    validateSchemaList(scimObject, resourceSchema);
}
Also used : User(org.wso2.charon3.core.objects.User) Date(java.util.Date)

Example 2 with SCIMObject

use of org.wso2.charon3.core.objects.SCIMObject 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);
    }
}
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)

Example 3 with SCIMObject

use of org.wso2.charon3.core.objects.SCIMObject in project charon by wso2.

the class AbstractValidator method setDisplayNameInComplexMultiValuedAttributes.

/*
     * This method is basically for adding display sub attribute to multivalued attributes
     * which has 'display' as a sub attribute in the respective attribute schema
     *
     * @param scimObject
     * @param resourceSchema
     * @throws CharonException
     * @throws BadRequestException
     */
protected static void setDisplayNameInComplexMultiValuedAttributes(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException {
    Map<String, Attribute> attributeList = scimObject.getAttributeList();
    ArrayList<AttributeSchema> attributeSchemaList = resourceSchema.getAttributesList();
    for (AttributeSchema attributeSchema : attributeSchemaList) {
        if (attributeSchema.getMultiValued() && attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            if (attributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY) != null) {
                if (attributeList.containsKey(attributeSchema.getName())) {
                    Attribute multiValuedAttribute = attributeList.get(attributeSchema.getName());
                    setDisplayNameInComplexMultiValuedSubAttributes(multiValuedAttribute, attributeSchema);
                }
            }
        } else if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            // this is only valid for extension schema
            List<SCIMAttributeSchema> subAttributeSchemaList = attributeSchema.getSubAttributeSchemas();
            for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                if (subAttributeSchema.getMultiValued() && subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                    if (subAttributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY) != null) {
                        Attribute extensionAttribute = attributeList.get(attributeSchema.getName());
                        if (extensionAttribute != null) {
                            if ((((ComplexAttribute) extensionAttribute).getSubAttribute(subAttributeSchema.getName())) != null) {
                                Attribute multiValuedAttribute = (attributeList.get(attributeSchema.getName())).getSubAttribute(subAttributeSchema.getName());
                                setDisplayNameInComplexMultiValuedSubAttributes(multiValuedAttribute, subAttributeSchema);
                            }
                        }
                    }
                }
            }
        }
    }
}
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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with SCIMObject

use of org.wso2.charon3.core.objects.SCIMObject in project charon by wso2.

the class AbstractValidator method removeAnyReadOnlyAttributes.

/*
     * Check for readonlyAttributes and remove them if they have been modified. - (create method)
     *
     * @param scimObject
     * @param resourceSchema
     * @throws CharonException
     */
public static void removeAnyReadOnlyAttributes(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException {
    // No need to check for immutable as immutable attributes can be defined at resource creation
    // 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 read-only attributes.
        if (attributeSchema.getMutability() == SCIMDefinitions.Mutability.READ_ONLY) {
            if (attributeList.containsKey(attributeSchema.getName())) {
                String error = "Read only attribute: " + attributeSchema.getName() + " is set from consumer in the SCIM Object. " + "Removing it.";
                logger.debug(error);
                scimObject.deleteAttribute(attributeSchema.getName());
            }
        }
        // check for readonly sub attributes.
        AbstractAttribute attribute = (AbstractAttribute) attributeList.get(attributeSchema.getName());
        removeAnyReadOnlySubAttributes(attribute, attributeSchema);
    }
}
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) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute)

Example 5 with SCIMObject

use of org.wso2.charon3.core.objects.SCIMObject in project charon by wso2.

the class JSONEncoder method getSCIMObjectAsJSONObject.

/*
     * Make JSON object from given SCIM object.
     *
     * @param scimObject
     * @return the resulting string after encoding.
     */
public JSONObject getSCIMObjectAsJSONObject(SCIMObject scimObject) throws CharonException {
    // root json object containing the encoded SCIM Object.
    JSONObject rootObject = new JSONObject();
    try {
        // encode schemas
        this.encodeArrayOfValues(SCIMConstants.CommonSchemaConstants.SCHEMAS, (scimObject.getSchemaList()).toArray(), rootObject);
        // encode attribute list
        Map<String, Attribute> attributes = scimObject.getAttributeList();
        if (attributes != null && !attributes.isEmpty()) {
            for (Attribute attribute : attributes.values()) {
                // using instanceof instead of polymorphic way, in order to make encoder pluggable.
                if (attribute instanceof SimpleAttribute) {
                    encodeSimpleAttribute((SimpleAttribute) attribute, rootObject);
                } else if (attribute instanceof ComplexAttribute) {
                    encodeComplexAttribute((ComplexAttribute) attribute, rootObject);
                } else if (attribute instanceof MultiValuedAttribute) {
                    encodeMultiValuedAttribute((MultiValuedAttribute) attribute, rootObject);
                }
            }
        }
    } catch (JSONException e) {
        String errorMessage = "Error in encoding resource..";
        // TODO:log the error
        throw new CharonException(errorMessage);
    }
    return rootObject;
}
Also used : JSONObject(org.json.JSONObject) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) JSONException(org.json.JSONException) CharonException(org.wso2.charon3.core.exceptions.CharonException) AbstractCharonException(org.wso2.charon3.core.exceptions.AbstractCharonException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Aggregations

Attribute (org.wso2.charon3.core.attributes.Attribute)6 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)6 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)6 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)6 AbstractAttribute (org.wso2.charon3.core.attributes.AbstractAttribute)5 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)4 ArrayList (java.util.ArrayList)3 JSONException (org.json.JSONException)3 JSONObject (org.json.JSONObject)3 List (java.util.List)2 JSONTokener (org.json.JSONTokener)2 CharonException (org.wso2.charon3.core.exceptions.CharonException)2 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)2 User (org.wso2.charon3.core.objects.User)2 Date (java.util.Date)1 JSONArray (org.json.JSONArray)1 AbstractCharonException (org.wso2.charon3.core.exceptions.AbstractCharonException)1 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)1 Group (org.wso2.charon3.core.objects.Group)1 SCIMObject (org.wso2.charon3.core.objects.SCIMObject)1