use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromDTOToHeaderCondition.
/**
* Converts a Header Condition DTO object into a model object
*
* @param dto Header Condition DTO object
* @return Header Condition model object derived from Header Condition DTO
*/
public static HeaderCondition fromDTOToHeaderCondition(ThrottleConditionDTO dto) {
HeaderCondition headerCondition = new HeaderCondition();
headerCondition = updateFieldsFromDTOToCondition(dto, headerCondition);
headerCondition.setHeader(dto.getHeaderCondition().getHeaderName());
headerCondition.setValue(dto.getHeaderCondition().getHeaderValue());
return headerCondition;
}
use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromHeaderConditionToDTO.
/**
* Converts a Header Condition model object into a DTO
*
* @param headerCondition Header Condition model object
* @return DTO object that was derived from Header Condition model object
*/
public static ThrottleConditionDTO fromHeaderConditionToDTO(HeaderCondition headerCondition) {
ThrottleConditionDTO throttleConditionDTO = new ThrottleConditionDTO();
throttleConditionDTO.setType(PolicyConstants.HEADER_CONDITION_TYPE);
throttleConditionDTO.setHeaderCondition(new HeaderConditionDTO());
throttleConditionDTO = updateFieldsFromConditionToDTO(headerCondition, throttleConditionDTO);
throttleConditionDTO.getHeaderCondition().setHeaderName(headerCondition.getHeaderName());
throttleConditionDTO.getHeaderCondition().setHeaderValue(headerCondition.getValue());
return throttleConditionDTO;
}
use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.
the class BlacklistApiServiceImpl method blacklistConditionIdDelete.
/**
* Delete blacklist condition using ID
*
* @param conditionId condition ID of the block condition
* @param ifMatch IF-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return Response Object
* @throws NotFoundException Iif an error occurred when particular resource does not exits in the system.
*/
@Override
public Response blacklistConditionIdDelete(String conditionId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received Blacklist Condition DELETE request with id: " + conditionId);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
apiMgtAdminService.deleteBlockConditionByUuid(conditionId);
return Response.ok().build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while deleting blacklist condition with UUID " + conditionId;
org.wso2.carbon.apimgt.rest.api.common.dto.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.api.model.policy.Condition in project carbon-apimgt by wso2.
the class SampleTestObjectCreator method createDefaultPipelines.
/**
* create default pipeline for api policy
*
* @return list of Pipeline objects is returned
*/
public static List<Pipeline> createDefaultPipelines() {
// Pipeline 1
IPCondition ipCondition = new IPCondition(PolicyConstants.IP_RANGE_TYPE);
ipCondition.setStartingIP("192.168.12.3");
ipCondition.setEndingIP("192.168.88.19");
IPCondition ipConditionSpecific = new IPCondition(PolicyConstants.IP_SPECIFIC_TYPE);
ipConditionSpecific.setSpecificIP("123.42.14.56");
// adding above conditions to condition list of pipeline 1
// contains conditions for each pipeline
List<Condition> conditionsList = new ArrayList<>();
conditionsList.add(ipCondition);
conditionsList.add(ipConditionSpecific);
// set quota policy with bandwidth limit
BandwidthLimit bandwidthLimit = new BandwidthLimit(TIME_UNIT_MONTH, 1, 1000, PolicyConstants.MB);
QuotaPolicy quotaPolicy1 = new QuotaPolicy();
quotaPolicy1.setType(PolicyConstants.BANDWIDTH_TYPE);
quotaPolicy1.setLimit(bandwidthLimit);
Pipeline pipeline1 = new Pipeline();
pipeline1.setId(0);
pipeline1.setConditions(conditionsList);
pipeline1.setQuotaPolicy(quotaPolicy1);
// End of pipeline 1 -> Beginning of pipeline 2
HeaderCondition headerCondition = new HeaderCondition();
headerCondition.setHeader("Browser");
headerCondition.setValue("Chrome");
JWTClaimsCondition jwtClaimsCondition = new JWTClaimsCondition();
jwtClaimsCondition.setClaimUrl("/path/path2");
jwtClaimsCondition.setAttribute("attributed");
QueryParameterCondition queryParameterCondition = new QueryParameterCondition();
queryParameterCondition.setParameter("Location");
queryParameterCondition.setValue("Colombo");
// adding conditions to condition list of pipeline2
conditionsList = new ArrayList<>();
conditionsList.add(headerCondition);
conditionsList.add(jwtClaimsCondition);
conditionsList.add(queryParameterCondition);
// pipeline 2 with request count as quota policy
RequestCountLimit requestCountLimit = new RequestCountLimit(TIME_UNIT_SECONDS, 1, 1000);
QuotaPolicy quotaPolicy2 = new QuotaPolicy();
quotaPolicy2.setType(REQUEST_COUNT_TYPE);
quotaPolicy2.setLimit(requestCountLimit);
Pipeline pipeline2 = new Pipeline();
pipeline2.setId(1);
pipeline2.setConditions(conditionsList);
pipeline2.setQuotaPolicy(quotaPolicy2);
// adding pipelines
// contains all the default pipelines
List<Pipeline> pipelineList = new ArrayList<>();
pipelineList.add(pipeline1);
pipelineList.add(pipeline2);
return pipelineList;
}
use of org.wso2.carbon.apimgt.api.model.policy.Condition in project carbon-apimgt by wso2.
the class PolicyDAOImplIT method testAddGetUpdateDeleteBlockConditions.
@Test(description = "Add, Get, Delete block condition")
public void testAddGetUpdateDeleteBlockConditions() throws Exception {
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
BlockConditions blockConditionsIP = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_IP);
BlockConditions blockConditionsIpRange = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITION_IP_RANGE);
BlockConditions blockConditionsApi = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_API);
BlockConditions blockConditionsApp = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_APPLICATION);
BlockConditions blockConditionsUser = SampleTestObjectCreator.createDefaultBlockCondition(APIMgtConstants.ThrottlePolicyConstants.BLOCKING_CONDITIONS_USER);
String uuidIp = policyDAO.addBlockConditions(blockConditionsIP);
String uuidIpRange = policyDAO.addBlockConditions(blockConditionsIpRange);
String uuidApi = policyDAO.addBlockConditions(blockConditionsApi);
String uuidApp = policyDAO.addBlockConditions(blockConditionsApp);
String uuidUser = policyDAO.addBlockConditions(blockConditionsUser);
BlockConditions blockConditionsAddedIP = policyDAO.getBlockConditionByUUID(uuidIp);
BlockConditions blockConditionsAddedIpRange = policyDAO.getBlockConditionByUUID(uuidIpRange);
BlockConditions blockConditionsAddedApi = policyDAO.getBlockConditionByUUID(uuidApi);
BlockConditions blockConditionsAddedApp = policyDAO.getBlockConditionByUUID(uuidApp);
BlockConditions blockConditionsAddedUser = policyDAO.getBlockConditionByUUID(uuidUser);
Assert.assertEquals(blockConditionsIP.getConditionValue(), blockConditionsAddedIP.getConditionValue());
Assert.assertEquals(blockConditionsApi.getConditionValue(), blockConditionsAddedApi.getConditionValue());
Assert.assertEquals(blockConditionsApp.getConditionValue(), blockConditionsAddedApp.getConditionValue());
Assert.assertEquals(blockConditionsUser.getConditionValue(), blockConditionsAddedUser.getConditionValue());
Assert.assertEquals(blockConditionsIpRange.getStartingIP(), blockConditionsAddedIpRange.getStartingIP());
Assert.assertTrue(policyDAO.updateBlockConditionStateByUUID(uuidIp, true));
Assert.assertTrue(policyDAO.getBlockConditionByUUID(uuidIp).isEnabled());
Assert.assertTrue(policyDAO.getBlockConditions().size() == 5);
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidIp));
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidIpRange));
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidApi));
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidApp));
Assert.assertTrue(policyDAO.deleteBlockConditionByUuid(uuidUser));
}
Aggregations