Search in sources :

Example 1 with OpenAPI3RouterFactory

use of io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory in project vertx-web by vert-x3.

the class OpenAPI3Examples method mountOptions.

public void mountOptions(AsyncResult<OpenAPI3RouterFactory> ar) {
    OpenAPI3RouterFactory routerFactory = ar.result();
    // Create and mount options to router factory
    RouterFactoryOptions options = new RouterFactoryOptions().setMountNotImplementedHandler(true).setMountValidationFailureHandler(false);
    routerFactory.setOptions(options);
}
Also used : RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) OpenAPI3RouterFactory(io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory)

Example 2 with OpenAPI3RouterFactory

use of io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory in project api-framework by vinscom.

the class OpenAPI3RouteBuilder method getRouter.

@Override
public Router getRouter(Router pRouter) {
    OpenAPI3RouterFactory apiFactory = OpenAPI3RouterFactory.rxCreateRouterFactoryFromFile(getVertx(), getOpenAPI3File().getAbsolutePath()).blockingGet();
    getServices().getServices().forEach((api) -> {
        RESTService service = (RESTService) api;
        apiFactory.addHandlerByOperationId(service.getOperationId(), (routingContext) -> {
            routingContext.put(FrameworkConstants.RoutingContext.Attribute.BODY_AS_JSON, service.isBodyAsJson());
            if (isSecurityEnable()) {
                if (routingContext.user() == null) {
                    routingContext.fail(401);
                    return;
                }
                routingContext.user().isAuthorized(AUTHORIZATION_PREFIX + ":" + service.getOperationId(), (event) -> {
                    boolean authSuccess = event.succeeded() ? event.result() : false;
                    if (authSuccess) {
                        process(routingContext, service.getServiceUniqueId());
                    } else {
                        routingContext.fail(401);
                    }
                });
            } else {
                getLog().warn("Security disabled for " + service.getServiceUniqueId());
                process(routingContext, service.getServiceUniqueId());
            }
        });
    });
    return apiFactory.getRouter();
}
Also used : RESTService(in.erail.service.RESTService) OpenAPI3RouterFactory(io.vertx.reactivex.ext.web.api.contract.openapi3.OpenAPI3RouterFactory)

Example 3 with OpenAPI3RouterFactory

use of io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory in project vertx-examples by vert-x3.

the class OpenAPI3Server method start.

public void start(Future future) {
    // Load the api spec. This operation is asynchronous
    OpenAPI3RouterFactory.create(this.vertx, "petstore.yaml", openAPI3RouterFactoryAsyncResult -> {
        if (openAPI3RouterFactoryAsyncResult.failed()) {
            // Something went wrong during router factory initialization
            Throwable exception = openAPI3RouterFactoryAsyncResult.cause();
            logger.error("oops, something went wrong during factory initialization", exception);
            future.fail(exception);
        }
        // Spec loaded with success
        OpenAPI3RouterFactory routerFactory = openAPI3RouterFactoryAsyncResult.result();
        // Add an handler with operationId
        routerFactory.addHandlerByOperationId("listPets", routingContext -> {
            // Load the parsed parameters
            RequestParameters params = routingContext.get("parsedParameters");
            // Handle listPets operation
            RequestParameter limitParameter = params.queryParameter(/* Parameter name */
            "limit");
            if (limitParameter != null) {
                // limit parameter exists, use it!
                Integer limit = limitParameter.getInteger();
            } else {
            // limit parameter doesn't exist (it's not required).
            // If it's required you don't have to check if it's null!
            }
            routingContext.response().setStatusMessage("OK").end();
        });
        // Add a failure handler
        routerFactory.addFailureHandlerByOperationId("listPets", routingContext -> {
            // This is the failure handler
            Throwable failure = routingContext.failure();
            if (failure instanceof ValidationException)
                // Handle Validation Exception
                routingContext.response().setStatusCode(400).setStatusMessage("ValidationException thrown! " + ((ValidationException) failure).type().name()).end();
        });
        // Add a security handler
        routerFactory.addSecurityHandler("api_key", routingContext -> {
            // Handle security here
            routingContext.next();
        });
        // Before router creation you can enable/disable various router factory behaviours
        RouterFactoryOptions factoryOptions = new RouterFactoryOptions().setMountValidationFailureHandler(// Disable mounting of dedicated validation failure handler
        false).setMountResponseContentTypeHandler(// Mount ResponseContentTypeHandler automatically
        true);
        // Now you have to generate the router
        Router router = routerFactory.setOptions(factoryOptions).getRouter();
        // Now you can use your Router instance
        server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost"));
        server.requestHandler(router::accept).listen((ar) -> {
            if (ar.succeeded()) {
                logger.info("Server started on port " + ar.result().actualPort());
                future.complete();
            } else {
                logger.error("oops, something went wrong during server initialization", ar.cause());
                future.fail(ar.cause());
            }
        });
    });
}
Also used : ValidationException(io.vertx.ext.web.api.validation.ValidationException) RequestParameter(io.vertx.ext.web.api.RequestParameter) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Router(io.vertx.ext.web.Router) OpenAPI3RouterFactory(io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory) RequestParameters(io.vertx.ext.web.api.RequestParameters)

Example 4 with OpenAPI3RouterFactory

use of io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory in project vertx-web by vert-x3.

the class OpenAPI3Examples method mainExample.

public void mainExample(Vertx vertx) {
    // Load the api spec. This operation is asynchronous
    OpenAPI3RouterFactory.create(vertx, "src/main/resources/petstore.yaml", openAPI3RouterFactoryAsyncResult -> {
        if (openAPI3RouterFactoryAsyncResult.succeeded()) {
            // Spec loaded with success, retrieve the router
            OpenAPI3RouterFactory routerFactory = openAPI3RouterFactoryAsyncResult.result();
            // You can enable or disable different features of router factory through mounting RouterFactoryOptions
            // For example you can enable or disable the default failure handler for ValidationException
            RouterFactoryOptions options = new RouterFactoryOptions().setMountValidationFailureHandler(false);
            // Mount the options
            routerFactory.setOptions(options);
            // Add an handler with operationId
            routerFactory.addHandlerByOperationId("listPets", routingContext -> {
                // Handle listPets operation
                routingContext.response().setStatusMessage("Called listPets").end();
            });
            // Add a failure handler to the same operationId
            routerFactory.addFailureHandlerByOperationId("listPets", routingContext -> {
                // This is the failure handler
                Throwable failure = routingContext.failure();
                if (failure instanceof ValidationException)
                    // Handle Validation Exception
                    routingContext.response().setStatusCode(400).setStatusMessage("ValidationException thrown! " + ((ValidationException) failure).type().name()).end();
            });
            // Add an handler with a combination of HttpMethod and path
            routerFactory.addHandler(HttpMethod.POST, "/pets", routingContext -> {
                // Extract request body and use it
                RequestParameters params = routingContext.get("parsedParameters");
                JsonObject pet = params.body().getJsonObject();
                routingContext.response().putHeader("content-type", "application/json; charset=utf-8").end(pet.encodePrettily());
            });
            // Add a security handler
            routerFactory.addSecurityHandler("api_key", routingContext -> {
                // Handle security here
                routingContext.next();
            });
            // Now you have to generate the router
            Router router = routerFactory.getRouter();
            // Now you can use your Router instance
            HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080).setHost("localhost"));
            server.requestHandler(router::accept).listen();
        } else {
            // Something went wrong during router factory initialization
            Throwable exception = openAPI3RouterFactoryAsyncResult.cause();
        }
    });
}
Also used : ValidationException(io.vertx.ext.web.api.validation.ValidationException) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) HttpServer(io.vertx.core.http.HttpServer) HttpServerOptions(io.vertx.core.http.HttpServerOptions) JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.ext.web.Router) OpenAPI3RouterFactory(io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory) RequestParameters(io.vertx.ext.web.api.RequestParameters)

Aggregations

RouterFactoryOptions (io.vertx.ext.web.api.contract.RouterFactoryOptions)3 OpenAPI3RouterFactory (io.vertx.ext.web.api.contract.openapi3.OpenAPI3RouterFactory)3 HttpServerOptions (io.vertx.core.http.HttpServerOptions)2 Router (io.vertx.ext.web.Router)2 RequestParameters (io.vertx.ext.web.api.RequestParameters)2 ValidationException (io.vertx.ext.web.api.validation.ValidationException)2 RESTService (in.erail.service.RESTService)1 HttpServer (io.vertx.core.http.HttpServer)1 JsonObject (io.vertx.core.json.JsonObject)1 RequestParameter (io.vertx.ext.web.api.RequestParameter)1 OpenAPI3RouterFactory (io.vertx.reactivex.ext.web.api.contract.openapi3.OpenAPI3RouterFactory)1