use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.
the class JSONDecoder method buildComplexMultiValuedAttribute.
/*
* Return complex type multi valued attribute with the user defined
* value included and necessary attribute characteristics set
* @param attributeSchema - Attribute schema
* @param attributeValues - values for the attribute
* @return MultiValuedAttribute
*/
public MultiValuedAttribute buildComplexMultiValuedAttribute(AttributeSchema attributeSchema, JSONArray attributeValues) throws CharonException, BadRequestException {
try {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
List<Attribute> complexAttributeValues = new ArrayList<Attribute>();
// iterate through JSONArray and create the list of string values.
for (int i = 0; i < attributeValues.length(); i++) {
Object attributeValue = attributeValues.get(i);
if (attributeValue instanceof JSONObject) {
JSONObject complexAttributeValue = (JSONObject) attributeValue;
complexAttributeValues.add(buildComplexValue(attributeSchema, complexAttributeValue));
} else {
String error = "Unknown JSON representation for the MultiValued attribute " + attributeSchema.getName() + " which has data type as " + attributeSchema.getType();
throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
}
}
multiValuedAttribute.setAttributeValues(complexAttributeValues);
return (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
} catch (JSONException e) {
String error = "Error in accessing JSON value of multivalued attribute";
throw new CharonException(error, e);
}
}
use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.
the class JSONDecoder method buildPrimitiveMultiValuedAttribute.
/*
* Return a primitive type multi valued attribute with the user defined value included and necessary
* attribute characteristics set
*
* @param attributeSchema - Attribute schema
* @param attributeValues - values for the attribute
* @return MultiValuedAttribute
*/
public MultiValuedAttribute buildPrimitiveMultiValuedAttribute(AttributeSchema attributeSchema, JSONArray attributeValues) throws CharonException, BadRequestException {
try {
MultiValuedAttribute multiValuedAttribute = new MultiValuedAttribute(attributeSchema.getName());
List<Object> primitiveValues = new ArrayList<Object>();
// iterate through JSONArray and create the list of string values.
for (int i = 0; i < attributeValues.length(); i++) {
Object attributeValue = attributeValues.get(i);
if (attributeValue instanceof String || attributeValue instanceof Boolean || attributeValue instanceof Integer || attributeValue == null) {
// If an attribute is passed without a value, no need to save it.
if (attributeValue == null) {
continue;
}
primitiveValues.add(attributeValue);
} else {
String error = "Unknown JSON representation for the MultiValued attribute " + attributeSchema.getName() + " which has data type as " + attributeSchema.getType();
throw new BadRequestException(error, ResponseCodeConstants.INVALID_SYNTAX);
}
}
multiValuedAttribute.setAttributePrimitiveValues(primitiveValues);
return (MultiValuedAttribute) DefaultAttributeFactory.createAttribute(attributeSchema, multiValuedAttribute);
} catch (JSONException e) {
String error = "Error in accessing JSON value of multivalued attribute";
throw new CharonException(error, e);
}
}
use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.
the class JSONEncoder method getSCIMObjectAsJSONObject.
/*
* Make JSON object from given SCIM object.
*
* @param scimObject
* @return the resulting string after encoding.
*/
public JSONObject getSCIMObjectAsJSONObject(SCIMObject scimObject) throws CharonException {
// root json object containing the encoded SCIM Object.
JSONObject rootObject = new JSONObject();
try {
// encode schemas
this.encodeArrayOfValues(SCIMConstants.CommonSchemaConstants.SCHEMAS, (scimObject.getSchemaList()).toArray(), rootObject);
// encode attribute list
Map<String, Attribute> attributes = scimObject.getAttributeList();
if (attributes != null && !attributes.isEmpty()) {
for (Attribute attribute : attributes.values()) {
// using instanceof instead of polymorphic way, in order to make encoder pluggable.
if (attribute instanceof SimpleAttribute) {
encodeSimpleAttribute((SimpleAttribute) attribute, rootObject);
} else if (attribute instanceof ComplexAttribute) {
encodeComplexAttribute((ComplexAttribute) attribute, rootObject);
} else if (attribute instanceof MultiValuedAttribute) {
encodeMultiValuedAttribute((MultiValuedAttribute) attribute, rootObject);
}
}
}
} catch (JSONException e) {
String errorMessage = "Error in encoding resource..";
// TODO:log the error
throw new CharonException(errorMessage);
}
return rootObject;
}
use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.
the class AbstractSCIMObject method deleteSubSubAttribute.
/*
* This deletion method is only applicable for extension schema
* Deleting a sub attribute of complex attribute is the responsibility of an attribute holder.
*
* @param grandParentAttribute
* @param parentAttribute
* @param childAttribute
*/
public void deleteSubSubAttribute(String childAttribute, String parentAttribute, String grandParentAttribute) throws CharonException {
if (attributeList.containsKey(grandParentAttribute)) {
ComplexAttribute grandParent = (ComplexAttribute) attributeList.get(grandParentAttribute);
Attribute parent = ((ComplexAttribute) grandParent).getSubAttribute(parentAttribute);
((ComplexAttribute) (parent)).removeSubAttribute(childAttribute);
}
}
use of org.wso2.charon3.core.exceptions.CharonException in project charon by wso2.
the class AbstractSCIMObject method setResourceType.
/*
* set the resourceType of the meta attribute
*
* @param resourceType
*/
public void setResourceType(String resourceType) throws BadRequestException, CharonException {
// create the resourceType attribute as defined in schema.
SimpleAttribute resourceTypeAttribute = (SimpleAttribute) DefaultAttributeFactory.createAttribute(SCIMSchemaDefinitions.RESOURCE_TYPE, new SimpleAttribute(SCIMConstants.CommonSchemaConstants.RESOURCE_TYPE, resourceType));
// check meta complex attribute already exist.
if (getMetaAttribute() != null) {
ComplexAttribute metaAttribute = getMetaAttribute();
// check version attribute already exist
if (metaAttribute.isSubAttributeExist(resourceTypeAttribute.getName())) {
String error = "Read only attribute is tried to modify";
throw new CharonException(error);
} else {
metaAttribute.setSubAttribute(resourceTypeAttribute);
}
} else {
// create meta attribute and set the sub attribute.
createMetaAttribute();
getMetaAttribute().setSubAttribute(resourceTypeAttribute);
}
}
Aggregations