use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingDenyPoliciesPost.
/**
* Add a Block Condition
*
* @param body DTO of new block condition to be created
* @param contentType Content-Type header
* @return Created block condition along with the location of it with Location header
*/
@Override
public Response throttlingDenyPoliciesPost(String contentType, BlockingConditionDTO body, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
// Add the block condition. It will throw BlockConditionAlreadyExistsException if the condition already
// exists in the system
String uuid = null;
if (ConditionTypeEnum.API.equals(body.getConditionType()) || ConditionTypeEnum.APPLICATION.equals(body.getConditionType()) || ConditionTypeEnum.USER.equals(body.getConditionType())) {
uuid = apiProvider.addBlockCondition(body.getConditionType().toString(), (String) body.getConditionValue(), body.isConditionStatus());
} else if (ConditionTypeEnum.IP.equals(body.getConditionType()) || ConditionTypeEnum.IPRANGE.equals(body.getConditionType())) {
if (body.getConditionValue() instanceof Map) {
JSONObject jsonObject = new JSONObject();
jsonObject.putAll((Map) body.getConditionValue());
if (ConditionTypeEnum.IP.equals(body.getConditionType())) {
RestApiAdminUtils.validateIPAddress(jsonObject.get("fixedIp").toString());
}
if (ConditionTypeEnum.IPRANGE.equals(body.getConditionType())) {
RestApiAdminUtils.validateIPAddress(jsonObject.get("startingIp").toString());
RestApiAdminUtils.validateIPAddress(jsonObject.get("endingIp").toString());
}
uuid = apiProvider.addBlockCondition(body.getConditionType().toString(), jsonObject.toJSONString(), body.isConditionStatus());
}
}
// retrieve the new blocking condition and send back as the response
BlockConditionsDTO newBlockingCondition = apiProvider.getBlockConditionByUUID(uuid);
BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(newBlockingCondition);
return Response.created(new URI(RestApiConstants.RESOURCE_PATH_THROTTLING_BLOCK_CONDITIONS + "/" + uuid)).entity(dto).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceAlreadyExists(e)) {
RestApiUtil.handleResourceAlreadyExistsError("A black list item with type: " + body.getConditionType() + ", value: " + body.getConditionValue() + " already exists", e, log);
} else {
String errorMessage = "Error while adding Blocking Condition. Condition type: " + body.getConditionType() + ", " + "value: " + body.getConditionValue() + ". " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException | ParseException e) {
String errorMessage = "Error while retrieving Blocking Condition resource location: Condition type: " + body.getConditionType() + ", " + "value: " + body.getConditionValue() + ". " + e.getMessage();
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.
the class BlockingConditionMappingUtil method fromBlockConditionListToListDTO.
/**
* Converts a List of Block Condition in to REST API LIST DTO Object
*
* @param blockConditionList A List of Block Conditions
* @return REST API List DTO object derived from Block Condition list
*/
public static BlockingConditionListDTO fromBlockConditionListToListDTO(List<BlockConditionsDTO> blockConditionList) throws ParseException {
BlockingConditionListDTO listDTO = new BlockingConditionListDTO();
List<BlockingConditionDTO> blockingConditionDTOList = new ArrayList<>();
if (blockConditionList != null) {
for (BlockConditionsDTO blockCondition : blockConditionList) {
BlockingConditionDTO dto = fromBlockingConditionToDTO(blockCondition);
blockingConditionDTOList.add(dto);
}
}
listDTO.setCount(blockingConditionDTOList.size());
listDTO.setList(blockingConditionDTOList);
return listDTO;
}
use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.
the class ApiMgtDAO method getBlockConditionByUUID.
/**
* Get details of a block condition by UUID
*
* @param uuid uuid of the block condition
* @return Block condition represented by the UUID
* @throws APIManagementException
*/
public BlockConditionsDTO getBlockConditionByUUID(String uuid) throws APIManagementException {
Connection connection = null;
PreparedStatement selectPreparedStatement = null;
ResultSet resultSet = null;
BlockConditionsDTO blockCondition = null;
try {
String query = SQLConstants.ThrottleSQLConstants.GET_BLOCK_CONDITION_BY_UUID_SQL;
connection = APIMgtDBUtil.getConnection();
connection.setAutoCommit(true);
selectPreparedStatement = connection.prepareStatement(query);
selectPreparedStatement.setString(1, uuid);
resultSet = selectPreparedStatement.executeQuery();
if (resultSet.next()) {
blockCondition = new BlockConditionsDTO();
blockCondition.setEnabled(resultSet.getBoolean("ENABLED"));
blockCondition.setConditionType(resultSet.getString("TYPE"));
blockCondition.setConditionValue(resultSet.getString("BLOCK_CONDITION"));
blockCondition.setConditionId(resultSet.getInt("CONDITION_ID"));
blockCondition.setTenantDomain(resultSet.getString("DOMAIN"));
blockCondition.setUUID(resultSet.getString("UUID"));
}
} catch (SQLException e) {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException ex) {
handleException("Failed to rollback getting Block condition by uuid " + uuid, ex);
}
}
handleException("Failed to get Block condition by uuid " + uuid, e);
} finally {
APIMgtDBUtil.closeAllConnections(selectPreparedStatement, connection, resultSet);
}
return blockCondition;
}
use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testUpdateBlockConditionByUUID.
@Test
public void testUpdateBlockConditionByUUID() throws APIManagementException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
Mockito.when(apimgtDAO.updateBlockConditionStateByUUID("testID", "testState")).thenReturn(false, true);
BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
blockConditionsDTO.setConditionType("testType");
blockConditionsDTO.setConditionValue("USER");
Mockito.when(apimgtDAO.getBlockConditionByUUID("testState")).thenReturn(blockConditionsDTO);
// updateState false
assertFalse(apiProvider.updateBlockConditionByUUID("testID", "testState"));
// updateState true
assertTrue(apiProvider.updateBlockConditionByUUID("testID", "testState"));
}
use of org.wso2.carbon.apimgt.internal.service.dto.BlockConditionsDTO in project carbon-apimgt by wso2.
the class APIProviderImplTest method testGetBlockConditionByUUID.
@Test
public void testGetBlockConditionByUUID() throws APIManagementException {
APIProviderImplWrapper apiProvider = new APIProviderImplWrapper(apimgtDAO, scopesDAO);
BlockConditionsDTO blockConditionsDTO = new BlockConditionsDTO();
Mockito.when(apimgtDAO.getBlockConditionByUUID("testUUID")).thenReturn(blockConditionsDTO);
// Normal Path
assertNotNull(apiProvider.getBlockConditionByUUID("testUUID"));
Mockito.when(apimgtDAO.getBlockConditionByUUID("testUUID")).thenThrow(BlockConditionNotFoundException.class);
// BlockConditionNotFound exception
try {
assertNull(apiProvider.getBlockConditionByUUID("testUUID"));
} catch (APIManagementException e) {
Assert.assertEquals(null, e.getMessage());
}
}
Aggregations