use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultBlockCondition.
public static BlockConditions createDefaultBlockCondition(String conditionType) {
BlockConditions blockConditions = new BlockConditions();
blockConditions.setConditionType(conditionType);
blockConditions.setEnabled(true);
if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP)) {
blockConditions.setConditionValue(SAMPLE_IP_1);
} else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE)) {
blockConditions.setStartingIP(SAMPLE_IP_1);
blockConditions.setEndingIP(SAMPLE_IP_2);
} else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API)) {
try {
API.APIBuilder apiBuilder = SampleTestObjectCreator.createDefaultAPI();
API api = apiBuilder.build();
DAOFactory.getApiDAO().addAPI(api);
blockConditions.setConditionValue(api.getContext());
} catch (APIMgtDAOException e) {
log.error("Error while adding default api in default block condition", e);
}
} else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION)) {
try {
Application app = createDefaultApplication();
DAOFactory.getApplicationDAO().addApplication(app);
blockConditions.setConditionValue(app.getId() + ":" + app.getName());
} catch (APIMgtDAOException e) {
log.error("Error while adding default app in default block condition", e);
}
} else if (conditionType.equals(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_USER)) {
blockConditions.setConditionValue(ADMIN);
}
return blockConditions;
}
use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtil method fromBlockingConditionToDTO.
/**
* Converts a single Block Condition model object into REST API DTO object.
*
* @param blockCondition Block condition model object
* @return Block condition DTO object derived from block condition model object
* @throws UnsupportedThrottleLimitTypeException - If error occurs
*/
public static BlockingConditionDTO fromBlockingConditionToDTO(BlockConditions blockCondition) throws UnsupportedThrottleLimitTypeException {
if (blockCondition.getUuid() == null) {
return null;
}
BlockingConditionDTO dto = new BlockingConditionDTO();
dto.setConditionId(blockCondition.getUuid());
dto.setConditionType(blockCondition.getConditionType());
dto.setStatus(blockCondition.isEnabled());
if (APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE.equals(blockCondition.getConditionType())) {
dto.setIpCondition(fromBlockConditionToIpConditionDTO(blockCondition));
}
String conditionValue = blockCondition.getConditionValue();
dto.setConditionValue(conditionValue);
return dto;
}
use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class BlacklistApiServiceImpl method blacklistGet.
/**
* Get blacklist conditions.
*
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return Response object
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response blacklistGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received BlockCondition GET request");
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
List<BlockConditions> blockConditions = apiMgtAdminService.getBlockConditions();
BlockingConditionListDTO listDTO = BlockingConditionMappingUtil.fromBlockConditionListToListDTO(blockConditions);
return Response.ok().entity(listDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while getting blacklist ";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class MappingUtilTestCase method fromBlockConditionListToListDTOTest.
@Test
public void fromBlockConditionListToListDTOTest() {
List<BlockConditions> blockConditionsList = new ArrayList<>();
blockConditionsList.add(SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE));
blockConditionsList.add(SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API));
blockConditionsList.add(SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP));
BlockingConditionListDTO blockingConditionDTOList = MappingUtil.fromBlockConditionListToListDTO(blockConditionsList);
Assert.assertEquals(blockingConditionDTOList.getList().size(), blockConditionsList.size());
}
use of org.wso2.carbon.apimgt.core.models.BlockConditions in project carbon-apimgt by wso2.
the class MappingUtilTestCase method fromBlockingConditionToDTOTest.
@Test
public void fromBlockingConditionToDTOTest() {
BlockConditions blockCondition = SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP);
BlockingConditionDTO blockingConditionDTo = MappingUtil.fromBlockingConditionToDTO(blockCondition);
Assert.assertEquals(blockCondition.getUuid(), blockingConditionDTo.getUuid());
Assert.assertEquals(blockCondition.getConditionType(), blockingConditionDTo.getConditionType());
Assert.assertEquals(blockCondition.getConditionValue(), blockingConditionDTo.getConditionValue());
Assert.assertEquals(Boolean.valueOf(blockCondition.isEnabled()), Boolean.valueOf(blockingConditionDTo.getEnabled()));
Assert.assertEquals(Long.valueOf(APIUtils.ipToLong(blockCondition.getConditionValue())), Long.valueOf(blockingConditionDTo.getFixedIp()));
}
Aggregations