Skip to content

Commit

Permalink
Added support for generic (req, resp) handlers in the routes API.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 22, 2017
1 parent 7563dd2 commit 747cb78
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
1 change: 1 addition & 0 deletions rapidoid-commons/src/main/resources/rapidoid-classes.txt
Expand Up @@ -407,6 +407,7 @@ org.rapidoid.http.handler.param.ParamRetrievers
org.rapidoid.http.handler.param.ReqParamRetriever
org.rapidoid.http.handler.param.RespParamRetriever
org.rapidoid.http.handler.ParamsAwareReqHandler
org.rapidoid.http.handler.ParamsAwareReqRespHandler
org.rapidoid.http.handler.param.ScreenParamRetriever
org.rapidoid.http.handler.param.UploadParamRetriever
org.rapidoid.http.handler.param.UploadsParamRetriever
Expand Down
Expand Up @@ -35,6 +35,8 @@ public interface HttpRoutes {

void on(String verb, String path, ReqHandler handler);

void on(String verb, String path, ReqRespHandler handler);

void remove(String verb, String path);

void addGenericHandler(HttpHandler handler);
Expand Down
@@ -0,0 +1,48 @@
package org.rapidoid.http.handler;

/*
* #%L
* rapidoid-http-fast
* %%
* Copyright (C) 2014 - 2017 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.FastHttp;
import org.rapidoid.http.HttpRoutes;
import org.rapidoid.http.Req;
import org.rapidoid.http.ReqRespHandler;
import org.rapidoid.http.impl.RouteOptions;
import org.rapidoid.net.abstracts.Channel;

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public class ParamsAwareReqRespHandler extends AbstractDecoratingHttpHandler {

private final ReqRespHandler handler;

public ParamsAwareReqRespHandler(FastHttp http, HttpRoutes routes, RouteOptions options, ReqRespHandler handler) {
super(http, routes, options);
this.handler = handler;
}

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

}
Expand Up @@ -13,13 +13,11 @@
import org.rapidoid.commons.Err;
import org.rapidoid.commons.Str;
import org.rapidoid.data.BufRange;
import org.rapidoid.http.HttpRoutes;
import org.rapidoid.http.HttpVerb;
import org.rapidoid.http.ReqHandler;
import org.rapidoid.http.Route;
import org.rapidoid.http.*;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.handler.HttpHandler;
import org.rapidoid.http.handler.ParamsAwareReqHandler;
import org.rapidoid.http.handler.ParamsAwareReqRespHandler;
import org.rapidoid.http.handler.StaticResourcesHandler;
import org.rapidoid.io.Res;
import org.rapidoid.log.Log;
Expand Down Expand Up @@ -418,11 +416,14 @@ public synchronized void on(String verb, String path, HttpHandler handler) {

@Override
public synchronized void on(String verb, String path, ReqHandler handler) {
addOrRemove(true, verb, path, handler(handler, new RouteOptions()));
HttpHandler hnd = new ParamsAwareReqHandler(null, null, new RouteOptions(), handler);
addOrRemove(true, verb, path, hnd);
}

public HttpHandler handler(ReqHandler reqHandler, RouteOptions options) {
return new ParamsAwareReqHandler(null, null, options, reqHandler);
@Override
public synchronized void on(String verb, String path, ReqRespHandler handler) {
HttpHandler hnd = new ParamsAwareReqRespHandler(null, null, new RouteOptions(), handler);
addOrRemove(true, verb, path, hnd);
}

@Override
Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.rapidoid.config.ConfigImpl;
import org.rapidoid.http.FastHttp;
import org.rapidoid.http.IsolatedIntegrationTest;
import org.rapidoid.http.Req;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.impl.HttpRoutesImpl;
import org.rapidoid.net.Server;
Expand All @@ -43,9 +42,9 @@ public void testFastHttpHandler() {
HttpRoutesImpl routes = new HttpRoutesImpl(customization);
FastHttp http = new FastHttp(routes);

routes.on("get", "/abc", Req::data);
routes.on("get", "/abc", (req, resp) -> resp.json(req.data()));

routes.on("get,post", "/xyz", req -> U.list(req.uri(), req.data()));
routes.on("get,post", "/xyz", (req, resp) -> resp.html(U.list(req.uri(), req.data())));

Server server = http.listen(7779);

Expand Down
Expand Up @@ -2,7 +2,7 @@ HTTP/1.1 200 OK
Connection: keep-alive
Server: Rapidoid
Date: XXXXX GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12
Content-Type: application/json
Content-Length: 19

{x=1, y=foo}
{"x":"1","y":"foo"}

0 comments on commit 747cb78

Please sign in to comment.