use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException 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;
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.
the class TagDAOImpl method getTags.
@Override
public List<Tag> getTags() throws APIMgtDAOException {
final String query = "SELECT NAME, COUNT FROM AM_TAGS";
List<Tag> tags = new ArrayList<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
Tag tag = new Tag.Builder().name(rs.getString("NAME")).count(rs.getInt("COUNT")).build();
tags.add(tag);
}
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting tags", e);
}
return tags;
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException 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.core.exception.APIMgtDAOException 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.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getBlockConditions.
@Override
public List<BlockConditions> getBlockConditions() throws APIMgtDAOException {
List<BlockConditions> blockConditionsList = new ArrayList<BlockConditions>();
String query = "SELECT CONDITION_ID, TYPE, VALUE, ENABLED, AM_BLOCK_CONDITIONS.UUID, STARTING_IP, ENDING_IP " + "FROM AM_BLOCK_CONDITIONS LEFT JOIN AM_IP_RANGE_CONDITION ON " + "AM_BLOCK_CONDITIONS.UUID = AM_IP_RANGE_CONDITION.UUID";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement selectPreparedStatement = connection.prepareStatement(query)) {
connection.setAutoCommit(true);
try (ResultSet resultSet = selectPreparedStatement.executeQuery()) {
while (resultSet.next()) {
BlockConditions blockConditions = new BlockConditions();
blockConditions.setEnabled(resultSet.getBoolean("ENABLED"));
blockConditions.setConditionType(resultSet.getString("TYPE"));
blockConditions.setConditionValue(resultSet.getString("VALUE"));
blockConditions.setConditionId(resultSet.getInt("CONDITION_ID"));
blockConditions.setUuid(resultSet.getString("UUID"));
blockConditions.setStartingIP(resultSet.getString("STARTING_IP"));
blockConditions.setEndingIP(resultSet.getString("ENDING_IP"));
blockConditionsList.add(blockConditions);
}
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting block conditions", e);
}
return blockConditionsList;
}
Aggregations