use of org.wso2.charon3.core.utils.codeutils.PatchOperation in project charon by wso2.
the class PatchOperationUtil method doPatchReplaceWithFiltersForLevelThree.
/*
* This method is to do patch replace for level three attributes with a filter present.
* @param oldResource
* @param attributeParts
* @param expressionNode
* @param operation
* @return
* @throws BadRequestException
* @throws CharonException
*/
private static AbstractSCIMObject doPatchReplaceWithFiltersForLevelThree(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode, PatchOperation operation, SCIMResourceTypeSchema schema, JSONDecoder decoder) throws BadRequestException, CharonException {
Attribute attribute = oldResource.getAttribute(attributeParts[0]);
boolean isValueFound = false;
if (attribute != null) {
Attribute subAttribute = attribute.getSubAttribute(attributeParts[1]);
if (subAttribute != null) {
if (subAttribute.getMultiValued()) {
List<Attribute> subValues = ((MultiValuedAttribute) subAttribute).getAttributeValues();
if (subValues != null) {
for (Attribute subValue : subValues) {
Map<String, Attribute> subSubAttributes = ((ComplexAttribute) subValue).getSubAttributesList();
// this map is to avoid concurrent modification exception.
Map<String, Attribute> tempSubSubAttributes = (Map<String, Attribute>) CopyUtil.deepCopy(subSubAttributes);
for (Iterator<Attribute> iterator = tempSubSubAttributes.values().iterator(); iterator.hasNext(); ) {
Attribute subSubAttribute = iterator.next();
if (subSubAttribute.getName().equals(expressionNode.getAttributeValue())) {
Attribute replacingAttribute = subSubAttributes.get(attributeParts[2]);
if (replacingAttribute == null) {
AttributeSchema replacingAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1] + "." + attributeParts[2], schema);
if (replacingAttributeSchema != null) {
if (replacingAttributeSchema.getMultiValued()) {
((ComplexAttribute) subValue).setSubAttribute(decoder.buildPrimitiveMultiValuedAttribute(replacingAttributeSchema, (JSONArray) operation.getValues()));
break;
} else {
((ComplexAttribute) subValue).setSubAttribute(decoder.buildSimpleAttribute(replacingAttributeSchema, operation.getValues()));
break;
}
} else {
throw new BadRequestException("No such attribute with the name : " + attributeParts[0] + "." + attributeParts[1] + "." + attributeParts[2]);
}
}
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 {
throw new BadRequestException("Attribute : " + attributeParts[1] + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
}
} else {
AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
if (subAttributeSchema != null) {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
String complexName = subAttributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + SCIMConstants.DEFAULT;
ComplexAttribute complexAttribute = new ComplexAttribute(complexName);
DefaultAttributeFactory.createAttribute(subAttributeSchema, complexAttribute);
AttributeSchema subSubAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1] + "." + attributeParts[2], schema);
if (subSubAttributeSchema != null) {
if (subSubAttributeSchema.getMultiValued()) {
MultiValuedAttribute multiValuedSubAttribute = new MultiValuedAttribute(subSubAttributeSchema.getName());
DefaultAttributeFactory.createAttribute(subSubAttributeSchema, multiValuedSubAttribute);
multiValuedSubAttribute.setAttributePrimitiveValue(operation.getValues());
complexAttribute.setSubAttribute(multiValuedSubAttribute);
multiValuedAttribute.setAttributeValue(complexAttribute);
((ComplexAttribute) (attribute)).setSubAttribute(multiValuedAttribute);
} else {
SimpleAttribute simpleAttribute = new SimpleAttribute(subSubAttributeSchema.getName(), operation.getValues());
DefaultAttributeFactory.createAttribute(subSubAttributeSchema, simpleAttribute);
complexAttribute.setSubAttribute(simpleAttribute);
multiValuedAttribute.setAttributeValue(complexAttribute);
((ComplexAttribute) (attribute)).setSubAttribute(multiValuedAttribute);
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "." + attributeParts[2] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
}
}
} else {
AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
if (attributeSchema != null) {
if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
ComplexAttribute extensionAttribute = new ComplexAttribute(attributeSchema.getName());
DefaultAttributeFactory.createAttribute(attributeSchema, extensionAttribute);
AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1], schema);
if (subAttributeSchema != null) {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(subAttributeSchema.getName());
DefaultAttributeFactory.createAttribute(subAttributeSchema, multiValuedAttribute);
String complexName = subAttributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + SCIMConstants.DEFAULT;
ComplexAttribute complexAttribute = new ComplexAttribute(complexName);
DefaultAttributeFactory.createAttribute(subAttributeSchema, complexAttribute);
AttributeSchema subSubAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + attributeParts[1] + "." + attributeParts[2], schema);
if (subSubAttributeSchema != null) {
if (subSubAttributeSchema.getMultiValued()) {
MultiValuedAttribute multiValuedSubAttribute = new MultiValuedAttribute(subSubAttributeSchema.getName());
DefaultAttributeFactory.createAttribute(subSubAttributeSchema, multiValuedSubAttribute);
multiValuedAttribute.setAttributePrimitiveValue(operation.getValues());
complexAttribute.setSubAttribute(multiValuedSubAttribute);
(multiValuedAttribute).setAttributeValue(complexAttribute);
extensionAttribute.setSubAttribute(multiValuedAttribute);
oldResource.setAttribute(extensionAttribute);
} else {
SimpleAttribute simpleAttribute = new SimpleAttribute(subSubAttributeSchema.getName(), operation.getValues());
DefaultAttributeFactory.createAttribute(subSubAttributeSchema, simpleAttribute);
complexAttribute.setSubAttribute(simpleAttribute);
(multiValuedAttribute).setAttributeValue(simpleAttribute);
extensionAttribute.setSubAttribute(multiValuedAttribute);
oldResource.setAttribute(extensionAttribute);
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "." + attributeParts[2] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + "." + attributeParts[1] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
}
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + "does not exists.", ResponseCodeConstants.INVALID_PATH);
}
}
return oldResource;
}
use of org.wso2.charon3.core.utils.codeutils.PatchOperation in project charon by wso2.
the class PatchOperationUtil method doPatchReplaceWithFiltersForLevelOne.
/*
* This method is to do patch replace for level one attributes with a filter present.
* @param oldResource
* @param attributeParts
* @param expressionNode
* @param operation
* @param schema
* @param decoder
* @return
* @throws BadRequestException
* @throws CharonException
* @throws JSONException
* @throws InternalErrorException
*/
private static AbstractSCIMObject doPatchReplaceWithFiltersForLevelOne(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode, PatchOperation operation, SCIMResourceTypeSchema schema, JSONDecoder decoder) throws BadRequestException, CharonException, JSONException, InternalErrorException {
Attribute attribute = oldResource.getAttribute(attributeParts[0]);
boolean isValueFound = false;
if (attribute != null) {
if (!attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
// this is multivalued primitive case
if (attribute.getMultiValued()) {
List<Object> valuesList = ((MultiValuedAttribute) (attribute)).getAttributePrimitiveValues();
for (Iterator<Object> iterator = valuesList.iterator(); iterator.hasNext(); ) {
Object item = iterator.next();
// we only support "EQ" filter
if (item.equals(expressionNode.getValue())) {
if (attribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || attribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
} else {
iterator.remove();
isValueFound = true;
}
}
}
if (!isValueFound) {
throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
}
valuesList.add(operation.getValues());
} else {
throw new BadRequestException("Attribute : " + expressionNode.getAttributeValue() + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
}
} else {
if (attribute.getMultiValued()) {
// this is for paths value as 'emails[value EQ vindula@wso2.com]'
// this is multivalued complex case
List<Attribute> subValues = ((MultiValuedAttribute) (attribute)).getAttributeValues();
if (subValues != null) {
for (Iterator<Attribute> subValueIterator = subValues.iterator(); subValueIterator.hasNext(); ) {
Attribute subValue = subValueIterator.next();
Map<String, Attribute> subValuesSubAttribute = ((ComplexAttribute) subValue).getSubAttributesList();
for (Iterator<Attribute> iterator = subValuesSubAttribute.values().iterator(); iterator.hasNext(); ) {
Attribute subAttribute = iterator.next();
if (subAttribute.getName().equals(expressionNode.getAttributeValue())) {
if (((SimpleAttribute) (subAttribute)).getValue().equals(expressionNode.getValue())) {
if (subValue.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subValue.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
} else {
subValueIterator.remove();
isValueFound = true;
}
}
}
}
}
if (!isValueFound) {
throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
}
AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
subValues.add(decoder.buildComplexAttribute(attributeSchema, (JSONObject) operation.getValues()));
}
} else {
// this is complex attribute which has multi valued primitive sub attribute.
Attribute subAttribute = attribute.getSubAttribute(expressionNode.getAttributeValue());
if (subAttribute != null) {
if (subAttribute.getMultiValued()) {
List<Object> valuesList = ((MultiValuedAttribute) (subAttribute)).getAttributePrimitiveValues();
for (Iterator<Object> iterator = valuesList.iterator(); iterator.hasNext(); ) {
Object item = iterator.next();
// we only support "EQ" filter
if (item.equals(expressionNode.getValue())) {
if (subAttribute.getMutability().equals(SCIMDefinitions.Mutability.READ_ONLY) || subAttribute.getMutability().equals(SCIMDefinitions.Mutability.IMMUTABLE)) {
throw new BadRequestException("Can not remove a immutable attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
} else {
iterator.remove();
isValueFound = true;
}
}
}
if (!isValueFound) {
throw new BadRequestException("No matching filter value found.", ResponseCodeConstants.NO_TARGET);
}
valuesList.add(operation.getValues());
} else {
throw new BadRequestException("Sub attribute : " + expressionNode.getAttributeValue() + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
}
} else {
AttributeSchema subAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + expressionNode.getAttributeValue(), schema);
if (subAttributeSchema.getMultiValued()) {
((ComplexAttribute) (attribute)).setSubAttribute(decoder.buildPrimitiveMultiValuedAttribute(subAttributeSchema, (JSONArray) operation.getValues()));
} else {
if (subAttributeSchema.getMultiValued()) {
((ComplexAttribute) (attribute)).setSubAttribute(decoder.buildSimpleAttribute(subAttributeSchema, operation.getValues()));
}
}
}
}
}
} else {
// add the attribute
AttributeSchema attributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0], schema);
if (attributeSchema != null) {
if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
if (attributeSchema.getMultiValued()) {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
String complexName = attributeSchema.getName() + "_" + SCIMConstants.DEFAULT + "_" + SCIMConstants.DEFAULT;
ComplexAttribute complexAttribute = new ComplexAttribute(complexName);
DefaultAttributeFactory.createAttribute(attributeSchema, complexAttribute);
AttributeSchema subValuesSubAttributeSchema = SchemaUtil.getAttributeSchema(attributeParts[0] + "." + expressionNode.getAttributeValue(), schema);
if (subValuesSubAttributeSchema != null) {
SimpleAttribute simpleAttribute = new SimpleAttribute(subValuesSubAttributeSchema.getName(), operation.getValues());
DefaultAttributeFactory.createAttribute(subValuesSubAttributeSchema, simpleAttribute);
complexAttribute.setSubAttribute(simpleAttribute);
multiValuedAttribute.setAttributeValue(complexAttribute);
oldResource.setAttribute(multiValuedAttribute);
} else {
throw new BadRequestException("No such attribute with name : " + attributeParts[0] + "." + expressionNode.getAttributeValue(), ResponseCodeConstants.INVALID_PATH);
}
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
}
} else {
if (attributeSchema.getMultiValued()) {
// primitive case
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
multiValuedAttribute.setAttributePrimitiveValue(operation.getValues());
oldResource.setAttribute(multiValuedAttribute);
} else {
throw new BadRequestException("Attribute : " + attributeParts[0] + " " + "is not a multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
}
}
} else {
throw new BadRequestException("No such attribute with the name : " + attributeParts[0], ResponseCodeConstants.INVALID_PATH);
}
}
return oldResource;
}
use of org.wso2.charon3.core.utils.codeutils.PatchOperation 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;
}
use of org.wso2.charon3.core.utils.codeutils.PatchOperation 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);
}
}
use of org.wso2.charon3.core.utils.codeutils.PatchOperation in project charon by wso2.
the class MeResourceManager method updateWithPATCH.
/**
* Update the user resource by sequence of operations.
*
* @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().getUserResourceSchema();
// get the user from the user core
User oldUser = userManager.getMe(existingId, ResourceManagerUtil.getAllAttributeURIs(schema));
if (oldUser == null) {
throw new NotFoundException("No associated user exits in the user store.");
}
// make a copy of the original user
User copyOfOldUser = (User) CopyUtil.deepCopy(oldUser);
// make another copy of original user.
// this will be used to restore to the original condition if failure occurs.
User originalUser = (User) CopyUtil.deepCopy(copyOfOldUser);
User newUser = null;
for (PatchOperation operation : opList) {
if (operation.getOperation().equals(SCIMConstants.OperationalConstants.ADD)) {
if (newUser == null) {
newUser = (User) PatchOperationUtil.doPatchAdd(operation, getDecoder(), oldUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
} else {
newUser = (User) PatchOperationUtil.doPatchAdd(operation, getDecoder(), newUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
}
} else if (operation.getOperation().equals(SCIMConstants.OperationalConstants.REMOVE)) {
if (newUser == null) {
newUser = (User) PatchOperationUtil.doPatchRemove(operation, oldUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
} else {
newUser = (User) PatchOperationUtil.doPatchRemove(operation, newUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
}
} else if (operation.getOperation().equals(SCIMConstants.OperationalConstants.REPLACE)) {
if (newUser == null) {
newUser = (User) PatchOperationUtil.doPatchReplace(operation, getDecoder(), oldUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
} else {
newUser = (User) PatchOperationUtil.doPatchReplace(operation, getDecoder(), newUser, copyOfOldUser, schema);
copyOfOldUser = (User) CopyUtil.deepCopy(newUser);
}
} 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);
User validatedUser = (User) ServerSideValidator.validateUpdatedSCIMObject(originalUser, newUser, schema);
newUser = userManager.updateMe(validatedUser, requiredAttributes);
// encode the newly created SCIM user object and add id attribute to Location header.
String encodedUser;
Map<String, String> httpHeaders = new HashMap<String, String>();
if (newUser != null) {
// create a deep copy of the user object since we are going to change it.
User copiedUser = (User) CopyUtil.deepCopy(newUser);
// need to remove password before returning
ServerSideValidator.validateReturnedAttributes(copiedUser, attributes, excludeAttributes);
encodedUser = getEncoder().encodeSCIMObject(copiedUser);
// add location header
httpHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.USER_ENDPOINT) + "/" + newUser.getId());
httpHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
} else {
String error = "Updated User 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, encodedUser, httpHeaders);
} catch (NotFoundException e) {
return encodeSCIMException(e);
} catch (BadRequestException e) {
return encodeSCIMException(e);
} catch (NotImplementedException e) {
return encodeSCIMException(e);
} catch (CharonException e) {
return encodeSCIMException(e);
} catch (InternalErrorException e) {
return encodeSCIMException(e);
} catch (RuntimeException e) {
CharonException e1 = new CharonException("Error in performing the patch operation on user resource.", e);
return encodeSCIMException(e1);
}
}
Aggregations