Skip to content

Commit

Permalink
Simplified the ViewRenderer API and implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Aug 23, 2016
1 parent ffcf7ce commit 5d6c5d1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 41 deletions.
Expand Up @@ -30,6 +30,6 @@
@Since("5.0.11")
public interface ViewRenderer {

boolean render(Req req, String viewName, Object[] model, OutputStream out) throws Exception;
void render(Req req, String viewName, Object[] model, OutputStream out) throws Exception;

}
Expand Up @@ -6,7 +6,8 @@
import org.rapidoid.http.Req;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.customize.ViewRenderer;
import org.rapidoid.io.Res;
import org.rapidoid.render.FileSystemTemplateStore;
import org.rapidoid.render.TemplateStore;
import org.rapidoid.render.Templates;

import java.io.OutputStream;
Expand Down Expand Up @@ -36,16 +37,10 @@
public class DefaultViewRenderer extends RapidoidThing implements ViewRenderer {

@Override
public boolean render(Req req, String viewName, Object[] model, OutputStream out) throws Exception {
public void render(Req req, String viewName, Object[] model, OutputStream out) throws Exception {
String[] path = Customization.of(req).templatesPath();
Res template = Res.from(viewName + ".html", path);

if (!template.exists()) {
return false;
}

Templates.fromRes(template).renderTo(out, model);
return true;
TemplateStore templates = new FileSystemTemplateStore(path);
Templates.load(viewName + ".html", templates).renderTo(out, model);
}

}
Expand Up @@ -312,6 +312,10 @@ public synchronized String view() {
return view != null ? view : HttpUtils.resName(req);
}

public boolean hasCustomView() {
return view != null;
}

@Override
public synchronized Resp view(String view) {
this.view = view;
Expand Down
Expand Up @@ -24,8 +24,9 @@
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.cls.Cls;
import org.rapidoid.http.MediaType;
import org.rapidoid.cls.TypeKind;
import org.rapidoid.http.HttpUtils;
import org.rapidoid.http.MediaType;
import org.rapidoid.http.Resp;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.customize.PageRenderer;
Expand All @@ -50,37 +51,62 @@ public static byte[] render(ReqImpl req, Resp resp) {
resp.result(result);
}

ViewRenderer viewRenderer = Customization.of(req).viewRenderer();
U.must(viewRenderer != null, "A view renderer wasn't configured!");
String content;

PageRenderer pageRenderer = Customization.of(req).pageRenderer();
U.must(pageRenderer != null, "A page renderer wasn't configured!");
if (shouldRenderView(resp)) {
content = renderView(req, resp, result);
} else {
Object cnt = U.or(result, "");
content = new String(HttpUtils.responseToBytes(req, cnt, MediaType.HTML_UTF_8, null));
}

return renderPage(req, resp, content);
}

private static boolean shouldRenderView(Resp resp) {
if (!resp.mvc()) return false;

if (((RespImpl) resp).hasCustomView() || resp.result() == null) return true;

TypeKind kind = Cls.kindOf(resp.result());

return !(resp.result() instanceof String)
&& !(resp.result() instanceof byte[])
&& !kind.isPrimitive()
&& !kind.isArray()
&& !kind.isNumber();
}

public static String renderView(ReqImpl req, Resp resp, Object result) {

boolean rendered;
String viewName = resp.view();
ByteArrayOutputStream out = new ByteArrayOutputStream();

ViewRenderer viewRenderer = Customization.of(req).viewRenderer();
U.must(viewRenderer != null, "A view renderer wasn't configured!");

MVCModel basicModel = new MVCModel(req, resp, resp.model(), resp.screen(), result);

Object[] renderModel = result != null
? U.array(basicModel, resp.model(), result)
: U.array(basicModel, resp.model());

try {
rendered = viewRenderer.render(req, viewName, renderModel, out);
viewRenderer.render(req, viewName, renderModel, out);
} catch (Throwable e) {
throw U.rte("Error while rendering view: " + viewName, e);
}

String renderResult = rendered ? new String(out.toByteArray()) : null;
return new String(out.toByteArray());
}

if (renderResult == null) {
Object cnt = U.or(result, "");
renderResult = new String(HttpUtils.responseToBytes(req, cnt, MediaType.HTML_UTF_8, null));
}
public static byte[] renderPage(ReqImpl req, Resp resp, String content) {

PageRenderer pageRenderer = Customization.of(req).pageRenderer();
U.must(pageRenderer != null, "A page renderer wasn't configured!");

try {
Object response = U.or(pageRenderer.renderPage(req, resp, renderResult), "");
Object response = U.or(pageRenderer.renderPage(req, resp, content), "");
return HttpUtils.responseToBytes(req, response, MediaType.HTML_UTF_8, null);

} catch (Exception e) {
Expand Down
Expand Up @@ -68,19 +68,14 @@ public Reader getTemplate(String name) throws Exception {
private volatile Mustache.Compiler compiler = Mustache.compiler().withLoader(defaultPartialLoader);

@Override
public boolean render(Req req, String viewName, Object[] model, OutputStream out) throws Exception {
public void render(Req req, String viewName, Object[] model, OutputStream out) throws Exception {
String[] path = Customization.of(req).templatesPath();
Res template = Res.from(viewName + ".html", path);

if (!template.exists()) {
return false;
}

Res template = Res.from(viewName + ".html", path).mustExist();
Template mustache = compiler.compile(template.getContent());

PrintWriter writer = new PrintWriter(out);
mustache.execute(model[model.length - 1], writer);
writer.flush();
return true;
}

/**
Expand Down
Expand Up @@ -41,18 +41,12 @@ public class MustacheViewRenderer extends RapidoidThing implements ViewRenderer
private final MustacheFactory mf = new DefaultMustacheFactory();

@Override
public boolean render(Req req, String viewName, Object[] model, OutputStream out) throws Exception {
public void render(Req req, String viewName, Object[] model, OutputStream out) throws Exception {
String[] path = Customization.of(req).templatesPath();
Res template = Res.from(viewName + ".html", path);

if (!template.exists()) {
return false;
}
Res template = Res.from(viewName + ".html", path).mustExist();

Mustache mustache = mf.compile(template.getCachedFileName());
mustache.execute(new PrintWriter(out), model).flush();

return true;
}

}
Expand Up @@ -67,7 +67,7 @@ public void testRender() {
return respo;
});

On.get("/defaults").mvc((Req req) -> req);
On.get("/defaults").mvc((Req req) -> new byte[0]);

onlyGet("/view1");
getAndPost("/view2");
Expand Down

0 comments on commit 5d6c5d1

Please sign in to comment.