Search in sources :

Example 16 with CharonException

use of org.wso2.charon3.core.exceptions.CharonException 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 17 with CharonException

use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.

the class GroupResourceManager method create.

/*
     * Create group in the service provider given the submitted payload that contains the SCIM group
     * resource, format and the handler to usermanager.
     *
     * @param scimObjectString - Payload of HTTP request, which contains the SCIM object.
     * @param usermanager
     * @param  attributes
     * @param excludeAttributes
     * @return
     */
@Override
public SCIMResponse create(String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
    JSONEncoder encoder = null;
    JSONDecoder decoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder
        decoder = getDecoder();
        // returns core-group schema
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        // decode the SCIM group object, encoded in the submitted payload.
        Group group = (Group) decoder.decodeResource(scimObjectString, schema, new Group());
        // validate decoded group
        ServerSideValidator.validateCreatedSCIMObject(group, SCIMSchemaDefinitions.SCIM_GROUP_SCHEMA);
        // handover the SCIM User object to the group usermanager provided by the SP.
        Group createdGroup;
        // need to send back the newly created group in the response payload
        createdGroup = ((UserManager) userManager).createGroup(group, requiredAttributes);
        // encode the newly created SCIM group object and add id attribute to Location header.
        String encodedGroup;
        Map<String, String> httpHeaders = new HashMap<String, String>();
        if (createdGroup != null) {
            encodedGroup = encoder.encodeSCIMObject(createdGroup);
            // add location header
            httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT) + "/" + createdGroup.getId());
            httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String message = "Newly created Group resource is null..";
            throw new InternalErrorException(message);
        }
        // put the uri of the Group object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_CREATED, encodedGroup, httpHeaders);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (ConflictException e) {
        return encodeSCIMException(e);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : Group(org.wso2.charon3.core.objects.Group) HashMap(java.util.HashMap) ConflictException(org.wso2.charon3.core.exceptions.ConflictException) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 18 with CharonException

use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.

the class GroupResourceManager method get.

/*
     * Retrieves a group resource given an unique group id. Mapped to HTTP GET request.
     *
     * @param id          - unique resource id
     * @param usermanager
     * @param attributes
     * @param excludeAttributes
     * @return SCIM response to be returned.
     */
@Override
public SCIMResponse get(String id, UserManager userManager, String attributes, String excludeAttributes) {
    JSONEncoder encoder = null;
    try {
        // obtain the correct encoder according to the format requested.
        encoder = getEncoder();
        // returns core-group schema
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        // API user should pass a usermanager usermanager to GroupResourceEndpoint.
        // retrieve the group from the provided usermanager.
        Group group = ((UserManager) userManager).getGroup(id, requiredAttributes);
        // if group not found, return an error in relevant format.
        if (group == null) {
            String message = "Group not found in the user store.";
            throw new NotFoundException(message);
        }
        ServerSideValidator.validateRetrievedSCIMObjectInList(group, schema, attributes, excludeAttributes);
        // convert the group into specific format.
        String encodedGroup = encoder.encodeSCIMObject(group);
        // if there are any http headers to be added in the response header.
        Map<String, String> httpHeaders = new HashMap<String, String>();
        httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedGroup, httpHeaders);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : Group(org.wso2.charon3.core.objects.Group) HashMap(java.util.HashMap) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) UserManager(org.wso2.charon3.core.extensions.UserManager) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 19 with CharonException

use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.

the class GroupResourceManager method updateWithPUT.

/*
     * method which corresponds to HTTP PUT - delete the group
     * @param existingId
     * @param scimObjectString
     * @param usermanager
     * @param attributes
     * @param excludeAttributes
     * @return
     */
@Override
public SCIMResponse updateWithPUT(String existingId, String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
    // needs to validate the incoming object. eg: id can not be set by the consumer.
    JSONEncoder encoder = null;
    JSONDecoder decoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder.
        decoder = getDecoder();
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        // decode the SCIM User object, encoded in the submitted payload.
        Group group = (Group) decoder.decodeResource(scimObjectString, schema, new Group());
        Group updatedGroup = null;
        if (userManager != null) {
            // retrieve the old object
            Group oldGroup = userManager.getGroup(existingId, ResourceManagerUtil.getAllAttributeURIs(schema));
            if (oldGroup != null) {
                Group newGroup = (Group) ServerSideValidator.validateUpdatedSCIMObject(oldGroup, group, schema);
                updatedGroup = userManager.updateGroup(oldGroup, newGroup, requiredAttributes);
            } else {
                String error = "No user exists with the given id: " + existingId;
                throw new NotFoundException(error);
            }
        } else {
            String error = "Provided user manager handler is null.";
            throw new InternalErrorException(error);
        }
        // encode the newly created SCIM user object and add id attribute to Location header.
        String encodedGroup;
        Map<String, String> httpHeaders = new HashMap<String, String>();
        if (updatedGroup != null) {
            // create a deep copy of the user object since we are going to change it.
            Group copiedGroup = (Group) CopyUtil.deepCopy(updatedGroup);
            // need to remove password before returning
            ServerSideValidator.validateReturnedAttributes(copiedGroup, attributes, excludeAttributes);
            encodedGroup = encoder.encodeSCIMObject(copiedGroup);
            // add location header
            httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT) + "/" + updatedGroup.getId());
            httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String error = "Updated Group resource is null.";
            throw new InternalErrorException(error);
        }
        // put the uri of the User object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedGroup, httpHeaders);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return encodeSCIMException(e);
    }
}
Also used : Group(org.wso2.charon3.core.objects.Group) HashMap(java.util.HashMap) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) JSONEncoder(org.wso2.charon3.core.encoder.JSONEncoder) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Example 20 with CharonException

use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.

the class GroupResourceManager method updateWithPATCH.

/*
     * method which corresponds to HTTP PATCH - patch the group
     * @param existingId
     * @param scimObjectString
     * @param usermanager
     * @param attributes
     * @param excludeAttributes
     * @return
     */
public SCIMResponse updateWithPATCH(String existingId, String scimObjectString, UserManager userManager, String attributes, String excludeAttributes) {
    try {
        if (userManager == null) {
            String error = "Provided user manager handler is null.";
            throw new InternalErrorException(error);
        }
        // obtain the json decoder.
        JSONDecoder decoder = getDecoder();
        // decode the SCIM User object, encoded in the submitted payload.
        List<PatchOperation> opList = decoder.decodeRequest(scimObjectString);
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        // get the group from the user core
        Group oldGroup = userManager.getGroup(existingId, ResourceManagerUtil.getAllAttributeURIs(schema));
        if (oldGroup == null) {
            throw new NotFoundException("No group with the id : " + existingId + " in the user store.");
        }
        // make a copy of the original group
        Group copyOfOldGroup = (Group) CopyUtil.deepCopy(oldGroup);
        // make another copy of original group.
        // this will be used to restore to the original condition if failure occurs.
        Group originalGroup = (Group) CopyUtil.deepCopy(copyOfOldGroup);
        Group newGroup = null;
        for (PatchOperation operation : opList) {
            if (operation.getOperation().equals(SCIMConstants.OperationalConstants.ADD)) {
                if (newGroup == null) {
                    newGroup = (Group) PatchOperationUtil.doPatchAdd(operation, getDecoder(), oldGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                } else {
                    newGroup = (Group) PatchOperationUtil.doPatchAdd(operation, getDecoder(), newGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                }
            } else if (operation.getOperation().equals(SCIMConstants.OperationalConstants.REMOVE)) {
                if (newGroup == null) {
                    newGroup = (Group) PatchOperationUtil.doPatchRemove(operation, oldGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                } else {
                    newGroup = (Group) PatchOperationUtil.doPatchRemove(operation, newGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                }
            } else if (operation.getOperation().equals(SCIMConstants.OperationalConstants.REPLACE)) {
                if (newGroup == null) {
                    newGroup = (Group) PatchOperationUtil.doPatchReplace(operation, getDecoder(), oldGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                } else {
                    newGroup = (Group) PatchOperationUtil.doPatchReplace(operation, getDecoder(), newGroup, copyOfOldGroup, schema);
                    copyOfOldGroup = (Group) CopyUtil.deepCopy(newGroup);
                }
            } else {
                throw new BadRequestException("Unknown operation.", ResponseCodeConstants.INVALID_SYNTAX);
            }
        }
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        Group validatedGroup = (Group) ServerSideValidator.validateUpdatedSCIMObject(originalGroup, newGroup, schema);
        newGroup = userManager.updateGroup(originalGroup, validatedGroup, requiredAttributes);
        // encode the newly created SCIM group object and add id attribute to Location header.
        String encodedGroup;
        Map<String, String> httpHeaders = new HashMap<String, String>();
        if (newGroup != null) {
            // create a deep copy of the group object since we are going to change it.
            Group copiedGroup = (Group) CopyUtil.deepCopy(newGroup);
            // validate before return.
            ServerSideValidator.validateReturnedAttributes(copiedGroup, attributes, excludeAttributes);
            encodedGroup = getEncoder().encodeSCIMObject(copiedGroup);
            // add location header
            httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + "/" + newGroup.getId());
            httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String error = "Updated group resource is null.";
            throw new CharonException(error);
        }
        // put the URI of the User object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedGroup, httpHeaders);
    } catch (NotFoundException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (BadRequestException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (CharonException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (RuntimeException e) {
        CharonException e1 = new CharonException("Error in performing the patch operation on group resource.", e);
        return AbstractResourceManager.encodeSCIMException(e1);
    }
}
Also used : Group(org.wso2.charon3.core.objects.Group) HashMap(java.util.HashMap) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) PatchOperation(org.wso2.charon3.core.utils.codeutils.PatchOperation) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) SCIMResourceTypeSchema(org.wso2.charon3.core.schema.SCIMResourceTypeSchema) CharonException(org.wso2.charon3.core.exceptions.CharonException) SCIMResponse(org.wso2.charon3.core.protocol.SCIMResponse)

Aggregations

CharonException (org.wso2.charon3.core.exceptions.CharonException)46 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)44 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)34 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)32 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)31 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)28 Attribute (org.wso2.charon3.core.attributes.Attribute)27 HashMap (java.util.HashMap)22 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)19 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)17 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 UserManager (org.wso2.charon3.core.extensions.UserManager)15 JSONObject (org.json.JSONObject)14 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)14 JSONException (org.json.JSONException)13 User (org.wso2.charon3.core.objects.User)13 ApiOperation (io.swagger.annotations.ApiOperation)12