Search in sources :

Example 1 with FilterTreeManager

use of org.wso2.charon3.core.utils.codeutils.FilterTreeManager in project charon by wso2.

the class UserResourceManager method listWithGET.

/*
     * To list all the resources of resource endpoint.
     *
     * @param usermanager
     * @param filter
     * @param startIndex
     * @param count
     * @param sortBy
     * @param sortOrder
     * @param attributes
     * @param excludeAttributes
     * @return
     */
public SCIMResponse listWithGET(UserManager userManager, String filter, int startIndex, int count, String sortBy, String sortOrder, String attributes, String excludeAttributes) {
    FilterTreeManager filterTreeManager = null;
    Node rootNode = null;
    JSONEncoder encoder = null;
    try {
        // According to SCIM 2.0 spec minus values will be considered as 0
        if (count < 0) {
            count = 0;
        }
        // According to SCIM 2.0 spec minus values will be considered as 1
        if (startIndex < 1) {
            startIndex = 1;
        }
        if (sortOrder != null) {
            if (!(sortOrder.equalsIgnoreCase(SCIMConstants.OperationalConstants.ASCENDING) || sortOrder.equalsIgnoreCase(SCIMConstants.OperationalConstants.DESCENDING))) {
                String error = " Invalid sortOrder value is specified";
                throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
            }
        }
        // ascending.
        if (sortOrder == null && sortBy != null) {
            sortOrder = SCIMConstants.OperationalConstants.ASCENDING;
        }
        // unless configured returns core-user schema or else returns extended user schema)
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getUserResourceSchema();
        if (filter != null) {
            filterTreeManager = new FilterTreeManager(filter, schema);
            rootNode = filterTreeManager.buildTree();
        }
        // obtain the json encoder
        encoder = getEncoder();
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        List<Object> returnedUsers;
        int totalResults = 0;
        // API user should pass a usermanager usermanager to UserResourceEndpoint.
        if (userManager != null) {
            List<Object> tempList = userManager.listUsersWithGET(rootNode, startIndex, count, sortBy, sortOrder, requiredAttributes);
            totalResults = (int) tempList.get(0);
            tempList.remove(0);
            returnedUsers = tempList;
            for (Object user : returnedUsers) {
                // perform service provider side validation.
                ServerSideValidator.validateRetrievedSCIMObjectInList((User) user, schema, attributes, excludeAttributes);
            }
            // create a listed resource object out of the returned users list.
            ListedResource listedResource = createListedResource(returnedUsers, startIndex, 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);
    } catch (IOException e) {
        String error = "Error in tokenization of the input filter";
        CharonException charonException = new CharonException(error);
        return AbstractResourceManager.encodeSCIMException(charonException);
    }
}
Also used : HashMap(java.util.HashMap) Node(org.wso2.charon3.core.utils.codeutils.Node) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) IOException(java.io.IOException) FilterTreeManager(org.wso2.charon3.core.utils.codeutils.FilterTreeManager) 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)

Example 2 with FilterTreeManager

use of org.wso2.charon3.core.utils.codeutils.FilterTreeManager 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 3 with FilterTreeManager

use of org.wso2.charon3.core.utils.codeutils.FilterTreeManager in project charon by wso2.

the class GroupResourceManager method listWithGET.

/*
     * Method to list the groups at the /Groups endpoint
     *
     * @param usermanager
     * @param filter
     * @param startIndex
     * @param count
     * @param sortBy
     * @param sortOrder
     * @param attributes
     * @param excludeAttributes
     * @return
     */
@Override
public SCIMResponse listWithGET(UserManager userManager, String filter, int startIndex, int count, String sortBy, String sortOrder, String attributes, String excludeAttributes) {
    // According to SCIM 2.0 spec minus values will be considered as 0
    if (count < 0) {
        count = 0;
    }
    // According to SCIM 2.0 spec minus values will be considered as 1
    if (startIndex < 1) {
        startIndex = 1;
    }
    FilterTreeManager filterTreeManager = null;
    Node rootNode = null;
    JSONEncoder encoder = null;
    try {
        // check whether provided sortOrder is valid or not
        if (sortOrder != null) {
            if (!(sortOrder.equalsIgnoreCase(SCIMConstants.OperationalConstants.ASCENDING) || sortOrder.equalsIgnoreCase(SCIMConstants.OperationalConstants.DESCENDING))) {
                String error = " Invalid sortOrder value is specified";
                throw new BadRequestException(error, ResponseCodeConstants.INVALID_VALUE);
            }
        }
        // ascending.
        if (sortOrder == null && sortBy != null) {
            sortOrder = SCIMConstants.OperationalConstants.ASCENDING;
        }
        // unless configured returns core-user schema or else returns extended user schema)
        SCIMResourceTypeSchema schema = SCIMResourceSchemaManager.getInstance().getGroupResourceSchema();
        if (filter != null) {
            filterTreeManager = new FilterTreeManager(filter, schema);
            rootNode = filterTreeManager.buildTree();
        }
        // obtain the json encoder
        encoder = getEncoder();
        // get the URIs of required attributes which must be given a value
        Map<String, Boolean> requiredAttributes = ResourceManagerUtil.getOnlyRequiredAttributesURIs((SCIMResourceTypeSchema) CopyUtil.deepCopy(schema), attributes, excludeAttributes);
        List<Object> returnedGroups;
        int totalResults = 0;
        // API group should pass a usermanager usermanager to GroupResourceEndpoint.
        if (userManager != null) {
            List<Object> tempList = userManager.listGroupsWithGET(rootNode, startIndex, count, sortBy, sortOrder, requiredAttributes);
            totalResults = (int) tempList.get(0);
            tempList.remove(0);
            returnedGroups = tempList;
            for (Object group : returnedGroups) {
                // perform service provider side validation.
                ServerSideValidator.validateRetrievedSCIMObjectInList((Group) group, SCIMSchemaDefinitions.SCIM_GROUP_SCHEMA, attributes, excludeAttributes);
            }
            // create a listed resource object out of the returned groups list.
            ListedResource listedResource = createListedResource(returnedGroups, startIndex, 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 encodeSCIMException(e);
    } catch (NotFoundException e) {
        return encodeSCIMException(e);
    } catch (InternalErrorException e) {
        return encodeSCIMException(e);
    } catch (BadRequestException e) {
        return encodeSCIMException(e);
    } catch (NotImplementedException e) {
        return encodeSCIMException(e);
    } catch (IOException e) {
        String error = "Error in tokenization of the input filter";
        CharonException charonException = new CharonException(error);
        return AbstractResourceManager.encodeSCIMException(charonException);
    }
}
Also used : HashMap(java.util.HashMap) Node(org.wso2.charon3.core.utils.codeutils.Node) NotImplementedException(org.wso2.charon3.core.exceptions.NotImplementedException) NotFoundException(org.wso2.charon3.core.exceptions.NotFoundException) InternalErrorException(org.wso2.charon3.core.exceptions.InternalErrorException) IOException(java.io.IOException) FilterTreeManager(org.wso2.charon3.core.utils.codeutils.FilterTreeManager) 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

IOException (java.io.IOException)3 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)3 FilterTreeManager (org.wso2.charon3.core.utils.codeutils.FilterTreeManager)3 Node (org.wso2.charon3.core.utils.codeutils.Node)3 HashMap (java.util.HashMap)2 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)2 CharonException (org.wso2.charon3.core.exceptions.CharonException)2 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)2 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)2 NotImplementedException (org.wso2.charon3.core.exceptions.NotImplementedException)2 ListedResource (org.wso2.charon3.core.objects.ListedResource)2 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)2 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)2 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONTokener (org.json.JSONTokener)1 SearchRequest (org.wso2.charon3.core.utils.codeutils.SearchRequest)1