org.computate.smartvillage.enus.model.iotnode.IotNodeEnUSGenApiServiceVertxProxyHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smartabyar-smartvillage Show documentation
Show all versions of smartabyar-smartvillage Show documentation
Red Hat Global Social Innovation Program is partnering with Boston University and Smarta Byar in order to collaborate on creating a global and open research platform allowing researchers to study what social sustainability means by using a digital twin of Veberöd, Sweden as the test village, supported by Smarta Byar. The goal of this collaboration is to build the open source technological infrastructure so that researchers can collaborate on this platform effectively to study mobility, public health, sustainability among other study areas to ultimately help define better the link between well being and eco-smart cities.
The newest version!
/*
* Copyright 2014 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.computate.smartvillage.enus.model.iotnode;
import org.computate.smartvillage.enus.model.iotnode.IotNodeEnUSGenApiService;
import io.vertx.core.Vertx;
import io.vertx.core.Handler;
import io.vertx.core.AsyncResult;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.core.eventbus.MessageConsumer;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.eventbus.ReplyException;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import io.vertx.serviceproxy.ProxyHandler;
import io.vertx.serviceproxy.ServiceException;
import io.vertx.serviceproxy.ServiceExceptionMessageCodec;
import io.vertx.serviceproxy.HelperUtils;
import io.vertx.serviceproxy.ServiceBinder;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.templ.handlebars.HandlebarsTemplateEngine;
import io.vertx.core.Vertx;
import io.vertx.pgclient.PgPool;
import io.vertx.kafka.client.producer.KafkaProducer;
import io.vertx.ext.web.api.service.ServiceRequest;
import io.vertx.core.WorkerExecutor;
import io.vertx.core.eventbus.EventBus;
import io.vertx.ext.web.api.service.ServiceResponse;
import io.vertx.ext.auth.oauth2.OAuth2Auth;
import io.vertx.ext.auth.authorization.AuthorizationProvider;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import java.util.Optional;
/*
Generated Proxy code - DO NOT EDIT
@author Roger the Robot
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public class IotNodeEnUSGenApiServiceVertxProxyHandler extends ProxyHandler {
public static final long DEFAULT_CONNECTION_TIMEOUT = 5 * 60; // 5 minutes
private final Vertx vertx;
private final IotNodeEnUSGenApiService service;
private final long timerID;
private long lastAccessed;
private final long timeoutSeconds;
private final boolean includeDebugInfo;
public IotNodeEnUSGenApiServiceVertxProxyHandler(Vertx vertx, IotNodeEnUSGenApiService service){
this(vertx, service, DEFAULT_CONNECTION_TIMEOUT);
}
public IotNodeEnUSGenApiServiceVertxProxyHandler(Vertx vertx, IotNodeEnUSGenApiService service, long timeoutInSecond){
this(vertx, service, true, timeoutInSecond);
}
public IotNodeEnUSGenApiServiceVertxProxyHandler(Vertx vertx, IotNodeEnUSGenApiService service, boolean topLevel, long timeoutInSecond){
this(vertx, service, true, timeoutInSecond, false);
}
public IotNodeEnUSGenApiServiceVertxProxyHandler(Vertx vertx, IotNodeEnUSGenApiService service, boolean topLevel, long timeoutSeconds, boolean includeDebugInfo) {
this.vertx = vertx;
this.service = service;
this.includeDebugInfo = includeDebugInfo;
this.timeoutSeconds = timeoutSeconds;
try {
this.vertx.eventBus().registerDefaultCodec(ServiceException.class,
new ServiceExceptionMessageCodec());
} catch (IllegalStateException ex) {}
if (timeoutSeconds != -1 && !topLevel) {
long period = timeoutSeconds * 1000 / 2;
if (period > 10000) {
period = 10000;
}
this.timerID = vertx.setPeriodic(period, this::checkTimedOut);
} else {
this.timerID = -1;
}
accessed();
}
private void checkTimedOut(long id) {
long now = System.nanoTime();
if (now - lastAccessed > timeoutSeconds * 1000000000) {
close();
}
}
@Override
public void close() {
if (timerID != -1) {
vertx.cancelTimer(timerID);
}
super.close();
}
private void accessed() {
this.lastAccessed = System.nanoTime();
}
public void handle(Message msg) {
try{
JsonObject json = msg.body();
String action = msg.headers().get("action");
if (action == null) throw new IllegalStateException("action not specified");
accessed();
switch (action) {
case "searchIotNode": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.searchIotNode(
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "getIotNode": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.getIotNode(
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "patchIotNode": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.patchIotNode(
(io.vertx.core.json.JsonObject)searchInJson(params, "body"),
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "patchIotNodeFuture": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.patchIotNodeFuture(
(io.vertx.core.json.JsonObject)searchInJson(params, "body"),
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "postIotNode": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.postIotNode(
(io.vertx.core.json.JsonObject)searchInJson(params, "body"),
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "postIotNodeFuture": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.postIotNodeFuture(
(io.vertx.core.json.JsonObject)searchInJson(params, "body"),
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "putimportIotNode": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.putimportIotNode(
(io.vertx.core.json.JsonObject)searchInJson(params, "body"),
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "putimportIotNodeFuture": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.putimportIotNodeFuture(
(io.vertx.core.json.JsonObject)searchInJson(params, "body"),
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "searchpageIotNodeId": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.searchpageIotNodeId(
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
case "searchpageIotNode": {
JsonObject contextSerialized = json.getJsonObject("context");
if (contextSerialized == null)
throw new IllegalStateException("Received action " + action + " without ServiceRequest \"context\"");
ServiceRequest context = new ServiceRequest(contextSerialized);
JsonObject params = context.getParams();
try {
service.searchpageIotNode(
context,
res -> {
if (res.failed()) {
if (res.cause() instanceof ServiceException) {
msg.reply(res.cause());
} else {
msg.reply(new ServiceException(-1, res.cause().getMessage()));
}
} else {
msg.reply(res.result() == null ? null : res.result().toJson());
}
}
);
} catch (Exception e) {
HelperUtils.manageFailure(msg, e, includeDebugInfo);
}
break;
}
default: throw new IllegalStateException("Invalid action: " + action);
}
} catch (Throwable t) {
if (includeDebugInfo) msg.reply(new ServiceException(500, t.getMessage(), HelperUtils.generateDebugInfo(t)));
else msg.reply(new ServiceException(500, t.getMessage()));
throw t;
}
}
public static Object searchInJson(JsonObject obj, String key) {
if ("body".equals(key)) return obj.getValue("body");
if (obj.getJsonObject("path").containsKey(key)) return obj.getJsonObject("path").getValue(key);
if (obj.getJsonObject("query").containsKey(key)) return obj.getJsonObject("query").getValue(key);
if (obj.getJsonObject("header").containsKey(key)) return obj.getJsonObject("header").getValue(key);
if (obj.getJsonObject("cookie").containsKey(key)) return obj.getJsonObject("cookie").getValue(key);
if (obj.getJsonObject("body").containsKey(key)) return obj.getJsonObject("body").getValue(key);
return null;
}
public static Optional