Search in sources :

Example 1 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method validateSubscription.

/**
 * Validates a subscription
 *
 * @param apiContext  Context of the API
 * @param apiVersion  Version of the API
 * @param clientId    Client id of the application
 * @return Subscription Validation Information
 * @throws APIMgtDAOException   If failed to get subscription validation results.
 */
public SubscriptionValidationResult validateSubscription(String apiContext, String apiVersion, String clientId) throws APIMgtDAOException {
    final String validateSubscriptionSql = "SELECT SUBS.API_ID AS API_ID, SUBS.APPLICATION_ID AS APP_ID, " + "SUBS.SUB_STATUS AS SUB_STATUS, API.PROVIDER AS API_PROVIDER, API.NAME AS API_NAME, " + "APP.NAME AS APP_NAME, APP.CREATED_BY AS APP_OWNER, POLICY.NAME AS SUBS_POLICY " + "FROM AM_SUBSCRIPTION SUBS, AM_API API, AM_APPLICATION APP, AM_SUBSCRIPTION_POLICY POLICY, " + "AM_APP_KEY_MAPPING KEYS " + "WHERE API.CONTEXT = ? AND API.VERSION = ? AND KEYS.CLIENT_ID= ? " + "AND APP.AND SUBS.API_ID = API.UUID AND SUBS.APPLICATION_ID = APP.UUID " + "AND SUBS.TIER_ID = POLICY.UUID AND KEYS.APPLICATION_ID = APP.UUID";
    SubscriptionValidationResult validationInfo = new SubscriptionValidationResult(false);
    try (Connection conn = DAOUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(validateSubscriptionSql)) {
        ps.setString(1, apiContext);
        ps.setString(2, apiVersion);
        ps.setString(3, clientId);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                validationInfo.setValid(true);
                validationInfo.setApiId(rs.getString("API_ID"));
                validationInfo.setApplicationId(rs.getString("APP_ID"));
                validationInfo.setSubscriptionStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
                validationInfo.setApiProvider(rs.getString("API_PROVIDER"));
                validationInfo.setApiName(rs.getString("API_NAME"));
                validationInfo.setApplicationName(rs.getString("APP_NAME"));
                validationInfo.setApplicationOwner(rs.getString("APP_OWNER"));
                validationInfo.setSubscriptionStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUBS_POLICY")));
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "validating subscription(apiContext: " + apiContext + ", apiVersion: " + apiVersion + ", clientId: " + clientId + ")", e);
    }
    return validationInfo;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) SubscriptionValidationResult(org.wso2.carbon.apimgt.core.models.SubscriptionValidationResult) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 2 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class ApiDAOImpl method getSubscripitonPolciesByAPIId.

private Set<Policy> getSubscripitonPolciesByAPIId(Connection connection, String apiId) throws SQLException {
    final String query = "SELECT amPolcySub.NAME FROM AM_API_SUBS_POLICY_MAPPING apimsubmapping," + "AM_SUBSCRIPTION_POLICY amPolcySub where apimsubmapping.SUBSCRIPTION_POLICY_ID=amPolcySub.UUID " + "AND apimsubmapping.API_ID = ?";
    Set<Policy> policies = new HashSet<>();
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, apiId);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            while (rs.next()) {
                policies.add(new SubscriptionPolicy(rs.getString("NAME")));
            }
        }
    }
    return policies;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) HashSet(java.util.HashSet)

Example 3 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class ApiDAOImpl method getResourcesOfApi.

/**
 * @see org.wso2.carbon.apimgt.core.dao.ApiDAO#getResourcesOfApi(String, String)
 */
@Override
public List<UriTemplate> getResourcesOfApi(String apiContext, String apiVersion) throws APIMgtDAOException {
    final String query = "SELECT operationMapping.OPERATION_ID AS OPERATION_ID,operationMapping.HTTP_METHOD AS " + "HTTP_METHOD,operationMapping.URL_PATTERN AS URL_PATTERN,operationMapping.AUTH_SCHEME AS AUTH_SCHEME," + "operationMapping.API_POLICY_ID AS API_POLICY_ID FROM AM_API_OPERATION_MAPPING operationMapping," + "AM_API api WHERE operationMapping.API_ID = api.UUID AND api.CONTEXT = ? AND api.VERSION = ?";
    List<UriTemplate> uriTemplates = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, apiContext);
        preparedStatement.setString(2, apiVersion);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                UriTemplate uriTemplate = new UriTemplate.UriTemplateBuilder().uriTemplate(resultSet.getString("URL_PATTERN")).authType(resultSet.getString("AUTH_SCHEME")).httpVerb(resultSet.getString("HTTP_METHOD")).policy(new APIPolicy(resultSet.getString("API_POLICY_ID"), "")).templateId(resultSet.getString("OPERATION_ID")).build();
                uriTemplates.add(uriTemplate);
            }
        }
    } catch (SQLException e) {
        String msg = "getting API resources for Context: " + apiContext + ", Version: " + apiVersion;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + msg, e);
    }
    return uriTemplates;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 4 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.

the class ApiDAOImpl method getApiPolicyByAPIId.

private Policy getApiPolicyByAPIId(Connection connection, String apiId) throws SQLException {
    final String query = "SELECT amapipolicy.NAME AS POLICY_NAME FROM AM_API_POLICY_MAPPING apimpolicymapping," + "AM_API_POLICY amapipolicy WHERE apimpolicymapping.API_POLICY_ID=amapipolicy.UUID " + "AND apimpolicymapping.API_ID = ?";
    Policy apiPolicy = null;
    try (PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, apiId);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            if (rs.next()) {
                apiPolicy = new APIPolicy(rs.getString("POLICY_NAME"));
            }
        }
    }
    return apiPolicy;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.core.models.policy.Policy) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) APIPolicy(org.wso2.carbon.apimgt.core.models.policy.APIPolicy)

Example 5 with Policy

use of org.wso2.carbon.apimgt.api.model.policy.Policy 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)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)114 Test (org.testng.annotations.Test)106 ArrayList (java.util.ArrayList)96 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)79 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)73 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)73 Test (org.junit.Test)72 PreparedStatement (java.sql.PreparedStatement)68 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)64 Policy (org.wso2.carbon.apimgt.core.models.policy.Policy)63 SQLException (java.sql.SQLException)62 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)62 Connection (java.sql.Connection)58 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)58 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)54 ResultSet (java.sql.ResultSet)49 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)43 HashMap (java.util.HashMap)40 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)40 CustomPolicy (org.wso2.carbon.apimgt.core.models.policy.CustomPolicy)40