Search in sources :

Example 36 with CharonException

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

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

Example 38 with CharonException

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

Example 39 with CharonException

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

the class AbstractSCIMObject method deleteSubSubAttribute.

/*
     * This deletion method is only applicable for extension schema
     * Deleting a sub attribute of complex attribute is the responsibility of an attribute holder.
     *
     * @param grandParentAttribute
     * @param parentAttribute
     * @param childAttribute
     */
public void deleteSubSubAttribute(String childAttribute, String parentAttribute, String grandParentAttribute) throws CharonException {
    if (attributeList.containsKey(grandParentAttribute)) {
        ComplexAttribute grandParent = (ComplexAttribute) attributeList.get(grandParentAttribute);
        Attribute parent = ((ComplexAttribute) grandParent).getSubAttribute(parentAttribute);
        ((ComplexAttribute) (parent)).removeSubAttribute(childAttribute);
    }
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) 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)

Example 40 with CharonException

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

the class AbstractSCIMObject method setResourceType.

/*
     * set the resourceType of the meta attribute
     *
     * @param resourceType
     */
public void setResourceType(String resourceType) throws BadRequestException, CharonException {
    // create the resourceType attribute as defined in schema.
    SimpleAttribute resourceTypeAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.RESOURCE_TYPE, new SimpleAttribute(SCIMConstants.CommonSchemaConstants.RESOURCE_TYPE, resourceType));
    // check meta complex attribute already exist.
    if (getMetaAttribute() != null) {
        ComplexAttribute metaAttribute = getMetaAttribute();
        // check version attribute already exist
        if (metaAttribute.isSubAttributeExist(resourceTypeAttribute.getName())) {
            String error = "Read only attribute is tried to modify";
            throw new CharonException(error);
        } else {
            metaAttribute.setSubAttribute(resourceTypeAttribute);
        }
    } else {
        // create meta attribute and set the sub attribute.
        createMetaAttribute();
        getMetaAttribute().setSubAttribute(resourceTypeAttribute);
    }
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) CharonException(org.wso2.charon3.core.exceptions.CharonException)

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