Search in sources :

Example 16 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject in project charon by wso2.

the class ServiceProviderConfigResourceManager method getServiceProviderConfig.

private SCIMResponse getServiceProviderConfig() {
    JSONEncoder encoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder
        JSONDecoder decoder = getDecoder();
        // get the service provider config schema
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getServiceProviderConfigResourceSchema();
        // create a string in json format with relevant values
        String scimObjectString = encoder.buildServiceProviderConfigJsonBody(CharonConfiguration.getInstance().getConfig());
        // decode the SCIM service provider config object, encoded in the submitted payload.
        AbstractSCIMObject serviceProviderConfigObject = (AbstractSCIMObject) decoder.decodeResource(scimObjectString, schema, new AbstractSCIMObject());
        // encode the newly created SCIM service provider config object and add id attribute to Location header.
        String encodedObject;
        Map<String, String> responseHeaders = new HashMap<String, String>();
        if (serviceProviderConfigObject != null) {
            // create a deep copy of the service provider config object since we are going to change it.
            AbstractSCIMObject copiedObject = (AbstractSCIMObject) CopyUtil.deepCopy(serviceProviderConfigObject);
            encodedObject = encoder.encodeSCIMObject(copiedObject);
            // add location header
            responseHeaders.put(SCIMConstants.LOCATION_HEADER, getResourceEndpointURL(SCIMConstants.SERVICE_PROVIDER_CONFIG_ENDPOINT));
            responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
        } else {
            String error = "Newly created User resource is null.";
            throw new InternalErrorException(error);
        }
        // put the uri of the service provider config object in the response header parameter.
        return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedObject, responseHeaders);
    } catch (CharonException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (JSONException e) {
        return null;
    }
}
Also used : AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) HashMap(java.util.HashMap) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) JSONException(org.json.JSONException) 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 17 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject 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);
    }
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute)

Example 18 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject 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);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) ArrayList(java.util.ArrayList) List(java.util.List)

Example 19 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject 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);
    }
}
Also used : MultiValuedAttribute(org.wso2.charon3.core.attributes.MultiValuedAttribute) ComplexAttribute(org.wso2.charon3.core.attributes.ComplexAttribute) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute) Attribute(org.wso2.charon3.core.attributes.Attribute) SimpleAttribute(org.wso2.charon3.core.attributes.SimpleAttribute) AbstractAttribute(org.wso2.charon3.core.attributes.AbstractAttribute)

Example 20 with AbstractSCIMObject

use of org.wso2.charon3.core.objects.AbstractSCIMObject in project charon by wso2.

the class JSONDecoder method decodeResource.

/**
 * Decode the resource string sent in the SCIM request payload.
 *
 * @param scimResourceString - json encoded string of user info
 * @param resourceSchema     - SCIM defined user schema
 * @param scimObject         - a container holding the attributes and schema list
 * @return SCIMObject
 */
public SCIMObject decodeResource(String scimResourceString, ResourceTypeSchema resourceSchema, AbstractSCIMObject scimObject) throws BadRequestException, CharonException, InternalErrorException {
    try {
        // decode the string into json representation
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        // get the attribute schemas list from the schema that defines the given resource
        List<AttributeSchema> attributeSchemas = resourceSchema.getAttributesList();
        // set the schemas in scimobject
        for (int i = 0; i < resourceSchema.getSchemasList().size(); i++) {
            scimObject.setSchema(resourceSchema.getSchemasList().get(i));
        }
        // iterate through the schema and extract the attributes.
        for (AttributeSchema attributeSchema : attributeSchemas) {
            // obtain the user defined value for given key- attribute schema name
            Object attributeValObj = decodedJsonObj.opt(attributeSchema.getName());
            if (attributeValObj == null) {
                // user may define the attribute by its fully qualified uri
                attributeValObj = decodedJsonObj.opt(attributeSchema.getURI());
            }
            SCIMDefinitions.DataType attributeSchemaDataType = attributeSchema.getType();
            if (attributeSchemaDataType.equals(STRING) || attributeSchemaDataType.equals(BINARY) || attributeSchemaDataType.equals(BOOLEAN) || attributeSchemaDataType.equals(DATE_TIME) || attributeSchemaDataType.equals(DECIMAL) || attributeSchemaDataType.equals(INTEGER) || attributeSchemaDataType.equals(REFERENCE)) {
                if (!attributeSchema.getMultiValued()) {
                    if (attributeValObj instanceof String || attributeValObj instanceof Boolean || attributeValObj instanceof Integer || attributeValObj == null) {
                        // If an attribute is passed without a value, no need to save it.
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding schema data type is String/Boolean/Binary/Decimal/Integer/DataTime
                        // or Reference, it is a SimpleAttribute.
                        scimObject.setAttribute(buildSimpleAttribute(attributeSchema, attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the simple attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                } else {
                    if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                        // If an attribute is passed without a value, no need to save it.
                        if (attributeValObj == null) {
                            continue;
                        }
                        scimObject.setAttribute(buildPrimitiveMultiValuedAttribute(attributeSchema, (JSONArray) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the primitive multivalued attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                }
            } else if (attributeSchemaDataType.equals(COMPLEX)) {
                if (attributeSchema.getMultiValued() == true) {
                    if (attributeValObj instanceof JSONArray || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding json value object is JSONArray, it is a MultiValuedAttribute.
                        scimObject.setAttribute(buildComplexMultiValuedAttribute(attributeSchema, (JSONArray) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the complex multivalued attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                } else if (attributeSchema.getMultiValued() == false) {
                    if (attributeValObj instanceof JSONObject || attributeValObj == null) {
                        if (attributeValObj == null) {
                            continue;
                        }
                        // if the corresponding json value object is JSONObject, it is a ComplexAttribute.
                        scimObject.setAttribute(buildComplexAttribute(attributeSchema, (JSONObject) attributeValObj), resourceSchema);
                    } else {
                        logger.error("Error decoding the complex attribute");
                        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
                    }
                }
            }
        }
        return scimObject;
    } catch (JSONException e) {
        logger.error("json error in decoding the resource");
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
}
Also used : JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) SCIMDefinitions(org.wso2.charon3.core.schema.SCIMDefinitions) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) AttributeSchema(org.wso2.charon3.core.schema.AttributeSchema) SCIMAttributeSchema(org.wso2.charon3.core.schema.SCIMAttributeSchema) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) JSONObject(org.json.JSONObject) SCIMObject(org.wso2.charon3.core.objects.SCIMObject)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)18 Attribute (org.wso2.charon3.core.attributes.Attribute)16 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)16 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)16 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)15 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)11 JSONException (org.json.JSONException)8 JSONObject (org.json.JSONObject)7 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)7 JSONArray (org.json.JSONArray)6 Map (java.util.Map)5 JSONTokener (org.json.JSONTokener)5 AbstractAttribute (org.wso2.charon3.core.attributes.AbstractAttribute)4 CharonException (org.wso2.charon3.core.exceptions.CharonException)4 List (java.util.List)3 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)3 User (org.wso2.charon3.core.objects.User)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2