Search in sources :

Example 46 with CharonException

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

the class ResourceManagerUtil method removeAttributesFromList.

/*
     * this is to remove given attribute from the given list.
     *
     * @param attributeSchemaList
     * @param attributeName
     * @throws CharonException
     */
private static void removeAttributesFromList(List<AttributeSchema> attributeSchemaList, String attributeName) throws CharonException {
    List<AttributeSchema> tempList = (List<AttributeSchema>) CopyUtil.deepCopy(attributeSchemaList);
    int count = 0;
    for (AttributeSchema attributeSchema : tempList) {
        if (attributeSchema.getName().equals(attributeName)) {
            attributeSchemaList.remove(count);
        }
        count++;
    }
}
Also used : SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) List(java.util.List) ArrayList(java.util.ArrayList)

Example 47 with CharonException

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

the class AbstractSCIMObject method setLastModified.

/*
     * set the last modified date and time of the resource
     *
     * @param lastModifiedDate
     */
public void setLastModified(Date lastModifiedDate) throws CharonException, BadRequestException {
    // create the lastModified date attribute as defined in schema.
    SimpleAttribute lastModifiedAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.LAST_MODIFIED, new SimpleAttribute(SCIMConstants.CommonSchemaConstants.LAST_MODIFIED, lastModifiedDate));
    // check meta complex attribute already exist.
    if (getMetaAttribute() != null) {
        ComplexAttribute metaAttribute = getMetaAttribute();
        // check last modified attribute already exist
        if (metaAttribute.isSubAttributeExist(lastModifiedAttribute.getName())) {
            metaAttribute.removeSubAttribute(lastModifiedAttribute.getName());
            metaAttribute.setSubAttribute(lastModifiedAttribute);
        } else {
            metaAttribute.setSubAttribute(lastModifiedAttribute);
        }
    } else {
        // create meta attribute and set the sub attribute.
        createMetaAttribute();
        getMetaAttribute().setSubAttribute(lastModifiedAttribute);
    }
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute)

Example 48 with CharonException

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

the class JSONDecoder method buildComplexValue.

/*
     * To build a complex type value of a Multi Valued Attribute. (eg. Email with value,type,primary as sub attributes
     *
     * @param attributeSchema
     * @param jsonObject
     * @return ComplexAttribute
     */
private ComplexAttribute buildComplexValue(AttributeSchema attributeSchema, JSONObject jsonObject) throws CharonException, BadRequestException {
    ComplexAttribute complexAttribute = new ComplexAttribute(attributeSchema.getName());
    Map<String, Attribute> subAttributesMap = new HashMap<String, Attribute>();
    List<SCIMAttributeSchema> subAttributeSchemas = ((SCIMAttributeSchema) attributeSchema).getSubAttributeSchemas();
    for (SCIMAttributeSchema subAttributeSchema : subAttributeSchemas) {
        Object subAttributeValue = jsonObject.opt(subAttributeSchema.getName());
        // setting up a name for the complex attribute for the reference purpose
        if (subAttributeSchema.getName().equals(SCIMConstants.CommonSchemaConstants.VALUE)) {
            // (value,type) pair is considered as a primary key for each entry
            if (subAttributeValue != null) {
                Object subAttributeValueForType = jsonObject.opt(SCIMConstants.CommonSchemaConstants.TYPE);
                if (subAttributeValueForType != null) {
                    complexAttribute.setName(attributeSchema.getName() + "_" + subAttributeValue + "_" + subAttributeValueForType);
                } else {
                    complexAttribute.setName(attributeSchema.getName() + "_" + subAttributeValue + "_" + SCIMConstants.DEFAULT);
                }
            } else {
                Object subAttributeValueFortype = jsonObject.opt(SCIMConstants.CommonSchemaConstants.TYPE);
                if (subAttributeValueFortype != null) {
                    complexAttribute.setName(attributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + subAttributeValueFortype);
                } else {
                    complexAttribute.setName(attributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + SCIMConstants.DEFAULT);
                }
            }
        }
        if (subAttributeValue != null) {
            if (subAttributeSchema.getMultiValued()) {
                if (subAttributeValue instanceof JSONArray) {
                    MultiValuedAttribute multiValuedAttribute = buildPrimitiveMultiValuedAttribute(subAttributeSchema, (JSONArray) subAttributeValue);
                    // let the attribute factory to set the sub attribute of a complex
                    // attribute to detect schema violations.
                    multiValuedAttribute = (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
                    subAttributesMap.put(subAttributeSchema.getName(), multiValuedAttribute);
                } else {
                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                }
            } else {
                if (subAttributeValue instanceof String || subAttributeValue instanceof Boolean || subAttributeValue instanceof Integer) {
                    SimpleAttribute simpleAttribute = buildSimpleAttribute(subAttributeSchema, subAttributeValue);
                    // let the attribute factory to set the sub attribute of a complex
                    // attribute to detect schema violations.
                    simpleAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(subAttributeSchema, simpleAttribute);
                    subAttributesMap.put(subAttributeSchema.getName(), simpleAttribute);
                } else {
                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                }
            }
        }
    }
    complexAttribute.setSubAttributesList(subAttributesMap);
    return (ComplexAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, 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) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) JSONArray(org.json.JSONArray) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) 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 49 with CharonException

use of org.wso2.charon3.core.exceptions.CharonException 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 50 with CharonException

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

Aggregations

CharonException (org.wso2.charon3.core.exceptions.CharonException)46 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)44 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)34 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)32 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)31 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)28 Attribute (org.wso2.charon3.core.attributes.Attribute)27 HashMap (java.util.HashMap)22 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)19 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)17 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 UserManager (org.wso2.charon3.core.extensions.UserManager)15 JSONObject (org.json.JSONObject)14 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)14 JSONException (org.json.JSONException)13 User (org.wso2.charon3.core.objects.User)13 ApiOperation (io.swagger.annotations.ApiOperation)12