use of org.wso2.carbon.apimgt.core.models.Application in project carbon-apimgt by wso2.
the class PolicyDAOImpl method addBlockConditions.
@Override
public String addBlockConditions(BlockConditions blockConditions) throws APIMgtDAOException {
boolean status = false;
boolean valid = false;
String uuid = null;
String conditionType = blockConditions.getConditionType();
String conditionValue = blockConditions.getConditionValue();
try {
String query = "INSERT INTO AM_BLOCK_CONDITIONS (TYPE, VALUE, ENABLED, UUID) VALUES (?,?,?,?)";
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API.equals(conditionType)) {
if (isValidContext(conditionValue)) {
valid = true;
} else {
throw new APIMgtDAOException("Couldn't Save Block Condition Due to Invalid API Context : " + conditionValue, ExceptionCodes.BLOCK_CONDITION_UNSUPPORTED_API_CONTEXT);
}
} else if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION.equals(conditionType)) {
String[] appArray = conditionValue.split(":");
if (appArray.length > 1) {
String appUuid = appArray[0];
String appName = appArray[1];
if (isValidApplication(appName, appUuid)) {
valid = true;
} else {
throw new APIMgtDAOException("Couldn't Save Block Condition Due to Invalid Application : " + appName + ", UUID :" + appUuid, ExceptionCodes.BLOCK_CONDITION_UNSUPPORTED_APP_ID_NAME);
}
}
} else if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_USER.equals(conditionType)) {
valid = true;
} else if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP.equals(conditionType)) {
valid = true;
} else if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(conditionType)) {
valid = isIPRangeConditionValid(blockConditions.getStartingIP(), blockConditions.getEndingIP());
}
if (valid) {
try (Connection connection = DAOUtil.getConnection();
PreparedStatement insertPreparedStatement = connection.prepareStatement(query)) {
try {
connection.setAutoCommit(false);
if (!isBlockConditionExist(blockConditions)) {
uuid = UUID.randomUUID().toString();
insertPreparedStatement.setString(1, conditionType);
insertPreparedStatement.setString(2, conditionValue);
insertPreparedStatement.setBoolean(3, blockConditions.isEnabled());
insertPreparedStatement.setString(4, uuid);
insertPreparedStatement.execute();
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(conditionType)) {
String ipConditionQuery = "INSERT INTO AM_IP_RANGE_CONDITION " + "(STARTING_IP, ENDING_IP, UUID) VALUES (?, ?, ?)";
try (PreparedStatement ipStatement = connection.prepareStatement(ipConditionQuery)) {
ipStatement.setString(1, blockConditions.getStartingIP());
ipStatement.setString(2, blockConditions.getEndingIP());
ipStatement.setString(3, uuid);
ipStatement.execute();
} catch (SQLException e) {
connection.rollback();
}
}
connection.commit();
} else {
throw new APIMgtDAOException("Condition with type: " + conditionType + ", value: " + conditionValue + " already exists", ExceptionCodes.BLOCK_CONDITION_ALREADY_EXISTS);
}
} catch (SQLException e) {
connection.rollback();
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding block condition: " + conditionType + " and " + conditionValue, e);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
}
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding block condition: " + conditionType + " and " + conditionValue, e);
}
return uuid;
}
use of org.wso2.carbon.apimgt.core.models.Application 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.core.models.Application in project carbon-apimgt by wso2.
the class SystemApplicationDaoImpl method getConsumerKeyForApplication.
@Override
public String getConsumerKeyForApplication(String appName) throws APIMgtDAOException {
final String query = "SELECT CONSUMER_KEY FROM AM_SYSTEM_APPS WHERE NAME = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, appName);
log.debug("Executing query: {} ", query);
try (ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
return resultSet.getString("CONSUMER_KEY");
} else {
throw new APIMgtDAOException("System Application with " + appName + " does not exist", ExceptionCodes.SYSTEM_APP_NOT_FOUND);
}
}
} catch (SQLException e) {
String errorMsg = "Error while creating database connection/prepared-statement";
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.core.models.Application in project carbon-apimgt by wso2.
the class SystemApplicationDaoImpl method addApplicationKey.
@Override
public void addApplicationKey(String appName, String consumerKey) throws APIMgtDAOException {
final String query = "INSERT INTO AM_SYSTEM_APPS (NAME,CONSUMER_KEY,CREATED_TIME) VALUES (?,?,?)";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
connection.setAutoCommit(false);
try {
statement.setString(1, appName);
statement.setString(2, consumerKey);
statement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()), Calendar.getInstance(TimeZone.getTimeZone("UTC")));
log.debug("Executing query: {} ", query);
statement.execute();
connection.commit();
} catch (SQLException ex) {
connection.rollback();
throw new APIMgtDAOException("Couldn't Create System Application", ex);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
} catch (SQLException e) {
String errorMsg = "Error while creating database connection/prepared-statement";
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.core.models.Application in project carbon-apimgt by wso2.
the class PolicyDAOImpl method addApplicationPolicy.
@Override
public void addApplicationPolicy(ApplicationPolicy policy) throws APIMgtDAOException {
try (Connection connection = DAOUtil.getConnection()) {
try {
connection.setAutoCommit(false);
addApplicationPolicy(policy, connection);
connection.commit();
} catch (SQLException e) {
connection.rollback();
String errorMessage = "Error in adding Application policy, policy name: " + policy.getPolicyName();
log.error(errorMessage, e);
throw new APIMgtDAOException(errorMessage, e);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
} catch (SQLException e) {
String errorMsg = "Error in obtaining DB connection";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
Aggregations