Search in sources :

Example 21 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject in project charon by wso2.

the class JSONDecoder method decode.

public AbstractSCIMObject decode(String scimResourceString, SCIMResourceTypeSchema schema) throws CharonException, BadRequestException {
    try {
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        AbstractSCIMObject scimObject = null;
        if (schema.getSchemasList().contains(SCIMConstants.GROUP_CORE_SCHEMA_URI)) {
            scimObject = (AbstractSCIMObject) decodeResource(decodedJsonObj.toString(), schema, new Group());
        } else {
            scimObject = (AbstractSCIMObject) decodeResource(decodedJsonObj.toString(), schema, new User());
        }
        return scimObject;
    } catch (JSONException | InternalErrorException | CharonException e) {
        throw new CharonException("Error in decoding the request", e);
    } catch (BadRequestException e) {
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
}
Also used : JSONTokener(org.json.JSONTokener) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) Group(org.wso2.charon3.core.objects.Group) User(org.wso2.charon3.core.objects.User) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) CharonException(org.wso2.charon3.core.exceptions.CharonException)

Example 22 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject in project charon by wso2.

the class AbstractValidator method validateReturnedAttributes.

/*
     * This method is to remove any defined and requested attributes and include
     * requested attributes if not they have been removed.
     *
     * @param scimObject
     * @param requestedAttributes
     * @param requestedExcludingAttributes
     */
public static void validateReturnedAttributes(AbstractSCIMObject scimObject, String requestedAttributes, String requestedExcludingAttributes) throws CharonException {
    List<String> requestedAttributesList = null;
    List<String> requestedExcludingAttributesList = null;
    if (requestedAttributes != null) {
        // make a list from the comma separated requestedAttributes
        requestedAttributesList = Arrays.asList(requestedAttributes.split(","));
    }
    if (requestedExcludingAttributes != null) {
        // make a list from the comma separated requestedExcludingAttributes
        requestedExcludingAttributesList = Arrays.asList(requestedExcludingAttributes.split(","));
    }
    Map<String, Attribute> attributeList = scimObject.getAttributeList();
    ArrayList<Attribute> attributeTemporyList = new ArrayList<Attribute>();
    for (Attribute attribute : attributeList.values()) {
        attributeTemporyList.add(attribute);
    }
    for (Attribute attribute : attributeTemporyList) {
        // check for never/request attributes.
        if (attribute.getReturned().equals(SCIMDefinitions.Returned.NEVER)) {
            scimObject.deleteAttribute(attribute.getName());
        }
        // If so return it.
        if (requestedAttributes == null && requestedExcludingAttributes == null) {
            if (attribute.getReturned().equals(SCIMDefinitions.Returned.REQUEST)) {
                scimObject.deleteAttribute(attribute.getName());
            }
        } else {
            // A request should only contains either attributes or exclude attribute params. Not both
            if (requestedAttributes != null) {
                // and add only the requested attributes
                if ((attribute.getReturned().equals(SCIMDefinitions.Returned.DEFAULT) || attribute.getReturned().equals(SCIMDefinitions.Returned.REQUEST)) && (!requestedAttributesList.contains(attribute.getName()) && !isSubAttributeExistsInList(requestedAttributesList, attribute))) {
                    scimObject.deleteAttribute(attribute.getName());
                }
            } else if (requestedExcludingAttributes != null) {
                // removing attributes which has returned as request. This is because no request is made
                if (attribute.getReturned().equals(SCIMDefinitions.Returned.REQUEST)) {
                    scimObject.deleteAttribute(attribute.getName());
                }
                // removed from the default set of attributes
                if ((attribute.getReturned().equals(SCIMDefinitions.Returned.DEFAULT)) && requestedExcludingAttributesList.contains(attribute.getName())) {
                    scimObject.deleteAttribute(attribute.getName());
                }
            }
        }
        // check the same for sub attributes
        if (attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            if (attribute.getMultiValued()) {
                List<Attribute> valuesList = ((MultiValuedAttribute) attribute).getAttributeValues();
                for (Attribute subAttribute : valuesList) {
                    Map<String, Attribute> valuesSubAttributeList = ((ComplexAttribute) subAttribute).getSubAttributesList();
                    ArrayList<Attribute> valuesSubAttributeTemporyList = new ArrayList<Attribute>();
                    // hence need to traverse on a copy
                    for (Attribute subSimpleAttribute : valuesSubAttributeList.values()) {
                        valuesSubAttributeTemporyList.add(subSimpleAttribute);
                    }
                    for (Attribute subSimpleAttribute : valuesSubAttributeTemporyList) {
                        removeValuesSubAttributeOnReturn(subSimpleAttribute, subAttribute, attribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                    }
                }
            } else {
                Map<String, Attribute> subAttributeList = ((ComplexAttribute) attribute).getSubAttributesList();
                ArrayList<Attribute> subAttributeTemporyList = new ArrayList<Attribute>();
                for (Attribute subAttribute : subAttributeList.values()) {
                    subAttributeTemporyList.add(subAttribute);
                }
                for (Attribute subAttribute : subAttributeTemporyList) {
                    if (subAttribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                        // this applicable for extension schema only
                        if (subAttribute.getMultiValued()) {
                            List<Attribute> valuesList = ((MultiValuedAttribute) subAttribute).getAttributeValues();
                            for (Attribute subSubValue : valuesList) {
                                Map<String, Attribute> subValuesSubAttributeList = ((ComplexAttribute) subSubValue).getSubAttributesList();
                                ArrayList<Attribute> valuesSubSubAttributeTemporyList = new ArrayList<Attribute>();
                                // hence need to traverse on a copy
                                for (Attribute subSubSimpleAttribute : subValuesSubAttributeList.values()) {
                                    valuesSubSubAttributeTemporyList.add(subSubSimpleAttribute);
                                }
                                for (Attribute subSubSimpleAttribute : valuesSubSubAttributeTemporyList) {
                                    removeValuesSubSubAttributeOnReturn(attribute, subAttribute, subSubValue, subSubSimpleAttribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                                }
                            }
                        } else {
                            ArrayList<Attribute> subSubAttributeTemporyList = new ArrayList<Attribute>();
                            Map<String, Attribute> subSubAttributeList = ((ComplexAttribute) subAttribute).getSubAttributesList();
                            for (Attribute subSubAttribute : subSubAttributeList.values()) {
                                subSubAttributeTemporyList.add(subSubAttribute);
                            }
                            for (Attribute subSubAttribute : subSubAttributeTemporyList) {
                                removeSubSubAttributesOnReturn(attribute, subAttribute, subSubAttribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                            }
                        }
                        removeSubAttributesOnReturn(subAttribute, attribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                    } else {
                        removeSubAttributesOnReturn(subAttribute, attribute, requestedAttributes, requestedExcludingAttributes, requestedAttributesList, requestedExcludingAttributesList, scimObject);
                    }
                }
            }
        }
    }
}
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) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 23 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject in project charon by wso2.

the class ServerSideValidator method validateUpdatedSCIMObject.

/*
     * Perform validation on SCIM Object update on service provider side
     *
     * @param oldObject
     * @param newObject
     * @param resourceSchema
     * @return
     * @throws CharonException
     */
public static AbstractSCIMObject validateUpdatedSCIMObject(AbstractSCIMObject oldObject, AbstractSCIMObject newObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException {
    AbstractSCIMObject validatedObject = null;
    if (newObject instanceof User) {
        // set display names for complex multivalued attributes
        setDisplayNameInComplexMultiValuedAttributes(newObject, resourceSchema);
    }
    // check for read only and immutable attributes
    validatedObject = checkIfReadOnlyAndImmutableAttributesModified(oldObject, newObject, resourceSchema);
    // copy meta attribute from old to new
    validatedObject.setAttribute(oldObject.getAttribute(SCIMConstants.CommonSchemaConstants.META));
    // copy id attribute to new group object
    validatedObject.setAttribute(oldObject.getAttribute(SCIMConstants.CommonSchemaConstants.ID));
    // edit last modified date
    Date date = new Date();
    validatedObject.setLastModified(date);
    // check for required attributes.
    validateSCIMObjectForRequiredAttributes(newObject, resourceSchema);
    // check for schema list
    validateSchemaList(validatedObject, resourceSchema);
    return validatedObject;
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) User(org.wso2.charon3.core.objects.User) Date(java.util.Date)

Example 24 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject in project charon by wso2.

the class PatchOperationUtil method doPatchReplaceOnPathWithFilters.

/*
     * This method is to do patch replace for level three attributes with a filter and path value present.
     * @param oldResource
     * @param copyOfOldResource
     * @param schema
     * @param decoder
     * @param operation
     * @param parts
     * @throws NotImplementedException
     * @throws BadRequestException
     * @throws CharonException
     * @throws JSONException
     * @throws InternalErrorException
     */
private static void doPatchReplaceOnPathWithFilters(AbstractSCIMObject oldResource, SCIMResourceTypeSchema schema, JSONDecoder decoder, PatchOperation operation, String[] parts) throws NotImplementedException, BadRequestException, CharonException, JSONException, InternalErrorException {
    if (parts.length != 1) {
        // currently we only support simple filters here.
        String[] filterParts = parts[1].split(" ");
        ExpressionNode expressionNode = new ExpressionNode();
        expressionNode.setAttributeValue(filterParts[0]);
        expressionNode.setOperation(filterParts[1]);
        expressionNode.setValue(filterParts[2]);
        if (expressionNode.getOperation().equalsIgnoreCase((SCIMConstants.OperationalConstants.EQ).trim())) {
            if (parts.length == 3) {
                parts[0] = parts[0] + parts[2];
            }
            String[] attributeParts = parts[0].split("[\\.]");
            if (attributeParts.length == 1) {
                doPatchReplaceWithFiltersForLevelOne(oldResource, attributeParts, expressionNode, operation, schema, decoder);
            } else if (attributeParts.length == 2) {
                doPatchReplaceWithFiltersForLevelTwo(oldResource, attributeParts, expressionNode, operation, schema, decoder);
            } else if (attributeParts.length == 3) {
                doPatchReplaceWithFiltersForLevelThree(oldResource, attributeParts, expressionNode, operation, schema, decoder);
            }
        } else {
            throw new NotImplementedException("Only Eq filter is supported");
        }
    }
}
Also used : ExpressionNode(org.wso2.charon3.core.utils.codeutils.ExpressionNode) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException)

Example 25 with AbstractSCIMObject

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

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)18 Attribute (org.wso2.charon3.core.attributes.Attribute)16 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)16 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)16 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)15 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)11 JSONException (org.json.JSONException)8 JSONObject (org.json.JSONObject)7 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)7 JSONArray (org.json.JSONArray)6 Map (java.util.Map)5 JSONTokener (org.json.JSONTokener)5 AbstractAttribute (org.wso2.charon3.core.attributes.AbstractAttribute)4 CharonException (org.wso2.charon3.core.exceptions.CharonException)4 List (java.util.List)3 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)3 User (org.wso2.charon3.core.objects.User)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2