use of org.wso2.carbon.apimgt.keymgt.model.entity.API 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.keymgt.model.entity.API in project carbon-apimgt by wso2.
the class PolicyDAOImpl method getAllApiPolicies.
/**
* Retrieves all API policies.
*
* @return List of {@link APIPolicy} instances, or an empty list if none is found
* @throws SQLException if an error occurs while retrieving policies
*/
private List<APIPolicy> getAllApiPolicies() throws SQLException {
List<APIPolicy> policyList = new ArrayList<>();
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";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
APIPolicy apiPolicy = new APIPolicy(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_NAME));
apiPolicy.setUuid(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_UUID));
setCommonPolicyDetails(apiPolicy, resultSet);
apiPolicy.setUserLevel(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_APPLICABLE_LEVEL));
apiPolicy.setPipelines(getPipelines(apiPolicy.getUuid(), connection));
policyList.add(apiPolicy);
}
}
}
return policyList;
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.API 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);
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.API in project carbon-apimgt by wso2.
the class PolicyDAOImpl method addApiPolicy.
@Override
public void addApiPolicy(APIPolicy policy) throws APIMgtDAOException {
try (Connection connection = DAOUtil.getConnection()) {
try {
connection.setAutoCommit(false);
addApiPolicy(policy, connection);
connection.commit();
} catch (SQLException e) {
connection.rollback();
String errorMessage = "Error in adding API 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);
}
}
use of org.wso2.carbon.apimgt.keymgt.model.entity.API in project carbon-apimgt by wso2.
the class APIPublisherImpl method removePendingLifecycleWorkflowTaskForAPI.
/**
* {@inheritDoc}
*/
@Override
public void removePendingLifecycleWorkflowTaskForAPI(String apiId) throws APIManagementException {
try {
API api = getApiDAO().getAPI(apiId);
if (APILCWorkflowStatus.PENDING.toString().equals(api.getWorkflowStatus())) {
// change the state back
getApiDAO().updateAPIWorkflowStatus(apiId, APILCWorkflowStatus.APPROVED);
// call executor's cleanup task
cleanupPendingTaskForAPIStateChange(apiId);
} else {
String msg = "API does not have a pending lifecycle state change.";
log.error(msg);
throw new APIManagementException(msg, ExceptionCodes.WORKFLOW_NO_PENDING_TASK);
}
} catch (APIMgtDAOException e) {
String msg = "Error occurred while changing api lifecycle workflow status";
log.error(msg, e);
throw new APIManagementException(msg, e.getErrorHandler());
}
}
Aggregations