Search in sources :

Example 26 with MultiValuedAttribute

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

the class JSONEncoder method encodeComplexAttributeValue.

/*
     * When an attribute value (of a multivalued attribute) becomes a complex attribute,
     * use this method to encode it.
     *
     * @param attributeValue
     * @param jsonArray
     */
protected void encodeComplexAttributeValue(ComplexAttribute attributeValue, JSONArray jsonArray) throws JSONException {
    JSONObject subObject = new JSONObject();
    Map<String, Attribute> subAttributes = attributeValue.getSubAttributesList();
    for (Attribute value : subAttributes.values()) {
        // using instanceof instead of polymorphic way, in order to make encoder pluggable.
        if (value instanceof SimpleAttribute) {
            encodeSimpleAttribute((SimpleAttribute) value, subObject);
        } else if (value instanceof ComplexAttribute) {
            encodeComplexAttribute((ComplexAttribute) value, subObject);
        } else if (value instanceof MultiValuedAttribute) {
            encodeMultiValuedAttribute((MultiValuedAttribute) value, subObject);
        }
    }
    jsonArray.put(subObject);
}
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) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 27 with MultiValuedAttribute

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

the class AbstractValidator method setDisplayNameInComplexMultiValuedSubAttributes.

/*
     * set the displayname sub attribute in complex type multi valued attribute
     * eg. display name of emails
     *
     * @param multiValuedAttribute
     * @param attributeSchema
     * @throws CharonException
     * @throws BadRequestException
     */
private static void setDisplayNameInComplexMultiValuedSubAttributes(Attribute multiValuedAttribute, AttributeSchema attributeSchema) throws CharonException, BadRequestException {
    List<Attribute> subValuesList = ((MultiValuedAttribute) (multiValuedAttribute)).getAttributeValues();
    for (Attribute subValue : subValuesList) {
        for (AttributeSchema subAttributeSchema : attributeSchema.getSubAttributeSchemas()) {
            if (subAttributeSchema.getName().equals(SCIMConstants.CommonSchemaConstants.VALUE)) {
                if (!subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX) && !subAttributeSchema.getMultiValued()) {
                    // take the value from the value sub attribute and put is as display attribute
                    SimpleAttribute simpleAttribute = null;
                    simpleAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.DISPLAY, ((SimpleAttribute) (subValue.getSubAttribute(subAttributeSchema.getName()))).getValue());
                    AttributeSchema subSchema = attributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY);
                    simpleAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(subSchema, simpleAttribute);
                    ((ComplexAttribute) (subValue)).setSubAttribute(simpleAttribute);
                } else if (!subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX) && subAttributeSchema.getMultiValued()) {
                    Attribute valueSubAttribute = (MultiValuedAttribute) (subValue.getSubAttribute(subAttributeSchema.getName()));
                    Object displayValue = null;
                    try {
                        displayValue = ((MultiValuedAttribute) (valueSubAttribute)).getAttributePrimitiveValues().get(0);
                    } catch (Exception e) {
                        String error = "Can not set display attribute value without a value attribute value.";
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX, error);
                    }
                    // if multiple values are available, get the first value and put it as display name
                    SimpleAttribute simpleAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.DISPLAY, displayValue);
                    AttributeSchema subSchema = attributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY);
                    simpleAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(subSchema, simpleAttribute);
                    ((ComplexAttribute) (subValue)).setSubAttribute(simpleAttribute);
                }
            }
        }
    }
}
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) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) CharonException(org.wso2.charon3.core.exceptions.CharonException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 28 with MultiValuedAttribute

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

the class AbstractValidator method validateReturnedAttributes.

/*
     * This method is to remove any defined and requested attributes and include
     * requested attributes if not they have been removed.
     *
     * @param scimObject
     * @param requestedAttributes
     * @param requestedExcludingAttributes
     */
public static void validateReturnedAttributes(AbstractSCIMObject scimObject, String requestedAttributes, String requestedExcludingAttributes) throws CharonException {
    List<String> requestedAttributesList = null;
    List<String> requestedExcludingAttributesList = null;
    if (requestedAttributes != null) {
        // make a list from the comma separated requestedAttributes
        requestedAttributesList = Arrays.asList(requestedAttributes.split(","));
    }
    if (requestedExcludingAttributes != null) {
        // make a list from the comma separated requestedExcludingAttributes
        requestedExcludingAttributesList = Arrays.asList(requestedExcludingAttributes.split(","));
    }
    Map<String, Attribute> attributeList = scimObject.getAttributeList();
    ArrayList<Attribute> attributeTemporyList = new ArrayList<Attribute>();
    for (Attribute attribute : attributeList.values()) {
        attributeTemporyList.add(attribute);
    }
    for (Attribute attribute : attributeTemporyList) {
        // check for never/request attributes.
        if (attribute.getReturned().equals(SCIMDefinitions.Returned.NEVER)) {
            scimObject.deleteAttribute(attribute.getName());
        }
        // If so return it.
        if (requestedAttributes == null && requestedExcludingAttributes == null) {
            if (attribute.getReturned().equals(SCIMDefinitions.Returned.REQUEST)) {
                scimObject.deleteAttribute(attribute.getName());
            }
        } else {
            // A request should only contains either attributes or exclude attribute params. Not both
            if (requestedAttributes != null) {
                // and add only the requested attributes
                if ((attribute.getReturned().equals(SCIMDefinitions.Returned.DEFAULT) || attribute.getReturned().equals(SCIMDefinitions.Returned.REQUEST)) && (!requestedAttributesList.contains(attribute.getName()) && !isSubAttributeExistsInList(requestedAttributesList, attribute))) {
                    scimObject.deleteAttribute(attribute.getName());
                }
            } else if (requestedExcludingAttributes != null) {
                // removing attributes which has returned as request. This is because no request is made
                if (attribute.getReturned().equals(SCIMDefinitions.Returned.REQUEST)) {
                    scimObject.deleteAttribute(attribute.getName());
                }
                // removed from the default set of attributes
                if ((attribute.getReturned().equals(SCIMDefinitions.Returned.DEFAULT)) && requestedExcludingAttributesList.contains(attribute.getName())) {
                    scimObject.deleteAttribute(attribute.getName());
                }
            }
        }
        // check the same for sub attributes
        if (attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            if (attribute.getMultiValued()) {
                List<Attribute> valuesList = ((MultiValuedAttribute) attribute).getAttributeValues();
                for (Attribute subAttribute : valuesList) {
                    Map<String, Attribute> valuesSubAttributeList = ((ComplexAttribute) subAttribute).getSubAttributesList();
                    ArrayList<Attribute> valuesSubAttributeTemporyList = new ArrayList<Attribute>();
                    // hence need to traverse on a copy
                    for (Attribute subSimpleAttribute : valuesSubAttributeList.values()) {
                        valuesSubAttributeTemporyList.add(subSimpleAttribute);
                    }
                    for (Attribute subSimpleAttribute : valuesSubAttributeTemporyList) {
                        removeValuesSubAttributeOnReturn(subSimpleAttribute, subAttribute, attribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                    }
                }
            } else {
                Map<String, Attribute> subAttributeList = ((ComplexAttribute) attribute).getSubAttributesList();
                ArrayList<Attribute> subAttributeTemporyList = new ArrayList<Attribute>();
                for (Attribute subAttribute : subAttributeList.values()) {
                    subAttributeTemporyList.add(subAttribute);
                }
                for (Attribute subAttribute : subAttributeTemporyList) {
                    if (subAttribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                        // this applicable for extension schema only
                        if (subAttribute.getMultiValued()) {
                            List<Attribute> valuesList = ((MultiValuedAttribute) subAttribute).getAttributeValues();
                            for (Attribute subSubValue : valuesList) {
                                Map<String, Attribute> subValuesSubAttributeList = ((ComplexAttribute) subSubValue).getSubAttributesList();
                                ArrayList<Attribute> valuesSubSubAttributeTemporyList = new ArrayList<Attribute>();
                                // hence need to traverse on a copy
                                for (Attribute subSubSimpleAttribute : subValuesSubAttributeList.values()) {
                                    valuesSubSubAttributeTemporyList.add(subSubSimpleAttribute);
                                }
                                for (Attribute subSubSimpleAttribute : valuesSubSubAttributeTemporyList) {
                                    removeValuesSubSubAttributeOnReturn(attribute, subAttribute, subSubValue, subSubSimpleAttribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                                }
                            }
                        } else {
                            ArrayList<Attribute> subSubAttributeTemporyList = new ArrayList<Attribute>();
                            Map<String, Attribute> subSubAttributeList = ((ComplexAttribute) subAttribute).getSubAttributesList();
                            for (Attribute subSubAttribute : subSubAttributeList.values()) {
                                subSubAttributeTemporyList.add(subSubAttribute);
                            }
                            for (Attribute subSubAttribute : subSubAttributeTemporyList) {
                                removeSubSubAttributesOnReturn(attribute, subAttribute, subSubAttribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                            }
                        }
                        removeSubAttributesOnReturn(subAttribute, attribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                    } else {
                        removeSubAttributesOnReturn(subAttribute, attribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, 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) ArrayList(java.util.ArrayList) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 29 with MultiValuedAttribute

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

the class AbstractValidator method validateSCIMObjectForRequiredSubAttributes.

/*
     * Validate SCIMObject for required sub attributes given the object and the corresponding schema.
     *
     * @param attribute
     * @param attributeSchema
     * @throws CharonException
     * @throws BadRequestException
     */
private static void validateSCIMObjectForRequiredSubAttributes(AbstractAttribute attribute, AttributeSchema attributeSchema) throws CharonException, BadRequestException {
    if (attribute != null) {
        List<SCIMAttributeSchema> subAttributesSchemaList = ((SCIMAttributeSchema) attributeSchema).getSubAttributeSchemas();
        if (subAttributesSchemaList != null) {
            for (SCIMAttributeSchema subAttributeSchema : subAttributesSchemaList) {
                if (subAttributeSchema.getRequired()) {
                    if (attribute instanceof ComplexAttribute) {
                        if (attribute.getSubAttribute(subAttributeSchema.getName()) == null) {
                            String error = "Required sub attribute: " + subAttributeSchema.getName() + " is missing in the SCIM Attribute: " + attribute.getName();
                            throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
                        }
                    } else if (attribute instanceof MultiValuedAttribute) {
                        List<Attribute> values = ((MultiValuedAttribute) attribute).getAttributeValues();
                        for (Attribute value : values) {
                            if (value instanceof ComplexAttribute) {
                                if (value.getSubAttribute(subAttributeSchema.getName()) == null) {
                                    String error = "Required sub attribute: " + subAttributeSchema.getName() + ", is missing in the SCIM Attribute: " + attribute.getName();
                                    throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
                                }
                            }
                        }
                    }
                }
                // Following is only applicable for extension schema validation.
                AbstractAttribute subAttribute = null;
                if (attribute instanceof ComplexAttribute) {
                    subAttribute = (AbstractAttribute) ((ComplexAttribute) attribute).getSubAttribute(subAttributeSchema.getName());
                } else if (attribute instanceof MultiValuedAttribute) {
                    List<Attribute> subAttributeList = ((MultiValuedAttribute) attribute).getAttributeValues();
                    for (Attribute subAttrbte : subAttributeList) {
                        if (subAttrbte.getName().equals(subAttributeSchema.getName())) {
                            subAttribute = (AbstractAttribute) subAttrbte;
                        }
                    }
                }
                List<SCIMAttributeSchema> subSubAttributesSchemaList = ((SCIMAttributeSchema) subAttributeSchema).getSubAttributeSchemas();
                if (subSubAttributesSchemaList != null) {
                    validateSCIMObjectForRequiredSubAttributes(subAttribute, 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) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute) ArrayList(java.util.ArrayList) List(java.util.List) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 30 with MultiValuedAttribute

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

the class AbstractValidator method removeAnyReadOnlySubAttributes.

/*
     * Check for readonlySubAttributes and remove them if they have been modified. - (create method)
     *
     * @param attribute
     * @param attributeSchema
     * @throws CharonException
     */
private static void removeAnyReadOnlySubAttributes(Attribute attribute, AttributeSchema attributeSchema) throws CharonException {
    if (attribute != null) {
        List<SCIMAttributeSchema> subAttributesSchemaList = ((SCIMAttributeSchema) attributeSchema).getSubAttributeSchemas();
        if (subAttributesSchemaList != null && !subAttributesSchemaList.isEmpty()) {
            for (SCIMAttributeSchema subAttributeSchema : subAttributesSchemaList) {
                if (subAttributeSchema.getMutability() == SCIMDefinitions.Mutability.READ_ONLY) {
                    if (attribute instanceof ComplexAttribute) {
                        if (attribute.getSubAttribute(subAttributeSchema.getName()) != null) {
                            String error = "Readonly sub attribute: " + subAttributeSchema.getName() + " is set in the SCIM Attribute: " + attribute.getName() + ". Removing it.";
                            logger.debug(error);
                            ((ComplexAttribute) attribute).removeSubAttribute(subAttributeSchema.getName());
                        }
                    } else if (attribute instanceof MultiValuedAttribute) {
                        List<Attribute> values = ((MultiValuedAttribute) attribute).getAttributeValues();
                        for (Attribute value : values) {
                            if (value instanceof ComplexAttribute) {
                                if (value.getSubAttribute(subAttributeSchema.getName()) != null) {
                                    String error = "Readonly sub attribute: " + subAttributeSchema.getName() + " is set in the SCIM Attribute: " + attribute.getName() + ". Removing it.";
                                    logger.debug(error);
                                    ((ComplexAttribute) value).removeSubAttribute(subAttributeSchema.getName());
                                }
                            }
                        }
                    }
                }
                // Otherwise no complex attribute can complex sub attributes.
                if (subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                    // check for readonly sub-sub attributes in extension.
                    // get attributes from schema.
                    Map<String, Attribute> subAttributeList = ((ComplexAttribute) attribute).getSubAttributesList();
                    for (Attribute subSubAttribute : subAttributeList.values()) {
                        removeAnyReadOnlySubAttributes(subSubAttribute, 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) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) ArrayList(java.util.ArrayList) List(java.util.List) 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