Search in sources :

Example 11 with Application

use of org.wso2.carbon.apimgt.api.model.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method updateApplication.

/**
 * Update an existing Application
 *
 * @param appID      The UUID of the Application that needs to be updated
 * @param updatedApp Substitute {@link Application} object that will replace the existing Application
 * @throws APIMgtDAOException   If failed to update applications.
 */
@Override
public void updateApplication(String appID, Application updatedApp) throws APIMgtDAOException {
    final String updateAppQuery = "UPDATE AM_APPLICATION SET NAME=?, APPLICATION_POLICY_ID=" + "?, DESCRIPTION=?, APPLICATION_STATUS=?, UPDATED_BY=?, LAST_UPDATED_TIME=? WHERE UUID=?";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(updateAppQuery)) {
            ps.setString(1, updatedApp.getName());
            ps.setString(2, updatedApp.getPolicy().getUuid());
            ps.setString(3, updatedApp.getDescription());
            ps.setString(4, updatedApp.getStatus());
            ps.setString(5, updatedApp.getUpdatedUser());
            ps.setTimestamp(6, Timestamp.valueOf(updatedApp.getUpdatedTime()));
            ps.setString(7, appID);
            ps.executeUpdate();
            updateApplicationPermission(conn, updatedApp.getPermissionMap(), updatedApp.getId());
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating application: " + appID, ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating application: " + appID, ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 12 with Application

use of org.wso2.carbon.apimgt.api.model.Application in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method deleteApplication.

/**
 * Remove an existing Application
 *
 * @param appID The UUID of the Application that needs to be deleted
 * @throws APIMgtDAOException   If failed to delete application.
 */
@Override
public void deleteApplication(String appID) throws APIMgtDAOException {
    final String appDeleteQuery = "DELETE FROM AM_APPLICATION WHERE UUID = ?";
    try (Connection conn = DAOUtil.getConnection()) {
        boolean originalAutoCommitState = conn.getAutoCommit();
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(appDeleteQuery)) {
            ps.setString(1, appID);
            ps.execute();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting application: " + appID, ex);
        } finally {
            conn.setAutoCommit(originalAutoCommitState);
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting application: " + appID, ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 13 with Application

use of org.wso2.carbon.apimgt.api.model.Application in project carbon-apimgt by wso2.

the class PolicyDAOImpl method isValidApplication.

/**
 * validate the blocking application.
 *
 * @param appName name of the application
 * @param uuid    uuid of the application
 * @return return true/false depends of the success
 * @throws APIMgtDAOException if failed validating application
 */
private boolean isValidApplication(String appName, String uuid) throws APIMgtDAOException {
    String query = "SELECT 1 FROM AM_APPLICATION WHERE UUID = ? AND NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        connection.setAutoCommit(false);
        statement.setString(1, uuid);
        statement.setString(2, appName);
        try (ResultSet resultSet = statement.executeQuery()) {
            return resultSet.next();
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "checking if Block condition with Application Name " + appName + " , Application ID = " + uuid + " exists", 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 14 with Application

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

Example 15 with Application

use of org.wso2.carbon.apimgt.api.model.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();
    }
}
Also used : 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)

Aggregations

Test (org.testng.annotations.Test)156 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)143 Application (org.wso2.carbon.apimgt.api.model.Application)130 Application (org.wso2.carbon.apimgt.core.models.Application)121 Test (org.junit.Test)102 ArrayList (java.util.ArrayList)98 SQLException (java.sql.SQLException)94 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)94 PreparedStatement (java.sql.PreparedStatement)88 Connection (java.sql.Connection)83 ResultSet (java.sql.ResultSet)73 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)71 HashMap (java.util.HashMap)70 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)63 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)63 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)61 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)60 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)59 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)58 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)57