Skip to content

Commit

Permalink
Introducing non-decorated handlers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Aug 28, 2016
1 parent e5c6d20 commit 9dedd03
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion rapidoid-commons/src/main/resources/rapidoid-classes.txt
Expand Up @@ -329,7 +329,7 @@ org.rapidoid.http.handler.lambda.SixParamLambdaHandler
org.rapidoid.http.handler.lambda.ThreeParamLambdaHandler org.rapidoid.http.handler.lambda.ThreeParamLambdaHandler
org.rapidoid.http.handler.lambda.TwoParamLambdaHandler org.rapidoid.http.handler.lambda.TwoParamLambdaHandler
org.rapidoid.http.handler.MethodReqHandler org.rapidoid.http.handler.MethodReqHandler
org.rapidoid.http.handler.NonBlockingHttpHandler org.rapidoid.http.handler.NonDecoratedHttpHandler
org.rapidoid.http.handler.optimized.CallableHttpHandler org.rapidoid.http.handler.optimized.CallableHttpHandler
org.rapidoid.http.handler.optimized.DelegatingParamsAwareReqHandler org.rapidoid.http.handler.optimized.DelegatingParamsAwareReqHandler
org.rapidoid.http.handler.optimized.DelegatingParamsAwareReqRespHandler org.rapidoid.http.handler.optimized.DelegatingParamsAwareReqRespHandler
Expand Down
Expand Up @@ -58,4 +58,9 @@ public interface RouteConfig {
String zone(); String zone();


RouteOptions zone(String zone); RouteOptions zone(String zone);

boolean decorated();

RouteOptions decorated(boolean decorated);

} }
Expand Up @@ -34,15 +34,15 @@


@Authors("Nikolche Mihajlovski") @Authors("Nikolche Mihajlovski")
@Since("5.2.0") @Since("5.2.0")
public class NonBlockingHttpHandler extends AbstractHttpHandler { public class NonDecoratedHttpHandler extends AbstractHttpHandler {


private final Callable<?> nonBlockingLogic; private final Callable<?> logic;
private final boolean returnsJson; private final boolean returnsJson;


public NonBlockingHttpHandler(RouteOptions options, Callable<?> nonBlockingLogic) { public NonDecoratedHttpHandler(RouteOptions options, Callable<?> logic) {
super(options); super(options);


this.nonBlockingLogic = nonBlockingLogic; this.logic = logic;
this.returnsJson = options.contentType() == MediaType.JSON; this.returnsJson = options.contentType() == MediaType.JSON;
} }


Expand All @@ -51,7 +51,7 @@ public HttpStatus handle(Channel ctx, boolean isKeepAlive, Req req, Object extra
Object result; Object result;


try { try {
result = nonBlockingLogic.call(); result = logic.call();


} catch (Exception e) { } catch (Exception e) {
HttpIO.write200(ctx, isKeepAlive, contentType, "Internal server error!".getBytes()); HttpIO.write200(ctx, isKeepAlive, contentType, "Internal server error!".getBytes());
Expand Down
Expand Up @@ -10,6 +10,7 @@
import org.rapidoid.http.RouteConfig; import org.rapidoid.http.RouteConfig;
import org.rapidoid.u.U; import org.rapidoid.u.U;


import java.util.Arrays;
import java.util.Set; import java.util.Set;


/* /*
Expand Down Expand Up @@ -44,6 +45,8 @@ public class RouteOptions extends RapidoidThing implements RouteConfig {


private volatile String zone; private volatile String zone;


private volatile boolean decorated;

private volatile TransactionMode transactionMode = TransactionMode.NONE; private volatile TransactionMode transactionMode = TransactionMode.NONE;


private final Set<String> roles = Coll.synchronizedSet(); private final Set<String> roles = Coll.synchronizedSet();
Expand Down Expand Up @@ -139,6 +142,17 @@ public RouteOptions zone(String zone) {
return this; return this;
} }


@Override
public boolean decorated() {
return decorated;
}

@Override
public RouteOptions decorated(boolean decorated) {
this.decorated = decorated;
return this;
}

public RouteOptions copy() { public RouteOptions copy() {
RouteOptions copy = new RouteOptions(); RouteOptions copy = new RouteOptions();


Expand All @@ -149,6 +163,7 @@ public RouteOptions copy() {
copy.roles(U.arrayOf(String.class, roles)); copy.roles(U.arrayOf(String.class, roles));
copy.wrappers(wrappers()); copy.wrappers(wrappers());
copy.zone(zone()); copy.zone(zone());
copy.decorated(decorated());


return copy; return copy;
} }
Expand All @@ -161,12 +176,14 @@ public boolean equals(Object o) {
RouteOptions that = (RouteOptions) o; RouteOptions that = (RouteOptions) o;


if (mvc != that.mvc) return false; if (mvc != that.mvc) return false;
if (decorated != that.decorated) return false;
if (contentType != null ? !contentType.equals(that.contentType) : that.contentType != null) return false; if (contentType != null ? !contentType.equals(that.contentType) : that.contentType != null) return false;
if (view != null ? !view.equals(that.view) : that.view != null) return false; if (view != null ? !view.equals(that.view) : that.view != null) return false;
if (zone != null ? !zone.equals(that.zone) : that.zone != null) return false; if (zone != null ? !zone.equals(that.zone) : that.zone != null) return false;
if (transactionMode != that.transactionMode) return false; if (transactionMode != that.transactionMode) return false;
if (roles != null ? !roles.equals(that.roles) : that.roles != null) return false; if (roles != null ? !roles.equals(that.roles) : that.roles != null) return false;
return wrappers != null ? wrappers.equals(that.wrappers) : that.wrappers == null; return Arrays.equals(wrappers, that.wrappers);

} }


@Override @Override
Expand All @@ -175,9 +192,10 @@ public int hashCode() {
result = 31 * result + (view != null ? view.hashCode() : 0); result = 31 * result + (view != null ? view.hashCode() : 0);
result = 31 * result + (mvc ? 1 : 0); result = 31 * result + (mvc ? 1 : 0);
result = 31 * result + (zone != null ? zone.hashCode() : 0); result = 31 * result + (zone != null ? zone.hashCode() : 0);
result = 31 * result + (decorated ? 1 : 0);
result = 31 * result + (transactionMode != null ? transactionMode.hashCode() : 0); result = 31 * result + (transactionMode != null ? transactionMode.hashCode() : 0);
result = 31 * result + (roles != null ? roles.hashCode() : 0); result = 31 * result + (roles != null ? roles.hashCode() : 0);
result = 31 * result + (wrappers != null ? wrappers.hashCode() : 0); result = 31 * result + Arrays.hashCode(wrappers);
return result; return result;
} }
} }
Expand Up @@ -106,7 +106,12 @@ public static void registerPredefined(FastHttp http, HttpRoutes routes, String v


@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static void register(FastHttp http, HttpRoutes routes, String verb, String path, RouteOptions options, Callable<?> handler) { public static void register(FastHttp http, HttpRoutes routes, String verb, String path, RouteOptions options, Callable<?> handler) {
routes.on(verb, path, new CallableHttpHandler(http, routes, options, (Callable<Object>) handler));
HttpHandler httpHandler = options.decorated() ?
new CallableHttpHandler(http, routes, options, handler) :
new NonDecoratedHttpHandler(options, handler);

routes.on(verb, path, httpHandler);
} }


public static void register(FastHttp http, HttpRoutes routes, String verb, String path, RouteOptions options, NParamLambda lambda) { public static void register(FastHttp http, HttpRoutes routes, String verb, String path, RouteOptions options, NParamLambda lambda) {
Expand Down
Expand Up @@ -4,7 +4,6 @@
import org.rapidoid.annotation.Authors; import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since; import org.rapidoid.annotation.Since;
import org.rapidoid.annotation.TransactionMode; import org.rapidoid.annotation.TransactionMode;
import org.rapidoid.http.MediaType;
import org.rapidoid.http.*; import org.rapidoid.http.*;
import org.rapidoid.http.handler.HttpHandlers; import org.rapidoid.http.handler.HttpHandlers;
import org.rapidoid.http.impl.RouteOptions; import org.rapidoid.http.impl.RouteOptions;
Expand Down Expand Up @@ -397,4 +396,9 @@ public OnRoute zone(String zone) {
return this; return this;
} }


public OnRoute decorated(boolean decorated) {
options.decorated(decorated);
return this;
}

} }

0 comments on commit 9dedd03

Please sign in to comment.