Skip to content

Commit

Permalink
Refactored the HTTP handler components.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Dec 12, 2015
1 parent 0ec7944 commit 0816943
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 33 deletions.
Expand Up @@ -26,8 +26,8 @@
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.commons.MediaType; import org.rapidoid.commons.MediaType;
import org.rapidoid.http.Req; import org.rapidoid.http.Req;
import org.rapidoid.http.fast.handler.DelegatingFastParamsAwareHttpHandler;
import org.rapidoid.http.fast.handler.FastCallableHttpHandler; import org.rapidoid.http.fast.handler.FastCallableHttpHandler;
import org.rapidoid.http.fast.handler.FastParamsAwareHttpHandler;
import org.rapidoid.http.fast.handler.FastResourceHttpHandler; import org.rapidoid.http.fast.handler.FastResourceHttpHandler;
import org.rapidoid.http.fast.handler.FastStaticHttpHandler; import org.rapidoid.http.fast.handler.FastStaticHttpHandler;
import org.rapidoid.http.fast.handler.HttpHandlers; import org.rapidoid.http.fast.handler.HttpHandlers;
Expand Down Expand Up @@ -86,7 +86,7 @@ private void register(MediaType contentType, Callable<?> handler) {


private void register(MediaType contentType, ReqHandler handler) { private void register(MediaType contentType, ReqHandler handler) {
for (FastHttp http : httpImpls) { for (FastHttp http : httpImpls) {
http.on(verb, path, new FastParamsAwareHttpHandler(http, contentType, wrappers, handler)); http.on(verb, path, new DelegatingFastParamsAwareHttpHandler(http, contentType, wrappers, handler));
} }
} }


Expand Down
Expand Up @@ -26,7 +26,8 @@
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.commons.MediaType; import org.rapidoid.commons.MediaType;
import org.rapidoid.http.Req; import org.rapidoid.http.Req;
import org.rapidoid.http.fast.handler.FastParamsAwarePageHandler; import org.rapidoid.http.fast.handler.DelegatingFastParamsAwareHttpHandler;
import org.rapidoid.http.fast.handler.FastParamsAwareHttpHandler;
import org.rapidoid.http.fast.handler.FastResourceHttpHandler; import org.rapidoid.http.fast.handler.FastResourceHttpHandler;
import org.rapidoid.http.fast.handler.FastStaticHttpHandler; import org.rapidoid.http.fast.handler.FastStaticHttpHandler;
import org.rapidoid.http.fast.handler.HttpHandlers; import org.rapidoid.http.fast.handler.HttpHandlers;
Expand Down Expand Up @@ -78,8 +79,8 @@ public Object handle(Req req) throws Exception {


private void register(PageOptions options, ReqHandler handler) { private void register(PageOptions options, ReqHandler handler) {
for (FastHttp http : httpImpls) { for (FastHttp http : httpImpls) {
FastParamsAwarePageHandler hnd = new FastParamsAwarePageHandler(http, options.contentType, wrappers, FastParamsAwareHttpHandler hnd = new DelegatingFastParamsAwareHttpHandler(http, options.contentType,
handler); wrappers, handler);
http.on("GET", path, hnd); http.on("GET", path, hnd);
http.on("POST", path, hnd); http.on("POST", path, hnd);
} }
Expand Down
Expand Up @@ -4,7 +4,7 @@
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.commons.MediaType; import org.rapidoid.commons.MediaType;
import org.rapidoid.config.Conf; import org.rapidoid.config.Conf;
import org.rapidoid.http.fast.handler.FastParamsAwareHttpHandler; import org.rapidoid.http.fast.handler.DelegatingFastParamsAwareHttpHandler;
import org.rapidoid.http.fast.listener.FastHttpListener; import org.rapidoid.http.fast.listener.FastHttpListener;
import org.rapidoid.http.fast.listener.IgnorantHttpListener; import org.rapidoid.http.fast.listener.IgnorantHttpListener;
import org.rapidoid.net.Serve; import org.rapidoid.net.Serve;
Expand Down Expand Up @@ -105,7 +105,8 @@ public OnPage page(String path) {


public ServerSetup req(ReqHandler handler) { public ServerSetup req(ReqHandler handler) {
for (FastHttp http : httpImpls()) { for (FastHttp http : httpImpls()) {
http.addGenericHandler(new FastParamsAwareHttpHandler(http, MediaType.HTML_UTF_8, wrappers, handler)); http.addGenericHandler(new DelegatingFastParamsAwareHttpHandler(http, MediaType.HTML_UTF_8, wrappers,
handler));
} }


return this; return this;
Expand Down
Expand Up @@ -25,36 +25,25 @@
import org.rapidoid.commons.MediaType; import org.rapidoid.commons.MediaType;
import org.rapidoid.http.Req; import org.rapidoid.http.Req;
import org.rapidoid.http.fast.FastHttp; import org.rapidoid.http.fast.FastHttp;
import org.rapidoid.http.fast.HttpMetadata;
import org.rapidoid.http.fast.HttpWrapper; import org.rapidoid.http.fast.HttpWrapper;
import org.rapidoid.http.fast.ReqHandler; import org.rapidoid.http.fast.ReqHandler;
import org.rapidoid.net.abstracts.Channel; import org.rapidoid.net.abstracts.Channel;


@Authors("Nikolche Mihajlovski") @Authors("Nikolche Mihajlovski")
@Since("5.0.0") @Since("4.3.0")
public class FastParamsAwarePageHandler extends AbstractAsyncHttpHandler implements HttpMetadata { public class DelegatingFastParamsAwareHttpHandler extends FastParamsAwareHttpHandler {


private final ReqHandler handler; private final ReqHandler handler;


public FastParamsAwarePageHandler(FastHttp http, MediaType contentType, HttpWrapper[] wrappers, ReqHandler handler) { public DelegatingFastParamsAwareHttpHandler(FastHttp http, MediaType contentType, HttpWrapper[] wrappers,
ReqHandler handler) {
super(http, contentType, wrappers); super(http, contentType, wrappers);
this.handler = handler; this.handler = handler;
} }


@Override @Override
protected Object handleReq(Channel channel, Req req) throws Exception { protected Object doHandle(Channel channel, boolean isKeepAlive, Req req) throws Exception {
http.getListener().state(this, req); return handler.handle(req);

// call the handler, get the result
Object result = handler.handle(req);

http.getListener().result(this, contentType, result);
return result;
}

@Override
public boolean needsParams() {
return true;
} }


} }
Expand Up @@ -43,7 +43,7 @@ public FastCallableHttpHandler(FastHttp http, MediaType contentType, HttpWrapper
} }


@Override @Override
protected Object handleReq(Channel ctx, Req req) throws Exception { protected Object handleReq(Channel ctx, boolean isKeepAlive, Req req) throws Exception {
return handler.call(); return handler.call();
} }


Expand Down
Expand Up @@ -26,30 +26,28 @@
import org.rapidoid.http.Req; import org.rapidoid.http.Req;
import org.rapidoid.http.fast.FastHttp; import org.rapidoid.http.fast.FastHttp;
import org.rapidoid.http.fast.HttpWrapper; import org.rapidoid.http.fast.HttpWrapper;
import org.rapidoid.http.fast.ReqHandler;
import org.rapidoid.net.abstracts.Channel; import org.rapidoid.net.abstracts.Channel;


@Authors("Nikolche Mihajlovski") @Authors("Nikolche Mihajlovski")
@Since("4.3.0") @Since("4.3.0")
public class FastParamsAwareHttpHandler extends AbstractAsyncHttpHandler { public abstract class FastParamsAwareHttpHandler extends AbstractAsyncHttpHandler {


private final ReqHandler handler; public FastParamsAwareHttpHandler(FastHttp http, MediaType contentType, HttpWrapper[] wrappers) {

public FastParamsAwareHttpHandler(FastHttp http, MediaType contentType, HttpWrapper[] wrappers, ReqHandler handler) {
super(http, contentType, wrappers); super(http, contentType, wrappers);
this.handler = handler;
} }


@Override @Override
protected Object handleReq(Channel channel, Req req) throws Exception { protected Object handleReq(Channel channel, boolean isKeepAlive, Req req) throws Exception {
http.getListener().state(this, req); http.getListener().state(this, req);


Object result = handler.handle(req); Object result = doHandle(channel, isKeepAlive, req);


http.getListener().result(this, contentType, result); http.getListener().result(this, contentType, result);
return result; return result;
} }


protected abstract Object doHandle(Channel channel, boolean isKeepAlive, Req req) throws Exception;

@Override @Override
public boolean needsParams() { public boolean needsParams() {
return true; return true;
Expand Down

0 comments on commit 0816943

Please sign in to comment.