use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getApiPolicyByUuid.
@Override
public APIPolicy getApiPolicyByUuid(String uuid) throws APIMgtDAOException {
try {
String sqlQuery = "SELECT UUID, NAME, DEFAULT_QUOTA_TYPE, DEFAULT_TIME_UNIT, DEFAULT_UNIT_TIME, " + "DEFAULT_QUOTA, " + "DEFAULT_QUOTA_UNIT, DESCRIPTION, DISPLAY_NAME, IS_DEPLOYED, APPLICABLE_LEVEL from " + "AM_API_POLICY WHERE UUID = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
preparedStatement.setString(1, uuid);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
APIPolicy apiPolicy = new APIPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
setCommonPolicyDetails(apiPolicy, resultSet);
apiPolicy.setUserLevel(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
apiPolicy.setPipelines(getPipelines(apiPolicy.getUuid(), connection));
return apiPolicy;
} else {
// not found
String msg = "API Policy not found for id: " + uuid;
log.warn(msg);
throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
}
}
}
} catch (SQLException e) {
String errorMsg = "Error in retrieving API policy with id: " + uuid;
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.Policy 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();
}
}
use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class ThreatProtectionDAOImpl method updatePolicy.
/**
* Update a threat protection policy
* @param policy Policy to be updated
* @param connection SQL Connection
* @throws APIMgtDAOException If failed to update policy
*/
private void updatePolicy(ThreatProtectionPolicy policy, Connection connection) throws APIMgtDAOException {
final String sqlQuery = "UPDATE " + THREAT_PROTECTION_TABLE + " SET NAME = ?, " + "TYPE = ?, " + "POLICY = ? " + "WHERE UUID = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
preparedStatement.setString(1, policy.getName());
preparedStatement.setString(2, policy.getType());
preparedStatement.setBytes(3, policy.getPolicy().getBytes("UTF-8"));
preparedStatement.setString(4, policy.getUuid());
preparedStatement.executeUpdate();
} catch (SQLException e) {
String errorMsg = "Error updating Threat Protection policy";
throw new APIMgtDAOException(errorMsg, e);
} catch (UnsupportedEncodingException e) {
String errorMsg = "Charset error in threat protection policy";
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getSubscriptionPolicy.
@Override
public SubscriptionPolicy getSubscriptionPolicy(String policyName) throws APIMgtDAOException {
try {
final String query = "SELECT UUID, NAME, QUOTA_TYPE, TIME_UNIT, UNIT_TIME, QUOTA, QUOTA_UNIT, DESCRIPTION, " + "DISPLAY_NAME, CUSTOM_ATTRIBUTES, IS_DEPLOYED, RATE_LIMIT_COUNT, RATE_LIMIT_TIME_UNIT, " + "STOP_ON_QUOTA_REACH, BILLING_PLAN FROM AM_SUBSCRIPTION_POLICY WHERE NAME = ?";
try (Connection conn = DAOUtil.getConnection();
PreparedStatement statement = conn.prepareStatement(query)) {
statement.setString(1, policyName);
statement.execute();
try (ResultSet rs = statement.getResultSet()) {
if (rs.next()) {
return createSubscriptionPolicyFromResultSet(policyName, rs);
} else {
// not found
String msg = "Subscription Policy not found for name: " + policyName;
log.warn(msg);
throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
}
}
}
} catch (SQLException e) {
String errorMsg = "Error in retrieving Subscription policy for name: " + policyName;
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.api.model.policy.Policy in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getApiPolicy.
@Override
public APIPolicy getApiPolicy(String policyName) throws APIMgtDAOException {
try {
String sqlQuery = "SELECT UUID, NAME, DEFAULT_QUOTA_TYPE, DEFAULT_TIME_UNIT, DEFAULT_UNIT_TIME, " + "DEFAULT_QUOTA, DEFAULT_QUOTA_UNIT, DESCRIPTION, DISPLAY_NAME, IS_DEPLOYED, APPLICABLE_LEVEL " + "from AM_API_POLICY WHERE NAME = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
preparedStatement.setString(1, policyName);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
APIPolicy policy = new APIPolicy(policyName);
setCommonPolicyDetails(policy, resultSet);
policy.setUserLevel(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
policy.setPipelines(getPipelines(policy.getUuid(), connection));
return policy;
} else {
// not found
String msg = "API Policy not found for name: " + policyName;
log.warn(msg);
throw new APIMgtDAOException(msg, ExceptionCodes.POLICY_NOT_FOUND);
}
}
}
} catch (SQLException e) {
String errorMsg = "Error in retrieving API policy with name: " + policyName;
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
Aggregations