Search in sources :

Example 1 with Template

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();
}
Also used : org.osgl.$(org.osgl.$) H(org.osgl.http.H) ViewManager(act.view.ViewManager) Template(act.view.Template)

Example 2 with Template

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;
}
Also used : App(act.app.App) Template(act.view.Template) ITemplate(org.rythmengine.template.ITemplate)

Example 3 with 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;
}
Also used : ViewManager(act.view.ViewManager) ISObject(org.osgl.storage.ISObject) Template(act.view.Template)

Example 4 with Template

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);
}
Also used : MvcConfig(org.osgl.mvc.MvcConfig) S(org.osgl.util.S) H(org.osgl.http.H) ActionContext(act.app.ActionContext) Template(act.view.Template)

Aggregations

Template (act.view.Template)4 ViewManager (act.view.ViewManager)2 H (org.osgl.http.H)2 ActionContext (act.app.ActionContext)1 App (act.app.App)1 org.osgl.$ (org.osgl.$)1 MvcConfig (org.osgl.mvc.MvcConfig)1 ISObject (org.osgl.storage.ISObject)1 S (org.osgl.util.S)1 ITemplate (org.rythmengine.template.ITemplate)1