Search in sources :

Example 21 with SCIMResourceTypeSchema

use of org.wso2.charon3.core.schema.SCIMResourceTypeSchema 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 22 with SCIMResourceTypeSchema

use of org.wso2.charon3.core.schema.SCIMResourceTypeSchema 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 23 with SCIMResourceTypeSchema

use of org.wso2.charon3.core.schema.SCIMResourceTypeSchema in project charon by wso2.

the class JSONDecoder method decodeSearchRequestBody.

/*
     * decode the raw string and create a search object
     * @param scimResourceString
     * @return
     * @throws BadRequestException
     */
public SearchRequest decodeSearchRequestBody(String scimResourceString, SCIMResourceTypeSchema schema) throws BadRequestException {
    FilterTreeManager filterTreeManager = null;
    Node rootNode = null;
    // decode the string and create search object
    try {
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        SearchRequest searchRequest = new SearchRequest();
        ArrayList<String> attributes = new ArrayList<>();
        ArrayList<String> excludedAttributes = new ArrayList<>();
        JSONArray attributesValues = (JSONArray) decodedJsonObj.opt(SCIMConstants.OperationalConstants.ATTRIBUTES);
        JSONArray excludedAttributesValues = (JSONArray) decodedJsonObj.opt(SCIMConstants.OperationalConstants.EXCLUDED_ATTRIBUTES);
        JSONArray schemas = (JSONArray) decodedJsonObj.opt(SCIMConstants.CommonSchemaConstants.SCHEMAS);
        if (schemas.length() != 1) {
            throw new BadRequestException("Schema is invalid", ResponseCodeConstants.INVALID_VALUE);
        }
        if (attributesValues != null) {
            for (int i = 0; i < attributesValues.length(); i++) {
                attributes.add((String) attributesValues.get(i));
            }
        }
        if (excludedAttributesValues != null) {
            for (int i = 0; i < excludedAttributesValues.length(); i++) {
                excludedAttributes.add((String) excludedAttributesValues.get(i));
            }
        }
        if (decodedJsonObj.opt(SCIMConstants.OperationalConstants.FILTER) != null) {
            filterTreeManager = new FilterTreeManager((String) decodedJsonObj.opt(SCIMConstants.OperationalConstants.FILTER), schema);
            rootNode = filterTreeManager.buildTree();
        }
        searchRequest.setAttributes(attributes);
        searchRequest.setExcludedAttributes(excludedAttributes);
        searchRequest.setSchema((String) schemas.get(0));
        searchRequest.setCountStr(decodedJsonObj.optString(SCIMConstants.OperationalConstants.COUNT));
        searchRequest.setStartIndexStr(decodedJsonObj.optString(SCIMConstants.OperationalConstants.START_INDEX));
        searchRequest.setFilter(rootNode);
        if (!decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_BY).equals("")) {
            searchRequest.setSortBy(decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_BY));
        }
        if (!decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_ORDER).equals("")) {
            searchRequest.setSortOder(decodedJsonObj.optString(SCIMConstants.OperationalConstants.SORT_ORDER));
        }
        return searchRequest;
    } catch (JSONException | IOException e) {
        logger.error("Error while decoding the resource string");
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
}
Also used : SearchRequest(org.wso2.charon3.core.utils.codeutils.SearchRequest) Node(org.wso2.charon3.core.utils.codeutils.Node) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) IOException(java.io.IOException) FilterTreeManager(org.wso2.charon3.core.utils.codeutils.FilterTreeManager) JSONTokener(org.json.JSONTokener) JSONObject(org.json.JSONObject) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException)

Example 24 with SCIMResourceTypeSchema

use of org.wso2.charon3.core.schema.SCIMResourceTypeSchema in project charon by wso2.

the class JSONDecoder method decode.

public AbstractSCIMObject decode(String scimResourceString, SCIMResourceTypeSchema schema) throws CharonException, BadRequestException {
    try {
        JSONObject decodedJsonObj = new JSONObject(new JSONTokener(scimResourceString));
        AbstractSCIMObject scimObject = null;
        if (schema.getSchemasList().contains(SCIMConstants.GROUP_CORE_SCHEMA_URI)) {
            scimObject = (AbstractSCIMObject) decodeResource(decodedJsonObj.toString(), schema, new Group());
        } else {
            scimObject = (AbstractSCIMObject) decodeResource(decodedJsonObj.toString(), schema, new User());
        }
        return scimObject;
    } catch (JSONException | InternalErrorException | CharonException e) {
        throw new CharonException("Error in decoding the request", e);
    } catch (BadRequestException e) {
        throw new BadRequestException(ResponseCodeConstants.INVALID_SYNTAX);
    }
}
Also used : JSONTokener(org.json.JSONTokener) AbstractSCIMObject(org.wso2.charon3.core.objects.AbstractSCIMObject) Group(org.wso2.charon3.core.objects.Group) User(org.wso2.charon3.core.objects.User) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) BadRequestException(org.wso2.charon3.core.exceptions.BadRequestException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) CharonException(org.wso2.charon3.core.exceptions.CharonException)

Example 25 with SCIMResourceTypeSchema

use of org.wso2.charon3.core.schema.SCIMResourceTypeSchema in project charon by wso2.

the class GroupResourceManager method listWithPOST.

/*
     * this facilitates the querying using HTTP POST
     * @param resourceString
     * @param usermanager
     * @return
     */
@Override
public SCIMResponse listWithPOST(String resourceString, UserManager userManager) {
    JSONEncoder encoder = null;
    JSONDecoder decoder = null;
    try {
        // obtain the json encoder
        encoder = getEncoder();
        // obtain the json decoder
        decoder = getDecoder();
        // return core group schema
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        // create the search request object
        SearchRequest searchRequest = decoder.decodeSearchRequestBody(resourceString, schema);
        searchRequest.setCount(ResourceManagerUtil.processCount(searchRequest.getCountStr()));
        searchRequest.setStartIndex(ResourceManagerUtil.processCount(searchRequest.getStartIndexStr()));
        if (searchRequest.getSchema() != null && !searchRequest.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 (searchRequest.getSortOder() != null) {
            if (!(searchRequest.getSortOder().equalsIgnoreCase(SCIMConstants.OperationalConstants.ASCENDING) || searchRequest.getSortOder().equalsIgnoreCase(SCIMConstants.OperationalConstants.DESCENDING))) {
                String error = " Invalid sortOrder value is specified";
                throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
            }
        }
        // ascending.
        if (searchRequest.getSortOder() == null && searchRequest.getSortBy() != null) {
            searchRequest.setSortOder(SCIMConstants.OperationalConstants.ASCENDING);
        }
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), searchRequest.getAttributesAsString(), searchRequest.getExcludedAttributesAsString());
        List<Object> returnedGroups;
        int totalResults = 0;
        // API user should pass a usermanager usermanager to UserResourceEndpoint.
        if (userManager != null) {
            List<Object> tempList = userManager.listGroupsWithPost(searchRequest, requiredAttributes);
            totalResults = (int) tempList.get(0);
            tempList.remove(0);
            returnedGroups = tempList;
            for (Object group : returnedGroups) {
                // perform service provider side validation.
                ServerSideValidator.validateRetrievedSCIMObjectInList((Group) group, schema, searchRequest.getAttributesAsString(), searchRequest.getExcludedAttributesAsString());
            }
            // create a listed resource object out of the returned users list.
            ListedResource listedResource = createListedResource(returnedGroups, searchRequest.getStartIndex(), totalResults);
            // convert the listed resource into specific format.
            String encodedListedResource = encoder.encodeSCIMObject(listedResource);
            // if there are any http headers to be added in the response header.
            Map<String, String> responseHeaders = new HashMap<String, String>();
            responseHeaders.put(SCIMConstants.CONTENT_TYPE_HEADER, SCIMConstants.APPLICATION_JSON);
            return new SCIMResponse(ResponseCodeConstants.CODE_OK, encodedListedResource, responseHeaders);
        } else {
            String error = "Provided user manager handler is null.";
            // throw internal server error.
            throw new InternalErrorException(error);
        }
    } catch (CharonException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (NotFoundException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (BadRequestException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return AbstractResourceManager.encodeSCIMException(e);
    }
}
Also used : SearchRequest(org.wso2.charon3.core.utils.codeutils.SearchRequest) HashMap(java.util.HashMap) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) JSONDecoder(org.wso2.charon3.core.encoder.JSONDecoder) ListedResource(org.wso2.charon3.core.objects.ListedResource) 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)

Aggregations

BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)31 CharonException (org.wso2.charon3.core.exceptions.CharonException)21 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)21 HashMap (java.util.HashMap)18 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)18 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)17 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 JSONDecoder (org.wso2.charon3.core.encoder.JSONDecoder)14 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)14 User (org.wso2.charon3.core.objects.User)12 Attribute (org.wso2.charon3.core.attributes.Attribute)9 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)9 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)9 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)9 JSONException (org.json.JSONException)8 AbstractSCIMObject (org.wso2.charon3.core.objects.AbstractSCIMObject)8 AttributeSchema (org.wso2.charon3.core.schema.AttributeSchema)8 JSONArray (org.json.JSONArray)6 JSONObject (org.json.JSONObject)6