use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO in project carbon-apimgt by wso2.
the class BlacklistApiServiceImpl method blacklistPost.
/**
* Add a blacklist condition.
*
* @param body DTO object including the blacklist condition data
* @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 blacklistPost(BlockingConditionDTO body, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received BlockCondition POST request with body: " + body);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
// Add the block condition. It will throw BlockConditionAlreadyExistsException if the condition already
// exists in the system
BlockConditions blockConditions = BlockingConditionMappingUtil.fromBlockingConditionDTOToBlockCondition(body);
String uuid = apiMgtAdminService.addBlockCondition(blockConditions);
// retrieve the new blocking condition and send back as the response
BlockConditions newBlockingCondition = apiMgtAdminService.getBlockConditionByUUID(uuid);
BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(newBlockingCondition);
return Response.status(Response.Status.CREATED).entity(dto).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while adding blocking condition with UUID " + body.getConditionId();
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.rest.api.admin.v1.dto.BlockingConditionDTO in project carbon-apimgt by wso2.
the class MappingUtilTestCase method fromBlockingConditionToDTOIPRangeTest.
@Test
public void fromBlockingConditionToDTOIPRangeTest() {
BlockConditions blockConditionIPRANGE = SampleTestObjectCreator.createUniqueBlockConditions(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE);
BlockingConditionDTO blockingConditionDTo = MappingUtil.fromBlockingConditionToDTO(blockConditionIPRANGE);
Assert.assertEquals(blockConditionIPRANGE.getUuid(), blockingConditionDTo.getUuid());
Assert.assertEquals(blockConditionIPRANGE.getConditionType(), blockingConditionDTo.getConditionType());
Assert.assertEquals(blockConditionIPRANGE.getConditionValue(), blockingConditionDTo.getConditionValue());
Assert.assertEquals(Boolean.valueOf(blockConditionIPRANGE.isEnabled()), Boolean.valueOf(blockingConditionDTo.getEnabled()));
Assert.assertEquals(Long.valueOf(APIUtils.ipToLong(blockConditionIPRANGE.getEndingIP())), Long.valueOf(blockingConditionDTo.getEndingIP()));
Assert.assertEquals(Long.valueOf(APIUtils.ipToLong(blockConditionIPRANGE.getStartingIP())), Long.valueOf(blockingConditionDTo.getStartingIP()));
// Test for null handling
blockConditionIPRANGE.setUuid(null);
Assert.assertNull(MappingUtil.fromBlockingConditionToDTO(blockConditionIPRANGE));
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingDenyPolicyConditionIdPatch.
/**
* Updates an existing condition status of a blocking condition
*
* @param conditionId Id of the block condition
* @param body content to update
* @param contentType Content-Type header
* @return 200 response if successful
*/
@Override
public Response throttlingDenyPolicyConditionIdPatch(String conditionId, String contentType, BlockingConditionStatusDTO body, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
// This will give BlockConditionNotFoundException if there's no block condition exists with UUID
BlockConditionsDTO existingCondition = apiProvider.getBlockConditionByUUID(conditionId);
if (!RestApiAdminUtils.isBlockConditionAccessibleToUser(username, existingCondition)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, log);
}
// update the status
apiProvider.updateBlockConditionByUUID(conditionId, String.valueOf(body.isConditionStatus()));
// retrieve the new blocking condition and send back as the response
BlockConditionsDTO newBlockingCondition = apiProvider.getBlockConditionByUUID(conditionId);
BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(newBlockingCondition);
return Response.ok().entity(dto).build();
} catch (APIManagementException | ParseException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, e, log);
} else {
String errorMessage = "Error while updating Block Condition Status. Id : " + conditionId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO in project carbon-apimgt by wso2.
the class ThrottlingApiServiceImpl method throttlingDenyPolicyConditionIdGet.
/**
* Get a specific Block condition by its id
*
* @param conditionId Id of the block condition
* @return Matched block condition for the given Id
*/
@Override
public Response throttlingDenyPolicyConditionIdGet(String conditionId, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
// This will give BlockConditionNotFoundException if there's no block condition exists with UUID
BlockConditionsDTO blockCondition = apiProvider.getBlockConditionByUUID(conditionId);
if (!RestApiAdminUtils.isBlockConditionAccessibleToUser(username, blockCondition)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, log);
}
BlockingConditionDTO dto = BlockingConditionMappingUtil.fromBlockingConditionToDTO(blockCondition);
return Response.ok().entity(dto).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_BLOCK_CONDITION, conditionId, e, log);
} else {
String errorMessage = "Error while retrieving Block Condition. Id : " + conditionId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (ParseException e) {
String errorMessage = "Error while retrieving Blocking Conditions";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BlockingConditionDTO 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
*/
public static BlockingConditionDTO fromBlockingConditionToDTO(BlockConditionsDTO blockCondition) throws ParseException {
BlockingConditionDTO dto = new BlockingConditionDTO();
dto.setConditionId(blockCondition.getUUID());
dto.setConditionType(BlockingConditionDTO.ConditionTypeEnum.fromValue(blockCondition.getConditionType()));
if (APIConstants.BLOCKING_CONDITIONS_API.equals(blockCondition.getConditionType()) || APIConstants.BLOCKING_CONDITIONS_APPLICATION.equals(blockCondition.getConditionType()) || APIConstants.BLOCKING_CONDITIONS_USER.equals(blockCondition.getConditionType())) {
dto.setConditionValue(blockCondition.getConditionValue());
} else if (APIConstants.BLOCKING_CONDITIONS_IP.equals(blockCondition.getConditionType()) || APIConstants.BLOCK_CONDITION_IP_RANGE.equalsIgnoreCase(blockCondition.getConditionType())) {
Object parse = new JSONParser().parse(blockCondition.getConditionValue());
dto.setConditionValue(parse);
}
dto.setConditionStatus(blockCondition.isEnabled());
return dto;
}
Aggregations