use of org.apache.velocity.Template in project carbon-apimgt by wso2.
the class CustomThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForCustomPolicy.
/**
* Generate policy for global level.
*
* @return policy template as a string
* @throws APITemplateException throws if any error occurred
*/
public String getThrottlePolicyTemplateForCustomPolicy() throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating Siddhi app for custom policy :" + customPolicy.toString());
}
// get velocity template for custom throttle policy and generate the template
StringWriter writer = new StringWriter();
VelocityContext context = new VelocityContext();
VelocityEngine velocityengine = initVelocityEngine();
Template template = velocityengine.getTemplate(getTemplatePathForGlobal());
setConstantContext(context);
// set values for velocity context
context.put(POLICY, customPolicy);
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 SubscriptionThrottlePolicyTemplateBuilder method getThrottlePolicyForSubscriptionLevel.
/**
* Generate policy for subscription level.
*
* @return throttle policies for subscription level
* @throws APITemplateException throws if generation failure occur
*/
public String getThrottlePolicyForSubscriptionLevel() throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating Siddhi app for subscriptionLevel :" + subscriptionPolicy.toString());
}
// get velocity template for Subscription policy and generate the template
StringWriter writer = new StringWriter();
VelocityEngine velocityengine = initVelocityEngine();
Template template = velocityengine.getTemplate(getTemplatePathForSubscription());
VelocityContext context = new VelocityContext();
setConstantContext(context);
// set values for velocity context
context.put(POLICY, subscriptionPolicy);
context.put(QUOTA_POLICY, subscriptionPolicy.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 APIThrottlePolicyTemplateBuilder method getThrottlePolicyTemplateForPipelines.
/**
* Generate policy for api level throttling
*
* @return throttle policies for api level
* @throws APITemplateException throws if generation failure occur
*/
public Map<String, String> getThrottlePolicyTemplateForPipelines() throws APITemplateException {
if (log.isDebugEnabled()) {
log.debug("Generating Siddhi App for apiLevel :" + apiPolicy.toString());
}
// get velocity template for API policy pipeline and generate the template
Map<String, String> policyArray = new HashMap<String, String>();
StringWriter writer;
VelocityContext context;
VelocityEngine velocityengine = initVelocityEngine();
Template template = velocityengine.getTemplate(getTemplatePathForAPI());
// Generate template for pipeline conditions if pipelines not null
if (apiPolicy.getPipelines() != null) {
for (Pipeline pipeline : apiPolicy.getPipelines()) {
// set values for velocity context
context = new VelocityContext();
setConstantContext(context);
context.put(PIPELINE_ITEM, pipeline);
context.put(POLICY, apiPolicy);
context.put(QUOTA_POLICY, pipeline.getQuotaPolicy());
context.put(PIPELINE, CONDITION + UNDERSCORE + pipeline.getId());
String conditionString = getPolicyCondition(pipeline.getConditions());
context.put(CONDITION, AND + conditionString);
writer = new StringWriter();
template.merge(context, writer);
if (log.isDebugEnabled()) {
log.debug("Generated Siddhi App : " + writer.toString());
}
String policyName = PolicyConstants.POLICY_LEVEL_RESOURCE + UNDERSCORE + apiPolicy.getPolicyName() + UNDERSCORE + CONDITION + UNDERSCORE + pipeline.getId();
policyArray.put(policyName, writer.toString());
}
}
return policyArray;
}
use of org.apache.velocity.Template in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getConfigStringFromTemplate.
@Override
public String getConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "template.xml";
try {
// build the context for template and apply the necessary decorators
apiConfigContext.validate();
ConfigContext configContext = new ResourceConfigContext(apiConfigContext, apiResources);
VelocityContext context = configContext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
use of org.apache.velocity.Template in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getCompositeAPIConfigStringFromTemplate.
@Override
public String getCompositeAPIConfigStringFromTemplate(List<TemplateBuilderDTO> apiResources, List<CompositeAPIEndpointDTO> compositeApiEndpoints) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "composite_template.xml";
try {
// build the context for template and apply the necessary decorators
apiConfigContext.validate();
CompositeAPIConfigContext configContext = new CompositeAPIConfigContext(apiConfigContext, apiResources, compositeApiEndpoints);
VelocityContext context = configContext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityengine.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());
velocityengine.init();
Template template = velocityengine.getTemplate(templatePath);
template.merge(context, writer);
} catch (ResourceNotFoundException e) {
log.error("Template " + templatePath + " not Found", e);
throw new APITemplateException("Template " + templatePath + " not Found", ExceptionCodes.TEMPLATE_EXCEPTION);
} catch (ParseErrorException e) {
log.error("Syntax error in " + templatePath, e);
throw new APITemplateException("Syntax error in " + templatePath, ExceptionCodes.TEMPLATE_EXCEPTION);
}
return writer.toString();
}
Aggregations