Search in sources :

Example 6 with Database

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;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) AnalyticsDAO(org.wso2.carbon.apimgt.core.dao.AnalyticsDAO) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 7 with Database

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);
            }
        }
    }
}
Also used : JWTClaimsCondition(org.wso2.carbon.apimgt.core.models.policy.JWTClaimsCondition) Condition(org.wso2.carbon.apimgt.core.models.policy.Condition) QueryParameterCondition(org.wso2.carbon.apimgt.core.models.policy.QueryParameterCondition) IPCondition(org.wso2.carbon.apimgt.core.models.policy.IPCondition) HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Limit(org.wso2.carbon.apimgt.core.models.policy.Limit) RequestCountLimit(org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit) BandwidthLimit(org.wso2.carbon.apimgt.core.models.policy.BandwidthLimit) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline)

Example 8 with Database

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);
            }
        }
    }
}
Also used : HeaderCondition(org.wso2.carbon.apimgt.core.models.policy.HeaderCondition) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 9 with Database

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);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 10 with Database

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);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

SQLException (java.sql.SQLException)29 PreparedStatement (java.sql.PreparedStatement)28 Connection (java.sql.Connection)24 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)23 ResultSet (java.sql.ResultSet)16 ArrayList (java.util.ArrayList)13 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)12 Map (java.util.Map)6 DeviceMgtPluginException (org.wso2.iot.sampledevice.plugin.exception.DeviceMgtPluginException)6 HashMap (java.util.HashMap)5 BpelDatabase (org.apache.ode.bpel.engine.BpelDatabase)5 File (java.io.File)4 InstanceFilter (org.apache.ode.bpel.common.InstanceFilter)4 BpelDAOConnection (org.apache.ode.bpel.dao.BpelDAOConnection)4 InstanceManagementException (org.wso2.carbon.bpel.skeleton.ode.integration.mgt.services.InstanceManagementException)4 Device (org.wso2.carbon.device.mgt.common.Device)4 IOException (java.io.IOException)3 Collection (java.util.Collection)3 DataSource (javax.sql.DataSource)3 ProcessInstanceDAO (org.apache.ode.bpel.dao.ProcessInstanceDAO)3