Skip to content

Commit

Permalink
Redesigned MasterPage API.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Aug 29, 2016
1 parent da9af70 commit ebe370b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 9 deletions.
Expand Up @@ -49,4 +49,6 @@ public static RapidoidThreadLocals get() {


public final ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); public final ByteArrayOutputStream baos1 = new ByteArrayOutputStream();


public final ByteArrayOutputStream pageRenderingBaos = new ByteArrayOutputStream();

} }
6 changes: 6 additions & 0 deletions rapidoid-commons/src/main/java/org/rapidoid/web/Screen.java
Expand Up @@ -23,6 +23,7 @@
import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;


import java.io.OutputStream;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;


Expand All @@ -39,6 +40,11 @@ public interface Screen {
*/ */
String render(); String render();


/**
* Renders the screen to the specified OutputStream.
*/
void render(OutputStream out);

/** /**
* Sets the "<b>title</b>" attribute in the MVC model of the response, used for GUI page rendering. * Sets the "<b>title</b>" attribute in the MVC model of the response, used for GUI page rendering.
*/ */
Expand Down
Expand Up @@ -7,6 +7,7 @@
import org.rapidoid.commons.Env; import org.rapidoid.commons.Env;
import org.rapidoid.u.U; import org.rapidoid.u.U;


import java.io.OutputStream;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;


Expand Down Expand Up @@ -53,6 +54,11 @@ public String render() {
throw U.rte("The Screen cannot render itself!"); throw U.rte("The Screen cannot render itself!");
} }


@Override
public void render(OutputStream out) {
throw U.rte("The Screen cannot render itself!");
}

@Override @Override
public String home() { public String home() {
return home; return home;
Expand Down
14 changes: 14 additions & 0 deletions rapidoid-gui/src/main/java/org/rapidoid/gui/HtmlPage.java
Expand Up @@ -14,8 +14,11 @@
import org.rapidoid.render.Template; import org.rapidoid.render.Template;
import org.rapidoid.render.Templates; import org.rapidoid.render.Templates;
import org.rapidoid.u.U; import org.rapidoid.u.U;
import org.rapidoid.util.StreamUtils;
import org.rapidoid.web.ScreenBean; import org.rapidoid.web.ScreenBean;


import java.io.IOException;
import java.io.OutputStream;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
Expand Down Expand Up @@ -72,6 +75,17 @@ public String render() {
return html; return html;
} }


@Override
public void render(OutputStream out) {
String html = render();

try {
StreamUtils.writeUTF8(out, html);
} catch (IOException e) {
throw U.rte(e);
}
}

@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private Map<String, Object> pageModel() { private Map<String, Object> pageModel() {
IReqInfo req = ReqInfo.get(); IReqInfo req = ReqInfo.get();
Expand Down
Expand Up @@ -23,12 +23,13 @@
import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.http.Req; import org.rapidoid.http.Req;
import org.rapidoid.http.Resp;
import java.io.OutputStream;


@Authors("Nikolche Mihajlovski") @Authors("Nikolche Mihajlovski")
@Since("5.2.0") @Since("5.2.0")
public interface MasterPage { public interface MasterPage {


Object renderPage(Req req, Resp resp, String content) throws Exception; void renderPage(Req req, String content, OutputStream out) throws Exception;


} }
Expand Up @@ -8,8 +8,10 @@
import org.rapidoid.http.Resp; import org.rapidoid.http.Resp;
import org.rapidoid.http.customize.MasterPage; import org.rapidoid.http.customize.MasterPage;
import org.rapidoid.u.U; import org.rapidoid.u.U;
import org.rapidoid.util.StreamUtils;
import org.rapidoid.web.Screen; import org.rapidoid.web.Screen;


import java.io.OutputStream;
import java.util.regex.Pattern; import java.util.regex.Pattern;


/* /*
Expand Down Expand Up @@ -41,15 +43,20 @@ public class DefaultMasterPage extends RapidoidThing implements MasterPage {
private static final Pattern FULL_PAGE_PATTERN = Pattern.compile(FULL_PAGE_REGEX); private static final Pattern FULL_PAGE_PATTERN = Pattern.compile(FULL_PAGE_REGEX);


@Override @Override
public Object renderPage(Req req, Resp resp, String content) throws Exception { public void renderPage(Req req, String content, OutputStream out) throws Exception {
U.notNull(content, "page content"); U.notNull(content, "page content");


if (isFullPage(req, content)) return GUI.hardcoded(content); Resp resp = req.response();

if (isFullPage(req, content)) {
StreamUtils.writeUTF8(out, content);
return;
}


Screen screen = resp.screen(); Screen screen = resp.screen();
screen.content(GUI.hardcoded(content)); screen.content(GUI.hardcoded(content));


return screen.render(); screen.render(out);
} }


private boolean isFullPage(Req req, String content) { private boolean isFullPage(Req req, String content) {
Expand Down
Expand Up @@ -55,7 +55,7 @@ public static byte[] render(ReqImpl req, Resp resp) {
content = new String(HttpUtils.responseToBytes(req, cnt, MediaType.HTML_UTF_8, null)); content = new String(HttpUtils.responseToBytes(req, cnt, MediaType.HTML_UTF_8, null));
} }


return renderPage(req, resp, content); return renderPage(req, content);
} }


private static boolean shouldRenderView(Resp resp) { private static boolean shouldRenderView(Resp resp) {
Expand Down Expand Up @@ -106,18 +106,22 @@ public static String renderView(ReqImpl req, Resp resp, Object result) {
return new String(out.toByteArray()); return new String(out.toByteArray());
} }


public static byte[] renderPage(ReqImpl req, Resp resp, String content) { public static byte[] renderPage(ReqImpl req, String content) {


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


ByteArrayOutputStream out = Msc.locals().pageRenderingBaos;
out.reset();

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


} catch (Exception e) { } catch (Exception e) {
throw U.rte("Error while rendering page!", e); throw U.rte("Error while rendering page!", e);
} }

return out.toByteArray();
} }


private static Object wrapGuiContent(Object content) { private static Object wrapGuiContent(Object content) {
Expand Down

0 comments on commit ebe370b

Please sign in to comment.