fix(gateway-vertx) Ensure ApiVerticle parses body early to avoid losing it if OAuth2 kicks in.
MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/api/ApiResourceImpl.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/api/ApiResourceImpl.java
@@ -27,8 +27,6 @@ import io.apiman.gateway.engine.beans.exceptions.PublishingException;
import io.apiman.gateway.engine.beans.exceptions.RegistrationException;
import io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig;
import io.netty.handler.codec.http.HttpResponseStatus;
-import io.vertx.core.Handler;
-import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
@@ -88,13 +86,11 @@ public class ApiResourceImpl implements IApiResource, IRouteBuilder {
}
public void publish() {
- routingContext.request().bodyHandler((Handler<Buffer>) buffer -> {
try {
- publish(Json.decodeValue(buffer.toString("utf-8"), Api.class));
+ publish(Json.decodeValue(routingContext.getBodyAsString(), Api.class));
} catch (Exception e) {
error(routingContext, HttpResponseStatus.BAD_REQUEST, e.getMessage(), e);
}
- });
}
@Override

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/api/ClientResourceImpl.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/api/ClientResourceImpl.java
@@ -24,8 +24,6 @@ import io.apiman.gateway.engine.beans.Client;
import io.apiman.gateway.engine.beans.exceptions.RegistrationException;
import io.apiman.gateway.platforms.vertx3.common.config.VertxEngineConfig;
import io.netty.handler.codec.http.HttpResponseStatus;
-import io.vertx.core.Handler;
-import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.Json;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
@@ -79,13 +77,11 @@ public class ClientResourceImpl implements IClientResource, IRouteBuilder {
}
public void register() {
- routingContext.request().bodyHandler((Handler<Buffer>) buffer -> {
try {
- register(Json.decodeValue(buffer.toString("utf-8"), Client.class)); //$NON-NLS-1$
+ register(Json.decodeValue(routingContext.getBodyAsString(), Client.class));
} catch (Exception e) {
error(routingContext, HttpResponseStatus.BAD_REQUEST, e.getMessage(), e);
}
- });
}
@Override

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/api/auth/AuthFactory.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/api/auth/AuthFactory.java
@@ -43,9 +43,17 @@ public class AuthFactory {
}
}
- public static AuthHandler getAuth(Vertx vertx, Router router, VertxEngineConfig apimanConfig, JsonObject auth) {
- String type = auth.getString("type", "NONE");
- JsonObject authConfig = auth.getJsonObject("config", new JsonObject());
+ /**
+ * Creates an auth handler of the type indicated in the `auth` section of config.
+ *
+ * @param vertx the vert.x instance
+ * @param router the vert.x web router to protect
+ * @param apimanConfig the apiman config
+ * @return an auth handler
+ */
+ public static AuthHandler getAuth(Vertx vertx, Router router, VertxEngineConfig apimanConfig) {
+ String type = apimanConfig.getAuth().getString("type", "NONE");
+ JsonObject authConfig = apimanConfig.getAuth().getJsonObject("config", new JsonObject());
switch(AuthType.getType(type)) {
case BASIC:

MODIFY
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/verticles/ApiVerticle.java
gateway/platforms/vertx3/vertx3/src/main/java/io/apiman/gateway/platforms/vertx3/verticles/ApiVerticle.java
@@ -26,6 +26,7 @@ import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.net.JksOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.AuthHandler;
+import io.vertx.ext.web.handler.BodyHandler;
/**
* API verticle provides the Gateway API RESTful API. Config is validated and pushed into the registry
@@ -48,9 +49,15 @@ public class ApiVerticle extends ApimanVerticleWithEngine {
Router router = Router.router(vertx)
.exceptionHandler(log::error);
- AuthHandler handler = AuthFactory.getAuth(vertx, router, apimanConfig, apimanConfig.getAuth());
+ // Ensure body handler is attached early so that if AuthHandler takes an external action
+ // we don't end up losing the body (e.g OAuth2).
+ router.route()
+ .handler(BodyHandler.create());
- router.route("/*").handler(handler);
+ AuthHandler authHandler = AuthFactory.getAuth(vertx, router, apimanConfig);
+
+ router.route("/*")
+ .handler(authHandler);
clientResource.buildRoutes(router);
apiResource.buildRoutes(router);

fix(gateway-vertx) Ensure ApiVerticle parses body early to avoid losing it if OAuth2 kicks in.