Search in sources :

Example 1 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.

the class ServerSideValidator method validateCreatedSCIMObject.

/*
     * Validate created SCIMObject according to the spec
     *
     * @param scimObject
     * @param resourceSchema
     * @throw CharonException
     * @throw BadRequestException
     * @throw NotFoundException
     */
public static void validateCreatedSCIMObject(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException, NotFoundException {
    if (scimObject instanceof User) {
        // set display names for complex multivalued attributes
        setDisplayNameInComplexMultiValuedAttributes(scimObject, resourceSchema);
    }
    // remove any read only attributes
    removeAnyReadOnlyAttributes(scimObject, resourceSchema);
    // add created and last modified dates
    String id = UUID.randomUUID().toString();
    scimObject.setId(id);
    Date date = new Date();
    // set the created date and time
    scimObject.setCreatedDate(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(date)));
    // creates date and the last modified are the same if not updated.
    scimObject.setLastModified(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(date)));
    // set location and resourceType
    if (resourceSchema.isSchemaAvailable(SCIMConstants.USER_CORE_SCHEMA_URI)) {
        String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.USER_ENDPOINT), scimObject.getId());
        scimObject.setLocation(location);
        scimObject.setResourceType(SCIMConstants.USER);
    } else if (resourceSchema.isSchemaAvailable(SCIMConstants.GROUP_CORE_SCHEMA_URI)) {
        String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT), scimObject.getId());
        scimObject.setLocation(location);
        scimObject.setResourceType(SCIMConstants.GROUP);
    }
    // check for required attributes
    validateSCIMObjectForRequiredAttributes(scimObject, resourceSchema);
    validateSchemaList(scimObject, resourceSchema);
}
Also used : User(org.wso2.charon3.core.objects.User) Date(java.util.Date)

Example 2 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException in project charon by wso2.

the class PatchOperationUtil method doPatchRemoveWithFiltersForLevelTwo.

/*
     *
     * @param oldResource
     * @param attributeParts
     * @param expressionNode
     * @return
     * @throws BadRequestException
     * @throws CharonException
     */
private static AbstractSCIMObject doPatchRemoveWithFiltersForLevelTwo(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode) throws BadRequestException, CharonException {
    Attribute attribute = oldResource.getAttribute(attributeParts[0]);
    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 removingAttribute = subAttributes.get(attributeParts[1]);
                                if (removingAttribute == null) {
                                    throw new BadRequestException("No such sub attribute with the name : " + attributeParts[1] + " " + "within the attribute " + attributeParts[0], 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
                    oldResource.deleteAttribute(attribute.getName());
                }
            }
        } else if (attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
            // this is only valid for extension
            Attribute subAttribute = attribute.getSubAttribute(attributeParts[1]);
            if (subAttribute == null) {
                throw new BadRequestException("No such sub attribute with the name : " + attributeParts[1] + " " + "within the attribute " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
            }
            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.getRequired().equals(true)) {
                                    throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
                                } else {
                                    subValueIterator.remove();
                                }
                            }
                        }
                    }
                }
                // if the attribute has no values, make it unassigned
                if (((MultiValuedAttribute) (subAttribute)).getAttributeValues().size() == 0) {
                    ((ComplexAttribute) attribute).removeSubAttribute(subAttribute.getName());
                }
            }
        } else {
            throw new BadRequestException("Attribute : " + expressionNode.getAttributeValue() + " " + "is not a multivalued attribute.", 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) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) Iterator(java.util.Iterator) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) List(java.util.List) Map(java.util.Map) MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute)

Example 3 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException 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 4 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException 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 5 with BadRequestException

use of org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException 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)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)63 CharonException (org.wso2.charon3.core.exceptions.CharonException)31 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)30 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)27 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)23 HashMap (java.util.HashMap)22 Attribute (org.wso2.charon3.core.attributes.Attribute)20 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 JSONObject (org.json.JSONObject)16 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)16 JSONException (org.json.JSONException)15 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)14 User (org.wso2.charon3.core.objects.User)12 JSONArray (org.json.JSONArray)11 BadRequestException (org.wso2.carbon.apimgt.rest.api.util.exception.BadRequestException)10