Search in sources :

Example 46 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine 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)

Example 47 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.

the class ApplicationThrottlePolicyTemplateBuilder method getThrottlePolicyForAppLevel.

/**
 * Generate application level policy.
 *
 * @return throttle policies for app level
 * @throws APITemplateException throws if generation failure occur
 */
public String getThrottlePolicyForAppLevel() throws APITemplateException {
    if (log.isDebugEnabled()) {
        log.debug("Generating Siddhi app for appLevel :" + applicationPolicy.toString());
    }
    // get velocity template for Application policy and generate the template
    StringWriter writer = new StringWriter();
    VelocityEngine velocityengine = initVelocityEngine();
    Template template = velocityengine.getTemplate(getTemplatePathForApplication());
    VelocityContext context = new VelocityContext();
    setConstantContext(context);
    // set values for velocity context
    context.put(POLICY, applicationPolicy);
    context.put(QUOTA_POLICY, applicationPolicy.getDefaultQuotaPolicy());
    template.merge(context, writer);
    if (log.isDebugEnabled()) {
        log.debug("Generated Siddhi app for policy : " + writer.toString());
    }
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template)

Example 48 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.

the class ContainerBasedGatewayTemplateBuilder method generateTemplate.

/**
 * Generate template for given template values
 *
 * @param templateValues Template values
 * @param template       Template
 * @return template as a String
 */
public String generateTemplate(Map<String, String> templateValues, String template) {
    StringWriter writer = new StringWriter();
    VelocityEngine velocityengine = initVelocityEngine();
    String templateLocation = cmsTemplateLocation + template;
    Template generateTemplate = velocityengine.getTemplate(templateLocation);
    VelocityContext context = setVelocityContextValues(templateValues);
    generateTemplate.merge(context, writer);
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template)

Example 49 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project carbon-apimgt by wso2.

the class ContainerBasedGatewayTemplateBuilder method initVelocityEngine.

/**
 * Init velocity engine.
 *
 * @return Velocity engine for service template
 */
private VelocityEngine initVelocityEngine() {
    VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, CLASS_PATH);
    velocityEngine.setProperty(CLASS_PATH_RESOURCE_LOADER, ClasspathResourceLoader.class.getName());
    velocityEngine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
    velocityEngine.init();
    return velocityEngine;
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) CommonsLogLogChute(org.apache.velocity.runtime.log.CommonsLogLogChute) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)

Aggregations

VelocityEngine (org.apache.velocity.app.VelocityEngine)49 VelocityContext (org.apache.velocity.VelocityContext)23 StringWriter (java.io.StringWriter)21 Template (org.apache.velocity.Template)21 Properties (java.util.Properties)15 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)14 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)9 IOException (java.io.IOException)8 ParseErrorException (org.apache.velocity.exception.ParseErrorException)7 File (java.io.File)4 Writer (java.io.Writer)4 CommonsLogLogChute (org.apache.velocity.runtime.log.CommonsLogLogChute)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 CopyrightManager (com.intellij.copyright.CopyrightManager)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 HashMap (java.util.HashMap)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2