Search in sources :

Example 31 with Template

use of org.apache.velocity.Template in project stanbol by apache.

the class BenchmarkServlet method getTemplate.

/**
     * @return
     */
private Template getTemplate(String templatePath) {
    final Template t;
    ClassLoader tcl = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(BenchmarkServlet.class.getClassLoader());
    try {
        t = velocity.getTemplate(templatePath);
    } finally {
        Thread.currentThread().setContextClassLoader(tcl);
    }
    return t;
}
Also used : Template(org.apache.velocity.Template)

Example 32 with Template

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);
}
Also used : SimpleMailMessage(org.springframework.mail.SimpleMailMessage) StringWriter(java.io.StringWriter) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException) IOException(java.io.IOException) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException) Template(org.apache.velocity.Template)

Example 33 with Template

use of org.apache.velocity.Template in project Gargoyle by callakrsos.

the class MailUtil method getTemplateFromFile.

/**
	 * 파일로부터 템플릿 정보를 얻어온다.
	 *
	 * @Date 2015. 9. 13.
	 * @param templateFileName
	 * @return
	 * @throws Exception
	 * @User KYJ
	 */
public static Template getTemplateFromFile(final String templateFileName) throws Exception {
    String readFileToString = "";
    if (templateFileName.startsWith("classpath:")) {
        String res = templateFileName.replace("classpath:", "");
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream(res);
        readFileToString = ValueUtil.toString(resourceAsStream);
    } else
        readFileToString = FileUtils.readFileToString(new File(templateFileName));
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(readFileToString);
    SimpleNode node = runtimeServices.parse(reader, templateFileName);
    Template template = new Template();
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();
    return template;
}
Also used : RuntimeServices(org.apache.velocity.runtime.RuntimeServices) InputStream(java.io.InputStream) StringReader(java.io.StringReader) File(java.io.File) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode) Template(org.apache.velocity.Template)

Aggregations

Template (org.apache.velocity.Template)33 VelocityContext (org.apache.velocity.VelocityContext)20 StringWriter (java.io.StringWriter)11 IOException (java.io.IOException)9 VelocityEngine (org.apache.velocity.app.VelocityEngine)9 File (java.io.File)6 Writer (java.io.Writer)6 Properties (java.util.Properties)5 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)5 FileWriter (java.io.FileWriter)4 FileOutputStream (java.io.FileOutputStream)3 PrintWriter (java.io.PrintWriter)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringReader (java.io.StringReader)2 Map (java.util.Map)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)2 VelocityException (org.apache.velocity.exception.VelocityException)2