Search in sources :

Example 26 with Route

use of io.vertx.ext.web.Route in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method defineHelloRoute.

private void defineHelloRoute() {
    Route route = router.route(HttpMethod.GET, "/").produces(APPLICATION_JSON);
    route.handler(routingContext -> {
        execute(200, routingContext, new ServerAction() {

            @Override
            public Object execute() throws Exception {
                return engine.hello();
            }
        }, false);
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Route(io.vertx.ext.web.Route) UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) MissingInstanceException(com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException)

Example 27 with Route

use of io.vertx.ext.web.Route in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method defineRunInstanceMethodRoute.

private void defineRunInstanceMethodRoute() {
    Route route = router.route(HttpMethod.POST, "/instances/:" + PARAM_INSTANCE + "/methods/:" + PARAM_METHOD).produces(APPLICATION_JSON);
    route.blockingHandler(routingContext -> {
        HttpServerRequest request = routingContext.request();
        String instanceId = request.getParam(PARAM_INSTANCE);
        String methodId = request.getParam(PARAM_METHOD);
        Map<String, Object> methodArgs = fetchMethodArgs(routingContext.getBodyAsString());
        execute(200, routingContext, new ServerAction() {

            @Override
            public Object execute() throws Exception {
                logger.logInfo("TEST http://localhost:" + String.valueOf(port) + request.uri() + " AT " + new Date().toString());
                APIMethodResult result = engine.callAPIOnInstance(instanceId, methodId, methodArgs);
                if (!result.wasExceptionRaised()) {
                    return result.getResult();
                }
                logger.logDebug("RESULT error happened: " + result.getMetadata());
                throw new APICallException(result);
            }
        }, true);
    });
}
Also used : HttpServerRequest(io.vertx.core.http.HttpServerRequest) JsonObject(io.vertx.core.json.JsonObject) APIMethodResult(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) Route(io.vertx.ext.web.Route) UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) MissingInstanceException(com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException) Date(java.util.Date)

Example 28 with Route

use of io.vertx.ext.web.Route in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method defineGetInstanceRoute.

private void defineGetInstanceRoute() {
    Route route = router.route(HttpMethod.GET, "/instances/:" + PARAM_INSTANCE).produces(APPLICATION_JSON);
    route.blockingHandler(routingContext -> {
        String instanceId = routingContext.request().getParam(PARAM_INSTANCE);
        execute(200, routingContext, new ServerAction() {

            @Override
            public Object execute() throws Exception {
                return engine.fetchInstance(instanceId).toInstance();
            }
        }, false);
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Route(io.vertx.ext.web.Route) UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) MissingInstanceException(com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException)

Example 29 with Route

use of io.vertx.ext.web.Route in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method defineListModuleInstancesRoute.

private void defineListModuleInstancesRoute() {
    Route route = router.route(HttpMethod.GET, "/modules/:" + PARAM_MODULE + "/instances").produces(APPLICATION_JSON);
    route.blockingHandler(routingContext -> {
        String moduleId = routingContext.request().getParam(PARAM_MODULE);
        execute(200, routingContext, new ServerAction() {

            @Override
            public Object execute() throws Exception {
                return engine.listModuleInstances(moduleId).stream().map(m -> m.toInstance()).collect(Collectors.toList());
            }
        }, false);
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) Route(io.vertx.ext.web.Route) UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) MissingInstanceException(com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException)

Example 30 with Route

use of io.vertx.ext.web.Route in project VX-API-Gateway by EliMirren.

the class VxApiApplication method addRouteToRouter.

/**
 * 通用给Router添加route
 *
 * @param api
 *          配置信息
 * @param router
 *          要添加的router
 * @param routeMaps
 *          要添加的route集合
 * @param result
 *          结果
 */
public void addRouteToRouter(VxApis api, Router router, Map<String, List<Route>> routeMaps, Handler<AsyncResult<Boolean>> result) {
    vertx.executeBlocking(fut -> {
        // 存储部署的路由
        List<Route> routes = new ArrayList<>();
        // 流量限制处理器
        if (api.getLimitUnit() != null) {
            // 权限认证的route;
            Route limitRoute = router.route();
            initApiLimit(api, limitRoute);
            routes.add(limitRoute);
        }
        // 入口参数检查
        if (api.getEnterParam() != null) {
            // 权限认证的route;
            Route checkRoute = router.route();
            initParamCheck(api, checkRoute);
            routes.add(checkRoute);
        }
        // 认证处理器
        if (api.getAuthOptions() != null) {
            // 权限认证的route;
            Route authRoute = router.route();
            try {
                initAuthHandler(api, authRoute);
                routes.add(authRoute);
            } catch (Exception e) {
                authRoute.remove();
                // 清空已经成功的路由
                routes.forEach(r -> r.remove());
                LOG.error("添加权限认证-->失败:" + e);
                fut.fail(e);
                return;
            }
        }
        // 前置处理器
        if (api.getBeforeHandlerOptions() != null) {
            // 前置处理器的route;
            Route beforeRoute = router.route();
            try {
                initBeforeHandler(api, beforeRoute);
                routes.add(beforeRoute);
            } catch (Exception e) {
                LOG.error("添加前置处理器-->失败:" + e);
                beforeRoute.remove();
                // 清空已经成功的路由
                routes.forEach(r -> r.remove());
                fut.fail(e);
                return;
            }
        }
        // 检查是否有后置处理器,有next给后置处理器,如果没有则response
        boolean isAfterHandler = api.getAfterHandlerOptions() != null;
        // 添加与后台交互的中心处理器
        Route serverRoute = router.route();
        try {
            initServerHandler(isAfterHandler, api, serverRoute);
            routes.add(serverRoute);
        } catch (Exception e) {
            LOG.error("添加服务处理器-->失败:" + e);
            serverRoute.remove();
            // 清空已经成功的路由
            routes.forEach(r -> r.remove());
            fut.fail(e);
            return;
        }
        // 后置处理器
        if (isAfterHandler) {
            // 前置处理器的route;
            Route afterRoute = router.route();
            try {
                initAfterHandler(api, afterRoute);
                routes.add(afterRoute);
            } catch (Exception e) {
                LOG.error("添加后置处理器-->失败:" + e);
                afterRoute.remove();
                // 清空已经成功的路由
                routes.forEach(r -> r.remove());
                fut.fail(e);
                return;
            }
        }
        // 添加异常处理器
        Route exRoute = router.route();
        initExceptionHanlder(api, exRoute);
        routes.add(exRoute);
        routeMaps.put(api.getApiName(), routes);
        fut.complete();
        if (LOG.isDebugEnabled()) {
            LOG.debug(appName + ": 服务器创建API成功");
        }
    }, result);
}
Also used : VxApiServerOptions(com.szmirren.vxApi.core.options.VxApiServerOptions) VxApiBeforeHandler(com.szmirren.vxApi.spi.handler.VxApiBeforeHandler) HttpMethodEnum(com.szmirren.vxApi.core.enums.HttpMethodEnum) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) BodyHandler(io.vertx.ext.web.handler.BodyHandler) VxApiEventBusAddressConstant(com.szmirren.vxApi.core.common.VxApiEventBusAddressConstant) ApiServerTypeEnum(com.szmirren.vxApi.core.enums.ApiServerTypeEnum) VxApiAuthFactory(com.szmirren.vxApi.spi.auth.VxApiAuthFactory) PemKeyCertOptions(io.vertx.core.net.PemKeyCertOptions) Map(java.util.Map) VxApiAfterHandler(com.szmirren.vxApi.spi.handler.VxApiAfterHandler) VxApiAfterHandlerFactory(com.szmirren.vxApi.spi.handler.VxApiAfterHandlerFactory) JsonObject(io.vertx.core.json.JsonObject) CookieHandler(io.vertx.ext.web.handler.CookieHandler) VxApiApplicationOptions(com.szmirren.vxApi.core.options.VxApiApplicationOptions) ClusteredSessionStore(io.vertx.ext.web.sstore.ClusteredSessionStore) VxApiCustomHandler(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandler) Set(java.util.Set) Message(io.vertx.core.eventbus.Message) VxApiTrackInfos(com.szmirren.vxApi.core.entity.VxApiTrackInfos) Future(io.vertx.core.Future) VxApisDTO(com.szmirren.vxApi.core.options.VxApisDTO) VxApis(com.szmirren.vxApi.core.entity.VxApis) List(java.util.List) VxApiGatewayAttribute(com.szmirren.vxApi.core.common.VxApiGatewayAttribute) VxApiAuth(com.szmirren.vxApi.spi.auth.VxApiAuth) Logger(org.apache.logging.log4j.Logger) VxApiAfterHandlerOptions(com.szmirren.vxApi.spi.handler.VxApiAfterHandlerOptions) StrUtil(com.szmirren.vxApi.core.common.StrUtil) SessionStore(io.vertx.ext.web.sstore.SessionStore) HttpServerResponse(io.vertx.core.http.HttpServerResponse) AbstractVerticle(io.vertx.core.AbstractVerticle) VxApiApplicationDTO(com.szmirren.vxApi.core.options.VxApiApplicationDTO) VxApiCustomHandlerOptions(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandlerOptions) HttpClient(io.vertx.core.http.HttpClient) VxApiCustomHandlerFactory(com.szmirren.vxApi.spi.customHandler.VxApiCustomHandlerFactory) VxApiRouteHandlerApiLimit(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerApiLimit) LocalSessionStore(io.vertx.ext.web.sstore.LocalSessionStore) VxApiRouteHandlerRedirectType(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerRedirectType) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) PfxOptions(io.vertx.core.net.PfxOptions) LinkedHashMap(java.util.LinkedHashMap) CompositeFuture(io.vertx.core.CompositeFuture) VxApiBeforeHandlerOptions(com.szmirren.vxApi.spi.handler.VxApiBeforeHandlerOptions) SessionHandler(io.vertx.ext.web.handler.SessionHandler) VxApiRouteHandlerParamCheck(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerParamCheck) VxApiCertOptions(com.szmirren.vxApi.core.options.VxApiCertOptions) AsyncResult(io.vertx.core.AsyncResult) LinkedHashSet(java.util.LinkedHashSet) Route(io.vertx.ext.web.Route) VxApiCorsOptions(com.szmirren.vxApi.core.options.VxApiCorsOptions) MalformedURLException(java.net.MalformedURLException) VxApiAuthOptions(com.szmirren.vxApi.spi.auth.VxApiAuthOptions) JsonArray(io.vertx.core.json.JsonArray) VxApiBeforeHandlerFactory(com.szmirren.vxApi.spi.handler.VxApiBeforeHandlerFactory) HttpMethod(io.vertx.core.http.HttpMethod) VxApiRouteHandlerHttpService(com.szmirren.vxApi.core.handler.route.VxApiRouteHandlerHttpService) Handler(io.vertx.core.Handler) LogManager(org.apache.logging.log4j.LogManager) CorsHandler(io.vertx.ext.web.handler.CorsHandler) ArrayList(java.util.ArrayList) Route(io.vertx.ext.web.Route) MalformedURLException(java.net.MalformedURLException)

Aggregations

Route (io.vertx.ext.web.Route)30 JsonObject (io.vertx.core.json.JsonObject)17 ServerCacheException (com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException)14 UnknownAPIException (com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException)14 APICallException (com.arm.mbed.cloud.sdk.testutils.APICallException)14 MissingInstanceException (com.arm.mbed.cloud.sdk.testserver.cache.MissingInstanceException)13 RoutingContext (io.vertx.ext.web.RoutingContext)10 Router (io.vertx.ext.web.Router)7 HttpMethod (io.vertx.core.http.HttpMethod)4 AbstractVerticle (io.vertx.core.AbstractVerticle)3 Handler (io.vertx.core.Handler)3 HttpServerResponse (io.vertx.core.http.HttpServerResponse)3 TemplateHandler (io.vertx.ext.web.handler.TemplateHandler)3 TemplateHandlerImpl (io.vertx.ext.web.handler.impl.TemplateHandlerImpl)3 TemplateEngine (io.vertx.ext.web.templ.TemplateEngine)3 Date (java.util.Date)3 Map (java.util.Map)3 Set (java.util.Set)3 Test (org.junit.Test)3 APIMethodResult (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)2