Search in sources :

Example 21 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project carbon-identity-framework by wso2.

the class IdentityProviderManager method getExpressionNodes.

/**
 * Get the filter node as a list.
 *
 * @param filter value of the filter.
 * @return node tree.
 * @throws IdentityProviderManagementClientException Error when validate filters.
 */
private List<ExpressionNode> getExpressionNodes(String filter) throws IdentityProviderManagementClientException {
    // Filter example : name sw "te" and name ew "st" and isEnabled eq "true".
    List<ExpressionNode> expressionNodes = new ArrayList<>();
    FilterTreeBuilder filterTreeBuilder;
    try {
        if (StringUtils.isNotBlank(filter)) {
            filterTreeBuilder = new FilterTreeBuilder(filter);
            Node rootNode = filterTreeBuilder.buildTree();
            setExpressionNodeList(rootNode, expressionNodes);
        }
    } catch (IOException | IdentityException e) {
        String message = "Error occurred while validate filter, filter: " + filter;
        throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message, e);
    }
    return expressionNodes;
}
Also used : FilterTreeBuilder(org.wso2.carbon.identity.core.model.FilterTreeBuilder) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) OperationNode(org.wso2.carbon.identity.core.model.OperationNode) ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) Node(org.wso2.carbon.identity.core.model.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IdentityException(org.wso2.carbon.identity.base.IdentityException)

Example 22 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project carbon-identity-framework by wso2.

the class IdPManagementDAO method appendFilterQuery.

/**
 * Create a sql query and prepared statement for filter.
 *
 * @param expressionNodes    list of filters.
 * @param filterQueryBuilder Sql builder object.
 * @throws IdentityProviderManagementClientException throw invalid filer attribute exception.
 */
private void appendFilterQuery(List<ExpressionNode> expressionNodes, FilterQueryBuilder filterQueryBuilder) throws IdentityProviderManagementClientException {
    StringBuilder filter = new StringBuilder();
    if (CollectionUtils.isEmpty(expressionNodes)) {
        filterQueryBuilder.setFilterQuery(IdPManagementConstants.EMPTY_STRING);
    } else {
        for (ExpressionNode expressionNode : expressionNodes) {
            String operation = expressionNode.getOperation();
            String value = expressionNode.getValue();
            String attributeName = expressionNode.getAttributeValue();
            if (StringUtils.isNotBlank(attributeName) && StringUtils.isNotBlank(value) && StringUtils.isNotBlank(operation)) {
                switch(attributeName) {
                    case IdPManagementConstants.IDP_NAME:
                        attributeName = IdPManagementConstants.NAME;
                        break;
                    case IdPManagementConstants.IDP_DESCRIPTION:
                        attributeName = IdPManagementConstants.DESCRIPTION;
                        break;
                    case IdPManagementConstants.IDP_HOME_REALM_ID:
                        attributeName = IdPManagementConstants.HOME_REALM_ID;
                        break;
                    case IdPManagementConstants.IDP_IS_ENABLED:
                        attributeName = IdPManagementConstants.IS_ENABLED;
                        break;
                    case IdPManagementConstants.IDP_UUID:
                        attributeName = IdPManagementConstants.UUID;
                        break;
                    default:
                        String message = "Invalid filter attribute name. Filter attribute : " + attributeName;
                        throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message);
                }
                if (IdPManagementConstants.EQ.equals(operation)) {
                    filter.append(attributeName).append(" = ? AND ");
                    filterQueryBuilder.setFilterAttributeValue(value);
                } else if (IdPManagementConstants.SW.equals(operation)) {
                    filter.append(attributeName).append(" like ? AND ");
                    filterQueryBuilder.setFilterAttributeValue(value + "%");
                } else if (IdPManagementConstants.EW.equals(operation)) {
                    filter.append(attributeName).append(" like ? AND ");
                    filterQueryBuilder.setFilterAttributeValue("%" + value);
                } else if (IdPManagementConstants.CO.equals(operation)) {
                    filter.append(attributeName).append(" like ? AND ");
                    filterQueryBuilder.setFilterAttributeValue("%" + value + "%");
                } else {
                    String message = "Invalid filter value. filter: " + operation;
                    throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message);
                }
            }
        }
        if (StringUtils.isBlank(filter.toString())) {
            filterQueryBuilder.setFilterQuery(IdPManagementConstants.EMPTY_STRING);
        } else {
            filterQueryBuilder.setFilterQuery(filter.toString());
        }
    }
}
Also used : ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode)

Example 23 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project carbon-identity-framework by wso2.

the class IdPManagementDAO method getCountOfFilteredIdPs.

/**
 * Get number of IdP count for a matching filter.
 *
 * @param tenantId       Tenant Id of the identity provider.
 * @param expressionNode filter value list for IdP search.
 * @return number of IdP count for a given filter
 * @throws IdentityProviderManagementServerException Error when getting count of Identity Providers.
 * @throws IdentityProviderManagementClientException Error when append the filer string.
 */
int getCountOfFilteredIdPs(int tenantId, List<ExpressionNode> expressionNode) throws IdentityProviderManagementServerException, IdentityProviderManagementClientException {
    String sqlStmt = IdPManagementConstants.SQLQueries.GET_IDP_COUNT_SQL;
    int countOfFilteredIdp = 0;
    FilterQueryBuilder filterQueryBuilder = new FilterQueryBuilder();
    appendFilterQuery(expressionNode, filterQueryBuilder);
    Map<Integer, String> filterAttributeValue = filterQueryBuilder.getFilterAttributeValue();
    sqlStmt = sqlStmt + filterQueryBuilder.getFilterQuery() + IdPManagementConstants.SQLQueries.GET_IDP_COUNT_SQL_TAIL;
    try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false);
        PreparedStatement prepStmt = dbConnection.prepareStatement(sqlStmt)) {
        for (Map.Entry<Integer, String> prepareStatement : filterAttributeValue.entrySet()) {
            prepStmt.setString(prepareStatement.getKey(), prepareStatement.getValue());
        }
        prepStmt.setInt(filterAttributeValue.entrySet().size() + 1, tenantId);
        try (ResultSet rs = prepStmt.executeQuery()) {
            if (rs.next()) {
                countOfFilteredIdp = Integer.parseInt(rs.getString(1));
            }
        }
    } catch (SQLException e) {
        String message = "Error occurred while retrieving Identity Provider count for a tenant : " + IdentityTenantUtil.getTenantDomain(tenantId);
        throw IdPManagementUtil.handleServerException(IdPManagementConstants.ErrorMessage.ERROR_CODE_CONNECTING_DATABASE, message, e);
    }
    return countOfFilteredIdp;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) FilterQueryBuilder(org.wso2.carbon.idp.mgt.model.FilterQueryBuilder) Map(java.util.Map) HashMap(java.util.HashMap)

Example 24 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project carbon-identity-framework by wso2.

the class IdPManagementDAO method getIdPsSearch.

/**
 * Get all identity provider's Basic information along with additionally requested information depends on the
 * requiredAttributes for a given matching filter.
 *
 * @param tenantId           Tenant Id of the identity provider.
 * @param expressionNode     List of filter value for IdP search.
 * @param limit              Limit per page.
 * @param offset             Offset value.
 * @param sortOrder          Order of IdP ASC/DESC.
 * @param sortBy             The attribute need to sort.
 * @param requiredAttributes Required attributes which needs to be return.
 * @return Identity Provider's Basic Information array along with requested attribute information.
 * @throws IdentityProviderManagementServerException Error when getting list of Identity Providers.
 * @throws IdentityProviderManagementClientException Error when append the filer string.
 */
List<IdentityProvider> getIdPsSearch(int tenantId, List<ExpressionNode> expressionNode, int limit, int offset, String sortOrder, String sortBy, List<String> requiredAttributes) throws IdentityProviderManagementServerException, IdentityProviderManagementClientException {
    FilterQueryBuilder filterQueryBuilder = new FilterQueryBuilder();
    appendFilterQuery(expressionNode, filterQueryBuilder);
    String sortedOrder = sortBy + " " + sortOrder;
    try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false);
        ResultSet resultSet = getIdpQueryResultSet(dbConnection, sortedOrder, tenantId, offset, limit, filterQueryBuilder, requiredAttributes)) {
        return populateIdentityProviderList(resultSet, dbConnection, requiredAttributes, tenantId);
    } catch (SQLException e) {
        String message = "Error occurred while retrieving Identity Provider for tenant: " + IdentityTenantUtil.getTenantDomain(tenantId);
        throw IdPManagementUtil.handleServerException(IdPManagementConstants.ErrorMessage.ERROR_CODE_CONNECTING_DATABASE, message, e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) FilterQueryBuilder(org.wso2.carbon.idp.mgt.model.FilterQueryBuilder)

Example 25 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project carbon-identity-framework by wso2.

the class IdPManagementDAOTest method getIdPsSearchWithExpressionNodesData.

@DataProvider
public Object[][] getIdPsSearchWithExpressionNodesData() {
    ExpressionNode expressionNode1 = new ExpressionNode();
    expressionNode1.setAttributeValue("name");
    expressionNode1.setOperation("co");
    expressionNode1.setValue("IdP");
    List<ExpressionNode> expressionNodesList1 = new ArrayList<>();
    expressionNodesList1.add(expressionNode1);
    ExpressionNode expressionNode2 = new ExpressionNode();
    List<ExpressionNode> expressionNodesList2 = new ArrayList<>();
    expressionNode2.setAttributeValue("name");
    expressionNode2.setOperation("eq");
    expressionNode2.setValue("testIdP1");
    expressionNodesList2.add(expressionNode2);
    ExpressionNode expressionNode3 = new ExpressionNode();
    List<ExpressionNode> expressionNodesList3 = new ArrayList<>();
    expressionNode3.setAttributeValue("name");
    expressionNode3.setOperation("ew");
    expressionNode3.setValue("2");
    expressionNodesList3.add(expressionNode3);
    return new Object[][] { { SAMPLE_TENANT_ID, expressionNodesList1, 2, 0, "ASC", "NAME", 2, "testIdP1" }, { SAMPLE_TENANT_ID, expressionNodesList1, 2, 0, "DESC", "NAME", 2, "testIdP2" }, { SAMPLE_TENANT_ID2, expressionNodesList1, 1, 1, "ASC", "NAME", 0, "" }, { SAMPLE_TENANT_ID, expressionNodesList2, 1, 0, "ASC", "NAME", 1, "testIdP1" }, { SAMPLE_TENANT_ID, expressionNodesList3, 1, 0, "ASC", "NAME", 1, "testIdP2" } };
}
Also used : ExpressionNode(org.wso2.carbon.identity.core.model.ExpressionNode) ArrayList(java.util.ArrayList) DataProvider(org.testng.annotations.DataProvider)

Aggregations

ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)21 ArrayList (java.util.ArrayList)16 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)16 ExpressionNode (org.wso2.carbon.identity.core.model.ExpressionNode)15 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)12 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)8 ExpressionNode (org.wso2.charon3.core.utils.codeutils.ExpressionNode)7 Map (java.util.Map)6 IdentityException (org.wso2.carbon.identity.base.IdentityException)6 Attribute (org.wso2.charon3.core.attributes.Attribute)6 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)6 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)6 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)6 Connection (java.sql.Connection)5 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)5 JSONObject (org.json.JSONObject)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 Node (org.wso2.carbon.identity.core.model.Node)5 IdpSearchResult (org.wso2.carbon.idp.mgt.model.IdpSearchResult)5