Search in sources :

Example 31 with CharonException

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

the class AbstractValidator method setDisplayNameInComplexMultiValuedAttributes.

/*
     * This method is basically for adding display sub attribute to multivalued attributes
     * which has 'display' as a sub attribute in the respective attribute schema
     *
     * @param scimObject
     * @param resourceSchema
     * @throws CharonException
     * @throws BadRequestException
     */
protected static void setDisplayNameInComplexMultiValuedAttributes(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException {
    Map<String, Attribute> attributeList = scimObject.getAttributeList();
    ArrayList<AttributeSchema> attributeSchemaList = resourceSchema.getAttributesList();
    for (AttributeSchema attributeSchema : attributeSchemaList) {
        if (attributeSchema.getMultiValued() && attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            if (attributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY) != null) {
                if (attributeList.containsKey(attributeSchema.getName())) {
                    Attribute multiValuedAttribute = attributeList.get(attributeSchema.getName());
                    setDisplayNameInComplexMultiValuedSubAttributes(multiValuedAttribute, attributeSchema);
                }
            }
        } else if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            // this is only valid for extension schema
            List<SCIMAttributeSchema> subAttributeSchemaList = attributeSchema.getSubAttributeSchemas();
            for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                if (subAttributeSchema.getMultiValued() && subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                    if (subAttributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY) != null) {
                        Attribute extensionAttribute = attributeList.get(attributeSchema.getName());
                        if (extensionAttribute != null) {
                            if ((((ComplexAttribute) extensionAttribute).getSubAttribute(subAttributeSchema.getName())) != null) {
                                Attribute multiValuedAttribute = (attributeList.get(attributeSchema.getName())).getSubAttribute(subAttributeSchema.getName());
                                setDisplayNameInComplexMultiValuedSubAttributes(multiValuedAttribute, 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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 32 with CharonException

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

the class AbstractValidator method removeAnyReadOnlyAttributes.

/*
     * Check for readonlyAttributes and remove them if they have been modified. - (create method)
     *
     * @param scimObject
     * @param resourceSchema
     * @throws CharonException
     */
public static void removeAnyReadOnlyAttributes(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException {
    // No need to check for immutable as immutable attributes can be defined at resource creation
    // get attributes from schema.
    List<AttributeSchema> attributeSchemaList = resourceSchema.getAttributesList();
    // get attribute list from scim object.
    Map<String, Attribute> attributeList = scimObject.getAttributeList();
    for (AttributeSchema attributeSchema : attributeSchemaList) {
        // check for read-only attributes.
        if (attributeSchema.getMutability() == SCIMDefinitions.Mutability.READ_ONLY) {
            if (attributeList.containsKey(attributeSchema.getName())) {
                String error = "Read only attribute: " + attributeSchema.getName() + " is set from consumer in the SCIM Object. " + "Removing it.";
                logger.debug(error);
                scimObject.deleteAttribute(attributeSchema.getName());
            }
        }
        // check for readonly sub attributes.
        AbstractAttribute attribute = (AbstractAttribute) attributeList.get(attributeSchema.getName());
        removeAnyReadOnlySubAttributes(attribute, attributeSchema);
    }
}
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) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute)

Example 33 with CharonException

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

the class AbstractValidator method checkIfReadOnlyAndImmutableSubAttributesModified.

/*
     * check for read only and immutable sub attributes which has been modified on update request
     *
     * @param newAttributeList
     * @param oldAttributeList
     * @param attributeSchema
     * @throws BadRequestException
     * @throws CharonException
     */
private static void checkIfReadOnlyAndImmutableSubAttributesModified(Map<String, Attribute> newAttributeList, Map<String, Attribute> oldAttributeList, AttributeSchema attributeSchema) throws BadRequestException, CharonException {
    // check for sub attributes.
    AbstractAttribute newAttribute = (AbstractAttribute) newAttributeList.get(attributeSchema.getName());
    AbstractAttribute oldAttribute = (AbstractAttribute) oldAttributeList.get(attributeSchema.getName());
    List<SCIMAttributeSchema> subAttributeSchemaList = attributeSchema.getSubAttributeSchemas();
    if (subAttributeSchemaList != null) {
        if (SCIMResourceSchemaManager.getInstance().getExtensionName() != null) {
            if (attributeSchema.getName().equals(SCIMResourceSchemaManager.getInstance().getExtensionName())) {
                checkIfReadOnlyAndImmutableExtensionAttributesModified(subAttributeSchemaList, newAttribute, oldAttribute);
            }
        }
        if (newAttribute != null && oldAttribute != null) {
            if (attributeSchema.getMultiValued()) {
                // this is complex multivalued case
                List<Attribute> newSubValuesList = ((MultiValuedAttribute) newAttribute).getAttributeValues();
                List<Attribute> oldSubValuesList = ((MultiValuedAttribute) oldAttribute).getAttributeValues();
                // if size aren't equal, they do not preserver immutable quality
                if (newSubValuesList.size() != oldSubValuesList.size() && attributeSchema.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                    throw new BadRequestException(ResponseCodeConstants.MUTABILITY);
                }
                // no need to check sub attributes of sub values separately for equality, stop at the sub value level
                for (Attribute subValue : newSubValuesList) {
                    if (!isListContains((((ComplexAttribute) subValue).getName()), oldSubValuesList) && attributeSchema.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                        throw new BadRequestException(ResponseCodeConstants.MUTABILITY);
                    }
                }
            } else {
                // A complex attribute itself can not be immutable if it's sub variables are not immutable
                checkForReadOnlyAndImmutableInComplexAttributes(newAttribute, oldAttribute, subAttributeSchemaList);
            }
        } else if (newAttribute == null && oldAttribute != null) {
            if (attributeSchema.getMultiValued()) {
                List<Attribute> oldSubValuesList = ((MultiValuedAttribute) oldAttribute).getAttributeValues();
                Attribute clonedMultiValuedAttribute = (Attribute) CopyUtil.deepCopy(oldAttribute);
                clonedMultiValuedAttribute.deleteSubAttributes();
                for (Attribute subValue : oldSubValuesList) {
                    Attribute clonedSubValue = (Attribute) CopyUtil.deepCopy(subValue);
                    clonedSubValue.deleteSubAttributes();
                    for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                        if (subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                            if (((ComplexAttribute) subValue).isSubAttributeExist(subAttributeSchema.getName())) {
                                Attribute clonedSubValuesAttribute = (Attribute) CopyUtil.deepCopy(((ComplexAttribute) subValue).getSubAttribute(subAttributeSchema.getName()));
                                ((ComplexAttribute) clonedSubValue).setSubAttribute(clonedSubValuesAttribute);
                            }
                        }
                    }
                    ((MultiValuedAttribute) (clonedMultiValuedAttribute)).setAttributeValue(clonedSubValue);
                }
            } else {
                Map<String, Attribute> oldSubAttributeList = ((ComplexAttribute) (oldAttribute)).getSubAttributesList();
                Attribute clonedAttribute = (Attribute) CopyUtil.deepCopy(oldAttribute);
                clonedAttribute.deleteSubAttributes();
                for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                    if (subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                        if (oldSubAttributeList.containsKey(subAttributeSchema.getName())) {
                            ((ComplexAttribute) (clonedAttribute)).setSubAttribute((Attribute) CopyUtil.deepCopy(oldSubAttributeList.get(subAttributeSchema.getName())));
                        }
                    }
                }
                newAttributeList.put(clonedAttribute.getName(), clonedAttribute);
            }
        } else if (newAttribute != null && oldAttribute == null) {
            if (attributeSchema.getMultiValued()) {
                if (attributeSchema.getMultiValued()) {
                    List<Attribute> newSubValuesList = ((MultiValuedAttribute) newAttribute).getAttributeValues();
                    for (Attribute subValue : newSubValuesList) {
                        for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                            if (subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                                ((ComplexAttribute) (subValue)).removeSubAttribute(subAttributeSchema.getName());
                            }
                        }
                    }
                }
            } else {
                // this is complex attribute case
                Map<String, Attribute> newSubAttributeList = ((ComplexAttribute) (newAttribute)).getSubAttributesList();
                for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
                    if (subAttributeSchema.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                        if (newSubAttributeList.containsKey(subAttributeSchema.getName())) {
                            String error = "Read only attribute: " + subAttributeSchema.getName() + " is set from consumer in the SCIM Object. Removing it.";
                            logger.debug(error);
                            ((ComplexAttribute) newAttribute).removeSubAttribute(subAttributeSchema.getName());
                        }
                    }
                }
            }
        }
    }
}
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) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 34 with CharonException

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

the class SCIMUserSchemaExtensionBuilder method readConfiguration.

/*
     * This method reads configuration file and stores in the memory as an
     * configuration map
     *
     * @param configFilePath
     * @throws CharonException
     */
private void readConfiguration(String configFilePath) throws CharonException {
    File provisioningConfig = new File(configFilePath);
    try {
        InputStream inputStream = new FileInputStream(provisioningConfig);
        // Scanner scanner = new Scanner(new FileInputStream(provisioningConfig));
        Scanner scanner = new Scanner(inputStream, "utf-8").useDelimiter("\\A");
        String jsonString = scanner.hasNext() ? scanner.next() : "";
        JSONArray attributeConfigArray = new JSONArray(jsonString);
        for (int index = 0; index < attributeConfigArray.length(); ++index) {
            JSONObject attributeConfig = attributeConfigArray.getJSONObject(index);
            ExtensionAttributeSchemaConfig attrubteConfig = new ExtensionAttributeSchemaConfig(attributeConfig);
            extensionConfig.put(attrubteConfig.getName(), attrubteConfig);
            /**
             * NOTE: Assume last config is the root config
             */
            if (index == attributeConfigArray.length() - 1) {
                extensionRootAttributeName = attrubteConfig.getName();
            }
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        throw new CharonException(SCIMConfigConstants.SCIM_SCHEMA_EXTENSION_CONFIG + " file not found!", e);
    } catch (JSONException e) {
        throw new CharonException("Error while parsing " + SCIMConfigConstants.SCIM_SCHEMA_EXTENSION_CONFIG + " file!", e);
    } catch (IOException e) {
        throw new CharonException("Error while closing " + SCIMConfigConstants.SCIM_SCHEMA_EXTENSION_CONFIG + " file!", e);
    }
}
Also used : Scanner(java.util.Scanner) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) FileNotFoundException(java.io.FileNotFoundException) JSONException(org.json.JSONException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) JSONObject(org.json.JSONObject) CharonException(org.wso2.charon3.core.exceptions.CharonException) File(java.io.File)

Example 35 with CharonException

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

the class JSONDecoder method buildSimpleAttribute.

/*
     * Return a simple attribute with the user defined value included and necessary attribute characteristics set
     *
     * @param attributeSchema - Attribute schema
     * @param attributeValue  - value for the attribute
     * @return SimpleAttribute
     */
public SimpleAttribute buildSimpleAttribute(AttributeSchema attributeSchema, Object attributeValue) throws CharonException, BadRequestException {
    Object attributeValueObject = AttributeUtil.getAttributeValueFromString(attributeValue, attributeSchema.getType());
    SimpleAttribute simpleAttribute = new SimpleAttribute(attributeSchema.getName(), attributeValueObject);
    return (SimpleAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, simpleAttribute);
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject)

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