diff --git a/build.gradle b/build.gradle index 8082b839e31c08fa050cda22bac1c3de5cadd68b..264a9d0d5f2b16f20133bffed1a659ba603b5a76 100644 --- a/build.gradle +++ b/build.gradle @@ -52,6 +52,7 @@ dependencies { implementation 'org.testng:testng:7.1.0' implementation 'org.bdware.doip:doip-audit-tool:1.4.2' implementation 'org.bdware.doip:doip-sdk:1.5.1' + implementation 'org.neo4j.driver:neo4j-java-driver:4.4.1' testImplementation 'junit:junit:4.13.2' } @@ -131,4 +132,10 @@ task deployLocalDocker(dependsOn: ["buildYPK"]) { task putPackage(type: Exec, dependsOn: ["buildYPK"]) { workingDir "./build" commandLine "curl", "--user", "${REPO_USRNAME}:${REPO_PASSWORD}", "--upload-file", "./Registry-${currVersion}.ypk", "${REPO_HOST}/Registry/${currVersion}/Registry-${currVersion}.ypk" +} + +tasks.withType(JavaCompile) { + options.compilerArgs << '-Xlint:none' + options.compilerArgs << '-Xlint:deprecation' << "-Werror" + options.encoding = "UTF-8" } \ No newline at end of file diff --git a/src/main/java/org/bdware/datanet/registry/Controller.java b/src/main/java/org/bdware/datanet/registry/Controller.java index f673eb13c264c2b195708354ef3ed8472465014a..9f0ce4787e9c1799cbab6be2a8ab130afa045de9 100644 --- a/src/main/java/org/bdware/datanet/registry/Controller.java +++ b/src/main/java/org/bdware/datanet/registry/Controller.java @@ -1,10 +1,7 @@ package org.bdware.datanet.registry; import com.google.common.reflect.TypeToken; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import com.google.gson.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bdware.doip.audit.AuditDoaClient; @@ -35,6 +32,8 @@ import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.util.*; +import org.neo4j.driver.*; + public class Controller implements RepositoryHandler, RegistryHandler, TopicHandler { private final AuditIrpClient auditIrpClient; @@ -267,27 +266,70 @@ public class Controller implements RepositoryHandler, RegistryHandler, TopicHand return metaIndex.search(entries, offset, count); } + // 对neo4j的搜索调用 + public JsonObject handleSearchByNeo4j(DoipMessage req) { + JsonObject ret = new JsonObject(); + String context = req.header.parameters.attributes.get("id").getAsString(); + Driver driver = GraphDatabase.driver("neo4j://47.93.156.31:21087", AuthTokens.basic("neo4j", "password")); + try (Session session = driver.session()){ + String query = "MATCH (n {context: $context})- [r] - (connected) " + + "RETURN n.context AS context, type(r) AS relationship, connected.context AS connectedContext"; + Map params = new HashMap<>(); + params.put("context", context); + Result result = session.run(query, params); + + Gson gson = new Gson(); + JsonArray jsonArray = new JsonArray(); + while (result.hasNext()) { + Record record = result.next(); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("context", record.get("context").asString()); + jsonObject.addProperty("relationship", record.get("relationship").asString()); + jsonObject.addProperty("connectedContext", record.get("connectedContext").asString()); + jsonArray.add(jsonObject); + } + + ret.addProperty("code", 0); + ret.add("data", JsonParser.parseString(gson.toJson(jsonArray))); + return ret; + } catch (Exception e) { + ret.addProperty("code", 1); + ret.addProperty("message", e.getMessage()); + } finally { + return ret; + } + } + @Override public DoipMessage handleSearch(DoipMessage req) { try { - JsonArray jsonArray = req.header.parameters.attributes.get("searchMode").getAsJsonArray(); - int offset = req.header.parameters.attributes.get("offset").getAsInt(); - int count = req.header.parameters.attributes.get("count").getAsInt(); - //获取传进来的参数 - Iterator iterator = jsonArray.iterator(); - List entries = new ArrayList<>(); - while (iterator.hasNext()) { - JsonElement element = iterator.next(); - JsonObject jo = element.getAsJsonObject(); - entries.add(new MetaIndex.SearchEntry(jo.get("key").getAsString(), jo.get("value").getAsString(), jo.get("type").getAsString())); + if(req.header.parameters.attributes.get("isNeo4j").getAsBoolean()) { + JsonObject ret = handleSearchByNeo4j(req); + DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + builder.createResponse(DoipResponseCode.Success, req); + builder.setBody(ret.toString().getBytes(StandardCharsets.UTF_8)); + return builder.create(); + } + else { + JsonArray jsonArray = req.header.parameters.attributes.get("searchMode").getAsJsonArray(); + int offset = req.header.parameters.attributes.get("offset").getAsInt(); + int count = req.header.parameters.attributes.get("count").getAsInt(); + //获取传进来的参数 + Iterator iterator = jsonArray.iterator(); + List entries = new ArrayList<>(); + while (iterator.hasNext()) { + JsonElement element = iterator.next(); + JsonObject jo = element.getAsJsonObject(); + entries.add(new MetaIndex.SearchEntry(jo.get("key").getAsString(), jo.get("value").getAsString(), jo.get("type").getAsString())); + } + this.pageInfo.searchCount++; + this.countTable.put("searchCount", Integer.toString(this.pageInfo.searchCount)); + JsonObject ret = metaIndex.search(entries, offset, count); + DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); + builder.createResponse(DoipResponseCode.Success, req); + builder.setBody(ret.toString().getBytes(StandardCharsets.UTF_8)); + return builder.create(); } - this.pageInfo.searchCount++; - this.countTable.put("searchCount", Integer.toString(this.pageInfo.searchCount)); - JsonObject ret = metaIndex.search(entries, offset, count); - DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); - builder.createResponse(DoipResponseCode.Success, req); - builder.setBody(ret.toString().getBytes(StandardCharsets.UTF_8)); - return builder.create(); } catch (Exception e) { e.printStackTrace(); DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();