use of org.wso2.charon3.core.objects.SCIMObject in project charon by wso2.
the class ServerSideValidator method validateCreatedSCIMObject.
/*
* Validate created SCIMObject according to the spec
*
* @param scimObject
* @param resourceSchema
* @throw CharonException
* @throw BadRequestException
* @throw NotFoundException
*/
public static void validateCreatedSCIMObject(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException, NotFoundException {
if (scimObject instanceof User) {
// set display names for complex multivalued attributes
setDisplayNameInComplexMultiValuedAttributes(scimObject, resourceSchema);
}
// remove any read only attributes
removeAnyReadOnlyAttributes(scimObject, resourceSchema);
// add created and last modified dates
String id = UUID.randomUUID().toString();
scimObject.setId(id);
Date date = new Date();
// set the created date and time
scimObject.setCreatedDate(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(date)));
// creates date and the last modified are the same if not updated.
scimObject.setLastModified(AttributeUtil.parseDateTime(AttributeUtil.formatDateTime(date)));
// set location and resourceType
if (resourceSchema.isSchemaAvailable(SCIMConstants.USER_CORE_SCHEMA_URI)) {
String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.USER_ENDPOINT), scimObject.getId());
scimObject.setLocation(location);
scimObject.setResourceType(SCIMConstants.USER);
} else if (resourceSchema.isSchemaAvailable(SCIMConstants.GROUP_CORE_SCHEMA_URI)) {
String location = createLocationHeader(AbstractResourceManager.getResourceEndpointURL(SCIMConstants.GROUP_ENDPOINT), scimObject.getId());
scimObject.setLocation(location);
scimObject.setResourceType(SCIMConstants.GROUP);
}
// check for required attributes
validateSCIMObjectForRequiredAttributes(scimObject, resourceSchema);
validateSchemaList(scimObject, resourceSchema);
}
use of org.wso2.charon3.core.objects.SCIMObject in project charon by wso2.
the class AbstractValidator method validateSCIMObjectForRequiredAttributes.
/*
* Validate SCIMObject for required attributes given the object and the corresponding schema.
*
* @param scimObject
* @param resourceSchema
*/
public static void validateSCIMObjectForRequiredAttributes(AbstractSCIMObject scimObject, ResourceTypeSchema resourceSchema) throws BadRequestException, CharonException {
// get attributes from schema.
List<AttributeSchema> attributeSchemaList = resourceSchema.getAttributesList();
// get attribute list from scim object.
Map<String, Attribute> attributeList = scimObject.getAttributeList();
for (AttributeSchema attributeSchema : attributeSchemaList) {
// check for required attributes.
if (attributeSchema.getRequired()) {
if (!attributeList.containsKey(attributeSchema.getName())) {
String error = "Required attribute " + attributeSchema.getName() + " is missing in the SCIM " + "Object.";
throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
}
}
// check for required sub attributes.
AbstractAttribute attribute = (AbstractAttribute) attributeList.get(attributeSchema.getName());
validateSCIMObjectForRequiredSubAttributes(attribute, attributeSchema);
}
}
use of org.wso2.charon3.core.objects.SCIMObject in project charon by wso2.
the class AbstractValidator method setDisplayNameInComplexMultiValuedAttributes.
/*
* This method is basically for adding display sub attribute to multivalued attributes
* which has 'display' as a sub attribute in the respective attribute schema
*
* @param scimObject
* @param resourceSchema
* @throws CharonException
* @throws BadRequestException
*/
protected static void setDisplayNameInComplexMultiValuedAttributes(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException, BadRequestException {
Map<String, Attribute> attributeList = scimObject.getAttributeList();
ArrayList<AttributeSchema> attributeSchemaList = resourceSchema.getAttributesList();
for (AttributeSchema attributeSchema : attributeSchemaList) {
if (attributeSchema.getMultiValued() && attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
if (attributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY) != null) {
if (attributeList.containsKey(attributeSchema.getName())) {
Attribute multiValuedAttribute = attributeList.get(attributeSchema.getName());
setDisplayNameInComplexMultiValuedSubAttributes(multiValuedAttribute, attributeSchema);
}
}
} else if (attributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
// this is only valid for extension schema
List<SCIMAttributeSchema> subAttributeSchemaList = attributeSchema.getSubAttributeSchemas();
for (AttributeSchema subAttributeSchema : subAttributeSchemaList) {
if (subAttributeSchema.getMultiValued() && subAttributeSchema.getType().equals(SCIMDefinitions.DataType.COMPLEX)) {
if (subAttributeSchema.getSubAttributeSchema(SCIMConstants.CommonSchemaConstants.DISPLAY) != null) {
Attribute extensionAttribute = attributeList.get(attributeSchema.getName());
if (extensionAttribute != null) {
if ((((ComplexAttribute) extensionAttribute).getSubAttribute(subAttributeSchema.getName())) != null) {
Attribute multiValuedAttribute = (attributeList.get(attributeSchema.getName())).getSubAttribute(subAttributeSchema.getName());
setDisplayNameInComplexMultiValuedSubAttributes(multiValuedAttribute, subAttributeSchema);
}
}
}
}
}
}
}
}
use of org.wso2.charon3.core.objects.SCIMObject in project charon by wso2.
the class AbstractValidator method removeAnyReadOnlyAttributes.
/*
* Check for readonlyAttributes and remove them if they have been modified. - (create method)
*
* @param scimObject
* @param resourceSchema
* @throws CharonException
*/
public static void removeAnyReadOnlyAttributes(AbstractSCIMObject scimObject, SCIMResourceTypeSchema resourceSchema) throws CharonException {
// No need to check for immutable as immutable attributes can be defined at resource creation
// get attributes from schema.
List<AttributeSchema> attributeSchemaList = resourceSchema.getAttributesList();
// get attribute list from scim object.
Map<String, Attribute> attributeList = scimObject.getAttributeList();
for (AttributeSchema attributeSchema : attributeSchemaList) {
// check for read-only attributes.
if (attributeSchema.getMutability() == SCIMDefinitions.Mutability.READ_ONLY) {
if (attributeList.containsKey(attributeSchema.getName())) {
String error = "Read only attribute: " + attributeSchema.getName() + " is set from consumer in the SCIM Object. " + "Removing it.";
logger.debug(error);
scimObject.deleteAttribute(attributeSchema.getName());
}
}
// check for readonly sub attributes.
AbstractAttribute attribute = (AbstractAttribute) attributeList.get(attributeSchema.getName());
removeAnyReadOnlySubAttributes(attribute, attributeSchema);
}
}
use of org.wso2.charon3.core.objects.SCIMObject 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;
}
Aggregations