package examples;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.parser.core.models.AuthorizationValue;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.jwt.JWTAuth;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.api.RequestParameter;
import io.vertx.ext.web.api.RequestParameters;
import io.vertx.ext.web.api.contract.RouterFactoryOptions;
import io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory;
import io.vertx.ext.web.api.validation.ValidationException;
import io.vertx.ext.web.handler.JWTAuthHandler;
import java.util.Collections;
import java.util.List;
public class OpenAPI3Examples {
public void constructRouterFactory(Vertx vertx) {
OpenAPI3RouterFactory.create(vertx, "src/main/resources/petstore.yaml", ar -> {
if (ar.succeeded()) {
OpenAPI3RouterFactory routerFactory = ar.result();
} else {
Throwable exception = ar.cause();
}
});
}
public void constructRouterFactoryFromUrl(Vertx vertx) {
OpenAPI3RouterFactory.create(
vertx,
"https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml",
ar -> {
if (ar.succeeded()) {
OpenAPI3RouterFactory routerFactory = ar.result();
} else {
Throwable exception = ar.cause();
}
});
}
public void (Vertx vertx) {
AuthorizationValue authorizationValue = new AuthorizationValue()
.type("header")
.keyName("Authorization")
.value("Bearer xx.yy.zz");
List<JsonObject> authorizations = Collections.singletonList(JsonObject.mapFrom(authorizationValue));
OpenAPI3RouterFactory.create(
vertx,
"https://raw.githubusercontent.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore.yaml",
authorizations,
ar -> {
if (ar.succeeded()) {
OpenAPI3RouterFactory routerFactory = ar.result();
} else {
Throwable exception = ar.cause();
}
});
}
public void addRoute(Vertx vertx, OpenAPI3RouterFactory routerFactory) {
routerFactory.addHandlerByOperationId("awesomeOperation", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
RequestParameter body = params.body();
JsonObject jsonBody = body.getJsonObject();
});
routerFactory.addFailureHandlerByOperationId("awesomeOperation", routingContext -> {
});
}
public void addSecurityHandler(OpenAPI3RouterFactory routerFactory, Handler securityHandler) {
routerFactory.addSecurityHandler("security_scheme_name", securityHandler);
}
public void addJWT(OpenAPI3RouterFactory routerFactory, JWTAuth jwtAuthProvider) {
routerFactory.addSecurityHandler("jwt_auth", JWTAuthHandler.create(jwtAuthProvider));
}
public void addOperationModelKey(OpenAPI3RouterFactory routerFactory, RouterFactoryOptions options) {
options.setOperationModelKey("operationPOJO");
routerFactory.setOptions(options);
routerFactory.addHandlerByOperationId("listPets", routingContext -> {
io.swagger.v3.oas.models.Operation operation = routingContext.get("operationPOJO");
routingContext
.response()
.setStatusCode(200)
.setStatusMessage("OK")
.end(operation.getOperationId());
});
}
public void generateRouter(Vertx vertx, OpenAPI3RouterFactory routerFactory) {
Router router = routerFactory.getRouter();
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost"));
server.requestHandler(router).listen();
}
public void mainExample(Vertx vertx) {
OpenAPI3RouterFactory.create(vertx, "src/main/resources/petstore.yaml",
openAPI3RouterFactoryAsyncResult -> {
if (openAPI3RouterFactoryAsyncResult.succeeded()) {
OpenAPI3RouterFactory routerFactory = openAPI3RouterFactoryAsyncResult.result();
RouterFactoryOptions options = new RouterFactoryOptions()
.setMountValidationFailureHandler(false);
routerFactory.setOptions(options);
routerFactory.addHandlerByOperationId("listPets", routingContext -> {
routingContext.response().setStatusMessage("Called listPets").end();
});
routerFactory.addFailureHandlerByOperationId("listPets", routingContext -> {
Throwable failure = routingContext.failure();
if (failure instanceof ValidationException)
routingContext.response().setStatusCode(400).setStatusMessage("ValidationException thrown! " + (
(ValidationException) failure).type().name()).end();
});
routerFactory.addSecurityHandler("api_key", RoutingContext::next);
Router router = routerFactory.getRouter();
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost"));
server.requestHandler(router).listen();
} else {
Throwable exception = openAPI3RouterFactoryAsyncResult.cause();
}
});
}
}