Search in sources :

Example 26 with ComplexAttribute

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

the class JSONDecoder method decodeResource.

/**
 * Decode the resource string sent in the SCIM request payload.
 *
 * @param scimResourceString - json encoded string of user info
 * @param resourceSchema     - SCIM defined user schema
 * @param scimObject         - a container holding the attributes and schema list
 * @return SCIMObject
 */
public SCIMObject decodeResource(String scimResourceString, ResourceTypeSchema resourceSchema, AbstractSCIMObject scimObject) throws BadRequestException, CharonException, InternalErrorException {
    try {
        // decode the string into json representation
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        // get the attribute schemas list from the schema that defines the given resource
        List<AttributeSchema> attributeSchemas = resourceSchema.getAttributesList();
        // set the schemas in scimobject
        for (int i = 0; i < resourceSchema.getSchemasList().size(); i++) {
            scimObject.setSchema(resourceSchema.getSchemasList().get(i));
        }
        // iterate through the schema and extract the attributes.
        for (AttributeSchema attributeSchema : attributeSchemas) {
            // obtain the user defined value for given key- attribute schema name
            Object attributeValObj = decodedJsonObj.opt(attributeSchema.getName());
            if (attributeValObj == null) {
                // user may define the attribute by its fully qualified uri
                attributeValObj = decodedJsonObj.opt(attributeSchema.getURI());
            }
            SCIMDefinitions.DataType attributeSchemaDataType = attributeSchema.getType();
            if (attributeSchemaDataType.equals(STRING) || attributeSchemaDataType.equals(BINARY) || attributeSchemaDataType.equals(BOOLEAN) || attributeSchemaDataType.equals(DATE_TIME) || attributeSchemaDataType.equals(DECIMAL) || attributeSchemaDataType.equals(INTEGER) || attributeSchemaDataType.equals(REFERENCE)) {
                if (!attributeSchema.getMultiValued()) {
                    if (attributeValObj instanceof String || attributeValObj instanceof Boolean || attributeValObj instanceof Integer || attributeValObj == null) {
                        // If an attribute is passed without a value, no need to save it.
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding schema data type is String/Boolean/Binary/Decimal/Integer/DataTime
                        // or Reference, it is a SimpleAttribute.
                        scimObject.setAttribute(buildSimpleAttribute(attributeSchema, attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the simple attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                } else {
                    if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                        // If an attribute is passed without a value, no need to save it.
                        if (attributeValObj == null) {
                            continue;
                        }
                        scimObject.setAttribute(buildPrimitiveMultiValuedAttribute(attributeSchema, (JSONArray) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the primitive multivalued attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                }
            } else if (attributeSchemaDataType.equals(COMPLEX)) {
                if (attributeSchema.getMultiValued() == true) {
                    if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding json value object is JSONArray, it is a MultiValuedAttribute.
                        scimObject.setAttribute(buildComplexMultiValuedAttribute(attributeSchema, (JSONArray) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the complex multivalued attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                } else if (attributeSchema.getMultiValued() == false) {
                    if (attributeValObj instanceof JSONObject || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding json value object is JSONObject, it is a ComplexAttribute.
                        scimObject.setAttribute(buildComplexAttribute(attributeSchema, (JSONObject) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the complex attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                }
            }
        }
        return scimObject;
    } catch (JSONException e) {
        logger.error("json error in decoding the resource");
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) SCIMDefinitions(org.wso2.charon3.core.schema.SCIMDefinitions) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject)

Example 27 with ComplexAttribute

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

the class JSONDecoder method buildComplexAttribute.

/*
     * Return a complex attribute with the user defined sub values included and necessary attribute characteristics set
     *
     * @param complexAttributeSchema - complex attribute schema
     * @param jsonObject             - sub attributes values for the complex attribute
     * @return ComplexAttribute
     */
public ComplexAttribute buildComplexAttribute(AttributeSchema complexAttributeSchema, JSONObject jsonObject) throws BadRequestException, CharonException, InternalErrorException, JSONException {
    ComplexAttribute complexAttribute = new ComplexAttribute(complexAttributeSchema.getName());
    Map<String, Attribute> subAttributesMap = new HashMap<String, Attribute>();
    // list of sub attributes of the complex attribute
    List<SCIMAttributeSchema> subAttributeSchemas = ((SCIMAttributeSchema) complexAttributeSchema).getSubAttributeSchemas();
    // iterate through the complex attribute schema and extract the sub attributes.
    for (AttributeSchema subAttributeSchema : subAttributeSchemas) {
        // obtain the user defined value for given key- attribute schema name
        Object attributeValObj = jsonObject.opt(subAttributeSchema.getName());
        SCIMDefinitions.DataType subAttributeSchemaType = subAttributeSchema.getType();
        if (subAttributeSchemaType.equals(STRING) || subAttributeSchemaType.equals(BINARY) || subAttributeSchemaType.equals(BOOLEAN) || subAttributeSchemaType.equals(DATE_TIME) || subAttributeSchemaType.equals(DECIMAL) || subAttributeSchemaType.equals(INTEGER) || subAttributeSchemaType.equals(REFERENCE)) {
            if (!subAttributeSchema.getMultiValued()) {
                if (attributeValObj instanceof String || attributeValObj instanceof Boolean || attributeValObj instanceof Integer || attributeValObj == null) {
                    // If an attribute is passed without a value, no need to save it.
                    if (attributeValObj == null) {
                        continue;
                    }
                    // if the corresponding schema data type is String/Boolean/Binary/Decimal/Integer/DataTime
                    // or Reference, it is a SimpleAttribute.
                    subAttributesMap.put(subAttributeSchema.getName(), buildSimpleAttribute(subAttributeSchema, attributeValObj));
                } else {
                    logger.error("Error decoding the sub attribute");
                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                }
            } else {
                if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                    // If an attribute is passed without a value, no need to save it.
                    if (attributeValObj == null) {
                        continue;
                    }
                    subAttributesMap.put(subAttributeSchema.getName(), buildPrimitiveMultiValuedAttribute(subAttributeSchema, (JSONArray) attributeValObj));
                } else {
                    logger.error("Error decoding the sub attribute");
                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                }
            }
        // this case is only valid for the extension schema
        // As according to the spec we have complex attribute inside complex attribute only for extension,
        // we need to treat it separately
        } else if (complexAttributeSchema.getName().equals(SCIMResourceSchemaManager.getInstance().getExtensionName())) {
            if (subAttributeSchemaType.equals(COMPLEX)) {
                // check for user defined extension's schema violation
                List<SCIMAttributeSchema> subList = subAttributeSchema.getSubAttributeSchemas();
                for (AttributeSchema attributeSchema : subList) {
                    if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                        String error = "Complex attribute can not have complex sub attributes";
                        throw new InternalErrorException(error);
                    }
                }
                if (subAttributeSchema.getMultiValued() == true) {
                    if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
                        JSONArray attributeValues = null;
                        List<Attribute> complexAttributeValues = new ArrayList<Attribute>();
                        try {
                            attributeValues = (JSONArray) attributeValObj;
                        } catch (Exception e) {
                            logger.error("Error decoding the extension");
                            throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                        }
                        // 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(subAttributeSchema, complexAttributeValue));
                            } else {
                                String error = "Unknown JSON representation for the MultiValued attribute " + subAttributeSchema.getName() + " which has data type as " + subAttributeSchema.getType();
                                throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
                            }
                            multiValuedAttribute.setAttributeValues(complexAttributeValues);
                            MultiValuedAttribute complexMultiValuedSubAttribute = (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
                            subAttributesMap.put(complexMultiValuedSubAttribute.getName(), complexMultiValuedSubAttribute);
                        }
                    } else {
                        logger.error("Error decoding the extension sub attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                } else {
                    if (attributeValObj instanceof JSONObject || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        ComplexAttribute complexSubAttribute = buildComplexAttribute(subAttributeSchema, (JSONObject) attributeValObj);
                        subAttributesMap.put(complexSubAttribute.getName(), complexSubAttribute);
                    } else {
                        logger.error("Error decoding the extension sub attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                }
            }
        } else {
            String error = "Complex attribute can not have complex sub attributes";
            throw new InternalErrorException(error);
        }
    }
    complexAttribute.setSubAttributesList(subAttributesMap);
    return (ComplexAttribute) DefaultAttributeFactory.createAttribute(complexAttributeSchema, complexAttribute);
}
Also used : 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) HashMap(java.util.HashMap) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) JSONArray(org.json.JSONArray) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) SCIMDefinitions(org.wso2.charon3.core.schema.SCIMDefinitions) JSONException(org.json.JSONException) CharonException(org.wso2.charon3.core.exceptions.CharonException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) IOException(java.io.IOException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) JSONObject(org.json.JSONObject) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 with ComplexAttribute

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

the class JSONEncoder method encodeMultiValuedAttribute.

/*
     * Encode the simple attribute and include it in root json object to be returned.
     *
     * @param multiValuedAttribute
     * @param jsonObject
     */
public void encodeMultiValuedAttribute(MultiValuedAttribute multiValuedAttribute, JSONObject jsonObject) throws JSONException {
    JSONArray jsonArray = new JSONArray();
    // TODO:what if values are set as list of string values.For the moment it is ok, since only schemas
    // attribute has such values and we handle it separately in encoding.
    List<Object> stringAttributeValues = multiValuedAttribute.getAttributePrimitiveValues();
    List<Attribute> attributeValues = multiValuedAttribute.getAttributeValues();
    // if values are strings,
    if (attributeValues != null && !attributeValues.isEmpty()) {
        for (Attribute attributeValue : attributeValues) {
            if (attributeValue instanceof SimpleAttribute) {
                encodeSimpleAttributeValue((SimpleAttribute) attributeValue, jsonArray);
            } else if (attributeValue instanceof ComplexAttribute) {
                encodeComplexAttributeValue((ComplexAttribute) attributeValue, jsonArray);
            }
        }
    }
    if (stringAttributeValues != null && !stringAttributeValues.isEmpty()) {
        for (Object arrayValue : stringAttributeValues) {
            jsonArray.put(arrayValue);
        }
    }
    jsonObject.put(multiValuedAttribute.getName(), jsonArray);
}
Also used : 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) JSONArray(org.json.JSONArray) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject)

Example 29 with ComplexAttribute

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

use of org.wso2.charon3.core.attributes.ComplexAttribute 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)

Aggregations

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