Search in sources :

Example 1 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions 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 2 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getBlockConditionByUUID.

@Override
public BlockConditions getBlockConditionByUUID(String uuid) throws APIMgtDAOException {
    BlockConditions blockCondition = new BlockConditions();
    String query = "SELECT CONDITION_ID,TYPE,VALUE,ENABLED,UUID FROM AM_BLOCK_CONDITIONS WHERE UUID =?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement selectPreparedStatement = connection.prepareStatement(query)) {
        selectPreparedStatement.setString(1, uuid);
        try (ResultSet resultSet = selectPreparedStatement.executeQuery()) {
            if (resultSet.next()) {
                blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
                blockCondition.setConditionType(resultSet.getString("TYPE"));
                blockCondition.setConditionValue(resultSet.getString("VALUE"));
                blockCondition.setConditionId(resultSet.getInt("CONDITION_ID"));
                blockCondition.setUuid(resultSet.getString("UUID"));
                if (blockCondition.getConditionType().equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
                    String ipQuery = "SELECT STARTING_IP, ENDING_IP FROM AM_IP_RANGE_CONDITION WHERE UUID = ?";
                    try (PreparedStatement selectIpStatement = connection.prepareStatement(ipQuery)) {
                        selectIpStatement.setString(1, uuid);
                        try (ResultSet rs = selectIpStatement.executeQuery()) {
                            if (rs.next()) {
                                blockCondition.setStartingIP(rs.getString("STARTING_IP"));
                                blockCondition.setEndingIP(rs.getString("ENDING_IP"));
                            }
                        }
                    }
                }
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting block condition by uuid " + uuid, e);
    }
    return blockCondition;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.

the class PolicyDAOImpl method getBlockConditions.

@Override
public List<BlockConditions> getBlockConditions() throws APIMgtDAOException {
    List<BlockConditions> blockConditionsList = new ArrayList<BlockConditions>();
    String query = "SELECT CONDITION_ID, TYPE, VALUE, ENABLED, AM_BLOCK_CONDITIONS.UUID, STARTING_IP, ENDING_IP " + "FROM AM_BLOCK_CONDITIONS LEFT JOIN AM_IP_RANGE_CONDITION ON " + "AM_BLOCK_CONDITIONS.UUID = AM_IP_RANGE_CONDITION.UUID";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement selectPreparedStatement = connection.prepareStatement(query)) {
        connection.setAutoCommit(true);
        try (ResultSet resultSet = selectPreparedStatement.executeQuery()) {
            while (resultSet.next()) {
                BlockConditions blockConditions = new BlockConditions();
                blockConditions.setEnabled(resultSet.getBoolean("ENABLED"));
                blockConditions.setConditionType(resultSet.getString("TYPE"));
                blockConditions.setConditionValue(resultSet.getString("VALUE"));
                blockConditions.setConditionId(resultSet.getInt("CONDITION_ID"));
                blockConditions.setUuid(resultSet.getString("UUID"));
                blockConditions.setStartingIP(resultSet.getString("STARTING_IP"));
                blockConditions.setEndingIP(resultSet.getString("ENDING_IP"));
                blockConditionsList.add(blockConditions);
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting block conditions", e);
    }
    return blockConditionsList;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.

the class APIGatewayPublisherImpl method deleteBlockCondition.

@Override
public void deleteBlockCondition(BlockConditions blockConditions) throws GatewayException {
    if (blockConditions != null) {
        BlockEvent blockEvent = new BlockEvent(APIMgtConstants.GatewayEventTypes.BLOCK_CONDITION_ADD);
        blockEvent.setConditionId(blockConditions.getConditionId());
        blockEvent.setUuid(blockConditions.getUuid());
        blockEvent.setConditionType(blockConditions.getConditionType());
        blockEvent.setEnabled(blockConditions.isEnabled());
        blockEvent.setConditionValue(blockConditions.getConditionValue());
        if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP.equals(blockConditions.getConditionType())) {
            blockEvent.setFixedIp(APIUtils.ipToLong(blockConditions.getConditionValue()));
        }
        if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(blockConditions.getConditionType())) {
            blockEvent.setStartingIP(APIUtils.ipToLong(blockConditions.getStartingIP()));
            blockEvent.setEndingIP(APIUtils.ipToLong(blockConditions.getEndingIP()));
        }
        publishToThrottleTopic(blockEvent);
        if (log.isDebugEnabled()) {
            log.debug("BlockCondition : " + blockConditions.getUuid() + " delete event has been successfully " + "published " + "to broker");
        }
    }
}
Also used : BlockEvent(org.wso2.carbon.apimgt.core.models.events.BlockEvent)

Example 5 with BlockConditions

use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.

the class APIMgtAdminServiceImpl method updateBlockConditionStateByUUID.

@Override
public boolean updateBlockConditionStateByUUID(String uuid, Boolean state) throws APIManagementException {
    try {
        if (policyDAO.updateBlockConditionStateByUUID(uuid, state)) {
            BlockConditions blockConditions = getBlockConditionByUUID(uuid);
            apiGateway.updateBlockCondition(blockConditions);
            return true;
        } else {
            return false;
        }
    } catch (APIMgtDAOException e) {
        String errorMessage = "Couldn't update block condition with UUID: " + uuid + ", state: " + state;
        log.error(errorMessage, e);
        throw new APIManagementException(errorMessage, ExceptionCodes.APIMGT_DAO_EXCEPTION);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) BlockConditions(org.wso2.carbon.apimgt.core.models.BlockConditions)

Aggregations

BlockConditions (org.wso2.carbon.apimgt.core.models.BlockConditions)32 Test (org.testng.annotations.Test)14 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)12 ArrayList (java.util.ArrayList)8 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)8 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)7 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.admin.dto.BlockingConditionDTO)7 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Response (javax.ws.rs.core.Response)5 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)5 APIMgtAdminServiceImpl (org.wso2.carbon.apimgt.core.impl.APIMgtAdminServiceImpl)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 BlacklistApiServiceImpl (org.wso2.carbon.apimgt.rest.api.admin.impl.BlacklistApiServiceImpl)4 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)4 BlockingConditionDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionDTO)4 BlockingConditionListDTO (org.wso2.carbon.apimgt.rest.api.core.dto.BlockingConditionListDTO)4 ResultSet (java.sql.ResultSet)3