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;
}
Aggregations