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