Search in sources :

Example 16 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class ApiDAOImpl method getCompositeAPIs.

@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<CompositeAPI> getCompositeAPIs(Set<String> roles, String user, int offset, int limit) throws APIMgtDAOException {
    // TODO: 6/5/17 Implement pagination support when implementing pagination support for
    // other list operations.
    final String query = COMPOSITE_API_SUMMARY_SELECT + " WHERE API_TYPE_ID = " + "(SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?) AND PROVIDER = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, ApiType.COMPOSITE.toString());
        statement.setString(2, user);
        return getCompositeAPISummaryList(connection, statement);
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting Composite APIs", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 17 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class ApiDAOImpl method searchAPIsByAttributeInStore.

/**
 * @see ApiDAO#searchAPIsByAttributeInStore(List roles, List labels, Map attributeMap, int offset, int limit)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<API> searchAPIsByAttributeInStore(List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = sqlStatements.prepareAttributeSearchStatementForStore(connection, roles, labels, attributeMap, offset, limit)) {
        DatabaseMetaData md = connection.getMetaData();
        Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            String tableName = null, columnName = null;
            if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
                // if the search is related to tags, need to check NAME column in AM_TAGS table
                tableName = connection.getMetaData().getDriverName().contains("PostgreSQL") ? AM_TAGS_TABLE_NAME.toLowerCase(Locale.ENGLISH) : AM_TAGS_TABLE_NAME;
                columnName = connection.getMetaData().getDriverName().contains("PostgreSQL") ? APIMgtConstants.TAG_NAME_COLUMN.toLowerCase(Locale.ENGLISH) : APIMgtConstants.TAG_NAME_COLUMN.toUpperCase(Locale.ENGLISH);
            } else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
                // if the search is related to subcontext, need to check URL_PATTERN column in
                // AM_API_OPERATION_MAPPING table
                tableName = connection.getMetaData().getDriverName().contains("PostgreSQL") ? AM_API_OPERATION_MAPPING_TABLE_NAME.toLowerCase(Locale.ENGLISH) : AM_API_OPERATION_MAPPING_TABLE_NAME;
                columnName = connection.getMetaData().getDriverName().contains("PostgreSQL") ? APIMgtConstants.URL_PATTERN_COLUMN.toLowerCase(Locale.ENGLISH) : APIMgtConstants.URL_PATTERN_COLUMN.toUpperCase(Locale.ENGLISH);
            } else {
                // if the search is related to any other attribute, need to check that attribute
                // in AM_API table
                tableName = connection.getMetaData().getDriverName().contains("PostgreSQL") ? AM_API_TABLE_NAME.toLowerCase(Locale.ENGLISH) : AM_API_TABLE_NAME;
                columnName = connection.getMetaData().getDriverName().contains("PostgreSQL") ? entry.getKey().toLowerCase(Locale.ENGLISH) : entry.getKey().toUpperCase(Locale.ENGLISH);
            }
            if (!checkTableColumnExists(md, tableName, columnName)) {
                throw new APIMgtDAOException("Attribute does not exist with name: " + entry.getKey(), ExceptionCodes.API_ATTRIBUTE_NOT_FOUND);
            }
        }
        return constructAPISummaryList(connection, statement);
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "searching APIs by attribute", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) DatabaseMetaData(java.sql.DatabaseMetaData) Map(java.util.Map) HashMap(java.util.HashMap) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 18 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class PolicyDAOImpl method addAPIPipeline.

/**
 * Adding pipelines of API policy to database
 *
 * @param connection connection to db
 * @param uuid       policy id/ uuid of the policy
 * @throws SQLException if error occurred while inserting pipeline to db
 */
private static void addAPIPipeline(Connection connection, List<Pipeline> pipelines, String uuid) throws SQLException, APIMgtDAOException {
    final String query = "INSERT INTO AM_CONDITION_GROUP (UUID, QUOTA_TYPE, UNIT_TIME, TIME_UNIT, DESCRIPTION, QUOTA, " + "QUOTA_UNIT) VALUES (?,?,?,?,?,?,?)";
    String dbProductName = connection.getMetaData().getDatabaseProductName();
    try (PreparedStatement statement = connection.prepareStatement(query, new String[] { DAOUtil.getConvertedAutoGeneratedColumnName(dbProductName, APIMgtConstants.ThrottlePolicyConstants.COLUMN_CONDITION_GROUP_ID) })) {
        for (Pipeline pipeline : pipelines) {
            statement.setString(1, uuid);
            statement.setString(2, pipeline.getQuotaPolicy().getType());
            statement.setLong(3, pipeline.getQuotaPolicy().getLimit().getUnitTime());
            statement.setString(4, pipeline.getQuotaPolicy().getLimit().getTimeUnit());
            statement.setString(5, pipeline.getDescription());
            Limit limit = pipeline.getQuotaPolicy().getLimit();
            setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
            statement.executeUpdate();
            ResultSet rs = statement.getGeneratedKeys();
            if (rs.next()) {
                // get the auto increment id
                int conditionId = rs.getInt(1);
                List<Condition> conditionList = pipeline.getConditions();
                for (Condition condition : conditionList) {
                    if (PolicyConstants.IP_CONDITION_TYPE.equals(condition.getType()) || PolicyConstants.IP_SPECIFIC_TYPE.equals(condition.getType()) || PolicyConstants.IP_RANGE_TYPE.equals(condition.getType())) {
                        addIPCondition(connection, condition, conditionId);
                    } else if (PolicyConstants.HEADER_CONDITION_TYPE.equals(condition.getType())) {
                        addHeaderCondition(connection, condition, conditionId);
                    } else if (PolicyConstants.JWT_CLAIMS_CONDITION_TYPE.equals(condition.getType())) {
                        addJWTClaimCondition(connection, condition, conditionId);
                    } else if (PolicyConstants.QUERY_PARAMS_CONDITION_TYPE.equals(condition.getType())) {
                        addParamCondition(connection, condition, conditionId);
                    } else {
                        // unsupported Condition
                        throw new IllegalArgumentException("Unsupported Condition type: " + condition.getType());
                    }
                }
            } else {
                String errorMsg = "Unable to retrieve auto incremented id, hence unable to add Pipeline Condition";
                throw new IllegalStateException(errorMsg);
            }
        }
    }
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 19 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class PolicyDAOImpl method addApplicationPolicy.

/**
 * Adds an Application Policy
 *
 * @param policy {@link ApplicationPolicy} instance
 * @param connection DB Connection instance
 * @throws SQLException if an error occurs while adding an Application Policy
 */
private static void addApplicationPolicy(ApplicationPolicy policy, Connection connection) throws SQLException {
    final String query = "INSERT INTO AM_APPLICATION_POLICY (UUID, NAME, DISPLAY_NAME, " + "DESCRIPTION, QUOTA_TYPE, QUOTA, QUOTA_UNIT, UNIT_TIME, TIME_UNIT, IS_DEPLOYED, CUSTOM_ATTRIBUTES) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?)";
    Limit limit = policy.getDefaultQuotaPolicy().getLimit();
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, policy.getUuid());
        statement.setString(2, policy.getPolicyName());
        statement.setString(3, policy.getDisplayName());
        statement.setString(4, policy.getDescription());
        statement.setString(5, policy.getDefaultQuotaPolicy().getType());
        setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
        statement.setInt(8, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
        statement.setString(9, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
        statement.setBoolean(10, policy.isDeployed());
        statement.setBytes(11, policy.getCustomAttributes());
        statement.execute();
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Example 20 with Limit

use of org.wso2.carbon.apimgt.core.models.policy.Limit in project carbon-apimgt by wso2.

the class PolicyDAOImpl method addApiPolicy.

private static void addApiPolicy(APIPolicy policy, Connection connection) throws SQLException, APIMgtDAOException {
    final String query = "INSERT INTO AM_API_POLICY (UUID, NAME, DISPLAY_NAME, DESCRIPTION, " + "DEFAULT_QUOTA_TYPE, DEFAULT_QUOTA, DEFAULT_QUOTA_UNIT, DEFAULT_UNIT_TIME," + " DEFAULT_TIME_UNIT, APPLICABLE_LEVEL, IS_DEPLOYED) " + "VALUES (?,?,?,?,?,?,?,?,?,?,?)";
    Limit limit = policy.getDefaultQuotaPolicy().getLimit();
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, policy.getUuid());
        statement.setString(2, policy.getPolicyName());
        statement.setString(3, policy.getDisplayName());
        statement.setString(4, policy.getDescription());
        statement.setString(5, policy.getDefaultQuotaPolicy().getType());
        setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
        statement.setLong(8, policy.getDefaultQuotaPolicy().getLimit().getUnitTime());
        statement.setString(9, policy.getDefaultQuotaPolicy().getLimit().getTimeUnit());
        statement.setString(10, API_TIER_LEVEL);
        statement.setBoolean(11, policy.isDeployed());
        statement.execute();
        if (policy.getPipelines() != null) {
            addAPIPipeline(connection, policy.getPipelines(), policy.getUuid());
        }
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)

Aggregations

Test (org.testng.annotations.Test)29 HashMap (java.util.HashMap)26 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)19 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)15 PreparedStatement (java.sql.PreparedStatement)14 BandwidthLimit (org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit)14 Map (java.util.Map)13 ArrayList (java.util.ArrayList)11 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)11 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)11 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)11 Event (org.wso2.siddhi.core.event.Event)11 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)11 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)8 SQLException (java.sql.SQLException)8 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)8 Limit (org.wso2.carbon.apimgt.core.models.policy.Limit)8 QueryCallback (org.wso2.siddhi.core.query.output.callback.QueryCallback)8 QuotaPolicy (org.wso2.carbon.apimgt.core.models.policy.QuotaPolicy)7