|
@@ -6,6 +6,7 @@ import cc.iotkit.comp.IComponent;
|
|
|
import io.vertx.core.MultiMap;
|
|
|
import io.vertx.core.Vertx;
|
|
|
import io.vertx.core.http.HttpServer;
|
|
|
+import io.vertx.core.http.HttpServerRequest;
|
|
|
import io.vertx.core.http.HttpServerResponse;
|
|
|
import io.vertx.core.json.JsonObject;
|
|
|
import io.vertx.ext.web.Router;
|
|
@@ -14,16 +15,21 @@ import jdk.nashorn.api.scripting.NashornScriptEngine;
|
|
|
import jdk.nashorn.api.scripting.ScriptObjectMirror;
|
|
|
import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
|
|
|
import javax.script.ScriptEngineManager;
|
|
|
import javax.script.ScriptException;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
-@Slf4j
|
|
|
+import static java.nio.charset.StandardCharsets.UTF_8;
|
|
|
+
|
|
|
@Data
|
|
|
+@Slf4j
|
|
|
public class HttpBizComponent implements IComponent {
|
|
|
|
|
|
private final Vertx vertx = Vertx.vertx();
|
|
@@ -32,7 +38,9 @@ public class HttpBizComponent implements IComponent {
|
|
|
|
|
|
private Object scriptObj;
|
|
|
|
|
|
- private HttpConfig config;
|
|
|
+ private CompConfig config;
|
|
|
+
|
|
|
+ private HttpConfig httpConfig;
|
|
|
|
|
|
private String script;
|
|
|
|
|
@@ -40,7 +48,7 @@ public class HttpBizComponent implements IComponent {
|
|
|
|
|
|
@Override
|
|
|
public void create(CompConfig config) {
|
|
|
- this.config = JsonUtil.parse(config.getOther(), HttpConfig.class);
|
|
|
+ this.httpConfig = JsonUtil.parse(config.getOther(), HttpConfig.class);
|
|
|
try {
|
|
|
scriptObj = engine.eval(String.format("new (function () {\n%s})()", script));
|
|
|
} catch (ScriptException e) {
|
|
@@ -54,53 +62,75 @@ public class HttpBizComponent implements IComponent {
|
|
|
Router backendRouter = Router.router(vertx);
|
|
|
backendRouter.route().handler(BodyHandler.create())
|
|
|
.handler(rc -> {
|
|
|
- Map<String, Object> httpHeader = getData(rc.request().headers());
|
|
|
- log.info("request header:{}", JsonUtil.toJsonString(httpHeader));
|
|
|
- Map<String, List<Object>> httpParams = getListData(rc.request().params());
|
|
|
- log.info("request params:{}", JsonUtil.toJsonString(httpParams));
|
|
|
-
|
|
|
- String contentType = rc.request().headers().get("Content-Type");
|
|
|
- JsonObject responseHeader = new JsonObject();
|
|
|
- if ("application/json".equals(contentType)) {
|
|
|
- String bodyStr = rc.toString();
|
|
|
- Map body = JsonUtil.parse(bodyStr, Map.class);
|
|
|
- log.info("request body:{}", bodyStr);
|
|
|
-
|
|
|
- String response = "unknown error";
|
|
|
- String name = "onReceive";
|
|
|
- if (((ScriptObjectMirror) scriptObj).get(name) != null) {
|
|
|
- try {
|
|
|
- Object result = engine.invokeMethod(scriptObj, name, body);
|
|
|
- Object resultObj = JsonUtil.toObject((ScriptObjectMirror) result);
|
|
|
- if (resultObj instanceof Map) {
|
|
|
- JsonObject data = JsonObject.mapFrom(resultObj);
|
|
|
- responseHeader = data.getJsonObject("header");
|
|
|
- response = data.getString("content");
|
|
|
+ try {
|
|
|
+ Map<String, Object> httpHeader = getData(rc.request().headers());
|
|
|
+ log.info("request header:{}", JsonUtil.toJsonString(httpHeader));
|
|
|
+ Map<String, List<Object>> httpParams = getListData(rc.request().params());
|
|
|
+ log.info("request params:{}", JsonUtil.toJsonString(httpParams));
|
|
|
+
|
|
|
+ HttpServerRequest httpRequest = rc.request();
|
|
|
+ String contentType = httpRequest.headers().get("Content-Type");
|
|
|
+ JsonObject responseHeader = new JsonObject();
|
|
|
+ if ("application/json".equals(contentType)) {
|
|
|
+ String bodyStr = rc.getBody().toString();
|
|
|
+ Map body = JsonUtil.parse(bodyStr, Map.class);
|
|
|
+ log.info("request body:{}", bodyStr);
|
|
|
+
|
|
|
+ String response = "unknown error";
|
|
|
+ String name = "onReceive";
|
|
|
+ if (((ScriptObjectMirror) scriptObj).get(name) != null) {
|
|
|
+ try {
|
|
|
+ Object result = engine.invokeMethod(scriptObj,
|
|
|
+ name,
|
|
|
+ httpRequest.method().name(),
|
|
|
+ httpRequest.path(),
|
|
|
+ httpHeader,
|
|
|
+ httpParams,
|
|
|
+ body);
|
|
|
+ Object resultObj = JsonUtil.toObject((ScriptObjectMirror) result);
|
|
|
+ if (resultObj instanceof Map) {
|
|
|
+ JsonObject data = JsonObject.mapFrom(resultObj);
|
|
|
+ responseHeader = data.getJsonObject("header");
|
|
|
+ response = data.getString("content");
|
|
|
+ response = response == null ? "" : response;
|
|
|
+ }
|
|
|
+ } catch (Throwable e) {
|
|
|
+ log.error("invokeMethod onReceive error", e);
|
|
|
+ response = e.getMessage();
|
|
|
}
|
|
|
- } catch (Throwable e) {
|
|
|
- log.error("invokeMethod onReceive error", e);
|
|
|
- response = e.getMessage();
|
|
|
+ } else {
|
|
|
+ log.error("required [onReceive] method");
|
|
|
}
|
|
|
+
|
|
|
+ HttpServerResponse httpServerResponse = rc.response();
|
|
|
+ //设置响应头
|
|
|
+ responseHeader.getMap().forEach((key, value) -> {
|
|
|
+ //大写转换
|
|
|
+ key = key.replaceAll("([A-Z])", "-$1").toLowerCase();
|
|
|
+ httpServerResponse.putHeader(key, value.toString());
|
|
|
+ });
|
|
|
+
|
|
|
+ log.info("response,header:{},content:{}", responseHeader, response);
|
|
|
+ //设置响应内容
|
|
|
+ httpServerResponse
|
|
|
+ .end(response);
|
|
|
} else {
|
|
|
- log.error("required [onReceive] method");
|
|
|
+ rc.response().end("");
|
|
|
}
|
|
|
-
|
|
|
- HttpServerResponse httpServerResponse = rc.response();
|
|
|
- //设置响应头
|
|
|
- responseHeader.getMap().forEach((key, value) -> {
|
|
|
- //大写转换
|
|
|
- key = key.replaceAll("([A-Z])", "-$1").toLowerCase();
|
|
|
- httpServerResponse.putHeader(key, value.toString());
|
|
|
- });
|
|
|
-
|
|
|
- log.info("response,header:{},content:{}", responseHeader, response);
|
|
|
- //设置响应内容
|
|
|
- httpServerResponse
|
|
|
- .end(response);
|
|
|
+ } catch (Throwable e) {
|
|
|
+ log.error("handle request error", e);
|
|
|
+ rc.response().end("server error:" + e.getMessage());
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- backendServer.requestHandler(backendRouter).listen(config.getPort());
|
|
|
+ backendServer.requestHandler(backendRouter)
|
|
|
+ .listen(httpConfig.getPort(), (http) -> {
|
|
|
+ if (http.succeeded()) {
|
|
|
+ log.info("http server create succeed,port:{}", httpConfig.getPort());
|
|
|
+ } else {
|
|
|
+ log.error("http server create failed", http.cause());
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -131,4 +161,11 @@ public class HttpBizComponent implements IComponent {
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ HttpBizComponent component = new HttpBizComponent();
|
|
|
+ component.setScript(FileUtils.readFileToString(new File("/Users/sjg/home/gitee/open-source/iotkit-parent/protocol-gateway/http-biz-component/src/main/resources/component.js"), UTF_8));
|
|
|
+ component.create(new CompConfig(1000, "{\"port\":9081}"));
|
|
|
+ component.start();
|
|
|
+ }
|
|
|
+
|
|
|
}
|