Search in sources :

Example 16 with ExpressionNode

use of org.wso2.charon3.core.utils.codeutils.ExpressionNode in project charon by wso2.

the class PatchOperationUtil method doPatchRemoveWithFiltersForLevelThree.

/*
     *
     * @param oldResource
     * @param attributeParts
     * @param expressionNode
     * @return
     * @throws BadRequestException
     * @throws CharonException
     */
private static AbstractSCIMObject doPatchRemoveWithFiltersForLevelThree(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode) throws BadRequestException, CharonException {
    Attribute attribute = oldResource.getAttribute(attributeParts[0]);
    if (attribute != null) {
        Attribute subAttribute = attribute.getSubAttribute(attributeParts[1]);
        if (subAttribute != null) {
            if (subAttribute.getMultiValued()) {
                List<Attribute> subValues = ((MultiValuedAttribute) subAttribute).getAttributeValues();
                if (subValues != null) {
                    for (Attribute subValue : subValues) {
                        Map<String, Attribute> subSubAttributes = ((ComplexAttribute) subValue).getSubAttributesList();
                        // this map is to avoid concurrent modification exception.
                        Map<String, Attribute> tempSubSubAttributes = (Map<String, Attribute>) CopyUtil.deepCopy(subSubAttributes);
                        for (Iterator<Attribute> iterator = tempSubSubAttributes.values().iterator(); iterator.hasNext(); ) {
                            Attribute subSubAttribute = iterator.next();
                            if (subSubAttribute.getName().equals(expressionNode.getAttributeValue())) {
                                Attribute removingAttribute = subSubAttributes.get(attributeParts[2]);
                                if (removingAttribute == null) {
                                    throw new BadRequestException("No such sub attribute with the name : " + attributeParts[2] + " " + "within the attribute " + attributeParts[1], ResponseCodeConstants.INVALID_PATH);
                                }
                                if (removingAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || removingAttribute.getRequired().equals(true)) {
                                    throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                } else {
                                    ((ComplexAttribute) subValue).removeSubAttribute(removingAttribute.getName());
                                }
                            }
                        }
                    }
                    if (subValues.size() == 0) {
                        // if the attribute has no values, make it unassigned
                        ((ComplexAttribute) attribute).removeSubAttribute(subAttribute.getName());
                    }
                }
            } else {
                throw new BadRequestException("Attribute : " + attributeParts[1] + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
            }
        } else {
            throw new BadRequestException("No such sub attribute with the name : " + attributeParts[1] + " " + "within the attribute " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
        }
    } else {
        throw new BadRequestException("No such attribute with the name : " + attributeParts[0] + " " + "in the current resource", ResponseCodeConstants.INVALID_PATH);
    }
    return oldResource;
}
Also used : 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) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) Map(java.util.Map) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 17 with ExpressionNode

use of org.wso2.charon3.core.utils.codeutils.ExpressionNode in project charon by wso2.

the class PatchOperationUtil method doPatchReplaceWithFiltersForLevelOne.

/*
     * This method is to do patch replace for level one attributes with a filter present.
     * @param oldResource
     * @param attributeParts
     * @param expressionNode
     * @param operation
     * @param schema
     * @param decoder
     * @return
     * @throws BadRequestException
     * @throws CharonException
     * @throws JSONException
     * @throws InternalErrorException
     */
private static AbstractSCIMObject doPatchReplaceWithFiltersForLevelOne(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode, PatchOperation operation, SCIMResourceTypeSchema schema, JSONDecoder decoder) throws BadRequestException, CharonException, JSONException, InternalErrorException {
    Attribute attribute = oldResource.getAttribute(attributeParts[0]);
    boolean isValueFound = false;
    if (attribute != null) {
        if (!attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            // this is multivalued primitive case
            if (attribute.getMultiValued()) {
                List<Object> valuesList = ((MultiValuedAttribute) (attribute)).getAttributePrimitiveValues();
                for (Iterator<Object> iterator = valuesList.iterator(); iterator.hasNext(); ) {
                    Object item = iterator.next();
                    // we only support "EQ" filter
                    if (item.equals(expressionNode.getValue())) {
                        if (attribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || attribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                            throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                        } else {
                            iterator.remove();
                            isValueFound = true;
                        }
                    }
                }
                if (!isValueFound) {
                    throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
                }
                valuesList.add(operation.getValues());
            } else {
                throw new BadRequestException("Attribute : " + expressionNode.getAttributeValue() + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
            }
        } else {
            if (attribute.getMultiValued()) {
                // this is for paths value as 'emails[value EQ vindula@wso2.com]'
                // this is multivalued complex case
                List<Attribute> subValues = ((MultiValuedAttribute) (attribute)).getAttributeValues();
                if (subValues != null) {
                    for (Iterator<Attribute> subValueIterator = subValues.iterator(); subValueIterator.hasNext(); ) {
                        Attribute subValue = subValueIterator.next();
                        Map<String, Attribute> subValuesSubAttribute = ((ComplexAttribute) subValue).getSubAttributesList();
                        for (Iterator<Attribute> iterator = subValuesSubAttribute.values().iterator(); iterator.hasNext(); ) {
                            Attribute subAttribute = iterator.next();
                            if (subAttribute.getName().equals(expressionNode.getAttributeValue())) {
                                if (((SimpleAttribute) (subAttribute)).getValue().equals(expressionNode.getValue())) {
                                    if (subValue.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subValue.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                                        throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                    } else {
                                        subValueIterator.remove();
                                        isValueFound = true;
                                    }
                                }
                            }
                        }
                    }
                    if (!isValueFound) {
                        throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
                    }
                    AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
                    subValues.add(decoder.buildComplexAttribute(attributeSchema, (JSONObject) operation.getValues()));
                }
            } else {
                // this is complex attribute which has multi valued primitive sub attribute.
                Attribute subAttribute = attribute.getSubAttribute(expressionNode.getAttributeValue());
                if (subAttribute != null) {
                    if (subAttribute.getMultiValued()) {
                        List<Object> valuesList = ((MultiValuedAttribute) (subAttribute)).getAttributePrimitiveValues();
                        for (Iterator<Object> iterator = valuesList.iterator(); iterator.hasNext(); ) {
                            Object item = iterator.next();
                            // we only support "EQ" filter
                            if (item.equals(expressionNode.getValue())) {
                                if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                                    throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                } else {
                                    iterator.remove();
                                    isValueFound = true;
                                }
                            }
                        }
                        if (!isValueFound) {
                            throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
                        }
                        valuesList.add(operation.getValues());
                    } else {
                        throw new BadRequestException("Sub attribute : " + expressionNode.getAttributeValue() + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
                    }
                } else {
                    AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + expressionNode.getAttributeValue(), schema);
                    if (subAttributeSchema.getMultiValued()) {
                        ((ComplexAttribute) (attribute)).setSubAttribute(decoder.buildPrimitiveMultiValuedAttribute(subAttributeSchema, (JSONArray) operation.getValues()));
                    } else {
                        if (subAttributeSchema.getMultiValued()) {
                            ((ComplexAttribute) (attribute)).setSubAttribute(decoder.buildSimpleAttribute(subAttributeSchema, operation.getValues()));
                        }
                    }
                }
            }
        }
    } else {
        // add the attribute
        AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
        if (attributeSchema != null) {
            if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                if (attributeSchema.getMultiValued()) {
                    MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
                    DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
                    String complexName = attributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + SCIMConstants.DEFAULT;
                    ComplexAttribute complexAttribute = new ComplexAttribute(complexName);
                    DefaultAttributeFactory.createAttribute(attributeSchema, complexAttribute);
                    AttributeSchema subValuesSubAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + expressionNode.getAttributeValue(), schema);
                    if (subValuesSubAttributeSchema != null) {
                        SimpleAttribute simpleAttribute = new SimpleAttribute(subValuesSubAttributeSchema.getName(), operation.getValues());
                        DefaultAttributeFactory.createAttribute(subValuesSubAttributeSchema, simpleAttribute);
                        complexAttribute.setSubAttribute(simpleAttribute);
                        multiValuedAttribute.setAttributeValue(complexAttribute);
                        oldResource.setAttribute(multiValuedAttribute);
                    } else {
                        throw new BadRequestException("No such attribute with name : " + attributeParts[0] + "." + expressionNode.getAttributeValue(), ResponseCodeConstants.INVALID_PATH);
                    }
                } else {
                    throw new BadRequestException("Attribute : " + attributeParts[0] + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
                }
            } else {
                if (attributeSchema.getMultiValued()) {
                    // primitive case
                    MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
                    DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
                    multiValuedAttribute.setAttributePrimitiveValue(operation.getValues());
                    oldResource.setAttribute(multiValuedAttribute);
                } else {
                    throw new BadRequestException("Attribute : " + attributeParts[0] + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
                }
            }
        } else {
            throw new BadRequestException("No such attribute with the name : " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
        }
    }
    return oldResource;
}
Also used : 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) JSONArray(org.json.JSONArray) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) JSONObject(org.json.JSONObject) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject)

Example 18 with ExpressionNode

use of org.wso2.charon3.core.utils.codeutils.ExpressionNode in project charon by wso2.

the class PatchOperationUtil method doPatchReplaceWithFiltersForLevelTwo.

/*
     * This method is to do patch replace for level two attributes with a filter present.
     * @param oldResource
     * @param attributeParts
     * @param expressionNode
     * @param operation
     * @param schema
     * @param decoder
     * @return
     * @throws CharonException
     * @throws BadRequestException
     * @throws JSONException
     * @throws InternalErrorException
     */
private static AbstractSCIMObject doPatchReplaceWithFiltersForLevelTwo(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode, PatchOperation operation, SCIMResourceTypeSchema schema, JSONDecoder decoder) throws CharonException, BadRequestException, JSONException, InternalErrorException {
    Attribute attribute = oldResource.getAttribute(attributeParts[0]);
    boolean isValueFound = false;
    if (attribute != null) {
        if (attribute.getMultiValued()) {
            List<Attribute> subValues = ((MultiValuedAttribute) attribute).getAttributeValues();
            if (subValues != null) {
                for (Attribute subValue : subValues) {
                    Map<String, Attribute> subAttributes = ((ComplexAttribute) subValue).getSubAttributesList();
                    // this map is to avoid concurrent modification exception.
                    Map<String, Attribute> tempSubAttributes = (Map<String, Attribute>) CopyUtil.deepCopy(subAttributes);
                    for (Iterator<Attribute> iterator = tempSubAttributes.values().iterator(); iterator.hasNext(); ) {
                        Attribute subAttribute = iterator.next();
                        if (subAttribute.getName().equals(expressionNode.getAttributeValue())) {
                            if (((SimpleAttribute) subAttribute).getValue().equals(expressionNode.getValue())) {
                                Attribute replacingAttribute = subAttributes.get(attributeParts[1]);
                                if (replacingAttribute == null) {
                                    // add the attribute
                                    AttributeSchema replacingAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                                    if (replacingAttributeSchema.getMultiValued()) {
                                        MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(replacingAttributeSchema.getName());
                                        DefaultAttributeFactory.createAttribute(replacingAttributeSchema, multiValuedAttribute);
                                        multiValuedAttribute.setAttributePrimitiveValue(operation.getValues());
                                        ((ComplexAttribute) subValue).setSubAttribute(multiValuedAttribute);
                                        break;
                                    } else {
                                        SimpleAttribute simpleAttribute = new SimpleAttribute(replacingAttributeSchema.getName(), operation.getValues());
                                        DefaultAttributeFactory.createAttribute(replacingAttributeSchema, simpleAttribute);
                                        ((ComplexAttribute) subValue).setSubAttribute(simpleAttribute);
                                        break;
                                    }
                                }
                                if (replacingAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || replacingAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                                    throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                } else {
                                    if (replacingAttribute.getMultiValued()) {
                                        ((MultiValuedAttribute) replacingAttribute).getAttributePrimitiveValues().remove(expressionNode.getValue());
                                        ((MultiValuedAttribute) replacingAttribute).setAttributePrimitiveValue(operation.getValues());
                                    } else {
                                        ((SimpleAttribute) (replacingAttribute)).setValue(operation.getValues());
                                    }
                                    isValueFound = true;
                                }
                            }
                        }
                    }
                }
                if (!isValueFound) {
                    throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
                }
            }
        } else if (attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            // this is only valid for extension
            Attribute subAttribute = attribute.getSubAttribute(attributeParts[1]);
            if (subAttribute == null) {
                // add the attribute
                AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                if (subAttributeSchema != null) {
                    if (subAttributeSchema.getMultiValued()) {
                        MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
                        DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
                        multiValuedAttribute.setAttributeValue(decoder.buildComplexAttribute(subAttributeSchema, (JSONObject) operation.getValues()));
                        ((ComplexAttribute) (attribute)).setSubAttribute(multiValuedAttribute);
                    } else {
                        throw new BadRequestException("Attribute : " + attributeParts[1] + "is not a multi valued attribute.", ResponseCodeConstants.INVALID_PATH);
                    }
                } else {
                    throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
                }
            } else {
                List<Attribute> subValues = ((MultiValuedAttribute) (subAttribute)).getAttributeValues();
                if (subValues != null) {
                    for (Iterator<Attribute> subValueIterator = subValues.iterator(); subValueIterator.hasNext(); ) {
                        Attribute subValue = subValueIterator.next();
                        Map<String, Attribute> subValuesSubAttribute = ((ComplexAttribute) subValue).getSubAttributesList();
                        for (Iterator<Attribute> iterator = subValuesSubAttribute.values().iterator(); iterator.hasNext(); ) {
                            Attribute subSubAttribute = iterator.next();
                            if (subSubAttribute.getName().equals(expressionNode.getAttributeValue())) {
                                if (((SimpleAttribute) (subSubAttribute)).getValue().equals(expressionNode.getValue())) {
                                    if (subValue.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subValue.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                                        throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                    } else {
                                        subValueIterator.remove();
                                        isValueFound = true;
                                    }
                                }
                            }
                        }
                    }
                    AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                    subValues.add(decoder.buildComplexAttribute(attributeSchema, (JSONObject) operation.getValues()));
                    if (!isValueFound) {
                        throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
                    }
                }
            }
        } else {
            throw new BadRequestException("Attribute : " + expressionNode.getAttributeValue() + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
        }
    } else {
        // add the attribute
        AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
        if (attributeSchema != null) {
            if (attributeSchema.getMultiValued()) {
                MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
                DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
                String complexName = attributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + SCIMConstants.DEFAULT;
                ComplexAttribute complexAttribute = new ComplexAttribute(complexName);
                DefaultAttributeFactory.createAttribute(attributeSchema, complexAttribute);
                AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                if (subAttributeSchema != null) {
                    if (subAttributeSchema.getMultiValued()) {
                        MultiValuedAttribute multiValuedSubAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
                        DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedSubAttribute);
                        multiValuedAttribute.setAttributePrimitiveValue(operation.getValues());
                        complexAttribute.setSubAttribute(multiValuedSubAttribute);
                        multiValuedAttribute.setAttributeValue(complexAttribute);
                        oldResource.setAttribute(multiValuedAttribute);
                    } else {
                        SimpleAttribute simpleAttribute = new SimpleAttribute(subAttributeSchema.getName(), operation.getValues());
                        DefaultAttributeFactory.createAttribute(subAttributeSchema, simpleAttribute);
                        complexAttribute.setSubAttribute(simpleAttribute);
                        multiValuedAttribute.setAttributeValue(complexAttribute);
                        oldResource.setAttribute(multiValuedAttribute);
                    }
                } else {
                    throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
                }
            } else {
                ComplexAttribute extensionComplexAttribute = new ComplexAttribute(attributeSchema.getName());
                DefaultAttributeFactory.createAttribute(attributeSchema, extensionComplexAttribute);
                AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                if (subAttributeSchema != null) {
                    if (subAttributeSchema.getMultiValued()) {
                        MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
                        DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
                        multiValuedAttribute.setAttributeValue(decoder.buildComplexAttribute(subAttributeSchema, (JSONObject) operation.getValues()));
                        oldResource.setAttribute(multiValuedAttribute);
                    } else {
                        throw new BadRequestException("Attribute : " + attributeParts[1] + "is not a multi valued attribute.", ResponseCodeConstants.INVALID_PATH);
                    }
                } else {
                    throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
                }
            }
        } else {
            throw new BadRequestException("No such attribute with the name : " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
        }
    }
    return oldResource;
}
Also used : 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) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) JSONObject(org.json.JSONObject) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) Iterator(java.util.Iterator) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) List(java.util.List) Map(java.util.Map)

Example 19 with ExpressionNode

use of org.wso2.charon3.core.utils.codeutils.ExpressionNode in project charon by wso2.

the class FilterTreeManager method validateAndBuildFilterExpression.

/*
     * Validate the simple filter and build a ExpressionNode
     *
     * @param filterString
     * @param expressionNode
     * @throws BadRequestException
     */
private void validateAndBuildFilterExpression(String filterString, ExpressionNode expressionNode) throws BadRequestException {
    // verify filter string. validation should be case insensitive
    if (!(Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.EQ), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.NE), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.CO), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.SW), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.EW), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.PR), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.GT), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.GE), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.LT), Pattern.CASE_INSENSITIVE).matcher(filterString).find() || Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.LE), Pattern.CASE_INSENSITIVE).matcher(filterString).find())) {
        String message = "Given filter operator is not supported.";
        throw new BadRequestException(message, ResponseCodeConstants.INVALID_FILTER);
    }
    String trimmedFilter = filterString.trim();
    String[] filterParts = null;
    if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.EQ), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" eq | EQ | eQ | Eq ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.EQ, filterParts[1], expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.NE), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" ne | NE | nE | Ne ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.NE, filterParts[1], expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.CO), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" co | CO | cO | Co ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.CO, filterParts[1], expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.SW), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" sw | SW | sW | Sw ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.SW, filterParts[1], expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.EW), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" ew | EW | eW | Ew ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.EW, filterParts[1], expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.PR), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        // with filter PR, there should not be whitespace after.
        filterParts = trimmedFilter.split(" pr| PR| pR| Pr");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.PR, null, expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.GT), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" gt | GT | gT | Gt ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.GT, filterParts[1], expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.GE), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" ge | GE | gE | Ge ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.GE, filterParts[1], expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.LT), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" lt | LT | lT | Lt ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.LT, filterParts[1], expressionNode);
    } else if (Pattern.compile(Pattern.quote(SCIMConstants.OperationalConstants.LE), Pattern.CASE_INSENSITIVE).matcher(filterString).find()) {
        filterParts = trimmedFilter.split(" le | LE | lE | Le ");
        setExpressionNodeValues(filterParts[0], SCIMConstants.OperationalConstants.LE, filterParts[1], expressionNode);
    } else {
        throw new BadRequestException(ResponseCodeConstants.INVALID_FILTER);
    }
}
Also used : BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Example 20 with ExpressionNode

use of org.wso2.charon3.core.utils.codeutils.ExpressionNode 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)

Aggregations

SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)16 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)16 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)11 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)8 Attribute (org.wso2.charon3.core.attributes.Attribute)6 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)6 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)6 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)6 Map (java.util.Map)4 JSONObject (org.json.JSONObject)3 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)3 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)3 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)3 Iterator (java.util.Iterator)2 List (java.util.List)2 JSONArray (org.json.JSONArray)2 BLangNameReference (org.wso2.ballerinalang.compiler.tree.BLangNameReference)2 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)2 BLangVariableReference (org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)2 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)2