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();
}
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();
}
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();
}
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);
}
Aggregations