use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.
the class ThrottleConditionEvaluator method getApplicableConditions.
/**
* When called, provides a list of Applicable Condition Groups for the current request.
*
* @param synapseContext Message Context of the incoming request.
* @param authenticationContext AuthenticationContext populated by {@code APIAuthenticationHandler}
* @param inputConditionGroups All Condition Groups Attached with the resource/API being invoked.
* @return List of ConditionGroups applicable for the current request.
*/
public List<ConditionGroupDTO> getApplicableConditions(org.apache.synapse.MessageContext synapseContext, AuthenticationContext authenticationContext, ConditionGroupDTO[] inputConditionGroups) {
ArrayList<ConditionGroupDTO> matchingConditions = new ArrayList<>(inputConditionGroups.length);
ConditionGroupDTO defaultGroup = null;
for (ConditionGroupDTO conditionGroup : inputConditionGroups) {
if (APIConstants.THROTTLE_POLICY_DEFAULT.equals(conditionGroup.getConditionGroupId())) {
defaultGroup = conditionGroup;
} else if (isConditionGroupApplicable(synapseContext, authenticationContext, conditionGroup)) {
matchingConditions.add(conditionGroup);
}
}
// If no matching ConditionGroups are present, apply the default group.
if (matchingConditions.isEmpty()) {
matchingConditions.add(defaultGroup);
}
return matchingConditions;
}
use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.
the class SubscriptionValidationDataUtil method fromApiPolicyToApiPolicyListDTO.
public static ApiPolicyListDTO fromApiPolicyToApiPolicyListDTO(List<APIPolicy> model) {
ApiPolicyListDTO apiPolicyListDTO = new ApiPolicyListDTO();
if (model != null) {
for (APIPolicy apiPolicyModel : model) {
ApiPolicyDTO policyDTO = new ApiPolicyDTO();
policyDTO.setName(apiPolicyModel.getName());
policyDTO.setQuotaType(apiPolicyModel.getQuotaType());
policyDTO.setTenantId(apiPolicyModel.getTenantId());
policyDTO.setTenantDomain(apiPolicyModel.getTenantDomain());
policyDTO.setApplicableLevel(apiPolicyModel.getApplicableLevel());
policyDTO.setDefaultLimit(getThrottleLimitDTO(apiPolicyModel));
apiPolicyListDTO.getList().add(policyDTO);
List<APIPolicyConditionGroup> retrievedGroups = apiPolicyModel.getConditionGroups();
List<ApiPolicyConditionGroupDTO> condGroups = new ArrayList<ApiPolicyConditionGroupDTO>();
for (APIPolicyConditionGroup retGroup : retrievedGroups) {
ApiPolicyConditionGroupDTO group = new ApiPolicyConditionGroupDTO();
group.setConditionGroupId(retGroup.getConditionGroupId());
group.setQuotaType(retGroup.getQuotaType());
group.setDefaultLimit(getThrottleLimitDTO(retGroup));
group.setPolicyId(retGroup.getPolicyId());
List<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO> condition = new ArrayList<org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO>();
List<ConditionDTO> retrievedConditions = retGroup.getConditionDTOS();
for (ConditionDTO retrievedCondition : retrievedConditions) {
org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO conditionDTO = new org.wso2.carbon.apimgt.internal.service.dto.ConditionDTO();
conditionDTO.setConditionType(retrievedCondition.getConditionType());
conditionDTO.setIsInverted(retrievedCondition.isInverted());
conditionDTO.setName(retrievedCondition.getConditionName());
conditionDTO.setValue(retrievedCondition.getConditionValue());
condition.add(conditionDTO);
}
group.setCondition(condition);
condGroups.add(group);
}
policyDTO.setConditionGroups(condGroups);
}
apiPolicyListDTO.setCount(model.size());
} else {
apiPolicyListDTO.setCount(0);
}
return apiPolicyListDTO;
}
use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromConditionalGroupDTOToPipeline.
/**
* Converts a single Conditional Group DTO into a Pipeline object
*
* @param dto Conditional Group DTO
* @return Derived Pipeline object from Conditional Group DTO
* @throws UnsupportedThrottleLimitTypeException
* @throws UnsupportedThrottleConditionTypeException
*/
public static Pipeline fromConditionalGroupDTOToPipeline(ConditionalGroupDTO dto) throws UnsupportedThrottleLimitTypeException, UnsupportedThrottleConditionTypeException {
Pipeline pipeline = new Pipeline();
pipeline.setDescription(dto.getDescription());
pipeline.setEnabled(true);
pipeline.setQuotaPolicy(fromDTOToQuotaPolicy(dto.getLimit()));
List<Condition> conditions = fromDTOListToConditionList(dto.getConditions());
pipeline.setConditions(conditions);
return pipeline;
}
use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.
the class ThrottlePolicyTemplateBuilder method getThrottlePolicyForAPILevel.
/**
* Generate policy for API level throttling
*
* @param policy Policy with level 'api'. Policy can have multiple pipelines and a default condition which will be
* used as else condition
* @return a Map containing a set of policies for each condition group
* @throws APITemplateException if failed to generate policy
*/
public Map<String, String> getThrottlePolicyForAPILevel(ApiPolicy policy) throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating policy for API Level :" + policy.toString());
}
Map<String, String> policyArray = new HashMap<>();
try {
VelocityEngine velocityengine = new VelocityEngine();
APIUtil.initializeVelocityContext(velocityengine);
velocityengine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, CarbonUtils.getCarbonHome());
velocityengine.init();
Template template = velocityengine.getTemplate(getTemplatePathForAPI());
StringWriter writer;
VelocityContext context;
if (policy.getConditionGroups() != null) {
for (APIPolicyConditionGroup conditionGroup : policy.getConditionGroups()) {
if (conditionGroup.getDefaultLimit() == null) {
continue;
}
context = new VelocityContext();
setConstantContext(context);
context.put("policy", policy);
context.put("quotaPolicy", conditionGroup.getDefaultLimit());
context.put("pipeline", "condition_" + conditionGroup.getConditionGroupId());
String conditionString = getPolicyCondition(conditionGroup.getCondition());
JSONArray conditions = new JSONArray();
conditions.add(getPolicyConditionJson(conditionGroup.getCondition()));
context.put("condition", " AND " + conditionString);
context.put("evaluatedConditions", new String(Base64.encodeBase64(conditions.toJSONString().getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8));
writer = new StringWriter();
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Policy : " + writer.toString());
}
String policyName = policy.getTenantDomain() + APIConstants.DELEM_UNDERSCORE + PolicyConstants.POLICY_LEVEL_RESOURCE + APIConstants.DELEM_UNDERSCORE + policy.getName() + APIConstants.THROTTLE_POLICY_CONDITION + conditionGroup.getConditionGroupId();
policyArray.put(policyName, writer.toString());
}
}
} catch (VelocityException e) {
log.error("Velocity Error", e);
throw new APITemplateException("Velocity Error", e);
}
return policyArray;
}
use of org.wso2.charon.core.objects.Group in project carbon-apimgt by wso2.
the class TestUtil method getPolicyGlobalLevel.
/**
* Generate a dummy Global Policy object
*
* @return an Global Policy
*/
public static GlobalPolicy getPolicyGlobalLevel() {
GlobalPolicy globalPolicy = new GlobalPolicy();
globalPolicy.setId(1);
globalPolicy.setTenantId(-1234);
globalPolicy.setTenantDomain("carbon.super");
globalPolicy.setName("policy1");
globalPolicy.setQuotaType("requestCount");
QuotaPolicy defaultLimit = new QuotaPolicy();
defaultLimit.setQuotaType("requestCount");
RequestCountLimit requestCountLimit = new RequestCountLimit();
requestCountLimit.setRequestCount(1);
requestCountLimit.setTimeUnit("min");
requestCountLimit.setUnitTime(1);
defaultLimit.setRequestCount(requestCountLimit);
globalPolicy.setDefaultLimit(defaultLimit);
globalPolicy.setSiddhiQuery("FROM RequestStream SELECT userId, " + "(userId == 'admin@carbon.super') AS isEligible, " + "str :concat('admin@carbon.super', '') as throttleKey INSERT INTO " + "EligibilityStream; FROM EligibilityStream [isEligible==true] " + "#throttler:timeBatch(1 min) SELECT throttleKey, (count(userId) >= 15) " + "as isThrottled, expiryTimeStamp group by throttleKey INSERT ALL EVENTS " + "into ResultStream;");
globalPolicy.setKeyTemplate("$userId:$apiContext:$apiVersion");
return globalPolicy;
}
Aggregations