Search in sources :

Example 1 with ServerCacheException

use of com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method defineModuleMethodTestRoute.

// TODO Remove when not needed anymore
@SuppressWarnings("boxing")
private void defineModuleMethodTestRoute() {
    Route route = router.route(HttpMethod.GET, "/:" + PARAM_MODULE + "/:" + PARAM_METHOD + "*").produces(APPLICATION_JSON);
    route.blockingHandler(routingContext -> {
        HttpServerRequest request = routingContext.request();
        String module = request.getParam(PARAM_MODULE);
        String method = request.getParam(PARAM_METHOD);
        Map<String, Object> params = retrieveQueryParameters(request);
        logger.logInfo("TEST http://localhost:" + String.valueOf(port) + request.uri() + " AT " + new Date().toString());
        ModuleInstance instance = null;
        try {
            instance = engine.createInstance(module, defaultConnectionConfiguration);
            APIMethodResult result = engine.callAPIOnInstance(instance.getId(), method, params);
            if (!result.wasExceptionRaised()) {
                String resultJson = Serializer.convertLegacyResultToJson(result.getResult());
                logger.logDebug("RESULT: " + String.valueOf(resultJson));
                engine.deleteInstance(instance.getId());
                respond(200, routingContext, resultJson);
            } else {
                engine.deleteInstance(instance.getId());
                logger.logDebug("RESULT error happened: " + result.getMetadata());
                if (result.getMetadata() == null) {
                    sendError(setResponse(500, routingContext), null, (result.getException().getMessage() == null) ? "Exception of type " + result.getException() + " was raised" : result.getException().getMessage());
                } else {
                    sendError(setResponse(500, routingContext), result.getMetadata().getStatusCode(), "An error occurred during call. Call metadata: " + result.getMetadata().toString());
                }
            }
        } catch (UnknownAPIException | APICallException | ServerCacheException e) {
            if (instance != null) {
                try {
                    engine.deleteInstance(instance.getId());
                } catch (ServerCacheException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            sendError(setResponse(500, routingContext), null, (e.getMessage() == null) ? "Exception of type " + e + " was raised" : e.getMessage());
        }
    });
}
Also used : UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) 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) ModuleInstance(com.arm.mbed.cloud.sdk.testserver.internal.model.ModuleInstance) Route(io.vertx.ext.web.Route) Date(java.util.Date) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException)

Example 2 with ServerCacheException

use of com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException in project mbed-cloud-sdk-java by ARMmbed.

the class TestServer method start.

public void start() {
    if (server == null) {
        Vertx vertx = Vertx.vertx(new VertxOptions().setWorkerPoolSize(40).setBlockedThreadCheckInterval(1000L * 60L * 10L).setMaxWorkerExecuteTime(1000L * 1000L * 1000L * 60L * 10L));
        HttpServerOptions options = new HttpServerOptions();
        options.setMaxInitialLineLength(HttpServerOptions.DEFAULT_MAX_INITIAL_LINE_LENGTH * 2);
        server = vertx.createHttpServer(options);
        router = Router.router(vertx);
        engine = new Engine(logger, new InstanceCache(vertx));
    }
    retrieveConfig();
    // Route registration
    router.route().handler(BodyHandler.create());
    defineInitialisationRoute();
    defineHelloRoute();
    definePingRoute();
    defineResetRoute();
    defineShutdownRoute();
    defineListModulesRoute();
    defineListModuleInstancesRoute();
    defineCreateModuleInstanceRoute();
    defineListInstancesRoute();
    defineGetInstanceRoute();
    defineDeleteInstanceRoute();
    defineListInstanceMethodsRoute();
    defineRunInstanceMethodRoute();
    defineModuleMethodTestRoute();
    logWelcomeMessage();
    try {
        engine.initialise();
    } catch (ServerCacheException e) {
        e.printStackTrace();
    }
    logger.logInfo("Java SDK test server listening on port " + String.valueOf(port) + "...");
    server.requestHandler(router::accept).listen(port);
}
Also used : HttpServerOptions(io.vertx.core.http.HttpServerOptions) InstanceCache(com.arm.mbed.cloud.sdk.testserver.cache.InstanceCache) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) Engine(com.arm.mbed.cloud.sdk.testserver.Engine) ServerCacheException(com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException)

Aggregations

ServerCacheException (com.arm.mbed.cloud.sdk.testserver.cache.ServerCacheException)2 Engine (com.arm.mbed.cloud.sdk.testserver.Engine)1 InstanceCache (com.arm.mbed.cloud.sdk.testserver.cache.InstanceCache)1 APIMethodResult (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)1 ModuleInstance (com.arm.mbed.cloud.sdk.testserver.internal.model.ModuleInstance)1 UnknownAPIException (com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException)1 APICallException (com.arm.mbed.cloud.sdk.testutils.APICallException)1 Vertx (io.vertx.core.Vertx)1 VertxOptions (io.vertx.core.VertxOptions)1 HttpServerOptions (io.vertx.core.http.HttpServerOptions)1 HttpServerRequest (io.vertx.core.http.HttpServerRequest)1 JsonObject (io.vertx.core.json.JsonObject)1 Route (io.vertx.ext.web.Route)1 Date (java.util.Date)1