use of org.wso2.charon.core.exceptions.CharonException 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.charon.core.exceptions.CharonException 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 {
// Check whether the patched attributes are permissions of Roles.
if (schema.isSchemaAvailable(SCIMConstants.ROLE_SCHEMA_URI) && SCIMConstants.RoleSchemaConstants.PERMISSIONS.equalsIgnoreCase(attributeParts[0])) {
JSONArray permissionsJSONArray = getJsonArray(operation);
// Assign permissions to the Role.
if (oldResource instanceof Role) {
((Role) oldResource).setPermissions(decoder.toList(permissionsJSONArray));
}
}
// Create and add the attribute.
createAttributeOnResourceWithPathWithoutFiltersForLevelOne(oldResource, schema, decoder, operation, attributeParts);
}
}
use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.
the class PatchOperationUtil method doPatchRemoveWithFiltersForLevelOne.
/*
*
* @param oldResource
* @param attributeParts
* @param expressionNode
* @return
* @throws BadRequestException
* @throws CharonException
*/
private static AbstractSCIMObject doPatchRemoveWithFiltersForLevelOne(AbstractSCIMObject oldResource, String[] attributeParts, ExpressionNode expressionNode) throws BadRequestException, CharonException {
Attribute attribute = oldResource.getAttribute(attributeParts[0]);
if (attribute != null) {
if (!attribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
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.getRequired().equals(true)) {
throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
} else {
iterator.remove();
}
}
}
// if the attribute has no values, make it unassigned
if (((MultiValuedAttribute) (attribute)).getAttributePrimitiveValues().size() == 0) {
oldResource.deleteAttribute(attribute.getName());
}
} 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.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) (attribute)).getAttributeValues().size() == 0) {
oldResource.deleteAttribute(attribute.getName());
}
}
} else {
// this is complex attribute which has multi valued primitive sub attribute.
Attribute subAttribute = attribute.getSubAttribute(expressionNode.getAttributeValue());
if (subAttribute != null) {
if (subAttribute.getMultiValued() && !subAttribute.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
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.getRequired().equals(true)) {
throw new BadRequestException("Can not remove a required attribute or a read-only attribute", ResponseCodeConstants.MUTABILITY);
} else {
iterator.remove();
}
}
}
// if the subAttribute has no values, make it unassigned
if (((MultiValuedAttribute) (subAttribute)).getAttributePrimitiveValues().size() == 0) {
((ComplexAttribute) (attribute)).removeSubAttribute(subAttribute.getName());
}
} else {
throw new BadRequestException("Sub attribute : " + expressionNode.getAttributeValue() + " " + "is not a primitive multivalued attribute.", ResponseCodeConstants.INVALID_PATH);
}
} else {
throw new BadRequestException("No sub attribute with the name : " + expressionNode.getAttributeValue() + " " + "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;
}
use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.
the class RoleResourceManager method processRoleList.
/**
* Method to process a list and return a SCIM response.
*
* @param roleList Filtered role list.
* @param encoder Json encoder.
* @param startIndex Starting index.
* @return SCIM response.
* @throws CharonException CharonException.
* @throws BadRequestException BadRequestException.
*/
private SCIMResponse processRoleList(List<Object> roleList, JSONEncoder encoder, int startIndex) throws CharonException, BadRequestException {
int totalResults = 0;
if (roleList == null) {
roleList = Collections.emptyList();
} else {
if (roleList.size() >= 1) {
if (roleList.get(0) instanceof Integer) {
totalResults = (int) roleList.get(0);
roleList.remove(0);
} else {
if (logger.isDebugEnabled()) {
logger.debug("First element in the list is not an int. Setting result count as: " + roleList.size());
}
totalResults = roleList.size();
}
}
}
for (Object role : roleList) {
ServerSideValidator.validateSCIMObjectForRequiredAttributes((Role) role, SCIMSchemaDefinitions.SCIM_ROLE_SCHEMA);
}
// Create a listed resource object out of the returned groups list.
ListedResource listedResource = createListedResource(roleList, startIndex, totalResults);
// Convert the listed resource into specific format.
String encodedListedResource = encoder.encodeSCIMObject(listedResource);
Map<String, String> responseHeaders = new HashMap<>();
responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedListedResource, responseHeaders);
}
use of org.wso2.charon.core.exceptions.CharonException in project charon by wso2.
the class RoleResourceManager method listWithPOSTRole.
@Override
public SCIMResponse listWithPOSTRole(String searchRequest, RoleManager roleManager) {
try {
if (roleManager == null) {
String error = "Provided role manager is null.";
throw new InternalErrorException(error);
}
JSONEncoder encoder = getEncoder();
JSONDecoder decoder = getDecoder();
SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getRoleResourceSchema();
// Create the search request object.
SearchRequest searchRequestObject = decoder.decodeSearchRequestBody(searchRequest, schema);
searchRequestObject.setCount(ResourceManagerUtil.processCount(searchRequestObject.getCountStr()));
searchRequestObject.setStartIndex(ResourceManagerUtil.processStartIndex(searchRequestObject.getStartIndexStr()));
if (searchRequestObject.getSchema() != null && !searchRequestObject.getSchema().equals(SCIMConstants.SEARCH_SCHEMA_URI)) {
throw new BadRequestException("Provided schema is invalid.", ResponseCodeConstants.INVALID_VALUE);
}
// Check whether provided sortOrder is valid or not.
if (searchRequestObject.getSortOder() != null) {
if (!(searchRequestObject.getSortOder().equalsIgnoreCase(SCIMConstants.OperationalConstants.ASCENDING) || searchRequestObject.getSortOder().equalsIgnoreCase(SCIMConstants.OperationalConstants.DESCENDING))) {
String error = " Invalid sortOrder value is specified";
throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
}
}
// ascending.
if (searchRequestObject.getSortOder() == null && searchRequestObject.getSortBy() != null) {
searchRequestObject.setSortOder(SCIMConstants.OperationalConstants.ASCENDING);
}
List<Object> rolesList = roleManager.listRolesWithPost(searchRequestObject);
int totalResults = (int) rolesList.get(0);
rolesList.remove(0);
List<Object> returnedRoles = rolesList;
for (Object role : returnedRoles) {
ServerSideValidator.validateRetrievedSCIMObjectInList((Role) role, schema, searchRequestObject.getAttributesAsString(), searchRequestObject.getExcludedAttributesAsString());
}
// Create a listed resource object out of the returned users list.
ListedResource listedResource = createListedResource(returnedRoles, searchRequestObject.getStartIndex(), totalResults);
String encodedListedResource = encoder.encodeSCIMObject(listedResource);
Map<String, String> responseHeaders = new HashMap<>();
responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedListedResource, responseHeaders);
} catch (CharonException | InternalErrorException | BadRequestException | NotImplementedException e) {
return AbstractResourceManager.encodeSCIMException(e);
}
}
Aggregations