Search in sources :

Example 1 with IPLevelDTO

use of org.wso2.carbon.apimgt.internal.service.dto.IPLevelDTO in project carbon-apimgt by wso2.

the class BlockConditionDBUtil method getBlockConditions.

public static BlockConditionsDTO getBlockConditions() {
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    List api = new ArrayList();
    List application = new ArrayList();
    List<IPLevelDTO> ip = new ArrayList();
    List user = new ArrayList();
    List custom = new ArrayList();
    String sqlQuery = "select * from AM_BLOCK_CONDITIONS";
    List subscription = new ArrayList();
    try {
        conn = BlockConditionDBUtil.getConnection();
        ps = conn.prepareStatement(sqlQuery);
        rs = ps.executeQuery();
        while (rs.next()) {
            String type = rs.getString("TYPE");
            String value = rs.getString("BLOCK_CONDITION");
            String enabled = rs.getString("ENABLED");
            String tenantDomain = rs.getString("DOMAIN");
            int conditionId = rs.getInt("CONDITION_ID");
            if (Boolean.parseBoolean(enabled)) {
                if (APIConstants.BLOCKING_CONDITIONS_API.equals(type)) {
                    api.add(value);
                } else if (APIConstants.BLOCKING_CONDITIONS_APPLICATION.equals(type)) {
                    application.add(value);
                } else if (APIConstants.BLOCKING_CONDITIONS_IP.equals(type) || APIConstants.BLOCK_CONDITION_IP_RANGE.equals(type)) {
                    IPLevelDTO ipLevelDTO = new IPLevelDTO();
                    ipLevelDTO.setTenantDomain(tenantDomain);
                    ipLevelDTO.setId(conditionId);
                    JsonElement iplevelJson = new JsonParser().parse(value);
                    if (iplevelJson instanceof JsonPrimitive) {
                        JsonPrimitive fixedIp = (JsonPrimitive) iplevelJson;
                        ipLevelDTO.setFixedIp(fixedIp.getAsString());
                        ipLevelDTO.setInvert(Boolean.FALSE);
                        ipLevelDTO.setType(APIConstants.BLOCKING_CONDITIONS_IP);
                    } else if (iplevelJson instanceof JsonObject) {
                        JsonObject ipBlockingJson = (JsonObject) iplevelJson;
                        if (ipBlockingJson.has(APIConstants.BLOCK_CONDITION_FIXED_IP)) {
                            ipLevelDTO.setType(APIConstants.BLOCKING_CONDITIONS_IP);
                            ipLevelDTO.setFixedIp(ipBlockingJson.get(APIConstants.BLOCK_CONDITION_FIXED_IP).getAsString());
                        }
                        if (ipBlockingJson.has(APIConstants.BLOCK_CONDITION_START_IP)) {
                            ipLevelDTO.setType(APIConstants.BLOCK_CONDITION_IP_RANGE);
                            ipLevelDTO.setStartingIp(ipBlockingJson.get(APIConstants.BLOCK_CONDITION_START_IP).getAsString());
                        }
                        if (ipBlockingJson.has(APIConstants.BLOCK_CONDITION_ENDING_IP)) {
                            ipLevelDTO.setEndingIp(ipBlockingJson.get(APIConstants.BLOCK_CONDITION_ENDING_IP).getAsString());
                        }
                        if (ipBlockingJson.has(APIConstants.BLOCK_CONDITION_INVERT)) {
                            ipLevelDTO.setInvert(ipBlockingJson.get(APIConstants.BLOCK_CONDITION_INVERT).getAsBoolean());
                        }
                    }
                    ip.add(ipLevelDTO);
                } else if (APIConstants.BLOCKING_CONDITIONS_USER.equals(type)) {
                    user.add(value);
                } else if ("CUSTOM".equals(type)) {
                    custom.add(value);
                } else if (APIConstants.BLOCKING_CONDITIONS_SUBSCRIPTION.equals(type)) {
                    subscription.add(value);
                }
            }
        }
    } catch (SQLException e) {
        log.error("Error while executing SQL", e);
    } finally {
        BlockConditionDBUtil.closeAllConnections(ps, conn, rs);
    }
    BlockConditionDBUtil.blockConditionsDTO = new BlockConditionsDTO();
    blockConditionsDTO.setApi(api);
    blockConditionsDTO.setApplication(application);
    blockConditionsDTO.setIp(ip);
    blockConditionsDTO.setUser(user);
    blockConditionsDTO.setCustom(custom);
    blockConditionsDTO.setSubscription(subscription);
    return blockConditionsDTO;
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) PreparedStatement(java.sql.PreparedStatement) BlockConditionsDTO(org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO) JsonElement(com.google.gson.JsonElement) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) List(java.util.List) IPLevelDTO(org.wso2.carbon.apimgt.internal.service.dto.IPLevelDTO) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 BlockConditionsDTO (org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO)1 IPLevelDTO (org.wso2.carbon.apimgt.internal.service.dto.IPLevelDTO)1