Search in sources :

Example 16 with APITemplateException

use of org.wso2.carbon.apimgt.core.template.APITemplateException in project carbon-apimgt by wso2.

the class APIConfigContextTestCase method testWithNullParameters.

@Test(expectedExceptions = APITemplateException.class)
public void testWithNullParameters() throws APITemplateException {
    API emptyNamedAPI = new API.APIBuilder(null, null, "1.0.0").context("testcontext").id(UUID.randomUUID().toString()).build();
    APIConfigContext apiConfigContext = new APIConfigContext(emptyNamedAPI, "org.test");
    apiConfigContext.validate();
}
Also used : API(org.wso2.carbon.apimgt.core.models.API) Test(org.testng.annotations.Test)

Example 17 with APITemplateException

use of org.wso2.carbon.apimgt.core.template.APITemplateException in project carbon-apimgt by wso2.

the class APIConfigContextTestCase method testWithEmptyParameters.

@Test(expectedExceptions = APITemplateException.class)
public void testWithEmptyParameters() throws APITemplateException {
    API emptyNamedAPI = new API.APIBuilder("", "", "").context("testcontext").id(UUID.randomUUID().toString()).build();
    APIConfigContext apiConfigContext = new APIConfigContext(emptyNamedAPI, "org.test");
    apiConfigContext.validate();
}
Also used : API(org.wso2.carbon.apimgt.core.models.API) Test(org.testng.annotations.Test)

Example 18 with APITemplateException

use of org.wso2.carbon.apimgt.core.template.APITemplateException in project carbon-apimgt by wso2.

the class PolicyExportManager method getCustomPolicySiddhiApps.

/**
 * Get execution plan/ siddhi apps for custom policies.
 *
 * @param customPolicies custom policy object list
 * @return Map<String, String> containing execution plan name and execution plans.
 * @throws APITemplateException If template generating fails
 */
private Map<String, String> getCustomPolicySiddhiApps(List<CustomPolicy> customPolicies) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Get execution plans for custom policies.");
    }
    Map<String, String> siddhiApps = new HashMap<>();
    String name;
    String executionPlan;
    CustomThrottlePolicyTemplateBuilder templateBuilder;
    for (CustomPolicy policy : customPolicies) {
        templateBuilder = new CustomThrottlePolicyTemplateBuilder(policy);
        name = CUSTOM + policy.getPolicyName();
        executionPlan = templateBuilder.getThrottlePolicyTemplateForCustomPolicy();
        siddhiApps.put(name, executionPlan);
    }
    return siddhiApps;
}
Also used : CustomPolicy(org.wso2.carbon.apimgt.core.models.policy.CustomPolicy) HashMap(java.util.HashMap) CustomThrottlePolicyTemplateBuilder(org.wso2.carbon.apimgt.core.template.CustomThrottlePolicyTemplateBuilder)

Example 19 with APITemplateException

use of org.wso2.carbon.apimgt.core.template.APITemplateException in project carbon-apimgt by wso2.

the class PolicyExportManager method getAppPolicySiddhiApps.

/**
 * Get execution plan/ siddhi apps for custom policies.
 *
 * @param applicationPolicies ApplicationPolicy object list
 * @return Map<String, String> containing execution plan name and execution plans.
 * @throws APITemplateException If template generating fails
 */
private Map<String, String> getAppPolicySiddhiApps(List<ApplicationPolicy> applicationPolicies) throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Get execution plans for application policy.");
    }
    Map<String, String> siddhiApps = new HashMap<>();
    String name;
    String executionPlan;
    ApplicationThrottlePolicyTemplateBuilder templateBuilder;
    for (ApplicationPolicy policy : applicationPolicies) {
        templateBuilder = new ApplicationThrottlePolicyTemplateBuilder(policy);
        name = APPLICATION + policy.getPolicyName();
        executionPlan = templateBuilder.getThrottlePolicyForAppLevel();
        siddhiApps.put(name, executionPlan);
    }
    return siddhiApps;
}
Also used : HashMap(java.util.HashMap) ApplicationThrottlePolicyTemplateBuilder(org.wso2.carbon.apimgt.core.template.ApplicationThrottlePolicyTemplateBuilder) ApplicationPolicy(org.wso2.carbon.apimgt.core.models.policy.ApplicationPolicy)

Example 20 with APITemplateException

use of org.wso2.carbon.apimgt.core.template.APITemplateException in project carbon-apimgt by wso2.

the class APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForAPILevelDefaultCondition.

/**
 * Generate default policy for api level throttling
 *
 * @return throttle policies for default api
 * @throws APITemplateException throws if generation failure occur
 */
public String getThrottlePolicyTemplateForAPILevelDefaultCondition() throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
    }
    // get velocity template for API policy and generate the template
    Set<String> conditionsSet = new HashSet<String>();
    List<Pipeline> pipelines = apiPolicy.getPipelines();
    VelocityEngine velocityengine = initVelocityEngine();
    Template template = velocityengine.getTemplate(getTemplatePathForAPIDefaultPolicy());
    StringWriter writer;
    VelocityContext context;
    // when APIPolicy contains pipelines, get template as a string
    if (pipelines != null) {
        for (Pipeline pipeline : pipelines) {
            String conditionString = getPolicyConditionForDefault(pipeline.getConditions());
            if (!StringUtils.isEmpty(conditionString)) {
                conditionsSet.add(conditionString);
            }
        }
    }
    // for default API policy
    context = new VelocityContext();
    setConstantContext(context);
    // default policy is defined as 'elseCondition' , set values for velocity context
    context.put(PIPELINE, ELSE_CONDITION);
    context.put(PIPELINE_ITEM, null);
    context.put(POLICY, apiPolicy);
    context.put(QUOTA_POLICY, apiPolicy.getDefaultQuotaPolicy());
    String conditionSetString = getConditionForDefault(conditionsSet);
    if (!StringUtils.isEmpty(conditionSetString)) {
        context.put(CONDITION, AND + conditionSetString);
    } else {
        context.put(CONDITION, EMPTY_STRING);
    }
    writer = new StringWriter();
    template.merge(context, writer);
    if (log.isDebugEnabled()) {
        log.debug("Generated Siddhi App : " + writer.toString());
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) HashSet(java.util.HashSet) Pipeline(org.wso2.carbon.apimgt.core.models.policy.Pipeline) Template(org.apache.velocity.Template)

Aggregations

Test (org.testng.annotations.Test)9 HashMap (java.util.HashMap)6 API (org.wso2.carbon.apimgt.core.models.API)6 StringWriter (java.io.StringWriter)5 Template (org.apache.velocity.Template)5 VelocityContext (org.apache.velocity.VelocityContext)5 VelocityEngine (org.apache.velocity.app.VelocityEngine)5 APITemplateException (org.wso2.carbon.apimgt.core.template.APITemplateException)5 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)4 APIConfigContext (org.wso2.carbon.apimgt.core.template.APIConfigContext)4 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)3 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)3 GatewaySourceGenerator (org.wso2.carbon.apimgt.core.api.GatewaySourceGenerator)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)3 CompositeAPIConfigContext (org.wso2.carbon.apimgt.core.template.CompositeAPIConfigContext)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 CommonsLogLogChute (org.apache.velocity.runtime.log.CommonsLogLogChute)2