Package yalp.mvc.results

Source Code of yalp.mvc.results.RenderTemplate

package yalp.mvc.results;

import java.util.Map;

import yalp.exceptions.UnexpectedException;
import yalp.libs.MimeTypes;
import yalp.mvc.Http.Request;
import yalp.mvc.Http.Response;
import yalp.templates.Template;

/**
* 200 OK with a template rendering
*/
public class RenderTemplate extends Result {

    private String name;
    private String content;

    public RenderTemplate(Template template, Map<String, Object> args) {
        this.name = template.name;
        if (args.containsKey("out")) {
            throw new RuntimeException("Assertion failed! args shouldn't contain out");
        }
        this.content = template.render(args);
    }

    public void apply(Request request, Response response) {
        try {
            final String contentType = MimeTypes.getContentType(name, "text/plain");
            response.out.write(content.getBytes(getEncoding()));
            setContentTypeIfNotSet(response, contentType);
        } catch (Exception e) {
            throw new UnexpectedException(e);
        }
    }

    public String getContent() {
        return content;
    }

}
TOP

Related Classes of yalp.mvc.results.RenderTemplate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.