Search in sources :

Example 61 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project ballerina by ballerina-lang.

the class ObjectTypeScopeResolver method isCursorBeforeNode.

/**
 * Check whether the cursor is positioned before the given node start.
 *
 * @param nodePosition      Position of the node
 * @param node              Node
 * @param treeVisitor       {@link TreeVisitor} current tree visitor instance
 * @param completionContext Completion operation context
 * @return {@link Boolean}      Whether the cursor is before the node start or not
 */
@Override
public boolean isCursorBeforeNode(DiagnosticPos nodePosition, Node node, TreeVisitor treeVisitor, TextDocumentServiceContext completionContext) {
    if (!(treeVisitor.getBlockOwnerStack().peek() instanceof BLangObject)) {
        return false;
    }
    BLangObject ownerObject = (BLangObject) treeVisitor.getBlockOwnerStack().peek();
    DiagnosticPos zeroBasedPos = CommonUtil.toZeroBasedPosition(nodePosition);
    DiagnosticPos blockOwnerPos = CommonUtil.toZeroBasedPosition((DiagnosticPos) treeVisitor.getBlockOwnerStack().peek().getPosition());
    int line = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
    int col = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();
    boolean isLastField = false;
    if ((!ownerObject.fields.isEmpty() && node instanceof BLangVariable && ownerObject.fields.indexOf(node) == ownerObject.fields.size() - 1 && ownerObject.functions.isEmpty()) || (!ownerObject.functions.isEmpty() && node instanceof BLangFunction && ownerObject.functions.indexOf(node) == ownerObject.functions.size() - 1)) {
        isLastField = true;
    }
    if ((line < zeroBasedPos.getStartLine() || (line == zeroBasedPos.getStartLine() && col < zeroBasedPos.getStartColumn())) || (isLastField && ((blockOwnerPos.getEndLine() > line && zeroBasedPos.getEndLine() < line) || (blockOwnerPos.getEndLine() == line && blockOwnerPos.getEndColumn() > col)))) {
        Map<Name, Scope.ScopeEntry> visibleSymbolEntries = treeVisitor.resolveAllVisibleSymbols(treeVisitor.getSymbolEnv());
        treeVisitor.populateSymbols(visibleSymbolEntries, null);
        treeVisitor.setTerminateVisitor(true);
        return true;
    }
    return false;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangObject(org.wso2.ballerinalang.compiler.tree.BLangObject) BLangFunction(org.wso2.ballerinalang.compiler.tree.BLangFunction) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 62 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project charon by wso2.

the class PatchOperationUtil method doPatchReplace.

/*
     * This is the main patch replace method.
     * @param operation
     * @param decoder
     * @param oldResource
     * @param copyOfOldResource
     * @param schema
     * @return
     * @throws CharonException
     * @throws NotImplementedException
     * @throws BadRequestException
     * @throws JSONException
     * @throws InternalErrorException
     */
public static AbstractSCIMObject doPatchReplace(PatchOperation operation, JSONDecoder decoder, AbstractSCIMObject oldResource, AbstractSCIMObject copyOfOldResource, SCIMResourceTypeSchema schema) throws CharonException, NotImplementedException, BadRequestException, InternalErrorException {
    if (operation.getPath() != null) {
        String path = operation.getPath();
        // split the path to extract the filter if present.
        String[] parts = path.split("[\\[\\]]");
        if (operation.getPath().contains("[")) {
            try {
                doPatchReplaceOnPathWithFilters(oldResource, schema, decoder, operation, parts);
            } catch (JSONException e) {
                throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
            }
        } else {
            doPatchReplaceOnPathWithoutFilters(oldResource, schema, decoder, operation, parts);
        }
    } else {
        doPatchReplaceOnResource(oldResource, copyOfOldResource, schema, decoder, operation);
    }
    // validate the updated object
    AbstractSCIMObject validatedResource = ServerSideValidator.validateUpdatedSCIMObject(copyOfOldResource, oldResource, schema);
    return validatedResource;
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONException(org.json.JSONException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Example 63 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project charon by wso2.

the class PatchOperationUtil method doPatchRemoveWithoutFilters.

/*
     * This is the patch remove operation when the path is specified without a filter in it.
     * @param parts
     * @param oldResource
     * @return
     * @throws BadRequestException
     * @throws CharonException
     */
private static AbstractSCIMObject doPatchRemoveWithoutFilters(String[] parts, AbstractSCIMObject oldResource) throws BadRequestException, CharonException {
    String[] attributeParts = parts[0].split("[\\.]");
    if (attributeParts.length == 1) {
        Attribute attribute = oldResource.getAttribute(parts[0]);
        if (attribute != null) {
            if (attribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || attribute.getRequired().equals(true)) {
                throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
            } else {
                String attributeName = attribute.getName();
                oldResource.deleteAttribute(attributeName);
            }
        } else {
            throw new BadRequestException("No such attribute with the name : " + attributeParts[0] + " " + "in the current resource", ResponseCodeConstants.INVALID_PATH);
        }
    } else {
        Attribute attribute = oldResource.getAttribute(attributeParts[0]);
        if (attribute != null) {
            if (attribute.getMultiValued()) {
                // this is multivalued complex case
                List<Attribute> subValuesList = ((MultiValuedAttribute) attribute).getAttributeValues();
                if (subValuesList != null) {
                    for (Attribute subValue : subValuesList) {
                        Map<String, Attribute> subSubAttributeList = ((ComplexAttribute) subValue).getSubAttributesList();
                        // need to remove attributes while iterating through the list.
                        for (Iterator<Attribute> iterator = subSubAttributeList.values().iterator(); iterator.hasNext(); ) {
                            Attribute subSubAttribute = iterator.next();
                            if (subSubAttribute.getName().equals(attributeParts[1])) {
                                if (subSubAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subSubAttribute.getRequired().equals(true)) {
                                    throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                } else {
                                    iterator.remove();
                                }
                            }
                        }
                    }
                }
            } else {
                Attribute subAttribute = attribute.getSubAttribute(attributeParts[1]);
                if (subAttribute != null) {
                    if (attributeParts.length == 3) {
                        if (subAttribute.getMultiValued()) {
                            List<Attribute> subSubValuesList = ((MultiValuedAttribute) subAttribute).getAttributeValues();
                            if (subSubValuesList != null) {
                                for (Attribute subSubValue : subSubValuesList) {
                                    Map<String, Attribute> subSubAttributeList = ((ComplexAttribute) subSubValue).getSubAttributesList();
                                    // need to remove attributes while iterating through the list.
                                    for (Iterator<Attribute> iterator = subSubAttributeList.values().iterator(); iterator.hasNext(); ) {
                                        Attribute subSubAttribute = iterator.next();
                                        if (subSubAttribute.getName().equals(attributeParts[2])) {
                                            if (subSubAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subSubAttribute.getRequired().equals(true)) {
                                                throw new BadRequestException("Can not remove a required attribute or a read-only " + "attribute", ResponseCodeConstants.MUTABILITY);
                                            } else {
                                                iterator.remove();
                                            }
                                        }
                                    }
                                }
                            }
                        } else {
                            Attribute subSubAttribute = subAttribute.getSubAttribute(attributeParts[2]);
                            if (subSubAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subSubAttribute.getRequired().equals(true)) {
                                throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                            } else {
                                String subSubAttributeName = subSubAttribute.getName();
                                ((ComplexAttribute) subAttribute).removeSubAttribute(subSubAttributeName);
                            }
                        }
                    } else {
                        // this is complex attribute's sub attribute check
                        if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttribute.getRequired().equals(true)) {
                            throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                        } else {
                            String subAttributeName = subAttribute.getName();
                            ((ComplexAttribute) attribute).removeSubAttribute(subAttributeName);
                        }
                    }
                } else {
                    throw new BadRequestException("No such sub attribute with the name : " + attributeParts[1] + " " + "in 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) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 64 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project charon by wso2.

the class PatchOperationUtil method doPatchReplaceOnPathWithoutFiltersForLevelOne.

/*
     * This performs patch on resource based on the path value.No filter is specified here.
     * And this is for level one attributes.
     * @param oldResource
     * @param schema
     * @param decoder
     * @param operation
     * @param attributeParts
     * @throws BadRequestException
     * @throws CharonException
     * @throws JSONException
     * @throws InternalErrorException
     */
private static void doPatchReplaceOnPathWithoutFiltersForLevelOne(AbstractSCIMObject oldResource, SCIMResourceTypeSchema schema, JSONDecoder decoder, PatchOperation operation, String[] attributeParts) throws BadRequestException, CharonException, InternalErrorException {
    Attribute attribute = oldResource.getAttribute(attributeParts[0]);
    if (attribute != null) {
        if (!attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            if (!attribute.getMultiValued()) {
                if (attribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || attribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                    throw new BadRequestException("Can not replace a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                } else {
                    ((SimpleAttribute) attribute).setValue(operation.getValues().toString());
                }
            } else {
                if (attribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || attribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                    throw new BadRequestException("Can not replace a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                } else {
                    ((MultiValuedAttribute) attribute).deletePrimitiveValues();
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(operation.getValues());
                    } catch (JSONException e) {
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                    for (int i = 0; i < jsonArray.length(); i++) {
                        try {
                            ((MultiValuedAttribute) attribute).setAttributePrimitiveValue(jsonArray.get(i));
                        } catch (JSONException e) {
                            throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                        }
                    }
                }
            }
        } else {
            if (attribute.getMultiValued()) {
                if (attribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || attribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                    throw new BadRequestException("Can not replace a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                } else {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                    } catch (JSONException e) {
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                    AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attribute.getName(), schema);
                    MultiValuedAttribute newMultiValuedAttribute = decoder.buildComplexMultiValuedAttribute(attributeSchema, jsonArray);
                    oldResource.deleteAttribute(attribute.getName());
                    oldResource.setAttribute(newMultiValuedAttribute);
                }
            } else {
                if (attribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || attribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                    throw new BadRequestException("Can not replace a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                } else {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(new JSONTokener(operation.getValues().toString()));
                    } catch (JSONException e) {
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                    AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attribute.getName(), schema);
                    ComplexAttribute newComplexAttribute = null;
                    try {
                        newComplexAttribute = decoder.buildComplexAttribute(attributeSchema, jsonObject);
                    } catch (JSONException e) {
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                    oldResource.deleteAttribute(attribute.getName());
                    oldResource.setAttribute(newComplexAttribute);
                }
            }
        }
    } else {
        // create and add the attribute
        AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
        if (attributeSchema != null) {
            if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                if (attributeSchema.getMultiValued()) {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                    } catch (JSONException e) {
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                    MultiValuedAttribute newMultiValuedAttribute = decoder.buildComplexMultiValuedAttribute(attributeSchema, jsonArray);
                    oldResource.setAttribute(newMultiValuedAttribute);
                } else {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(new JSONTokener(operation.getValues().toString()));
                    } catch (JSONException e) {
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                    ComplexAttribute newComplexAttribute = null;
                    try {
                        newComplexAttribute = decoder.buildComplexAttribute(attributeSchema, jsonObject);
                    } catch (JSONException e) {
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                    oldResource.setAttribute(newComplexAttribute);
                }
            } else {
                if (attributeSchema.getMultiValued()) {
                    JSONArray jsonArray = null;
                    try {
                        jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                    } catch (JSONException e) {
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                    MultiValuedAttribute newMultiValuedAttribute = decoder.buildPrimitiveMultiValuedAttribute(attributeSchema, jsonArray);
                    oldResource.setAttribute(newMultiValuedAttribute);
                } else {
                    SimpleAttribute simpleAttribute = decoder.buildSimpleAttribute(attributeSchema, operation.getValues());
                    oldResource.setAttribute(simpleAttribute);
                }
            }
        } else {
            throw new BadRequestException("No attribute with the name : " + attributeParts[0]);
        }
    }
}
Also used : JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) 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) JSONArray(org.json.JSONArray) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONException(org.json.JSONException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 65 with Operation

use of org.wso2.carbon.apimgt.common.analytics.publishers.dto.Operation in project charon by wso2.

the class PatchOperationUtil method doPatchReplaceOnPathWithoutFiltersForLevelTwo.

/*
     * This performs patch on resource based on the path value.No filter is specified here.
     * And this is for level two attributes.
     * @param oldResource
     * @param schema
     * @param decoder
     * @param operation
     * @param attributeParts
     * @throws BadRequestException
     * @throws CharonException
     * @throws JSONException
     * @throws InternalErrorException
     */
private static void doPatchReplaceOnPathWithoutFiltersForLevelTwo(AbstractSCIMObject oldResource, SCIMResourceTypeSchema schema, JSONDecoder decoder, PatchOperation operation, String[] attributeParts) throws BadRequestException, CharonException, InternalErrorException {
    Attribute attribute = oldResource.getAttribute(attributeParts[0]);
    if (attribute != null) {
        if (attribute.getMultiValued()) {
            List<Attribute> subValues = ((MultiValuedAttribute) attribute).getAttributeValues();
            for (Attribute subValue : subValues) {
                Attribute subAttribute = ((ComplexAttribute) subValue).getSubAttribute(attributeParts[1]);
                if (subAttribute != null) {
                    if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                        throw new BadRequestException("Can not replace a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                    } else {
                        if (subAttribute.getMultiValued()) {
                            ((MultiValuedAttribute) subAttribute).deletePrimitiveValues();
                            JSONArray jsonArray = null;
                            try {
                                jsonArray = new JSONArray(operation.getValues());
                            } catch (JSONException e) {
                                throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                            }
                            for (int i = 0; i < jsonArray.length(); i++) {
                                try {
                                    ((MultiValuedAttribute) subAttribute).setAttributePrimitiveValue(jsonArray.get(i));
                                } catch (JSONException e) {
                                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                                }
                            }
                        } else {
                            ((SimpleAttribute) subAttribute).setValue(operation.getValues());
                        }
                    }
                } else {
                    AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                    if (subAttributeSchema.getMultiValued()) {
                        JSONArray jsonArray = null;
                        try {
                            jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                        } catch (JSONException e) {
                            throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                        }
                        MultiValuedAttribute newMultiValuedAttribute = decoder.buildPrimitiveMultiValuedAttribute(subAttributeSchema, jsonArray);
                        ((ComplexAttribute) (subValue)).setSubAttribute(newMultiValuedAttribute);
                    } else {
                        SimpleAttribute simpleAttribute = decoder.buildSimpleAttribute(subAttributeSchema, operation.getValues());
                        ((ComplexAttribute) (subValue)).setSubAttribute(simpleAttribute);
                    }
                }
            }
        } else {
            Attribute subAttribute = ((attribute)).getSubAttribute(attributeParts[1]);
            AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
            if (subAttributeSchema != null) {
                if (subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                    // only extension schema reaches here.
                    if (subAttribute != null) {
                        if (!subAttribute.getMultiValued()) {
                            if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                                throw new BadRequestException("Can not replace a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                            } else {
                                ComplexAttribute newComplexAttribute = null;
                                try {
                                    newComplexAttribute = decoder.buildComplexAttribute(subAttributeSchema, (JSONObject) operation.getValues());
                                } catch (JSONException e) {
                                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                                }
                                ((ComplexAttribute) (attribute)).removeSubAttribute(attributeParts[1]);
                                ((ComplexAttribute) (attribute)).setSubAttribute(newComplexAttribute);
                            }
                        } else {
                            if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                                throw new BadRequestException("Can not replace a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                            } else {
                                JSONArray jsonArray = null;
                                try {
                                    jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                                } catch (JSONException e) {
                                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                                }
                                MultiValuedAttribute newMultiValuesAttribute = decoder.buildComplexMultiValuedAttribute(subAttributeSchema, jsonArray);
                                ((ComplexAttribute) (attribute)).removeSubAttribute(attributeParts[1]);
                                ((ComplexAttribute) (attribute)).setSubAttribute(newMultiValuesAttribute);
                            }
                        }
                    } else {
                        if (subAttributeSchema.getMultiValued()) {
                            JSONArray jsonArray = null;
                            try {
                                jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                            } catch (JSONException e) {
                                throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                            }
                            MultiValuedAttribute newMultiValuesAttribute = decoder.buildComplexMultiValuedAttribute(subAttributeSchema, jsonArray);
                            ((ComplexAttribute) (attribute)).setSubAttribute(newMultiValuesAttribute);
                        } else {
                            ComplexAttribute newComplexAttribute = null;
                            try {
                                newComplexAttribute = decoder.buildComplexAttribute(subAttributeSchema, (JSONObject) operation.getValues());
                            } catch (JSONException e) {
                                throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                            }
                            ((ComplexAttribute) (attribute)).setSubAttribute(newComplexAttribute);
                        }
                    }
                } else {
                    if (subAttribute != null) {
                        if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
                            throw new BadRequestException("Can not replace a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                        } else {
                            AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                            if (subAttribute.getMultiValued()) {
                                JSONArray jsonArray = null;
                                try {
                                    jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                                } catch (JSONException e) {
                                    throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                                }
                                MultiValuedAttribute newMultiValuedAttribute = decoder.buildPrimitiveMultiValuedAttribute(attributeSchema, jsonArray);
                                ((ComplexAttribute) (attribute)).removeSubAttribute(attributeParts[1]);
                                ((ComplexAttribute) (attribute)).setSubAttribute(newMultiValuedAttribute);
                            } else {
                                SimpleAttribute simpleAttribute = decoder.buildSimpleAttribute(attributeSchema, operation.getValues());
                                ((ComplexAttribute) (attribute)).removeSubAttribute(attributeParts[1]);
                                ((ComplexAttribute) (attribute)).setSubAttribute(simpleAttribute);
                            }
                        }
                    } else {
                        // add the values
                        AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                        if (attributeSchema.getMultiValued()) {
                            JSONArray jsonArray = null;
                            try {
                                jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                            } catch (JSONException e) {
                                throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                            }
                            MultiValuedAttribute newMultiValuedAttribute = decoder.buildPrimitiveMultiValuedAttribute(attributeSchema, jsonArray);
                            ((ComplexAttribute) (attribute)).setSubAttribute(newMultiValuedAttribute);
                        } else {
                            SimpleAttribute simpleAttribute = decoder.buildSimpleAttribute(attributeSchema, operation.getValues());
                            ((ComplexAttribute) (attribute)).setSubAttribute(simpleAttribute);
                        }
                    }
                }
            } else {
                throw new BadRequestException("No such attribute with the name : " + attributeParts[1], ResponseCodeConstants.NO_TARGET);
            }
        }
    } else {
        AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
        if (attributeSchema != null) {
            if (attributeSchema.getMultiValued()) {
                MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
                DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
                AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                if (subAttributeSchema != null) {
                    SimpleAttribute simpleAttribute = new SimpleAttribute(subAttributeSchema.getName(), operation.getValues());
                    DefaultAttributeFactory.createAttribute(subAttributeSchema, simpleAttribute);
                    String complexAttributeName = attributeSchema.getName() + "_" + operation.getValues() + "_" + SCIMConstants.DEFAULT;
                    ComplexAttribute complexAttribute = new ComplexAttribute(complexAttributeName);
                    DefaultAttributeFactory.createAttribute(attributeSchema, complexAttribute);
                    complexAttribute.setSubAttribute(simpleAttribute);
                    multiValuedAttribute.setAttributeValue(complexAttribute);
                    oldResource.setAttribute(multiValuedAttribute);
                } else {
                    throw new BadRequestException("No such attribute with the name : " + attributeParts[1], ResponseCodeConstants.NO_TARGET);
                }
            } else {
                ComplexAttribute complexAttribute = new ComplexAttribute(attributeSchema.getName());
                DefaultAttributeFactory.createAttribute(attributeSchema, complexAttribute);
                AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
                if (subAttributeSchema != null) {
                    if (subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
                        if (subAttributeSchema.getMultiValued()) {
                            JSONArray jsonArray = null;
                            try {
                                jsonArray = new JSONArray(new JSONTokener(operation.getValues().toString()));
                            } catch (JSONException e) {
                                throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                            }
                            MultiValuedAttribute multiValuedAttribute = decoder.buildComplexMultiValuedAttribute(subAttributeSchema, jsonArray);
                            complexAttribute.setSubAttribute(multiValuedAttribute);
                        } else {
                            ComplexAttribute subComplexAttribute = null;
                            try {
                                subComplexAttribute = decoder.buildComplexAttribute(subAttributeSchema, (JSONObject) operation.getValues());
                            } catch (JSONException e) {
                                throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                            }
                            complexAttribute.setSubAttribute(subComplexAttribute);
                        }
                    } else {
                        SimpleAttribute simpleAttribute = new SimpleAttribute(subAttributeSchema.getName(), operation.getValues());
                        DefaultAttributeFactory.createAttribute(subAttributeSchema, simpleAttribute);
                        complexAttribute.setSubAttribute(simpleAttribute);
                    }
                    oldResource.setAttribute(complexAttribute);
                } else {
                    throw new BadRequestException("No such attribute with the name : " + attributeParts[1], ResponseCodeConstants.NO_TARGET);
                }
            }
        } else {
            throw new BadRequestException("No such attribute with the name : " + attributeParts[0], ResponseCodeConstants.NO_TARGET);
        }
    }
}
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) JSONException(org.json.JSONException) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 HashMap (java.util.HashMap)55 ArrayList (java.util.ArrayList)43 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)42 Test (org.testng.annotations.Test)34 URITemplate (org.wso2.carbon.apimgt.api.model.URITemplate)29 HttpResponse (org.wso2.carbon.automation.test.utils.http.client.HttpResponse)29 Map (java.util.Map)28 IOException (java.io.IOException)23 API (org.wso2.carbon.apimgt.api.model.API)22 List (java.util.List)20 JSONObject (org.json.simple.JSONObject)20 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)20 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)19 LinkedHashMap (java.util.LinkedHashMap)18 FaultGatewaysException (org.wso2.carbon.apimgt.api.FaultGatewaysException)18 OperationPolicyData (org.wso2.carbon.apimgt.api.model.OperationPolicyData)18 APIInfo (org.wso2.carbon.apimgt.api.model.APIInfo)17 Connection (java.sql.Connection)16 PreparedStatement (java.sql.PreparedStatement)16