Search in sources :

Example 76 with BadRequestException

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

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

the class AbstractValidator method checkForSameValues.

/*
     * check for same values in a simple singular attributes or multivalued primitive type attributes
     *
     * @param oldAttributeList
     * @param newAttributeList
     * @param attributeSchema
     * @throws BadRequestException
     */
private static void checkForSameValues(Map<String, Attribute> oldAttributeList, Map<String, Attribute> newAttributeList, AttributeSchema attributeSchema) throws BadRequestException {
    Attribute newTemporyAttribute = newAttributeList.get(attributeSchema.getName());
    Attribute oldTemporyAttribute = oldAttributeList.get(attributeSchema.getName());
    if (newTemporyAttribute instanceof SimpleAttribute) {
        if (!((((SimpleAttribute) newTemporyAttribute).getValue()).equals(((SimpleAttribute) oldTemporyAttribute).getValue()))) {
            throw new BadRequestException(ResponseCodeConstants.MUTABILITY);
        }
    } else if (newTemporyAttribute instanceof MultiValuedAttribute && !attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
        if (!checkListEquality(((MultiValuedAttribute) newTemporyAttribute).getAttributePrimitiveValues(), ((MultiValuedAttribute) oldTemporyAttribute).getAttributePrimitiveValues())) {
            throw new BadRequestException(ResponseCodeConstants.MUTABILITY);
        }
    }
}
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) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 78 with BadRequestException

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

the class AbstractSCIMObject method setLocation.

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

Example 79 with BadRequestException

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

the class AbstractSCIMObject method setId.

/*
     * Set a value for the id attribute. If attribute not already created in the resource,
     * create attribute and set the value.
     * Unique identifier for the SCIM Resource as defined by the Service Provider
     * This is read-only. So can only set once.
     *
     * @param id Unique identifier for the SCIM Resource as defined by the Service Provider.
     * @throws CharonException
     * @throws BadRequestException
     */
public void setId(String id) throws CharonException, BadRequestException {
    if (isAttributeExist(SCIMConstants.CommonSchemaConstants.ID)) {
        String error = "Read only attribute is trying to be modified";
        throw new CharonException(error);
    } else {
        SimpleAttribute idAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.ID, id);
        idAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.ID, idAttribute);
        this.setAttribute(idAttribute);
    }
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) CharonException(org.wso2.charon3.core.exceptions.CharonException)

Example 80 with BadRequestException

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

the class Group method setDisplayName.

/*
     * set the display name of the group
     * @param displayName
     * @throws CharonException
     * @throws BadRequestException
     */
public void setDisplayName(String displayName) throws CharonException, BadRequestException {
    if (this.isAttributeExist(SCIMConstants.GroupSchemaConstants.DISPLAY_NAME)) {
        ((SimpleAttribute) this.attributeList.get(SCIMConstants.GroupSchemaConstants.DISPLAY_NAME)).updateValue(displayName);
    } else {
        SimpleAttribute displayAttribute = new SimpleAttribute(SCIMConstants.GroupSchemaConstants.DISPLAY_NAME, displayName);
        displayAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMGroupSchemaDefinition.DISPLAY_NAME, displayAttribute);
        this.attributeList.put(SCIMConstants.GroupSchemaConstants.DISPLAY_NAME, displayAttribute);
    }
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)63 CharonException (org.wso2.charon3.core.exceptions.CharonException)31 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)30 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)27 HashMap (java.util.HashMap)23 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)23 Attribute (org.wso2.charon3.core.attributes.Attribute)20 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 JSONException (org.json.JSONException)17 JSONObject (org.json.JSONObject)17 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)16 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)14 User (org.wso2.charon3.core.objects.User)12 JSONArray (org.json.JSONArray)11 ArrayList (java.util.ArrayList)9