Skip to content

Commit

Permalink
Refactored the POJO-based request handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Dec 15, 2015
1 parent 7cd219b commit 58a849b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 53 deletions.
Expand Up @@ -65,7 +65,7 @@ public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req) {

@SuppressWarnings("unchecked")
protected Object postprocessResult(Req req, Object result) throws Exception {
if (result instanceof Req || result instanceof Resp) {
if (result instanceof Req || result instanceof Resp || result instanceof HttpStatus) {
return result;

} else if (result == null) {
Expand Down Expand Up @@ -198,6 +198,10 @@ public void complete(Channel ctx, boolean isKeepAlive, Req req, Object result) {
Throwable error = (Throwable) result;
http.error(ctx, isKeepAlive, error);

} else if (result instanceof HttpStatus) {
HttpStatus status = (HttpStatus) result;
throw U.notExpected();

} else {
http.writeResult(ctx, isKeepAlive, result, contentType);
}
Expand Down
Expand Up @@ -160,7 +160,7 @@ private static boolean calcFinalResult(Req x, Object result) {
}
}

AppHandler.view(x, result, false, config);
// AppHandler.view(x, result, false, config); FIXME refactor scripting

return true;
}
Expand Down
61 changes: 29 additions & 32 deletions rapidoid-web/src/main/java/org/rapidoid/web/AppHandler.java
Expand Up @@ -41,6 +41,7 @@
import org.rapidoid.dispatch.PojoRequest;
import org.rapidoid.dispatch.impl.DispatchReqKind;
import org.rapidoid.http.Req;
import org.rapidoid.http.Resp;
import org.rapidoid.http.fast.FastHttp;
import org.rapidoid.http.fast.HttpStatus;
import org.rapidoid.http.fast.HttpUtils;
Expand All @@ -60,10 +61,13 @@ public class AppHandler extends FastParamsAwareHttpHandler {

private static final Pattern DIRECTIVE = Pattern.compile("\\s*<!--\\s*#\\s*(\\{.+\\})\\s*-->\\s*");

private final WebApp app;

private static volatile ITemplate PAGE_TEMPLATE;

public AppHandler(FastHttp http) {
public AppHandler(FastHttp http, WebApp app) {
super(http, null, null);
this.app = app;
}

@Override
Expand All @@ -80,9 +84,6 @@ protected Object doHandle(Channel channel, boolean isKeepAlive, Req req) throws
}
}

WebApp app = Ctxs.ctx().app();
PojoDispatcher dispatcher = app.getDispatcher();

// Prepare GUI state

// x.loadState();
Expand All @@ -96,7 +97,7 @@ protected Object doHandle(Channel channel, boolean isKeepAlive, Req req) throws
if (hasEvent) {
bindInputs(req);

DispatchResult dispatchResult = doDispatch(dispatcher, new WebReq(req));
DispatchResult dispatchResult = doDispatch(app.getDispatcher(), new WebReq(req));
if (dispatchResult != null) {
U.must(dispatchResult.getKind() == DispatchReqKind.PAGE);
result = dispatchResult.getResult();
Expand All @@ -114,15 +115,14 @@ protected Object doHandle(Channel channel, boolean isKeepAlive, Req req) throws
// dispatch REST services or PAGES (as POJO methods)

if (result == null) {
DispatchResult dres = doDispatch(dispatcher, new WebReq(req));
DispatchResult dres = doDispatch(app.getDispatcher(), new WebReq(req));

if (dres != null) {
result = dres.getResult();
config = dres.getConfig();

if (dres.getKind() == DispatchReqKind.SERVICE) {
req.response().contentType(MediaType.JSON_UTF_8);
return result;
return req.response().contentType(MediaType.JSON_UTF_8).content(result);
}
}
}
Expand Down Expand Up @@ -155,16 +155,17 @@ private void bindInputs(Req x) {
}
}

public static Object view(Req x, Object result, boolean hasEvent, Map<String, Object> config) {
public Resp view(Req x, Object result, boolean hasEvent, Map<String, Object> config) {
// serve dynamic pages from file templates

x.response().contentType(MediaType.HTML_UTF_8);

if (Cls.bool(config.get("raw"))) {
x.response().contentType(MediaType.HTML_UTF_8);
return result;
return x.response().content(result);
}

if (serveDynamicPage(x, result, hasEvent, config)) {
return x;
return x.response();
}

if (result != null) {
Expand All @@ -174,7 +175,7 @@ public static Object view(Req x, Object result, boolean hasEvent, Map<String, Ob
return null;
}

private static DispatchResult doDispatch(PojoDispatcher dispatcher, PojoRequest req) {
private DispatchResult doDispatch(PojoDispatcher dispatcher, PojoRequest req) {
try {
return dispatcher.dispatch(req);
} catch (PojoHandlerNotFoundException e) {
Expand All @@ -185,7 +186,8 @@ private static DispatchResult doDispatch(PojoDispatcher dispatcher, PojoRequest
}
}

public static boolean serveDynamicPage(Req x, Object result, boolean hasEvent, Map<String, Object> config) {
public boolean serveDynamicPage(Req x, Object result, boolean hasEvent, Map<String, Object> config) {

String filename = HttpUtils.resName(x) + ".html";
String firstFile = Conf.rootPath() + "/pages/" + filename;
String defaultFile = Conf.rootPathDefault() + "/pages/" + filename;
Expand All @@ -202,8 +204,6 @@ public static boolean serveDynamicPage(Req x, Object result, boolean hasEvent, M
return false;
}

WebApp app = Ctxs.ctx().app();

String title = U.or(app.getTitle(), x.host());
model.put("title", title);
model.put("embedded", hasEvent || x.param("_embedded", null) != null);
Expand All @@ -227,17 +227,15 @@ public static boolean serveDynamicPage(Req x, Object result, boolean hasEvent, M
return true;
}

private static void renderPage(Req x, Map<String, Object> model) {
// TODO Auto-generated method stub

private void renderPage(Req x, Map<String, Object> model) {
pageTemplate().render(x.out(), x, model);
}

private static String renderPageToHTML(Req x, Map<String, Object> model) {
// TODO Auto-generated method stub
return null;
private String renderPageToHTML(Req x, Map<String, Object> model) {
return pageTemplate().render(x, model);
}

private static void serveEventResponse(Req x, String html) {
private void serveEventResponse(Req x, String html) {
x.response().code(200);

if (x.response().redirect() != null) {
Expand All @@ -249,7 +247,7 @@ private static void serveEventResponse(Req x, String html) {
}
}

private static Map<String, Object> generatePageContent(Req x, Object result, Res resource) {
private Map<String, Object> generatePageContent(Req x, Object result, Res resource) {
String template = U.safe(resource.getContent());

Map<String, Object> model = model(x);
Expand All @@ -273,7 +271,7 @@ private static Map<String, Object> generatePageContent(Req x, Object result, Res
return model;
}

public static DispatchResult on(Req x, PojoDispatcher dispatcher, String event, Object[] args) {
public DispatchResult on(Req x, PojoDispatcher dispatcher, String event, Object[] args) {

// Map<String, Object> state = U.cast(x.locals());
Map<String, Object> state = null;
Expand All @@ -282,33 +280,33 @@ public static DispatchResult on(Req x, PojoDispatcher dispatcher, String event,
return doDispatch(dispatcher, req);
}

public static final void reload(Req x) {
public final void reload(Req x) {
Map<String, String> sel = U.map("body", PAGE_RELOAD);
x.response().json(U.map("_sel_", sel));
}

public static void render(Req req, ITemplate template, Object model) {
public void render(Req req, ITemplate template, Object model) {
template.render(req.out(), model, model(req));
}

public static void renderPage(Req req, Object model) {
public void renderPage(Req req, Object model) {
req.response().contentType(MediaType.HTML_UTF_8);
pageTemplate().render(req.out(), model, model(req));
req.done();
}

public static String renderPageToHTML(Req x, Object model) {
public String renderPageToHTML(Req x, Object model) {
return pageTemplate().render(model, model(x));
}

private static ITemplate pageTemplate() {
private ITemplate pageTemplate() {
if (PAGE_TEMPLATE == null) {
PAGE_TEMPLATE = Templates.fromFile("page.html");
}
return PAGE_TEMPLATE;
}

public static Map<String, Object> model(Req x) {
public Map<String, Object> model(Req x) {

Map<String, Object> model = U.map("req", x, "data", x.data(), "files", x.files(), "cookies", x.cookies(),
"headers", x.headers());
Expand All @@ -319,7 +317,6 @@ public static Map<String, Object> model(Req x) {
model.put("host", x.host());
model.put("dev", HttpUtils.isDevMode(x));

WebApp app = Ctxs.ctx().app();
model.put("app", app);
model.put("menu", app != null ? app.getMenu() : null);

Expand Down
42 changes: 42 additions & 0 deletions rapidoid-web/src/main/java/org/rapidoid/web/POJO.java
@@ -0,0 +1,42 @@
package org.rapidoid.web;

/*
* #%L
* rapidoid-web
* %%
* Copyright (C) 2014 - 2015 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.http.fast.FastHttp;
import org.rapidoid.http.fast.On;
import org.rapidoid.http.fast.handler.FastHttpHandler;

@Authors("Nikolche Mihajlovski")
@Since("5.0.8")
public class POJO {

public static FastHttpHandler handler() {
return handler(new RootWebApp());
}

public static FastHttpHandler handler(WebApp app) {
FastHttp http = On.getDefaultSetup().http();
return new AppHandler(http, app);
}

}
4 changes: 3 additions & 1 deletion rapidoid-web/src/main/java/org/rapidoid/web/Rapidoid.java
Expand Up @@ -30,6 +30,7 @@
import org.rapidoid.config.ConfigHelp;
import org.rapidoid.ctx.Ctx;
import org.rapidoid.ctx.Ctxs;
import org.rapidoid.http.fast.On;
import org.rapidoid.job.Jobs;
import org.rapidoid.log.Log;
import org.rapidoid.plugins.Plugins;
Expand Down Expand Up @@ -84,6 +85,7 @@ private static synchronized void initAndStart(WebApp app, String[] args, Object.
ctx.setApp(app);

// Apps.serve(app, args, config);
On.req(new AppHandler(On.getDefaultSetup().http(), app));

Jobs.execute(new Runnable() {
@Override
Expand Down Expand Up @@ -130,7 +132,7 @@ public static WebApp bootstrap(WebApp app, String[] args, Object... config) {
Log.args(configArgsArr);

AOP.reset();

AOP.intercept(new AuthInterceptor(), Admin.class, Manager.class, Moderator.class, LoggedIn.class,
DevMode.class, Role.class, HasRole.class);

Expand Down
14 changes: 5 additions & 9 deletions rapidoid-web/src/main/java/org/rapidoid/web/RootWebApp.java
Expand Up @@ -7,9 +7,6 @@
import org.rapidoid.config.Conf;
import org.rapidoid.config.Config;
import org.rapidoid.ctx.Classes;
import org.rapidoid.http.fast.FastHttp;
import org.rapidoid.http.fast.On;
import org.rapidoid.http.fast.handler.FastHttpHandler;
import org.rapidoid.scan.Scan;
import org.rapidoid.u.U;

Expand Down Expand Up @@ -37,15 +34,14 @@
@Since("4.1.0")
public class RootWebApp extends WebApp {

@SuppressWarnings("unchecked")
public RootWebApp() {
super("root", Collections.EMPTY_SET, Collections.EMPTY_SET, U.set("/"), Conf.dev() ? AppMode.DEVELOPMENT
: AppMode.PRODUCTION, null, Classes.from(Scan.getClasses()), new Config(Conf.root().toMap()), handler());
this(Classes.from(Scan.getClasses()));
}

private static FastHttpHandler handler() {
FastHttp http = On.getDefaultSetup().http();
return new AppHandler(http);
@SuppressWarnings("unchecked")
public RootWebApp(Classes classes) {
super("root", Collections.EMPTY_SET, Collections.EMPTY_SET, U.set("/"), Conf.dev() ? AppMode.DEVELOPMENT
: AppMode.PRODUCTION, null, classes, new Config(Conf.root().toMap()));
}

}
11 changes: 2 additions & 9 deletions rapidoid-web/src/main/java/org/rapidoid/web/WebApp.java
Expand Up @@ -50,12 +50,10 @@ public class WebApp {

private final Config config;

private final FastHttpHandler handler;

private volatile Object menu;

public WebApp(String id, Set<String> owners, Set<String> hostnames, Set<String> uriContexts, AppMode mode,
PojoDispatcher dispatcher, Classes classes, Config config, FastHttpHandler handler) {
PojoDispatcher dispatcher, Classes classes, Config config) {
this.id = id;
this.dispatcher = U.or(dispatcher, new WebPojoDispatcher(classes));
this.owners = U.safe(owners);
Expand All @@ -64,11 +62,10 @@ public WebApp(String id, Set<String> owners, Set<String> hostnames, Set<String>
this.mode = U.or(mode, AppMode.DEVELOPMENT);
this.classes = U.or(classes, new Classes());
this.config = U.or(config, new Config());
this.handler = handler;
}

public WebApp(String id, String uriPath, Classes classes, FastHttpHandler handler) {
this(id, null, null, uriPath != null ? U.set(uriPath) : null, AppMode.DEVELOPMENT, null, classes, null, handler);
this(id, null, null, uriPath != null ? U.set(uriPath) : null, AppMode.DEVELOPMENT, null, classes, null);
}

public WebApp(String id, FastHttpHandler handler) {
Expand Down Expand Up @@ -131,8 +128,4 @@ public Object getMenu() {
return menu;
}

public FastHttpHandler getHandler() {
return handler;
}

}

0 comments on commit 58a849b

Please sign in to comment.