Search in sources :

Example 56 with Template

use of org.apache.velocity.Template 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 57 with Template

use of org.apache.velocity.Template 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 58 with Template

use of org.apache.velocity.Template 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 59 with Template

use of org.apache.velocity.Template in project janusgraph by JanusGraph.

the class AbstractJanusGraphAssemblyIT method parseTemplateAndRunExpect.

protected void parseTemplateAndRunExpect(String expectTemplateName, Map<String, String> contextVars) throws IOException, InterruptedException {
    VelocityContext context = new VelocityContext();
    for (Map.Entry<String, String> ent : contextVars.entrySet()) {
        context.put(ent.getKey(), ent.getValue());
    }
    Template template = Velocity.getTemplate(expectTemplateName);
    String inputPath = EXPECT_DIR + File.separator + expectTemplateName;
    String outputPath = inputPath.substring(0, inputPath.length() - 3);
    Writer output = new FileWriter(outputPath);
    template.merge(context, output);
    output.close();
    expect(ZIPFILE_EXTRACTED, outputPath);
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) FileWriter(java.io.FileWriter) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) FileWriter(java.io.FileWriter) Writer(java.io.Writer) Template(org.apache.velocity.Template)

Aggregations

Template (org.apache.velocity.Template)59 VelocityContext (org.apache.velocity.VelocityContext)41 StringWriter (java.io.StringWriter)28 VelocityEngine (org.apache.velocity.app.VelocityEngine)21 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)12 IOException (java.io.IOException)11 ParseErrorException (org.apache.velocity.exception.ParseErrorException)8 Writer (java.io.Writer)7 File (java.io.File)6 Map (java.util.Map)6 Properties (java.util.Properties)6 FileWriter (java.io.FileWriter)5 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)5 PrintWriter (java.io.PrintWriter)4 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)4 SystemException (com.github.bordertech.wcomponents.util.SystemException)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 FileOutputStream (java.io.FileOutputStream)3 HashMap (java.util.HashMap)3 UIContext (com.github.bordertech.wcomponents.UIContext)2