use of org.apache.velocity.Template in project carbon-apimgt by wso2.
the class GatewaySourceGeneratorImpl method getEndpointConfigStringFromTemplate.
@Override
public String getEndpointConfigStringFromTemplate(Endpoint endpoint) throws APITemplateException {
StringWriter writer = new StringWriter();
String templatePath = "resources" + File.separator + "template" + File.separator + "endpoint.xml";
try {
// build the context for template and apply the necessary decorators
ConfigContext configcontext = new EndpointContext(endpoint, packageName);
VelocityContext context = configcontext.getContext();
VelocityEngine velocityengine = new VelocityEngine();
velocityengine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityengine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
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 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 Gargoyle by callakrsos.
the class Mailer method sendMail.
public void sendMail(SenderMailInfo mailSenderInfo, Mail mail, VelocityContext velocityContext) throws Exception {
if (this.mailUseYn != null) {
if ("N".equals(this.mailUseYn)) {
throw new Exception("Mail Serivce's configuration is not set useYn Y ");
}
}
String _encoding = MailConst.MAILER_DEFAULT_ENCODING;
SimpleMailMessage message = new SimpleMailMessage();
if (mail.getMailFrom() != null) {
message.setFrom(mail.getMailFrom());
} else if (ValueUtil.isNotEmpty(mailFrom)) {
message.setFrom(mailFrom);
} else {
String fromAddr = ResourceLoader.getInstance().get("mail.from.address");
message.setFrom(fromAddr);
}
message.setTo(mail.getMailTo());
if (mail.getMailSubject() != null) {
message.setSubject(mail.getMailSubject());
} else {
message.setSubject(this.mailTitle);
}
if (encoding != null)
_encoding = encoding;
if (mailSenderInfo != null) {
String sendUserId = mailSenderInfo.getSendUserId();
String sendUserPassword = mailSenderInfo.getSendUserPassword();
if (ValueUtil.isEmpty(sendUserId) || ValueUtil.isEmpty(sendUserPassword)) {
throw new IllegalArgumentException("user id or password is empty...");
}
mailSender.setUsername(sendUserId);
mailSender.setPassword(sendUserPassword);
}
// MailUtil.getTemplate(velocityEngine,mail.getTemplateName(),this.mailTemplate);
Template template = MailUtil.getTemplateFromFile(mailTemplate);
template.setEncoding(_encoding);
StringWriter stringWriter = new StringWriter();
template.merge(velocityContext, stringWriter);
message.setText(stringWriter.toString());
// MimeMessage createMimeMessage = mailSender.createMimeMessage();
// createMimeMessage.addHeader("text/html", stringWriter.toString());
mailSender.send(message);
}
Aggregations