use of io.vertx.ext.web.Route in project mbed-cloud-sdk-java by ARMmbed.
the class TestServer method defineListModulesRoute.
private void defineListModulesRoute() {
Route route = router.route(HttpMethod.GET, "/modules").produces(APPLICATION_JSON);
route.blockingHandler(routingContext -> {
execute(200, routingContext, new ServerAction() {
@Override
public Object execute() throws Exception {
return engine.listModules().stream().map(m -> ApiUtils.convertCamelToSnake(m)).collect(Collectors.toList());
}
}, false);
});
}
use of io.vertx.ext.web.Route in project mbed-cloud-sdk-java by ARMmbed.
the class TestServer method defineCreateModuleInstanceRoute.
private void defineCreateModuleInstanceRoute() {
Route route = router.route(HttpMethod.POST, "/modules/:" + PARAM_MODULE + "/instances").produces(APPLICATION_JSON);
route.blockingHandler(routingContext -> {
String moduleId = routingContext.request().getParam(PARAM_MODULE);
ConnectionOptions opts = retrieveConnectionOptions(routingContext.getBodyAsString());
execute(200, routingContext, new ServerAction() {
@Override
public Object execute() throws Exception {
return engine.createInstance(moduleId, opts).toInstance();
}
}, false);
});
}
use of io.vertx.ext.web.Route 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());
}
});
}
use of io.vertx.ext.web.Route in project mbed-cloud-sdk-java by ARMmbed.
the class TestServer method defineShutdownRoute.
private void defineShutdownRoute() {
Route route = router.route(HttpMethod.POST, "/shutdown").produces(APPLICATION_JSON);
route.blockingHandler(routingContext -> {
execute(202, routingContext, new ServerAction() {
@Override
public Object execute() throws Exception {
engine.shutdown();
return null;
}
}, false);
System.exit(0);
});
}
use of io.vertx.ext.web.Route in project mbed-cloud-sdk-java by ARMmbed.
the class TestServer method defineDeleteInstanceRoute.
private void defineDeleteInstanceRoute() {
Route route = router.route(HttpMethod.DELETE, "/instances/:" + PARAM_INSTANCE).produces(APPLICATION_JSON);
route.blockingHandler(routingContext -> {
String instanceId = fetchInstanceId(routingContext.request().getParam(PARAM_INSTANCE));
execute(200, routingContext, new ServerAction() {
@SuppressWarnings("boxing")
@Override
public Object execute() throws Exception {
return engine.deleteInstance(instanceId);
}
}, false);
});
}
Aggregations