Search in sources :

Example 1 with PolicyValidationData

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

the class PolicyDAOImpl method getAllPolicies.

@Override
public Set<PolicyValidationData> getAllPolicies() throws APIMgtDAOException {
    Set<PolicyValidationData> policyValidationDataList = new LinkedHashSet();
    final String apiPolicyQuery = "SELECT UUID,NAME FROM AM_API_POLICY";
    final String applicationPolicyQuery = "SELECT UUID,NAME FROM AM_APPLICATION_POLICY";
    final String subscriptionPolicyQuery = "SELECT UUID,NAME,STOP_ON_QUOTA_REACH FROM AM_SUBSCRIPTION_POLICY";
    try (Connection connection = DAOUtil.getConnection()) {
        try (Statement statement = connection.createStatement()) {
            try (ResultSet resultSet = statement.executeQuery(apiPolicyQuery)) {
                while (resultSet.next()) {
                    policyValidationDataList.add(new PolicyValidationData(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME), false));
                }
            }
            try (ResultSet resultSet = statement.executeQuery(applicationPolicyQuery)) {
                while (resultSet.next()) {
                    policyValidationDataList.add(new PolicyValidationData(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME), false));
                }
            }
            try (ResultSet resultSet = statement.executeQuery(subscriptionPolicyQuery)) {
                while (resultSet.next()) {
                    policyValidationDataList.add(new PolicyValidationData(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID), resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME), resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_STOP_ON_QUOTA_REACH)));
                }
            }
        }
    } catch (SQLException e) {
        String msg = "Error while retrieving policies";
        log.error(msg, e);
        throw new APIMgtDAOException(msg, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
    return policyValidationDataList;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Example 2 with PolicyValidationData

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

the class APIGatewayPublisherImpl method deletePolicy.

@Override
public void deletePolicy(PolicyValidationData policyValidationData) throws GatewayException {
    if (policyValidationData != null) {
        PolicyEvent policyEvent = new PolicyEvent(APIMgtConstants.GatewayEventTypes.POLICY_DELETE);
        policyEvent.setId(policyValidationData.getId());
        policyEvent.setName(policyValidationData.getName());
        policyEvent.setStopOnQuotaReach(policyValidationData.isStopOnQuotaReach());
        publishToThrottleTopic(policyEvent);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + policyValidationData.getName() + " delete event has been successfully " + "published " + "to broker");
        }
    }
}
Also used : PolicyEvent(org.wso2.carbon.apimgt.core.models.events.PolicyEvent)

Example 3 with PolicyValidationData

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

the class APIGatewayPublisherImpl method addPolicy.

@Override
public void addPolicy(PolicyValidationData policyValidationData) throws GatewayException {
    if (policyValidationData != null) {
        PolicyEvent policyEvent = new PolicyEvent(APIMgtConstants.GatewayEventTypes.POLICY_CREATE);
        policyEvent.setId(policyValidationData.getId());
        policyEvent.setName(policyValidationData.getName());
        policyEvent.setStopOnQuotaReach(policyValidationData.isStopOnQuotaReach());
        publishToThrottleTopic(policyEvent);
        if (log.isDebugEnabled()) {
            log.debug("Policy : " + policyValidationData.getName() + " add event has been successfully published " + "to broker");
        }
    }
}
Also used : PolicyEvent(org.wso2.carbon.apimgt.core.models.events.PolicyEvent)

Example 4 with PolicyValidationData

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

the class APIMgtAdminServiceImpl method addApiPolicy.

@Override
public String addApiPolicy(APIPolicy policy) throws APIManagementException {
    try {
        String policyUuid = policy.getUuid();
        if (policyUuid == null) {
            if (log.isDebugEnabled()) {
                log.debug("Policy id is null, hence generating a new UUID for the policy with name: " + policy.getPolicyName());
            }
            policyUuid = UUID.randomUUID().toString();
            policy.setUuid(policyUuid);
        }
        policyDAO.addApiPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policyUuid, policy.getPolicyName(), false);
        apiGateway.addPolicy(policyValidationData);
        return policyUuid;
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't add API policy for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Example 5 with PolicyValidationData

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

the class APIMgtAdminServiceImpl method addApplicationPolicy.

@Override
public String addApplicationPolicy(ApplicationPolicy policy) throws APIManagementException {
    try {
        String policyUuid = policy.getUuid();
        if (policyUuid == null) {
            if (log.isDebugEnabled()) {
                log.debug("Policy id is null, hence generating a new UUID for the policy with name: " + policy.getPolicyName());
            }
            policyUuid = UUID.randomUUID().toString();
            policy.setUuid(policyUuid);
        }
        policyDAO.addApplicationPolicy(policy);
        PolicyValidationData policyValidationData = new PolicyValidationData(policyUuid, policy.getPolicyName(), false);
        apiGateway.addPolicy(policyValidationData);
        return policyUuid;
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't add Application for uuid: " + policy.getUuid();
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) PolicyValidationData(org.wso2.carbon.apimgt.core.models.PolicyValidationData)

Aggregations

PolicyValidationData (org.wso2.carbon.apimgt.core.models.PolicyValidationData)12 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)9 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)9 PolicyEvent (org.wso2.carbon.apimgt.core.models.events.PolicyEvent)3 LinkedHashSet (java.util.LinkedHashSet)2 Test (org.testng.annotations.Test)2 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)2 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)2 ApplicationPolicy (org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)2 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1