use of cn.bran.japid.template.RenderResult in project Japid by branaway.
the class JapidController2 method runWithCache.
/**
* run a piece of rendering code with cache check and refilling
*
* @param runner
* @param ttl
* @param objects
*
* @deprecated use CacheableRunner directly in actions
*/
protected static void runWithCache(ActionRunner runner, String ttl, Object... objects) {
if (ttl == null || ttl.trim().length() == 0)
throw new RuntimeException("Cache expiration time must be defined.");
ttl = ttl.trim();
if (Character.isDigit(ttl.charAt(ttl.length() - 1))) {
// assuming second
ttl += "s";
}
String base = StackTraceUtils.getCaller();
RenderResult rr = getFromCache(base, objects);
if (rr == null) {
rr = runner.run();
cache(rr, ttl, base, objects);
}
// System.out.println("render show took ms: " + rr.getRenderTime());
throw new JapidResult(rr);
}
use of cn.bran.japid.template.RenderResult in project japid42 by branaway.
the class CacheablePlayActionRunner method render.
@Override
protected RenderResult render() {
Map<String, String> threadData = JapidController.threadData.get();
threadData.put(GlobalSettingsWithJapid.ACTION_METHOD, controllerClass.getName() + "." + actionName);
JapidResult jr = runPlayAction();
threadData.remove(GlobalSettingsWithJapid.ACTION_METHOD);
RenderResult rr = jr.getRenderResult();
return rr;
}
use of cn.bran.japid.template.RenderResult in project japid42 by branaway.
the class JapidController method renderText.
/**
* render a text in a RenderResult so it can work with invoke tag in
* templates.
*
* @param s
*/
protected static JapidResult renderText(String s) {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "text/plain; charset=utf-8");
return render(new RenderResult(headers, new StringBuilder(s), -1L));
}
use of cn.bran.japid.template.RenderResult in project japid42 by branaway.
the class JapidController method render.
/**
* render an array of objects to a template defined by a Template class.
*
* @param <T>
* a sub-class type of JapidTemplateBase
* @param c
* a sub-class of JapidTemplateBase
* @param args
* arguments
*/
public static <T extends JapidTemplateBaseWithoutPlay> JapidResult render(Class<T> c, Object... args) {
try {
RenderResult rr = RenderInvokerUtils.invokeRender(c, args);
JapidResult japidResult = new JapidResult(rr);
postProcess(japidResult);
return japidResult;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of cn.bran.japid.template.RenderResult in project japid42 by branaway.
the class JapidMailer method send.
@SuppressWarnings("unchecked")
public static Future<Boolean> send(Object... args) {
try {
final HashMap<String, Object> infoMap = getInfoMap();
// Body character set
final String charset = (String) infoMap.get(CHARSET);
// Headers
final Map<String, String> headers = (Map<String, String>) infoMap.get(HEADERS);
// Subject
final String subject = (String) infoMap.get(SUBJECT);
// xxx how to determine the method name???
// String templateName = (String) infoMap.get(METHOD);
String templateNameBase = StackTraceUtils.getCaller();
if (!templateNameBase.startsWith("notifiers")) {
throw new RuntimeException("The emailers must be put in the \"notifiers\" package.");
}
// if (templateNameBase.startsWith(NOTIFIERS)) {
// templateNameBase = templateNameBase.substring(NOTIFIERS.length());
// }
// if (templateNameBase.startsWith(CONTROLLERS)) {
// templateNameBase = templateNameBase.substring(CONTROLLERS.length());
// }
// templateNameBase = templateNameBase.substring(0, templateNameBase.indexOf("("));
// templateNameBase = templateNameBase.replace(".", "/");
// final Map<String, Object> templateHtmlBinding = new HashMap<String, Object>();
// final Map<String, Object> templateTextBinding = new HashMap<String, Object>();
// for (Object o : args) {
// List<String> names = LocalVariablesNamesTracer.getAllLocalVariableNames(o);
// for (String name : names) {
// templateHtmlBinding.put(name, o);
// templateTextBinding.put(name, o);
// }
// }
String templateClassName = "japidviews._" + templateNameBase;
String bodyHtml = null;
Class tClass = JapidRenderer.getClass(templateClassName);
if (tClass == null) {
String templateFileName = templateClassName.replace('.', '/') + ".html";
throw new RuntimeException("Japid Emailer: could not find a Japid template with the name of: " + templateFileName);
} else if (JapidTemplateBase.class.isAssignableFrom(tClass)) {
JapidResult jr = JapidController.render(tClass, args);
RenderResult rr = jr.getRenderResult();
bodyHtml = rr.getContent().toString();
} else {
throw new RuntimeException("The found class is not a Japid template class: " + templateClassName);
}
// System.out.println("email body: " + bodyHtml);
// The rule is as follow: If we ask for text/plain, we don't care about the HTML
// If we ask for HTML and there is a text/plain we add it as an alternative.
// If contentType is not specified look at the template available:
// - .txt only -> text/plain
// else
// - -> text/html
// String contentType = (String) infoMap.get(CONTENT_TYPE);
// String bodyText = "";
// try {
// Template templateHtml = TemplateLoader.load(templateNameBase + ".html");
// bodyHtml = templateHtml.render(templateHtmlBinding);
// } catch (TemplateNotFoundException e) {
// if (contentType != null && !contentType.startsWith(TEXT_PLAIN)) {
// throw e;
// }
// }
////
// try {
// Template templateText = TemplateLoader.load(templateName + ".txt");
// bodyText = templateText.render(templateTextBinding);
// } catch (TemplateNotFoundException e) {
// if (bodyHtml == null && (contentType == null || contentType.startsWith(TEXT_PLAIN))) {
// throw e;
// }
// }
// Content type
// bran html for now
// if (contentType == null) {
// if (bodyHtml != null) {
// contentType = TEXT_HTML;
// } else {
// contentType = TEXT_PLAIN;
// }
// }
// Recipients
final List<Object> recipientList = (List<Object>) infoMap.get(RECIPIENTS);
// From
final Object from = infoMap.get(FROM);
final Object replyTo = infoMap.get(REPLY_TO);
Email email = null;
if (infoMap.get(ATTACHMENTS) == null) {
// if (StringUtils.isEmpty(bodyHtml)) {
// email = new SimpleEmail();
// email.setMsg(bodyText);
// } else {
HtmlEmail htmlEmail = new HtmlEmail();
htmlEmail.setHtmlMsg(bodyHtml);
// if (!StringUtils.isEmpty(bodyText)) {
// htmlEmail.setTextMsg(bodyText);
// }
email = htmlEmail;
// }
} else {
// if (StringUtils.isEmpty(bodyHtml)) {
// email = new MultiPartEmail();
// email.setMsg(bodyText);
// } else {
HtmlEmail htmlEmail = new HtmlEmail();
htmlEmail.setHtmlMsg(bodyHtml);
// if (!StringUtils.isEmpty(bodyText)) {
// htmlEmail.setTextMsg(bodyText);
// }
email = htmlEmail;
// }
MultiPartEmail multiPartEmail = (MultiPartEmail) email;
List<EmailAttachment> objectList = (List<EmailAttachment>) infoMap.get(ATTACHMENTS);
for (EmailAttachment object : objectList) {
multiPartEmail.attach(object);
}
}
if (from != null) {
try {
InternetAddress iAddress = new InternetAddress(from.toString());
email.setFrom(iAddress.getAddress(), iAddress.getPersonal());
} catch (Exception e) {
email.setFrom(from.toString());
}
}
if (replyTo != null) {
try {
InternetAddress iAddress = new InternetAddress(replyTo.toString());
email.addReplyTo(iAddress.getAddress(), iAddress.getPersonal());
} catch (Exception e) {
email.addReplyTo(replyTo.toString());
}
}
if (recipientList != null) {
for (Object recipient : recipientList) {
try {
InternetAddress iAddress = new InternetAddress(recipient.toString());
email.addTo(iAddress.getAddress(), iAddress.getPersonal());
} catch (Exception e) {
email.addTo(recipient.toString());
}
}
} else {
throw new PlayException("JapidMailer", "You must specify at least one recipient.", null);
}
List<Object> ccsList = (List<Object>) infoMap.get(CCS);
if (ccsList != null) {
for (Object cc : ccsList) {
email.addCc(cc.toString());
}
}
List<Object> bccsList = (List<Object>) infoMap.get(BCCS);
if (bccsList != null) {
for (Object bcc : bccsList) {
try {
InternetAddress iAddress = new InternetAddress(bcc.toString());
email.addBcc(iAddress.getAddress(), iAddress.getPersonal());
} catch (Exception e) {
email.addBcc(bcc.toString());
}
}
}
if (!StringUtils.isEmpty(charset)) {
email.setCharset(charset);
}
email.setSubject(subject);
email.updateContentType(TEXT_HTML);
if (headers != null) {
for (String key : headers.keySet()) {
email.addHeader(key, headers.get(key));
}
}
// reset the infomap
infos.remove();
return Mail.send(email);
} catch (EmailException ex) {
throw new MailException("Cannot send email", ex);
}
}
Aggregations