use of com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException 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 com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException in project mbed-cloud-sdk-java by ARMmbed.
the class TestServer method execute.
private void execute(int successfulStatusCode, RoutingContext ctx, ServerAction action, boolean enclosePayload) {
try {
Object result = action.execute();
result(successfulStatusCode, ctx, result, enclosePayload);
} catch (UnknownAPIException | MissingInstanceException e) {
ErrorMessage message = generateErrorMessage(e);
error(404, ctx, message);
} catch (Exception e) {
ErrorMessage message = generateErrorMessage(e);
error(500, ctx, message);
}
}
use of com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException in project mbed-cloud-sdk-java by ARMmbed.
the class Engine method stopInstanceDaemons.
private void stopInstanceDaemons(String instanceId) throws ServerCacheException, MissingInstanceException {
APIModule module = cache.fetchModuleFromInstance(instanceId);
if (module != null) {
if (module.hasDaemonControlMethods()) {
ModuleInstance instance = retrieveInstance(instanceId);
APICaller caller = createAnApiCaller();
logger.logInfo("Stopping SDK module instance [" + instanceId + "] daemon threads");
List<APIMethod> stoppingMethods = module.getStopDaemonMethods();
if (stoppingMethods != null) {
stoppingMethods.forEach(m -> {
try {
caller.callAPIOnModuleInstance(instance, m.getName(), null);
} catch (UnknownAPIException | APICallException e) {
e.printStackTrace();
}
});
}
logger.logInfo("Shutting down SDK module instance [" + instanceId + "] daemon executor service");
List<APIMethod> shuttingdownMethods = module.getShutdownDaemonMethods();
if (shuttingdownMethods != null) {
shuttingdownMethods.forEach(m -> {
try {
caller.callAPIOnModuleInstance(instance, m.getName(), null);
} catch (UnknownAPIException | APICallException e) {
e.printStackTrace();
}
});
}
}
}
}
Aggregations