Search in sources :

Example 36 with ComplexAttribute

use of org.wso2.charon3.core.attributes.ComplexAttribute in project charon by wso2.

the class User method setGroup.

/*
     * set the associated groups of the user
     * @param type
     * @param value
     * @param display
     * @throws CharonException
     * @throws BadRequestException
     */
public void setGroup(String type, String value, String display) throws CharonException, BadRequestException {
    SimpleAttribute typeSimpleAttribute = null;
    SimpleAttribute valueSimpleAttribute = null;
    SimpleAttribute displaySimpleAttribute = null;
    ComplexAttribute complexAttribute = new ComplexAttribute();
    if (type != null) {
        typeSimpleAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.TYPE, type);
        typeSimpleAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMUserSchemaDefinition.GROUP_TYPE, typeSimpleAttribute);
        complexAttribute.setSubAttribute(typeSimpleAttribute);
    }
    if (value != null) {
        valueSimpleAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.VALUE, value);
        valueSimpleAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMUserSchemaDefinition.GROUP_VALUE, valueSimpleAttribute);
        complexAttribute.setSubAttribute(valueSimpleAttribute);
    }
    if (display != null) {
        displaySimpleAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.DISPLAY, display);
        displaySimpleAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMUserSchemaDefinition.GROUP_DISPLAY, displaySimpleAttribute);
        complexAttribute.setSubAttribute(displaySimpleAttribute);
    }
    if (complexAttribute.getSubAttributesList().size() != 0) {
        Object typeVal = SCIMConstants.DEFAULT;
        Object valueVal = SCIMConstants.DEFAULT;
        if (typeSimpleAttribute != null && typeSimpleAttribute.getValue() != null) {
            typeVal = typeSimpleAttribute.getValue();
        }
        if (valueSimpleAttribute != null && valueSimpleAttribute.getValue() != null) {
            valueVal = valueSimpleAttribute.getValue();
        }
        String complexAttributeName = SCIMConstants.UserSchemaConstants.GROUPS + "_" + valueVal + "_" + typeVal;
        complexAttribute.setName(complexAttributeName);
        DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMUserSchemaDefinition.GROUPS, complexAttribute);
        setGroup(complexAttribute);
    }
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute)

Example 37 with ComplexAttribute

use of org.wso2.charon3.core.attributes.ComplexAttribute in project charon by wso2.

the class User method setGroup.

private void setGroup(ComplexAttribute groupPropertiesAttribute) throws CharonException, BadRequestException {
    MultiValuedAttribute groupsAttribute;
    if (this.attributeList.containsKey(SCIMConstants.UserSchemaConstants.GROUPS)) {
        groupsAttribute = (MultiValuedAttribute) this.attributeList.get(SCIMConstants.UserSchemaConstants.GROUPS);
        groupsAttribute.setAttributeValue(groupPropertiesAttribute);
    } else {
        groupsAttribute = new MultiValuedAttribute(SCIMConstants.UserSchemaConstants.GROUPS);
        groupsAttribute.setAttributeValue(groupPropertiesAttribute);
        groupsAttribute = (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.SCIMUserSchemaDefinition.GROUPS, groupsAttribute);
        this.attributeList.put(SCIMConstants.UserSchemaConstants.GROUPS, groupsAttribute);
    }
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 38 with ComplexAttribute

use of org.wso2.charon3.core.attributes.ComplexAttribute in project charon by wso2.

the class PatchOperationUtil method doPatchReplaceOnResource.

/*
     *
     * @param oldResource
     * @param copyOfOldResource
     * @param schema
     * @param decoder
     * @param operation
     * @return
     * @throws CharonException
     */
private static AbstractSCIMObject doPatchReplaceOnResource(AbstractSCIMObject oldResource, AbstractSCIMObject copyOfOldResource, SCIMResourceTypeSchema schema, JSONDecoder decoder, PatchOperation operation) throws CharonException {
    try {
        AbstractSCIMObject attributeHoldingSCIMObject = decoder.decode(operation.getValues().toString(), schema);
        if (oldResource != null) {
            for (String attributeName : attributeHoldingSCIMObject.getAttributeList().keySet()) {
                Attribute oldAttribute = oldResource.getAttribute(attributeName);
                if (oldAttribute != null) {
                    // if the attribute is there, append it.
                    if (oldAttribute.getMultiValued()) {
                        // this is multivalued complex case.
                        MultiValuedAttribute attributeValue = (MultiValuedAttribute) attributeHoldingSCIMObject.getAttribute(attributeName);
                        if (oldAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE) || oldAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                            throw new BadRequestException("Immutable or Read-Only attributes can not be modified.", ResponseCodeConstants.MUTABILITY);
                        } else {
                            // delete the old attribute
                            oldResource.deleteAttribute(attributeName);
                            // replace with new attribute
                            oldResource.setAttribute(attributeValue);
                        }
                    } else if (oldAttribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                        // this is the complex attribute case.
                        Map<String, Attribute> subAttributeList = ((ComplexAttribute) attributeHoldingSCIMObject.getAttribute(attributeName)).getSubAttributesList();
                        for (Map.Entry<String, Attribute> subAttrib : subAttributeList.entrySet()) {
                            Attribute subAttribute = oldAttribute.getSubAttribute(subAttrib.getKey());
                            if (subAttribute != null) {
                                if (subAttribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                                    if (subAttribute.getMultiValued()) {
                                        // extension schema is the only one who reaches here.
                                        MultiValuedAttribute attributeSubValue = (MultiValuedAttribute) ((ComplexAttribute) attributeHoldingSCIMObject.getAttribute(attributeName)).getSubAttribute(subAttrib.getKey());
                                        if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE) || subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                                            throw new BadRequestException("Immutable or Read-Only attributes can not be modified.", ResponseCodeConstants.MUTABILITY);
                                        } else {
                                            // delete the old attribute
                                            ((ComplexAttribute) (oldAttribute)).removeSubAttribute(subAttribute.getName());
                                            // replace with new attribute
                                            ((ComplexAttribute) (oldAttribute)).setSubAttribute(attributeSubValue);
                                        }
                                    } else {
                                        // extension schema is the only one who reaches here.
                                        Map<String, Attribute> subSubAttributeList = ((ComplexAttribute) (attributeHoldingSCIMObject.getAttribute(attributeName).getSubAttribute(subAttrib.getKey()))).getSubAttributesList();
                                        for (Map.Entry<String, Attribute> subSubAttrb : subSubAttributeList.entrySet()) {
                                            Attribute subSubAttribute = oldAttribute.getSubAttribute(subAttrib.getKey()).getSubAttribute(subSubAttrb.getKey());
                                            if (subSubAttribute != null) {
                                                if (subSubAttribute.getMultiValued()) {
                                                    if (subSubAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE) || subSubAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                                                        throw new BadRequestException("Immutable or Read-Only attributes " + "can not be modified.", ResponseCodeConstants.MUTABILITY);
                                                    } else {
                                                        // delete the old attribute
                                                        ((ComplexAttribute) (oldAttribute.getSubAttribute(subAttrib.getKey()))).removeSubAttribute(subSubAttribute.getName());
                                                        // replace with new attribute
                                                        ((ComplexAttribute) (oldAttribute.getSubAttribute(subAttrib.getKey()))).setSubAttribute(subSubAttribute);
                                                    }
                                                } else {
                                                    ((SimpleAttribute) subSubAttribute).setValue(((SimpleAttribute) subSubAttrb.getValue()));
                                                }
                                            } else {
                                                ((ComplexAttribute) (subAttribute)).setSubAttribute(subSubAttrb.getValue());
                                            }
                                        }
                                    }
                                } else {
                                    if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE) || subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                                        throw new BadRequestException("Immutable or Read-Only " + "attributes can not be modified.", ResponseCodeConstants.MUTABILITY);
                                    } else {
                                        // delete the old attribute
                                        ((ComplexAttribute) (oldAttribute)).removeSubAttribute(subAttribute.getName());
                                        // replace with new attribute
                                        ((ComplexAttribute) (oldAttribute)).setSubAttribute(subAttributeList.get(subAttribute.getName()));
                                    }
                                }
                            } else {
                                // add the attribute
                                ((ComplexAttribute) oldAttribute).setSubAttribute(subAttrib.getValue());
                            }
                        }
                    } else {
                        if (oldAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE) || oldAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY)) {
                            throw new BadRequestException("Immutable or Read-Only attributes can not be modified.", ResponseCodeConstants.MUTABILITY);
                        } else {
                            // this is the simple attribute case.replace the value
                            ((SimpleAttribute) oldAttribute).setValue(((SimpleAttribute) attributeHoldingSCIMObject.getAttribute(oldAttribute.getName())).getValue());
                        }
                    }
                } else {
                    // add the attribute
                    oldResource.setAttribute(attributeHoldingSCIMObject.getAttributeList().get(attributeName));
                }
            }
            AbstractSCIMObject validatedResource = ServerSideValidator.validateUpdatedSCIMObject(copyOfOldResource, oldResource, schema);
            return validatedResource;
        } else {
            throw new CharonException("Error in getting the old resource.");
        }
    } catch (BadRequestException | CharonException e) {
        throw new CharonException("Error in performing the add operation", e);
    }
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) 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) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) CharonException(org.wso2.charon3.core.exceptions.CharonException) Map(java.util.Map) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 39 with ComplexAttribute

use of org.wso2.charon3.core.attributes.ComplexAttribute in project charon by wso2.

the class AbstractSCIMObject method setCreatedDate.

/*
     * set the created date and time of the resource
     *
     * @param createdDate
     */
public void setCreatedDate(Date createdDate) throws CharonException, BadRequestException {
    // create the created date attribute as defined in schema.
    SimpleAttribute createdDateAttribute = new SimpleAttribute(SCIMConstants.CommonSchemaConstants.CREATED, createdDate);
    createdDateAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.CREATED, createdDateAttribute);
    // check meta complex attribute already exist.
    if (getMetaAttribute() != null) {
        ComplexAttribute metaAttribute = getMetaAttribute();
        // check created date attribute already exist
        if (metaAttribute.isSubAttributeExist(createdDateAttribute.getName())) {
            // TODO:log info level log that created date already set and can't set again.
            String error = "Read only meta attribute is tried to modify";
            throw new CharonException(error);
        } else {
            metaAttribute.setSubAttribute(createdDateAttribute);
        }
    } else {
        // create meta attribute and set the sub attribute Created Date.
        createMetaAttribute();
        getMetaAttribute().setSubAttribute(createdDateAttribute);
    }
}
Also used : SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) CharonException(org.wso2.charon3.core.exceptions.CharonException)

Example 40 with ComplexAttribute

use of org.wso2.charon3.core.attributes.ComplexAttribute in project charon by wso2.

the class AbstractSCIMObject method toString.

public String toString() {
    String scimObjectStringValue = null;
    Map<String, Attribute> attributeList = this.getAttributeList();
    for (Attribute attribute : attributeList.values()) {
        if (attribute instanceof SimpleAttribute) {
            scimObjectStringValue = simpleAttributeToString(scimObjectStringValue, attribute);
        } else if (attribute instanceof ComplexAttribute) {
            ComplexAttribute complexAttribute = (ComplexAttribute) attribute;
            String complexValue = null;
            Map<String, Attribute> subAttributes = complexAttribute.getSubAttributesList();
            for (Attribute subAttribute : subAttributes.values()) {
                if (subAttribute instanceof SimpleAttribute) {
                    complexValue = simpleAttributeToString(complexValue, (Attribute) ((SimpleAttribute) subAttribute));
                } else if (subAttribute instanceof MultiValuedAttribute) {
                    if (!subAttribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                        String primitiveValue = null;
                        primitiveValue = multiValuedPrimitiveAttributeToString(((MultiValuedAttribute) subAttribute).getAttributePrimitiveValues(), subAttribute.getName());
                        if (complexValue == null) {
                            complexValue = primitiveValue;
                        } else {
                            complexValue = complexValue + "," + primitiveValue;
                        }
                    } else {
                        String multiValue = null;
                        List<Attribute> subAttributeList = ((MultiValuedAttribute) (subAttribute)).getAttributeValues();
                        for (Attribute subValue : subAttributeList) {
                            ComplexAttribute complexSubAttribute = (ComplexAttribute) subValue;
                            String complexSubValue = null;
                            Map<String, Attribute> subSubAttributes = complexSubAttribute.getSubAttributesList();
                            for (Attribute subSubAttribute : subSubAttributes.values()) {
                                if (subSubAttribute instanceof SimpleAttribute) {
                                    complexSubValue = simpleAttributeToString(complexSubValue, (Attribute) ((SimpleAttribute) subSubAttribute));
                                } else if (subSubAttribute instanceof MultiValuedAttribute) {
                                    complexSubValue = multiValuedPrimitiveAttributeToString(((MultiValuedAttribute) subSubAttribute).getAttributePrimitiveValues(), subSubAttribute.getName());
                                }
                            }
                            complexSubValue = "{" + complexSubValue + "}";
                            if (multiValue == null) {
                                multiValue = complexSubValue;
                            } else {
                                multiValue = multiValue + "," + complexSubValue;
                            }
                        }
                        if (scimObjectStringValue != null) {
                            scimObjectStringValue = scimObjectStringValue + "," + attribute.getName() + ":{" + subAttribute.getName() + ":[" + multiValue + "]}";
                        } else {
                            scimObjectStringValue = attribute.getName() + ":{" + subAttribute.getName() + ":[" + multiValue + "]}";
                        }
                    }
                } else if (subAttribute instanceof ComplexAttribute) {
                    ComplexAttribute complexSubAttribute = (ComplexAttribute) subAttribute;
                    String complexSubValue = null;
                    Map<String, Attribute> subSubAttributes = complexSubAttribute.getSubAttributesList();
                    for (Attribute subSubAttribute : subSubAttributes.values()) {
                        if (subSubAttribute instanceof SimpleAttribute) {
                            complexSubValue = simpleAttributeToString(complexSubValue, (Attribute) ((SimpleAttribute) subSubAttribute));
                        } else if (subSubAttribute instanceof MultiValuedAttribute) {
                            complexSubValue = multiValuedPrimitiveAttributeToString(((MultiValuedAttribute) subSubAttribute).getAttributePrimitiveValues(), subSubAttribute.getName());
                        }
                    }
                    complexSubValue = subAttribute.getName() + ":{" + complexSubValue + "}";
                    complexValue = complexSubValue;
                }
            }
            if (scimObjectStringValue == null) {
                scimObjectStringValue = attribute.getName() + ":{" + complexValue + "}";
            } else {
                scimObjectStringValue = scimObjectStringValue + "," + attribute.getName() + ":{" + complexValue + "}";
            }
        } else if (attribute instanceof MultiValuedAttribute) {
            MultiValuedAttribute multiValuedAttribute = (MultiValuedAttribute) attribute;
            if (multiValuedAttribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                String multiValue = null;
                List<Attribute> subAttributeList = multiValuedAttribute.getAttributeValues();
                for (Attribute subAttribute : subAttributeList) {
                    ComplexAttribute complexSubAttribute = (ComplexAttribute) subAttribute;
                    String complexSubValue = null;
                    Map<String, Attribute> subSubAttributes = complexSubAttribute.getSubAttributesList();
                    for (Attribute subSubAttribute : subSubAttributes.values()) {
                        if (subSubAttribute instanceof SimpleAttribute) {
                            complexSubValue = simpleAttributeToString(complexSubValue, (Attribute) ((SimpleAttribute) subSubAttribute));
                        } else if (subSubAttribute instanceof MultiValuedAttribute) {
                            complexSubValue = multiValuedPrimitiveAttributeToString(((MultiValuedAttribute) subSubAttribute).getAttributePrimitiveValues(), subSubAttribute.getName());
                        }
                    }
                    complexSubValue = "{" + complexSubValue + "}";
                    if (multiValue == null) {
                        multiValue = complexSubValue;
                    } else {
                        multiValue = multiValue + "," + complexSubValue;
                    }
                }
                if (scimObjectStringValue != null) {
                    scimObjectStringValue = scimObjectStringValue + "," + attribute.getName() + ":[" + multiValue + "]";
                } else {
                    scimObjectStringValue = attribute.getName() + ":[" + multiValue + "]";
                }
            } else {
                List<Object> primitiveValueList = multiValuedAttribute.getAttributePrimitiveValues();
                String complexValue = null;
                complexValue = multiValuedPrimitiveAttributeToString(primitiveValueList, multiValuedAttribute.getName());
                if (scimObjectStringValue == null) {
                    scimObjectStringValue = complexValue;
                } else {
                    scimObjectStringValue = scimObjectStringValue + "," + complexValue;
                }
            }
        }
    }
    return scimObjectStringValue;
}
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) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) HashMap(java.util.HashMap) Map(java.util.Map) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Aggregations

ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)37 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)35 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)32 Attribute (org.wso2.charon3.core.attributes.Attribute)30 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)17 JSONObject (org.json.JSONObject)12 ArrayList (java.util.ArrayList)9 JSONArray (org.json.JSONArray)9 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)8 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)8 List (java.util.List)7 Map (java.util.Map)7 CharonException (org.wso2.charon3.core.exceptions.CharonException)7 JSONException (org.json.JSONException)6 AbstractAttribute (org.wso2.charon3.core.attributes.AbstractAttribute)6 HashMap (java.util.HashMap)4 JSONTokener (org.json.JSONTokener)4 SCIMObject (org.wso2.charon3.core.objects.SCIMObject)4 SCIMAttributeSchema (org.wso2.charon3.core.schema.SCIMAttributeSchema)4 Iterator (java.util.Iterator)2