Search in sources :

Example 1 with EndpointConfig

use of io.neonbee.config.EndpointConfig in project neonbee by SAP.

the class ServerVerticle method mountEndpoints.

/**
 * Mounts all endpoints as sub routers to the given router.
 *
 * @param router             the main router of the server verticle
 * @param endpointConfigs    a list of endpoint configurations to mount
 * @param defaultAuthHandler the fallback auth. handler in case no auth. handler is specified by the endpoint
 * @param hooksHandler       the "once per request" handler, to be executed after authentication on an endpoint
 */
private Future<Void> mountEndpoints(Router router, List<EndpointConfig> endpointConfigs, Optional<AuthenticationHandler> defaultAuthHandler, HooksHandler hooksHandler) {
    if (endpointConfigs.isEmpty()) {
        LOGGER.warn("No endpoints configured");
        return succeededFuture();
    }
    // iterate the endpoint configurations, as order is important here!
    for (EndpointConfig endpointConfig : endpointConfigs) {
        String endpointType = endpointConfig.getType();
        if (Strings.isNullOrEmpty(endpointType)) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("Endpoint with configuration {} is missing the 'type' field", endpointConfig.toJson().encode());
            }
            return failedFuture(new IllegalArgumentException("Endpoint is missing the 'type' field"));
        }
        Endpoint endpoint;
        try {
            endpoint = Class.forName(endpointType).asSubclass(Endpoint.class).getDeclaredConstructor().newInstance();
        } catch (ClassNotFoundException e) {
            LOGGER.error("No class for endpoint type {}", endpointType, e);
            return failedFuture(new IllegalArgumentException("Endpoint class not found", e));
        } catch (ClassCastException e) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("Endpoint type {} must implement {}", endpointType, Endpoint.class.getName(), e);
            }
            return failedFuture(new IllegalArgumentException("Endpoint does not implement the Endpoint interface", e));
        } catch (NoSuchMethodException e) {
            LOGGER.error("Endpoint type {} must expose an empty constructor", endpointType, e);
            return failedFuture(new IllegalArgumentException("Endpoint does not expose an empty constructor", e));
        } catch (InvocationTargetException | IllegalAccessException | InstantiationException e) {
            LOGGER.error("Endpoint type {} could not be instantiated or threw an exception", endpointType, e);
            return failedFuture(Optional.ofNullable((Exception) e.getCause()).orElse(e));
        }
        EndpointConfig defaultEndpointConfig = endpoint.getDefaultConfig();
        if (!Optional.ofNullable(endpointConfig.isEnabled()).orElse(defaultEndpointConfig.isEnabled())) {
            LOGGER.info("Endpoint with type {} is disabled", endpointType);
            continue;
        }
        String endpointBasePath = Optional.ofNullable(endpointConfig.getBasePath()).orElse(defaultEndpointConfig.getBasePath());
        if (!endpointBasePath.endsWith("/")) {
            endpointBasePath += "/";
        }
        JsonObject endpointAdditionalConfig = Optional.ofNullable(defaultEndpointConfig.getAdditionalConfig()).map(JsonObject::copy).orElseGet(JsonObject::new);
        Optional.ofNullable(endpointConfig.getAdditionalConfig()).ifPresent(endpointAdditionalConfig::mergeIn);
        Router endpointRouter;
        try {
            endpointRouter = endpoint.createEndpointRouter(vertx, endpointBasePath, endpointAdditionalConfig);
        } catch (Exception e) {
            LOGGER.error("Failed to initialize endpoint router for endpoint with type {} with configuration {}", endpointType, endpointAdditionalConfig, e);
            return failedFuture(e);
        }
        Optional<AuthenticationHandler> endpointAuthHandler = createAuthChainHandler(Optional.ofNullable(endpointConfig.getAuthChainConfig()).orElse(defaultEndpointConfig.getAuthChainConfig())).or(() -> defaultAuthHandler);
        Route endpointRoute = router.route(endpointBasePath + "*");
        endpointAuthHandler.ifPresent(endpointRoute::handler);
        endpointRoute.handler(hooksHandler);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Mounting endpoint with type {} and configuration {}" + "to base path {} using {} authentication handler", endpointType, endpointConfig, endpointBasePath, endpointAuthHandler.isPresent() ? "an" + endpointAuthHandler.get().getClass().getSimpleName() : "no");
        }
        // requires a new route object, thus do not use the endpointRoute here, but call mountSubRouter instead
        router.mountSubRouter(endpointBasePath, endpointRouter);
    }
    // all endpoints have been mounted successfully
    return succeededFuture();
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Router(io.vertx.ext.web.Router) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Endpoint(io.neonbee.endpoint.Endpoint) AuthenticationHandler(io.vertx.ext.web.handler.AuthenticationHandler) EndpointConfig(io.neonbee.config.EndpointConfig) Route(io.vertx.ext.web.Route)

Example 2 with EndpointConfig

use of io.neonbee.config.EndpointConfig in project neonbee by SAP.

the class ODataV4EndpointTest method provideWorkingDirectoryBuilder.

@Override
protected WorkingDirectoryBuilder provideWorkingDirectoryBuilder(TestInfo testInfo, VertxTestContext testContext) {
    return super.provideWorkingDirectoryBuilder(testInfo, testContext).setCustomTask(root -> {
        // the server verticle should either use strict, cds or loose URI mapping
        String testMethodName = testInfo.getTestMethod().map(Method::getName).orElse(EMPTY);
        UriConversion uriConversion = STRICT;
        if (testMethodName.contains("LooseUriConversion")) {
            uriConversion = LOOSE;
        } else if (testMethodName.contains("CDSUriConversion")) {
            uriConversion = CDS;
        }
        DeploymentOptions opts = WorkingDirectoryBuilder.readDeploymentOptions(ServerVerticle.class, root);
        EndpointConfig epc = new EndpointConfig().setType(ODataV4Endpoint.class.getName()).setAdditionalConfig(new JsonObject().put(CONFIG_URI_CONVERSION, uriConversion.toString()));
        ServerConfig sc = new ServerConfig(opts.getConfig()).setEndpointConfigs(List.of(epc));
        opts.setConfig(sc.toJson());
        WorkingDirectoryBuilder.writeDeploymentOptions(ServerVerticle.class, opts, root);
    });
}
Also used : ServerConfig(io.neonbee.config.ServerConfig) DeploymentOptions(io.vertx.core.DeploymentOptions) UriConversion(io.neonbee.endpoint.odatav4.ODataV4Endpoint.UriConversion) JsonObject(io.vertx.core.json.JsonObject) EndpointConfig(io.neonbee.config.EndpointConfig)

Example 3 with EndpointConfig

use of io.neonbee.config.EndpointConfig in project neonbee by SAP.

the class ODataReadEntitiesTest method provideWorkingDirectoryBuilder.

@Override
protected WorkingDirectoryBuilder provideWorkingDirectoryBuilder(TestInfo testInfo, VertxTestContext testContext) {
    return super.provideWorkingDirectoryBuilder(testInfo, testContext).setCustomTask(root -> {
        // the server verticle should either use strict, cds or loose URI mapping
        String testMethodName = testInfo.getTestMethod().map(Method::getName).orElse(EMPTY);
        UriConversion uriConversion = STRICT;
        if (testMethodName.contains("LooseUriConversion")) {
            uriConversion = LOOSE;
        } else if (testMethodName.contains("CDSUriConversion")) {
            uriConversion = CDS;
        }
        DeploymentOptions opts = WorkingDirectoryBuilder.readDeploymentOptions(ServerVerticle.class, root);
        EndpointConfig epc = new EndpointConfig().setType(ODataV4Endpoint.class.getName()).setAdditionalConfig(new JsonObject().put(CONFIG_URI_CONVERSION, uriConversion.toString()));
        ServerConfig sc = new ServerConfig(opts.getConfig()).setEndpointConfigs(List.of(epc));
        opts.setConfig(sc.toJson());
        WorkingDirectoryBuilder.writeDeploymentOptions(ServerVerticle.class, opts, root);
    });
}
Also used : ServerConfig(io.neonbee.config.ServerConfig) DeploymentOptions(io.vertx.core.DeploymentOptions) UriConversion(io.neonbee.endpoint.odatav4.ODataV4Endpoint.UriConversion) JsonObject(io.vertx.core.json.JsonObject) EndpointConfig(io.neonbee.config.EndpointConfig)

Example 4 with EndpointConfig

use of io.neonbee.config.EndpointConfig in project neonbee by SAP.

the class PetStoreTest method provideWorkingDirectoryBuilder.

@Override
protected WorkingDirectoryBuilder provideWorkingDirectoryBuilder(TestInfo testInfo, VertxTestContext testContext) {
    WorkingDirectoryBuilder dirBuilder = WorkingDirectoryBuilder.standard();
    return dirBuilder.setCustomTask(workingDir -> {
        DeploymentOptions opts = WorkingDirectoryBuilder.readDeploymentOptions(ServerVerticle.class, workingDir);
        EndpointConfig epc = new EndpointConfig().setType(PetStoreEndpoint.class.getName());
        ServerConfig sc = new ServerConfig(opts.getConfig()).setEndpointConfigs(List.of(epc));
        opts.setConfig(sc.toJson());
        WorkingDirectoryBuilder.writeDeploymentOptions(ServerVerticle.class, opts, workingDir);
    });
}
Also used : ServerConfig(io.neonbee.config.ServerConfig) DeploymentOptions(io.vertx.core.DeploymentOptions) WorkingDirectoryBuilder(io.neonbee.test.helper.WorkingDirectoryBuilder) EndpointConfig(io.neonbee.config.EndpointConfig)

Aggregations

EndpointConfig (io.neonbee.config.EndpointConfig)4 ServerConfig (io.neonbee.config.ServerConfig)3 DeploymentOptions (io.vertx.core.DeploymentOptions)3 JsonObject (io.vertx.core.json.JsonObject)3 UriConversion (io.neonbee.endpoint.odatav4.ODataV4Endpoint.UriConversion)2 Endpoint (io.neonbee.endpoint.Endpoint)1 WorkingDirectoryBuilder (io.neonbee.test.helper.WorkingDirectoryBuilder)1 Route (io.vertx.ext.web.Route)1 Router (io.vertx.ext.web.Router)1 AuthenticationHandler (io.vertx.ext.web.handler.AuthenticationHandler)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1