Search in sources :

Example 11 with MultiValuedAttribute

use of org.wso2.charon3.core.attributes.MultiValuedAttribute in project charon by wso2.

the class ResourceTypeResourceManager method buildCombinedResourceType.

/*
     * This combines the user and group resource type AbstractSCIMObjects and build a
     * one root AbstractSCIMObjects
     *
     * @param userObject
     * @param groupObject
     * @return
     * @throws CharonException
     */
private AbstractSCIMObject buildCombinedResourceType(AbstractSCIMObject userObject, AbstractSCIMObject groupObject) throws CharonException {
    Map<String, Attribute> userObjectAttributeList = userObject.getAttributeList();
    Map<String, Attribute> groupObjectAttributeList = groupObject.getAttributeList();
    AbstractSCIMObject rootObject = new AbstractSCIMObject();
    MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(SCIMConstants.ResourceTypeSchemaConstants.RESOURCE_TYPE);
    ComplexAttribute userComplexAttribute = new ComplexAttribute();
    for (Attribute attribute : userObjectAttributeList.values()) {
        userComplexAttribute.setSubAttribute(attribute);
    }
    multiValuedAttribute.setAttributeValue(userComplexAttribute);
    ComplexAttribute groupComplexAttribute = new ComplexAttribute();
    for (Attribute attribute : groupObjectAttributeList.values()) {
        groupComplexAttribute.setSubAttribute(attribute);
    }
    multiValuedAttribute.setAttributeValue(groupComplexAttribute);
    rootObject.setAttribute(multiValuedAttribute);
    rootObject.setSchema(SCIMConstants.RESOURCE_TYPE_SCHEMA_URI);
    return rootObject;
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 12 with MultiValuedAttribute

use of org.wso2.charon3.core.attributes.MultiValuedAttribute 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 13 with MultiValuedAttribute

use of org.wso2.charon3.core.attributes.MultiValuedAttribute in project charon by wso2.

the class AbstractValidator method checkIfReadOnlyAndImmutableSubAttributesModified.

/*
     * check for read only and immutable sub attributes which has been modified on update request
     *
     * @param newAttributeList
     * @param oldAttributeList
     * @param attributeSchema
     * @throws BadRequestException
     * @throws CharonException
     */
private static void checkIfReadOnlyAndImmutableSubAttributesModified(Map<String, Attribute> newAttributeList, Map<String, Attribute> oldAttributeList, AttributeSchema attributeSchema) throws BadRequestException, CharonException {
    // check for sub attributes.
    AbstractAttribute newAttribute = (AbstractAttribute) newAttributeList.get(attributeSchema.getName());
    AbstractAttribute oldAttribute = (AbstractAttribute) oldAttributeList.get(attributeSchema.getName());
    List<SCIMAttributeSchema> subAttributeSchemaList = attributeSchema.getSubAttributeSchemas();
    if (subAttributeSchemaList != null) {
        if (SCIMResourceSchemaManager.getInstance().getExtensionName() != null) {
            if (attributeSchema.getName().equals(SCIMResourceSchemaManager.getInstance().getExtensionName())) {
                checkIfReadOnlyAndImmutableExtensionAttributesModified(subAttributeSchemaList, newAttribute, oldAttribute);
            }
        }
        if (newAttribute != null && oldAttribute != null) {
            if (attributeSchema.getMultiValued()) {
                // this is complex multivalued case
                List<Attribute> newSubValuesList = ((MultiValuedAttribute) newAttribute).getAttributeValues();
                List<Attribute> oldSubValuesList = ((MultiValuedAttribute) oldAttribute).getAttributeValues();
                // if size aren't equal, they do not preserver immutable quality
                if (newSubValuesList.size() != oldSubValuesList.size() && attributeSchema.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                    throw new BadRequestException(ResponseCodeConstants.MUTABILITY);
                }
                // no need to check sub attributes of sub values separately for equality, stop at the sub value level
                for (Attribute subValue : newSubValuesList) {
                    if (!isListContains((((ComplexAttribute) subValue).getName()), oldSubValuesList) && attributeSchema.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                        throw new BadRequestException(ResponseCodeConstants.MUTABILITY);
                    }
                }
            } else {
                // A complex attribute itself can not be immutable if it's sub variables are not immutable
                checkForReadOnlyAndImmutableInComplexAttributes(newAttribute, oldAttribute, subAttributeSchemaList);
            }
        } else if (newAttribute == null && oldAttribute != null) {
            if (attributeSchema.getMultiValued()) {
                List<Attribute> oldSubValuesList = ((MultiValuedAttribute) oldAttribute).getAttributeValues();
                Attribute clonedMultiValuedAttribute = (Attribute) CopyUtil.deepCopy(oldAttribute);
                clonedMultiValuedAttribute.deleteSubAttributes();
                for (Attribute subValue : oldSubValuesList) {
                    Attribute clonedSubValue = (Attribute) CopyUtil.deepCopy(subValue);
                    clonedSubValue.deleteSubAttributes();
                    for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                        if (subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                            if (((ComplexAttribute) subValue).isSubAttributeExist(subAttributeSchema.getName())) {
                                Attribute clonedSubValuesAttribute = (Attribute) CopyUtil.deepCopy(((ComplexAttribute) subValue).getSubAttribute(subAttributeSchema.getName()));
                                ((ComplexAttribute) clonedSubValue).setSubAttribute(clonedSubValuesAttribute);
                            }
                        }
                    }
                    ((MultiValuedAttribute) (clonedMultiValuedAttribute)).setAttributeValue(clonedSubValue);
                }
            } else {
                Map<String, Attribute> oldSubAttributeList = ((ComplexAttribute) (oldAttribute)).getSubAttributesList();
                Attribute clonedAttribute = (Attribute) CopyUtil.deepCopy(oldAttribute);
                clonedAttribute.deleteSubAttributes();
                for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                    if (subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                        if (oldSubAttributeList.containsKey(subAttributeSchema.getName())) {
                            ((ComplexAttribute) (clonedAttribute)).setSubAttribute((Attribute) CopyUtil.deepCopy(oldSubAttributeList.get(subAttributeSchema.getName())));
                        }
                    }
                }
                newAttributeList.put(clonedAttribute.getName(), clonedAttribute);
            }
        } else if (newAttribute != null && oldAttribute == null) {
            if (attributeSchema.getMultiValued()) {
                if (attributeSchema.getMultiValued()) {
                    List<Attribute> newSubValuesList = ((MultiValuedAttribute) newAttribute).getAttributeValues();
                    for (Attribute subValue : newSubValuesList) {
                        for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                            if (subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                                ((ComplexAttribute) (subValue)).removeSubAttribute(subAttributeSchema.getName());
                            }
                        }
                    }
                }
            } else {
                // this is complex attribute case
                Map<String, Attribute> newSubAttributeList = ((ComplexAttribute) (newAttribute)).getSubAttributesList();
                for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                    if (subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                        if (newSubAttributeList.containsKey(subAttributeSchema.getName())) {
                            String error = "Read only attribute: " + subAttributeSchema.getName() + " is set from consumer in the SCIM Object. Removing it.";
                            logger.debug(error);
                            ((ComplexAttribute) newAttribute).removeSubAttribute(subAttributeSchema.getName());
                        }
                    }
                }
            }
        }
    }
}
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) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with MultiValuedAttribute

use of org.wso2.charon3.core.attributes.MultiValuedAttribute in project charon by wso2.

the class JSONDecoder method buildComplexMultiValuedAttribute.

/*
     * Return complex type multi valued attribute with the user defined
     * value included and necessary attribute characteristics set
     * @param attributeSchema - Attribute schema
     * @param attributeValues - values for the attribute
     * @return MultiValuedAttribute
     */
public MultiValuedAttribute buildComplexMultiValuedAttribute(AttributeSchema attributeSchema, JSONArray attributeValues) throws CharonException, BadRequestException {
    try {
        MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
        List<Attribute> complexAttributeValues = new ArrayList<Attribute>();
        // iterate through JSONArray and create the list of string values.
        for (int i = 0; i < attributeValues.length(); i++) {
            Object attributeValue = attributeValues.get(i);
            if (attributeValue instanceof JSONObject) {
                JSONObject complexAttributeValue = (JSONObject) attributeValue;
                complexAttributeValues.add(buildComplexValue(attributeSchema, complexAttributeValue));
            } else {
                String error = "Unknown JSON representation for the MultiValued attribute " + attributeSchema.getName() + " which has data type as " + attributeSchema.getType();
                throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
            }
        }
        multiValuedAttribute.setAttributeValues(complexAttributeValues);
        return (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
    } catch (JSONException e) {
        String error = "Error in accessing JSON value of multivalued attribute";
        throw new CharonException(error, e);
    }
}
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) ArrayList(java.util.ArrayList) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONException(org.json.JSONException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject) CharonException(org.wso2.charon3.core.exceptions.CharonException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 15 with MultiValuedAttribute

use of org.wso2.charon3.core.attributes.MultiValuedAttribute in project charon by wso2.

the class JSONDecoder method buildPrimitiveMultiValuedAttribute.

/*
     * Return a primitive type multi valued attribute with the user defined value included and necessary
     * attribute characteristics set
     *
     * @param attributeSchema - Attribute schema
     * @param attributeValues - values for the attribute
     * @return MultiValuedAttribute
     */
public MultiValuedAttribute buildPrimitiveMultiValuedAttribute(AttributeSchema attributeSchema, JSONArray attributeValues) throws CharonException, BadRequestException {
    try {
        MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
        List<Object> primitiveValues = new ArrayList<Object>();
        // iterate through JSONArray and create the list of string values.
        for (int i = 0; i < attributeValues.length(); i++) {
            Object attributeValue = attributeValues.get(i);
            if (attributeValue instanceof String || attributeValue instanceof Boolean || attributeValue instanceof Integer || attributeValue == null) {
                // If an attribute is passed without a value, no need to save it.
                if (attributeValue == null) {
                    continue;
                }
                primitiveValues.add(attributeValue);
            } else {
                String error = "Unknown JSON representation for the MultiValued attribute " + attributeSchema.getName() + " which has data type as " + attributeSchema.getType();
                throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
            }
        }
        multiValuedAttribute.setAttributePrimitiveValues(primitiveValues);
        return (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
    } catch (JSONException e) {
        String error = "Error in accessing JSON value of multivalued attribute";
        throw new CharonException(error, e);
    }
}
Also used : ArrayList(java.util.ArrayList) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONException(org.json.JSONException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject) CharonException(org.wso2.charon3.core.exceptions.CharonException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Aggregations

MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)35 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)32 Attribute (org.wso2.charon3.core.attributes.Attribute)31 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)30 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)20 JSONObject (org.json.JSONObject)14 ArrayList (java.util.ArrayList)10 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)10 JSONArray (org.json.JSONArray)9 JSONException (org.json.JSONException)8 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)8 List (java.util.List)7 Map (java.util.Map)7 AbstractAttribute (org.wso2.charon3.core.attributes.AbstractAttribute)7 CharonException (org.wso2.charon3.core.exceptions.CharonException)6 SCIMObject (org.wso2.charon3.core.objects.SCIMObject)6 HashMap (java.util.HashMap)4 JSONTokener (org.json.JSONTokener)4 SCIMAttributeSchema (org.wso2.charon3.core.schema.SCIMAttributeSchema)3 Iterator (java.util.Iterator)2