use of act.view.Template in project actframework by actframework.
the class ActErrorPageRender method getTemplate.
private Template getTemplate(int statusCode, ActionContext context) {
H.Format format = context.accept();
String key = statusCode + "" + format;
$.Var<Template> templateBag = templateCache.get(key);
if (null == templateBag) {
ViewManager vm = Act.viewManager();
if (null == vm) {
// unit testing
return null;
}
context.templatePath(templatePath(statusCode, context));
Template t = vm.load(context);
if (null == t) {
// try default one
if (Act.isDev()) {
context.templatePath("/error/dev/errorPage." + context.accept().name());
} else {
context.templatePath("/error/errorPage." + context.accept().name());
}
t = vm.load(context);
}
templateBag = $.var(t);
templateCache.put(key, templateBag);
}
return templateBag.get();
}
use of act.view.Template in project actframework by actframework.
the class RythmView method loadTemplate.
@Override
protected Template loadTemplate(String resourcePath) {
App app = Act.app();
if (isDev) {
return loadTemplateFromResource(resourcePath, app);
}
if (missings.containsKey(resourcePath)) {
return null;
}
Template template = templates.get(resourcePath);
if (null == template) {
Template newTemplate = loadTemplateFromResource(resourcePath, app);
if (null != newTemplate) {
template = templates.putIfAbsent(resourcePath, newTemplate);
if (null == template) {
template = newTemplate;
}
} else {
missings.put(resourcePath, resourcePath);
}
}
return template;
}
use of act.view.Template in project actframework by actframework.
the class MailerContext method createMessage.
private MimeMessage createMessage() throws Exception {
MailerConfig config = mailerConfig();
if (null == config) {
throw E.unexpected("Cannot find mailer config for %s", confId);
}
Session session = mailerConfig().session();
if (Act.isDev()) {
session.setDebug(true);
}
MimeMessage msg = new MimeMessage(session);
msg.setFrom(from());
msg.setSubject(subject());
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, list2Array(to()));
msg.setRecipients(Message.RecipientType.CC, list2Array(cc()));
msg.setRecipients(Message.RecipientType.BCC, list2Array(bcc()));
String content = this.content;
if (null == content) {
ViewManager vm = Act.viewManager();
Template t = vm.load(this);
E.illegalStateIf(null == t, "Mail template not defined");
content = t.render(this);
}
if (attachments.isEmpty()) {
msg.setText(content, config().encoding(), accept().name());
} else {
Multipart mp = new MimeMultipart();
MimeBodyPart bp = new MimeBodyPart();
mp.addBodyPart(bp);
bp.setText(content, config().encoding(), accept().name());
for (ISObject sobj : attachments) {
String fileName = sobj.getAttribute(ISObject.ATTR_FILE_NAME);
if (S.blank(fileName)) {
fileName = sobj.getKey();
}
String contentType = sobj.getAttribute(ISObject.ATTR_CONTENT_TYPE);
if (S.blank(contentType)) {
contentType = "application/octet-stream";
}
MimeBodyPart attachment = new MimeBodyPart();
attachment.attachFile(sobj.asFile(), contentType, null);
attachment.setFileName(fileName);
mp.addBodyPart(attachment);
}
msg.setContent(mp);
}
msg.saveChanges();
return msg;
}
use of act.view.Template in project actframework by actframework.
the class ActErrorPageRender method renderTemplate.
@Override
protected String renderTemplate(ErrorResult error, H.Format format) {
ActionContext context = ActionContext.current();
if (null == context) {
return null;
}
Integer errorCode = error.errorCode();
int statusCode = error.statusCode();
fixRequestAcceptFormat(context);
Template t = getTemplate(statusCode, context);
if (null == t) {
String errorMsg = error.getMessage();
if (null == errorMsg) {
errorMsg = MvcConfig.errorMessage(error.status());
}
if (i18n()) {
String translated = context.i18n(true, errorMsg);
if (translated == errorMsg) {
translated = context.i18n(true, MvcConfig.class, errorMsg);
}
errorMsg = translated;
}
H.Format accept = context.accept();
if (H.Format.JSON == accept) {
return jsonContent(error, errorCode, errorMsg);
} else if (HTML == accept) {
String header = S.concat("HTTP/1.1 ", Integer.toString(statusCode), " ", errorMsg);
return S.concat("<!DOCTYPE html><html><head><meta charset='utf-8'><title>", header, "</title></head><body><h1>", header, "</h1></body></html>");
} else if (H.Format.XML == accept) {
S.Buffer sb = context.strBuf();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>");
if (null != errorCode) {
sb.append("<code>").append(errorCode).append("</code");
}
sb.append("<message>").append(errorMsg).append("</message></error>");
return sb.toString();
} else if (CSV == accept) {
if (null == errorCode) {
return S.concat("message\n", errorMsg);
} else {
return S.concat("code,message\n", Integer.toString(errorCode), ",", errorMsg);
}
} else if (H.Format.TXT == accept) {
return null == errorCode ? errorMsg : S.concat(Integer.toString(errorCode), " ", errorMsg);
} else {
// Unknown accept format
return "";
}
}
if (HTML == context.accept()) {
String header = S.concat("HTTP/1.1 ", Integer.toString(statusCode), " ", MvcConfig.errorMessage(error.status()));
context.renderArg("header", header);
}
context.renderArg(ARG_ERROR, error);
return t.render(context);
}
Aggregations