use of org.wso2.carbon.humantask.core.db.Database in project carbon-apimgt by wso2.
the class DAOFactory method getAnalyticsDAO.
/**
* To get the AnalyticsDao object. Depends on different vendors.
*
* @return AnalyticsDAO object
* @throws APIMgtDAOException if error during getting analytics database connection
*/
public static AnalyticsDAO getAnalyticsDAO() throws APIMgtDAOException {
AnalyticsDAO analyticsDAO;
boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
if (isAnalyticsEnabled) {
try (Connection connection = DAOUtil.getAnalyticsConnection()) {
analyticsDAO = getAnalyticsDaoImplForVendor(connection);
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
}
} else {
// if analytics is not enabled create a normal AMDB data connection to check db driver
try (Connection connection = DAOUtil.getConnection()) {
analyticsDAO = getAnalyticsDaoImplForVendor(connection);
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
}
}
return analyticsDAO;
}
use of org.wso2.carbon.humantask.core.db.Database in project carbon-apimgt by wso2.
the class PolicyDAOImpl method addAPIPipeline.
/**
* Adding pipelines of API policy to database
*
* @param connection connection to db
* @param uuid policy id/ uuid of the policy
* @throws SQLException if error occurred while inserting pipeline to db
*/
private static void addAPIPipeline(Connection connection, List<Pipeline> pipelines, String uuid) throws SQLException, APIMgtDAOException {
final String query = "INSERT INTO AM_CONDITION_GROUP (UUID, QUOTA_TYPE, UNIT_TIME, TIME_UNIT, DESCRIPTION, QUOTA, " + "QUOTA_UNIT) VALUES (?,?,?,?,?,?,?)";
String dbProductName = connection.getMetaData().getDatabaseProductName();
try (PreparedStatement statement = connection.prepareStatement(query, new String[] { DAOUtil.getConvertedAutoGeneratedColumnName(dbProductName, APIMgtConstants.ThrottlePolicyConstants.COLUMN_CONDITION_GROUP_ID) })) {
for (Pipeline pipeline : pipelines) {
statement.setString(1, uuid);
statement.setString(2, pipeline.getQuotaPolicy().getType());
statement.setLong(3, pipeline.getQuotaPolicy().getLimit().getUnitTime());
statement.setString(4, pipeline.getQuotaPolicy().getLimit().getTimeUnit());
statement.setString(5, pipeline.getDescription());
Limit limit = pipeline.getQuotaPolicy().getLimit();
setDefaultThrottlePolicyDetailsPreparedStmt(limit, statement);
statement.executeUpdate();
ResultSet rs = statement.getGeneratedKeys();
if (rs.next()) {
// get the auto increment id
int conditionId = rs.getInt(1);
List<Condition> conditionList = pipeline.getConditions();
for (Condition condition : conditionList) {
if (PolicyConstants.IP_CONDITION_TYPE.equals(condition.getType()) || PolicyConstants.IP_SPECIFIC_TYPE.equals(condition.getType()) || PolicyConstants.IP_RANGE_TYPE.equals(condition.getType())) {
addIPCondition(connection, condition, conditionId);
} else if (PolicyConstants.HEADER_CONDITION_TYPE.equals(condition.getType())) {
addHeaderCondition(connection, condition, conditionId);
} else if (PolicyConstants.JWT_CLAIMS_CONDITION_TYPE.equals(condition.getType())) {
addJWTClaimCondition(connection, condition, conditionId);
} else if (PolicyConstants.QUERY_PARAMS_CONDITION_TYPE.equals(condition.getType())) {
addParamCondition(connection, condition, conditionId);
} else {
// unsupported Condition
throw new IllegalArgumentException("Unsupported Condition type: " + condition.getType());
}
}
} else {
String errorMsg = "Unable to retrieve auto incremented id, hence unable to add Pipeline Condition";
throw new IllegalStateException(errorMsg);
}
}
}
}
use of org.wso2.carbon.humantask.core.db.Database in project carbon-apimgt by wso2.
the class PolicyDAOImpl method setHeaderConditions.
/**
* Add Header conditions of pipeline with pipeline Id: <code>pipelineId</code> to a
* provided {@link Condition} array
*
* @param connection Connection of the database
* @param pipelineId Id of the pipeline
* @param conditions condition array to populate
* @throws SQLException
*/
private void setHeaderConditions(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
final String query = "SELECT " + "HEADER_FIELD_NAME, " + "HEADER_FIELD_VALUE , IS_HEADER_FIELD_MAPPING " + " FROM " + "AM_HEADER_FIELD_CONDITION " + "WHERE " + "CONDITION_GROUP_ID =?";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setInt(1, pipelineId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
HeaderCondition headerCondition = new HeaderCondition();
headerCondition.setHeader(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_HEADER_FIELD_NAME));
headerCondition.setValue(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_HEADER_FIELD_VALUE));
headerCondition.setInvertCondition(resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_IS_HEADER_FIELD_MAPPING));
conditions.add(headerCondition);
}
}
}
}
use of org.wso2.carbon.humantask.core.db.Database 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.humantask.core.db.Database in project carbon-apimgt by wso2.
the class SystemApplicationDaoImpl method isConsumerKeyExistForApplication.
@Override
public boolean isConsumerKeyExistForApplication(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 true;
} else {
return false;
}
}
} catch (SQLException e) {
String errorMsg = "Error while creating database connection/prepared-statement";
throw new APIMgtDAOException(errorMsg, e);
}
}
Aggregations