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);
}
}
}
}
}
}
}
}
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);
}
}
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());
}
}
}
}
}
}
}
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);
}
}
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);
}
Aggregations