diff --git a/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/Crawler.kt b/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/Crawler.kt index 416243c..5751e06 100644 --- a/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/Crawler.kt +++ b/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/Crawler.kt @@ -18,76 +18,84 @@ private val CHUNK_SIZE = "chunkSize" fun main(args: Array) { val cli = CLI.create("crawler") - .setSummary("A REST Client to query all model element from server.") - .addArguments(listOf( - Argument() - .setArgName("username") - .setIndex(0) - .setDefaultValue("admin") - .setDescription("TWC username."), - Argument() - .setArgName("password") - .setIndex(1) - .setDefaultValue("admin") - .setDescription("TWC password.") - )) - .addOptions(listOf( - Option() - .setLongName("help") - .setShortName("h") - .setDescription("Show help site.") - .setFlag(true), - Option() - .setLongName("server") - .setShortName("S") - .setDescription("Set server path."), - Option() - .setLongName("port") - .setShortName("P") - .setDescription("Set server port number."), - Option() - .setLongName("instanceNum") - .setShortName("I") - .setDescription("Set number of RESTVerticle instances. Default:16") - .setDefaultValue("16"), - Option() - .setLongName("workspaceId") - .setShortName("W") - .setDescription("Select workspace to crawl"), - Option() - .setLongName("resourceId") - .setShortName("R") - .setDescription("Select resource to crawl"), - Option() - .setLongName("branchId") - .setShortName("B") - .setDescription("Select branch to crawl"), - Option() - .setLongName("revision") - .setShortName("REV") - .setDescription("Select revision to crawl"), - Option() - .setLongName("debug") - .setShortName("D") - .setDescription("Enable debug logging. Default: false") - .setFlag(true), - Option() - .setLongName("requestSingleElement") - .setShortName("RSE") - .setDescription("Request elements one-by-one. Default: false") - .setFlag(true), - Option() - .setLongName(CHUNK_SIZE) - .setShortName("C") - .setDescription("Set the size of chunks to use when crawling elements. Default: -1 to disable chunks") - .setDefaultValue("-1") - )) - - - - val commandLine = cli.parse(args.asList(),false) - - if(commandLine.isFlagEnabled("help")) { + .setSummary("A REST Client to query all model element from server.") + .addArguments( + listOf( + Argument() + .setArgName("username") + .setIndex(0) + .setDefaultValue("admin") + .setDescription("TWC username."), + Argument() + .setArgName("password") + .setIndex(1) + .setDefaultValue("admin") + .setDescription("TWC password.") + ) + ) + .addOptions( + listOf( + Option() + .setLongName("help") + .setShortName("h") + .setDescription("Show help site.") + .setFlag(true), + Option() + .setLongName("server") + .setShortName("S") + .setDescription("Set server path."), + Option() + .setLongName("port") + .setShortName("P") + .setDescription("Set server port number."), + Option() + .setLongName("ssl") + .setShortName("ssl") + .setDescription("SSL server connection. Default: false") + .setFlag(true), + Option() + .setLongName("instanceNum") + .setShortName("I") + .setDescription("Set number of RESTVerticle instances. Default:16") + .setDefaultValue("16"), + Option() + .setLongName("workspaceId") + .setShortName("W") + .setDescription("Select workspace to crawl"), + Option() + .setLongName("resourceId") + .setShortName("R") + .setDescription("Select resource to crawl"), + Option() + .setLongName("branchId") + .setShortName("B") + .setDescription("Select branch to crawl"), + Option() + .setLongName("revision") + .setShortName("REV") + .setDescription("Select revision to crawl"), + Option() + .setLongName("debug") + .setShortName("D") + .setDescription("Enable debug logging. Default: false") + .setFlag(true), + Option() + .setLongName("requestSingleElement") + .setShortName("RSE") + .setDescription("Request elements one-by-one. Default: false") + .setFlag(true), + Option() + .setLongName(CHUNK_SIZE) + .setShortName("C") + .setDescription("Set the size of chunks to use when crawling elements. Default: -1 to disable chunks") + .setDefaultValue("-1") + ) + ) + + + val commandLine = cli.parse(args.asList(), false) + + if (commandLine.isFlagEnabled("help")) { val builder = StringBuilder() cli.usage(builder) println(builder.toString()) @@ -96,10 +104,11 @@ fun main(args: Array) { val vertx = Vertx.vertx() val sd = vertx.sharedData() - val twcMap = sd.getLocalMap("twcMap") + val twcMap = sd.getLocalMap("twcMap") val serverOpt = commandLine.getOptionValue("server") val portOpt = commandLine.getOptionValue("port") + val isSslEnabled = commandLine.isFlagEnabled("ssl") val instanceNum = commandLine.getOptionValue("instanceNum").toInt() val workspaceId = commandLine.getOptionValue("workspaceId") val resourceId = commandLine.getOptionValue("resourceId") @@ -109,98 +118,106 @@ fun main(args: Array) { val debug = commandLine.isFlagEnabled("debug") val requestSingleElement = commandLine.isFlagEnabled("requestSingleElement") - twcMap.put("debug", debug) - if(debug) { + twcMap["debug"] = debug + if (debug) { println("Debug mode is enabled") } - twcMap.put("requestSingleElement", requestSingleElement) - if(requestSingleElement) { + twcMap["requestSingleElement"] = requestSingleElement + if (requestSingleElement) { println("Request single elements mode is enabled") } - twcMap.put(CHUNK_SIZE, chunkSize) + twcMap[CHUNK_SIZE] = chunkSize println("Chunk size is $chunkSize") - if(instanceNum!=null){ + if (instanceNum != null) { println("Instance number set to $instanceNum") - if(instanceNum<1){ + if (instanceNum < 1) { error("Number of Instances should be at least 1.") } } - if(workspaceId != null){ + if (workspaceId != null) { println("Workspace ID set to $workspaceId") - twcMap.put(DataConstants.WORKSPACE_ID, workspaceId) + twcMap[DataConstants.WORKSPACE_ID] = workspaceId } - if(resourceId != null){ + if (resourceId != null) { println("Resource ID set to $resourceId") - twcMap.put(DataConstants.RESOURCE_ID, resourceId) + twcMap[DataConstants.RESOURCE_ID] = resourceId } - if(branchId != null){ + if (branchId != null) { println("Branch ID set to $branchId") - twcMap.put(DataConstants.BRANCH_ID, branchId) + twcMap[DataConstants.BRANCH_ID] = branchId } - if(revision != null){ + if (revision != null) { println("Revision set to $revision") - twcMap.put(DataConstants.REVISION, revision.toInt()) + twcMap[DataConstants.REVISION] = revision.toInt() } - if(serverOpt != null && portOpt!=null){ - if(!File("server.config").exists()){ + if (serverOpt != null && portOpt != null) { + if (!File("server.config").exists()) { File("server.config").createNewFile() } - File("server.config").writeText(Json.encode(Server(serverOpt,portOpt.toInt()))) - twcMap.put("server_path",serverOpt) - twcMap.put("server_port",portOpt.toInt()) + File("server.config").writeText(Json.encode(Server(serverOpt, portOpt.toInt(), isSslEnabled))) + twcMap["server_path"] = serverOpt + twcMap["server_port"] = portOpt.toInt() + twcMap["server_ssl"] = isSslEnabled println("New Server config") } else { - if(File("server.config").exists()) { + if (File("server.config").exists()) { val config = File("server.config").readText() if (config != "") { val serverConf = JsonObject(config) val serverPath = serverConf.getString("path") val serverPort = serverConf.getInteger("port") - twcMap.put("server_path", serverPath) - twcMap.put("server_port", serverPort) + val sslEnabled = serverConf.getBoolean("ssl") + twcMap["server_path"] = serverPath + twcMap["server_port"] = serverPort + twcMap["server_ssl"] = sslEnabled } else { - error("Server config is empty.\n" + - "Please set Server Path and Port.\n" + - "(Check usage with -h (--help) cli option)") + error( + "Server config is empty.\n" + + "Please set Server Path and Port.\n" + + "(Check usage with -h (--help) cli option)" + ) } } else { - error("No server config found.\n" + - "Please set Server Path and Port.\n" + - "(Check usage with -h (--help) cli option)") + error( + "No server config found.\n" + + "Please set Server Path and Port.\n" + + "(Check usage with -h (--help) cli option)" + ) } } - println("Server config: ${twcMap.get("server_path")}:${twcMap.get("server_port")}") + println("Server config: ${twcMap["server_path"]}:${twcMap["server_port"]} (ssl: ${twcMap["server_ssl"]})") val restVerticle = RESTVerticle() var options = DeploymentOptions().setWorker(true).setHa(true).setInstances(instanceNum).setWorkerPoolSize(32) - twcMap.put("flag",0) - vertx.deployVerticle(restVerticle.javaClass.name,options,{ deploy -> + twcMap["flag"] = 0 + vertx.deployVerticle(restVerticle.javaClass.name, options) { deploy -> if (deploy.failed()) { error("Deploy failed: ${deploy.cause().message}") } else { val usr = commandLine.getArgumentValue("username") val pswd = commandLine.getArgumentValue("password") - twcMap.put("credential","Basic ${java.util.Base64.getEncoder().encodeToString("${usr}:${pswd}".toByteArray())}") - twcMap.put("username",usr) + twcMap["credential"] = + "Basic ${java.util.Base64.getEncoder().encodeToString("${usr}:${pswd}".toByteArray())}" + twcMap["username"] = usr - vertx.deployVerticle(MainVerticle(usr,pswd),{deploy-> - if(deploy.failed()){ + vertx.deployVerticle(MainVerticle(usr, pswd)) { deploy -> + if (deploy.failed()) { error("Deploy failed: ${deploy.cause().message}\n${deploy.cause().printStackTrace()}") } - }) + } } - }) + } } diff --git a/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/data/Data.kt b/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/data/Data.kt index e937e9b..7d456ed 100644 --- a/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/data/Data.kt +++ b/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/data/Data.kt @@ -1,18 +1,42 @@ package com.incquerylabs.vhci.modelaccess.twc.rest.data -import io.vertx.core.json.JsonArray -import io.vertx.core.json.JsonObject - data class Message(val event: String = "", val obj: Any = "") data class User(val username: String, val password: String) -data class Server(val path: String, val port: Int) +data class Server(val path: String, val port: Int, val ssl: Boolean) data class Repo(val id: String = "", val workspaces: List = emptyList()) data class Workspace(val id: String = "", val resources: List = emptyList()) data class Resource(val id: String = "", val workspace_id: String = "", val branches: List = emptyList()) -data class Branch(val id: String = "", val resource_id: String = "", val workspace_id: String = "", val revisions: List = emptyList()) -data class Revision(val id: Int = -1, val branch_id: String = "", val resource_id: String = "", val workspace_id: String = "", val elements: List = emptyList()) -data class Element(val id: String = "", val revision_id: Int = -1, val branch_id: String = "", val resource_id: String = "", val workspace_id: String = "", val elements: List = emptyList()) -data class Elements(val revision_id: Int = -1, val branch_id: String = "", val resource_id: String = "", val workspace_id: String = "", val elements: List = emptyList()) +data class Branch( + val id: String = "", + val resource_id: String = "", + val workspace_id: String = "", + val revisions: List = emptyList() +) + +data class Revision( + val id: Int = -1, + val branch_id: String = "", + val resource_id: String = "", + val workspace_id: String = "", + val elements: List = emptyList() +) + +data class Element( + val id: String = "", + val revision_id: Int = -1, + val branch_id: String = "", + val resource_id: String = "", + val workspace_id: String = "", + val elements: List = emptyList() +) + +data class Elements( + val revision_id: Int = -1, + val branch_id: String = "", + val resource_id: String = "", + val workspace_id: String = "", + val elements: List = emptyList() +) object DataConstants { const val REPO = "repo" diff --git a/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/verticles/MainVerticle.kt b/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/verticles/MainVerticle.kt index ff9f4f0..a88ff18 100644 --- a/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/verticles/MainVerticle.kt +++ b/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/verticles/MainVerticle.kt @@ -1,8 +1,16 @@ package com.incquerylabs.vhci.modelaccess.twc.rest.verticles -import com.incquerylabs.vhci.modelaccess.twc.rest.data.* +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Branch +import com.incquerylabs.vhci.modelaccess.twc.rest.data.DataConstants +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Element +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Elements +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Message +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Repo +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Resource +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Revision +import com.incquerylabs.vhci.modelaccess.twc.rest.data.User +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Workspace import io.vertx.core.AbstractVerticle -import io.vertx.core.json.Json import io.vertx.core.json.JsonObject class MainVerticle(val usr: String, val pswd: String) : AbstractVerticle() { @@ -16,55 +24,63 @@ class MainVerticle(val usr: String, val pswd: String) : AbstractVerticle() { val requestSingleElements = twcMap["requestSingleElement"] as Boolean val eb = vertx.eventBus() - vertx.setPeriodic(1000, { - vertx.sharedData().getCounter(DataConstants.QUERIES, { res -> + vertx.setPeriodic(1000) { + vertx.sharedData().getCounter(DataConstants.QUERIES) { res -> if (res.succeeded()) { val counter = res.result() counter.get { res -> queries = res.result().toInt() - vertx.sharedData().getCounter("sum", { res -> + vertx.sharedData().getCounter("sum") { res -> if (res.succeeded()) { val counter = res.result() counter.get { get -> if (get.succeeded()) { sum = get.result().toInt() - vertx.sharedData().getCounter("number", { res -> + vertx.sharedData().getCounter("number") { res -> if (res.succeeded()) { val counter = res.result() counter.get { get -> if (get.succeeded()) { number = get.result().toInt() - println("Total: ${queries}/${sum}/${++s} query/response/sec | Now: ${number} elem | AvgSpeed: ${sum / (s)} elem/sec") + println("Total: ${queries}/${sum}/${++s} query/response/sec | Now: $number elem | AvgSpeed: ${sum / (s)} elem/sec") counter.compareAndSet(number.toLong(), 0, {}) if (queries != 0 && sum != 0 && queries == sum) { - eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.LOGOUT, JsonObject()))) + eb.send( + DataConstants.TWCVERT_ADDRESS, + JsonObject.mapFrom( + Message( + DataConstants.LOGOUT, + JsonObject() + ) + ) + ) } } } } else { error("Counter: queries not available.") } - }) + } } } } else { error("Counter: queries not available.") } - }) + } } } else { // Something went wrong! } - }) + } - }) + } eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.LOGIN, User("$usr", "$pswd")))) - eb.consumer(DataConstants.TWCMAIN_ADDRESS, { message -> + eb.consumer(DataConstants.TWCMAIN_ADDRESS) { message -> val messageData = (message.body() as JsonObject).mapTo(Message::class.java) @@ -77,146 +93,212 @@ class MainVerticle(val usr: String, val pswd: String) : AbstractVerticle() { val branchId = twcMap[DataConstants.BRANCH_ID] val revision = twcMap[DataConstants.REVISION] when { - workspaceId == null -> eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_WORKSPACES, JsonObject()))) - resourceId == null -> eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_RESOURCES, JsonObject().put(DataConstants.WORKSPACE_ID, workspaceId)))) - branchId == null -> eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_BRANCHES, - JsonObject() + workspaceId == null -> eb.send( + DataConstants.TWCVERT_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.GET_WORKSPACES, JsonObject())) + ) + resourceId == null -> eb.send( + DataConstants.TWCVERT_ADDRESS, + JsonObject.mapFrom( + Message( + DataConstants.GET_RESOURCES, + JsonObject().put(DataConstants.WORKSPACE_ID, workspaceId) + ) + ) + ) + branchId == null -> eb.send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_BRANCHES, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) - .put(DataConstants.RESOURCE_ID, resourceId)))) - revision == null -> eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_REVISIONS, - JsonObject() + .put(DataConstants.RESOURCE_ID, resourceId) + ) + ) + ) + revision == null -> eb.send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_REVISIONS, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) .put(DataConstants.RESOURCE_ID, resourceId) .put(DataConstants.BRANCH_ID, branchId) - ))) - else -> vertx.eventBus().send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_ROOT_ELEMENT_IDS, - JsonObject() + ) + ) + ) + else -> vertx.eventBus().send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_ROOT_ELEMENT_IDS, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) .put(DataConstants.RESOURCE_ID, resourceId) .put(DataConstants.BRANCH_ID, branchId) .put(DataConstants.REVISION_ID, revision) - ))) + ) + ) + ) } } DataConstants.REPO -> { -// println("Received Repository") - val repo = JsonObject(messageData.obj as Map).mapTo(Repo::class.java) + // println("Received Repository") + val repo = JsonObject(messageData.obj as Map).mapTo(Repo::class.java) repo.workspaces.forEach { ws -> - val id = JsonObject(ws as Map).getString("@id") - eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_RESOURCES, JsonObject().put(DataConstants.WORKSPACE_ID, id)))) + val id = JsonObject(ws as Map).getString("@id") + eb.send( + DataConstants.TWCVERT_ADDRESS, + JsonObject.mapFrom( + Message( + DataConstants.GET_RESOURCES, + JsonObject().put(DataConstants.WORKSPACE_ID, id) + ) + ) + ) } } DataConstants.WORKSPACE -> { -// println("Received Workspace") - val workspace = JsonObject(messageData.obj as Map).mapTo(Workspace::class.java) + // println("Received Workspace") + val workspace = JsonObject(messageData.obj as Map).mapTo(Workspace::class.java) val workspaceId = workspace.id workspace.resources.forEach { res -> - val resourceId = JsonObject(res as Map).getString("@id") - eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_BRANCHES, - JsonObject() + val resourceId = JsonObject(res as Map).getString("@id") + eb.send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_BRANCHES, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) - .put(DataConstants.RESOURCE_ID, resourceId)))) + .put(DataConstants.RESOURCE_ID, resourceId) + ) + ) + ) } } DataConstants.RESOURCE -> { -// println("Received Resource") - val resource = JsonObject(messageData.obj as Map).mapTo(Resource::class.java) + // println("Received Resource") + val resource = JsonObject(messageData.obj as Map).mapTo(Resource::class.java) val resourceId = resource.id val workspaceId = resource.workspace_id resource.branches.forEach { branch -> - val branchId = JsonObject(branch as Map).getString("@id") - eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_REVISIONS, - JsonObject() + val branchId = JsonObject(branch as Map).getString("@id") + eb.send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_REVISIONS, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) .put(DataConstants.RESOURCE_ID, resourceId) .put(DataConstants.BRANCH_ID, branchId) - ))) + ) + ) + ) } } DataConstants.BRANCH -> { -// println("Received Branch") - val branch = JsonObject(messageData.obj as Map).mapTo(Branch::class.java) + // println("Received Branch") + val branch = JsonObject(messageData.obj as Map).mapTo(Branch::class.java) val branchId = branch.id val resourceId = branch.resource_id val workspaceId = branch.workspace_id val inputRevision = twcMap[DataConstants.REVISION] - if(inputRevision == null) { + if (inputRevision == null) { twcMap[DataConstants.REVISION] = branch.revisions.maxBy { it as Int } } branch.revisions.forEach { rev -> val revId = (rev as Int) - vertx.eventBus().send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_ROOT_ELEMENT_IDS, - JsonObject() + vertx.eventBus().send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_ROOT_ELEMENT_IDS, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) .put(DataConstants.RESOURCE_ID, resourceId) .put(DataConstants.BRANCH_ID, branchId) .put(DataConstants.REVISION_ID, revId) - ))) + ) + ) + ) } } DataConstants.REVISION -> { -// println("Received Revision") - val revision = JsonObject(messageData.obj as Map).mapTo(Revision::class.java) + // println("Received Revision") + val revision = JsonObject(messageData.obj as Map).mapTo(Revision::class.java) val revisionId = revision.id val branchId = revision.branch_id val resourceId = revision.resource_id val workspaceId = revision.workspace_id val inputRevision = twcMap[DataConstants.REVISION] - if(revisionId == inputRevision) { - if(requestSingleElements) { + if (revisionId == inputRevision) { + if (requestSingleElements) { revision.elements.forEach { element -> val elemId = element as String - vertx.eventBus().send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_ELEMENT, - JsonObject() + vertx.eventBus().send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_ELEMENT, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) .put(DataConstants.RESOURCE_ID, resourceId) .put(DataConstants.BRANCH_ID, branchId) .put(DataConstants.REVISION_ID, revisionId) .put(DataConstants.ELEMENT_ID, elemId) - ))) + ) + ) + ) } } else { val elementIds = revision.elements - vertx.eventBus().send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_ELEMENTS, - JsonObject() + vertx.eventBus().send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_ELEMENTS, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) .put(DataConstants.RESOURCE_ID, resourceId) .put(DataConstants.BRANCH_ID, branchId) .put(DataConstants.REVISION_ID, revisionId) .put(DataConstants.ELEMENT_IDS, elementIds) - ))) + ) + ) + ) } } } DataConstants.ELEMENT -> { -// println("Received Element") - val element = JsonObject(messageData.obj as Map).mapTo(Element::class.java) + // println("Received Element") + val element = JsonObject(messageData.obj as Map).mapTo(Element::class.java) val revisionId = element.revision_id val branchId = element.branch_id val resourceId = element.resource_id val workspaceId = element.workspace_id element.elements.forEach { element -> - val elementId = JsonObject(element as Map).getString("@id") + val elementId = JsonObject(element as Map).getString("@id") - vertx.eventBus().send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_ELEMENT, - JsonObject() + vertx.eventBus().send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_ELEMENT, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) .put(DataConstants.RESOURCE_ID, resourceId) .put(DataConstants.BRANCH_ID, branchId) .put(DataConstants.REVISION_ID, revisionId) .put(DataConstants.ELEMENT_ID, elementId) - ))) + ) + ) + ) } } DataConstants.ELEMENTS -> { -// println("Received Elements") - val elements = JsonObject(messageData.obj as Map).mapTo(Elements::class.java) + // println("Received Elements") + val elements = JsonObject(messageData.obj as Map).mapTo(Elements::class.java) val revisionId = elements.revision_id val branchId = elements.branch_id val resourceId = elements.resource_id @@ -224,26 +306,37 @@ class MainVerticle(val usr: String, val pswd: String) : AbstractVerticle() { val elementIds = elements.elements.map { element -> JsonObject.mapFrom(element).getString("@id") } - vertx.eventBus().send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.GET_ELEMENTS, - JsonObject() + vertx.eventBus().send( + DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom( + Message( + DataConstants.GET_ELEMENTS, + JsonObject() .put(DataConstants.WORKSPACE_ID, workspaceId) .put(DataConstants.RESOURCE_ID, resourceId) .put(DataConstants.BRANCH_ID, branchId) .put(DataConstants.REVISION_ID, revisionId) .put(DataConstants.ELEMENT_IDS, elementIds) - ))) + ) + ) + ) } DataConstants.ERROR -> { println("\nExit") - if(twcMap["cookies"] != null) { - eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.LOGOUT, JsonObject()))) + if (twcMap["cookies"] != null) { + eb.send( + DataConstants.TWCVERT_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.LOGOUT, JsonObject())) + ) } vertx.close() } DataConstants.EXIT -> { println("\nExit") - if(twcMap["cookies"] != null) { - eb.send(DataConstants.TWCVERT_ADDRESS, JsonObject.mapFrom(Message(DataConstants.LOGOUT, JsonObject()))) + if (twcMap["cookies"] != null) { + eb.send( + DataConstants.TWCVERT_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.LOGOUT, JsonObject())) + ) } vertx.close() } @@ -251,6 +344,6 @@ class MainVerticle(val usr: String, val pswd: String) : AbstractVerticle() { else -> error("Unknown Command: ${messageData.event}") } - }) + } } } \ No newline at end of file diff --git a/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/verticles/RESTVerticle.kt b/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/verticles/RESTVerticle.kt index 5192f26..29aa147 100644 --- a/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/verticles/RESTVerticle.kt +++ b/com.incquerylabs.vhci.modelaccess.twc.rest/src/main/kotlin/com/incquerylabs/vhci/modelaccess/twc/rest/verticles/RESTVerticle.kt @@ -1,9 +1,16 @@ package com.incquerylabs.vhci.modelaccess.twc.rest.verticles -import com.incquerylabs.vhci.modelaccess.twc.rest.data.* +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Branch +import com.incquerylabs.vhci.modelaccess.twc.rest.data.DataConstants +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Element +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Elements +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Message +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Repo +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Resource +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Revision +import com.incquerylabs.vhci.modelaccess.twc.rest.data.Workspace import io.vertx.core.AbstractVerticle import io.vertx.core.buffer.Buffer -import io.vertx.core.json.Json import io.vertx.core.json.JsonArray import io.vertx.core.json.JsonObject import io.vertx.core.shareddata.LocalMap @@ -18,8 +25,8 @@ class RESTVerticle() : AbstractVerticle() { var chunkSize = -1 override fun start() { - val client = WebClient.create(vertx, WebClientOptions()) val twcMap = vertx.sharedData().getLocalMap(DataConstants.TWCMAP) + val client = WebClient.create(vertx, WebClientOptions().setSsl(twcMap["server_ssl"] as Boolean)) serverPath = twcMap["server_path"].toString() port = twcMap["server_port"] as Int @@ -27,7 +34,7 @@ class RESTVerticle() : AbstractVerticle() { val debug = twcMap["debug"] as Boolean - vertx.eventBus().consumer(DataConstants.TWCVERT_ADDRESS, { message -> + vertx.eventBus().consumer(DataConstants.TWCVERT_ADDRESS) { message -> val json = message.body() as JsonObject val obj = json.getJsonObject("obj") @@ -93,7 +100,7 @@ class RESTVerticle() : AbstractVerticle() { else -> error("Unknown Command: ${json.getString("event")}") } - }) + } } @@ -104,56 +111,65 @@ class RESTVerticle() : AbstractVerticle() { val branchId = obj.getString(DataConstants.BRANCH_ID) val revisionId = obj.getInteger(DataConstants.REVISION_ID) val elementIds = obj.getJsonArray(DataConstants.ELEMENT_IDS) - vertx.sharedData().getCounter(DataConstants.QUERIES, { res -> + vertx.sharedData().getCounter(DataConstants.QUERIES) { res -> if (res.succeeded()) { val counter = res.result() counter.addAndGet(elementIds.size().toLong(), {}) } else { error("Counter: queries not available.") } - }) - - client.post(port, serverPath, - "/osmc/workspaces/$workspaceId/resources/$resourceId/branches/$branchId/revisions/$revisionId/elements") - .putHeader("content-type", "text/plain") - .putHeader("Authorization", "${twcMap.get("credential")}") - .putHeader("Cookie", "${twcMap["user_cookie"]}") - .putHeader("Cookie", "${twcMap["session_cookie"]}") - .sendBuffer(Buffer.buffer(elementIds.joinToString(",")), { ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 200) { - - val data = ar.result().bodyAsJsonObject() - - val containedElements = elementIds.flatMap { elementId -> - val element = data.getJsonArray(elementId as String) - saveElement(elementId, element) - element.getJsonObject(0).getJsonArray("ldp:contains") - } - if (!containedElements.isEmpty()) { - if (chunkSize > 1) { - containedElements.withIndex().groupBy { - it.index / chunkSize - }.values.map { it.map { it.value } }.forEach { chunkList -> - val elementM = Elements(revisionId, branchId, resourceId, workspaceId, chunkList) - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.ELEMENTS, elementM))) - } - } else { - val elementM = Elements(revisionId, branchId, resourceId, workspaceId, containedElements) - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.ELEMENTS, elementM))) + } + + client.post( + port, serverPath, + "/osmc/workspaces/$workspaceId/resources/$resourceId/branches/$branchId/revisions/$revisionId/elements" + ) + .putHeader("content-type", "text/plain") + .putHeader("Authorization", "${twcMap["credential"]}") + .putHeader("Cookie", "${twcMap["user_cookie"]}") + .putHeader("Cookie", "${twcMap["session_cookie"]}") + .sendBuffer(Buffer.buffer(elementIds.joinToString(","))) { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 200) { + + val data = ar.result().bodyAsJsonObject() + + val containedElements = elementIds.flatMap { elementId -> + val element = data.getJsonObject(elementId as String).getJsonArray("data") + saveElement(elementId, element) + element.getJsonObject(0).getJsonArray("ldp:contains") + } + if (!containedElements.isEmpty()) { + if (chunkSize > 1) { + containedElements.withIndex().groupBy { + it.index / chunkSize + }.values.map { it.map { it.value } }.forEach { chunkList -> + val elementM = Elements(revisionId, branchId, resourceId, workspaceId, chunkList) + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.ELEMENTS, elementM)) + ) } + } else { + val elementM = + Elements(revisionId, branchId, resourceId, workspaceId, containedElements) + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.ELEMENTS, elementM)) + ) } - - } else { - println("getElements: ${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() } + } else { - println("Query Root Element failed: ${ar.cause().message}") + println("getElements: ${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } + } else { + println("Query Root Element failed: ${ar.cause().message}") + myError() + } - }) + } } private fun getElement(client: WebClient, twcMap: LocalMap, obj: JsonObject) { @@ -161,67 +177,74 @@ class RESTVerticle() : AbstractVerticle() { } private fun getRootElement(client: WebClient, twcMap: LocalMap, obj: JsonObject) { - vertx.sharedData().getCounter(DataConstants.QUERIES, { res -> + vertx.sharedData().getCounter(DataConstants.QUERIES) { res -> if (res.succeeded()) { val counter = res.result() counter.incrementAndGet {} } else { error("Counter: queries not available.") } - }) + } val workspaceId = obj.getString(DataConstants.WORKSPACE_ID) val resourceId = obj.getString(DataConstants.RESOURCE_ID) val branchId = obj.getString(DataConstants.BRANCH_ID) val revisionId = obj.getInteger(DataConstants.REVISION_ID) val elementId = obj.getString(DataConstants.ELEMENT_ID) - client.get(port, serverPath, - "/osmc/workspaces/$workspaceId/resources/$resourceId/branches/$branchId/elements/$elementId") - .putHeader("content-type", "application/ld+json") - .putHeader("Authorization", "${twcMap.get("credential")}") - .putHeader("Cookie", "${twcMap.get("user_cookie")}") - .putHeader("Cookie", "${twcMap.get("session_cookie")}") - .sendJson(JsonObject(), { ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 200) { + client.get( + port, serverPath, + "/osmc/workspaces/$workspaceId/resources/$resourceId/branches/$branchId/elements/$elementId" + ) + .putHeader("content-type", "application/ld+json") + .putHeader("Authorization", "${twcMap["credential"]}") + .putHeader("Cookie", "${twcMap["user_cookie"]}") + .putHeader("Cookie", "${twcMap["session_cookie"]}") + .sendJson(JsonObject()) { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 200) { + + val data = ar.result().bodyAsJsonArray() + + saveElement(elementId, data) + + val element = Element( + elementId, revisionId, branchId, resourceId, workspaceId, + data.getJsonObject(0).getJsonArray("ldp:contains").list + ) + + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.ELEMENT, element)) + ) - val data = ar.result().bodyAsJsonArray() - - saveElement(elementId, data) - - val element = Element(elementId, revisionId, branchId, resourceId, workspaceId, - data.getJsonObject(0).getJsonArray("ldp:contains").list) - - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.ELEMENT, element))) - - } else { - println("getRootElement: ${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() - } } else { - println("Query Root Element failed: ${ar.cause().message}") + println("getRootElement: ${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } + } else { + println("Query Root Element failed: ${ar.cause().message}") + myError() + } - }) + } } private fun saveElement(elementId: String, data: JsonArray) { - vertx.sharedData().getCounter("sum", { res -> + vertx.sharedData().getCounter("sum") { res -> if (res.succeeded()) { val counter = res.result() counter.incrementAndGet {} } else { error("Counter: queries not available.") } - }) - vertx.sharedData().getCounter("number", { res -> + } + vertx.sharedData().getCounter("number") { res -> if (res.succeeded()) { val counter = res.result() counter.incrementAndGet {} } else { error("Counter: queries not available.") } - }) + } // vertx.executeBlocking({ future -> // val dir = File("./elements") // val file = File("./elements/${elementId}.json") @@ -244,207 +267,240 @@ class RESTVerticle() : AbstractVerticle() { val resourceId = obj.getString(DataConstants.RESOURCE_ID) val branchId = obj.getString(DataConstants.BRANCH_ID) val revId = obj.getInteger(DataConstants.REVISION_ID) - client.get(port, serverPath, - "/osmc/workspaces/${workspaceId}/resources/${resourceId}/branches/${branchId}/revisions/${revId}") - .putHeader("content-type", "application/ld+json") - .putHeader("Authorization", "${twcMap.get("credential")}") - .putHeader("Cookie", "${twcMap.get("user_cookie")}") - .putHeader("Cookie", "${twcMap.get("session_cookie")}") - .send { ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 200) { - - val data = ar.result().bodyAsJsonArray() - //println(data) - val revision = Revision(revId, branchId, resourceId, workspaceId, - data.getJsonObject(0).getJsonArray("rootObjectIDs").list) - //println(revision) - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.REVISION, revision))) - - } else { - println("getRootElementIds: ${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() - } + client.get( + port, serverPath, + "/osmc/workspaces/${workspaceId}/resources/${resourceId}/branches/${branchId}/revisions/${revId}" + ) + .putHeader("content-type", "application/ld+json") + .putHeader("Authorization", "${twcMap["credential"]}") + .putHeader("Cookie", "${twcMap["user_cookie"]}") + .putHeader("Cookie", "${twcMap["session_cookie"]}") + .send { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 200) { + + val data = ar.result().bodyAsJsonArray() + //println(data) + val revision = Revision( + revId, branchId, resourceId, workspaceId, + data.getJsonObject(0).getJsonArray("rootObjectIDs").list + ) + //println(revision) + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.REVISION, revision)) + ) + } else { - println("Query Root Element IDs failed: ${ar.cause().message}") + println("getRootElementIds: ${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } - + } else { + println("Query Root Element IDs failed: ${ar.cause().message}") + myError() } + + } } private fun getRevisions(client: WebClient, twcMap: LocalMap, obj: JsonObject) { val workspaceId = obj.getString(DataConstants.WORKSPACE_ID) val resourceId = obj.getString(DataConstants.RESOURCE_ID) val branchId = obj.getString(DataConstants.BRANCH_ID) - client.get(port, serverPath, - "/osmc/workspaces/${workspaceId}/resources/${resourceId}/branches/${branchId}/revisions") - .putHeader("content-type", "application/ld+json") - .putHeader("Authorization", "${twcMap.get("credential")}") - .putHeader("Cookie", "${twcMap.get("user_cookie")}") - .putHeader("Cookie", "${twcMap.get("session_cookie")}") - .send { ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 200) { - - val data = ar.result().bodyAsJsonArray() - val branch = Branch(branchId, resourceId, workspaceId, data.getJsonObject(0).getJsonArray("ldp:contains").list) - //println(resource) - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.BRANCH, branch))) - - } else { - println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() - } + client.get( + port, serverPath, + "/osmc/workspaces/${workspaceId}/resources/${resourceId}/branches/${branchId}/revisions" + ) + .putHeader("content-type", "application/ld+json") + .putHeader("Authorization", "${twcMap["credential"]}") + .putHeader("Cookie", "${twcMap["user_cookie"]}") + .putHeader("Cookie", "${twcMap["session_cookie"]}") + .send { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 200) { + + val data = ar.result().bodyAsJsonArray() + val branch = Branch( + branchId, + resourceId, + workspaceId, + data.getJsonObject(0).getJsonArray("ldp:contains").list + ) + //println(resource) + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.BRANCH, branch)) + ) + } else { - println("Query Revisions failed: ${ar.cause().message}") + println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } - + } else { + println("Query Revisions failed: ${ar.cause().message}") + myError() } + + } } private fun getBranches(client: WebClient, twcMap: LocalMap, obj: JsonObject) { val workspaceId = obj.getString(DataConstants.WORKSPACE_ID) val resourceId = obj.getString(DataConstants.RESOURCE_ID) client.get(port, serverPath, "/osmc/workspaces/${workspaceId}/resources/${resourceId}/branches") - .putHeader("content-type", "application/ld+json") - .putHeader("Authorization", "${twcMap.get("credential")}") - .putHeader("Cookie", "${twcMap.get("user_cookie")}") - .putHeader("Cookie", "${twcMap.get("session_cookie")}") - .send { ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 200) { - - val data = ar.result().bodyAsJsonObject() - val resource = Resource(resourceId, workspaceId, data.getJsonArray("ldp:contains").list) - //println(resource) - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.RESOURCE, resource))) - - } else { - println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() - } + .putHeader("content-type", "application/ld+json") + .putHeader("Authorization", "${twcMap["credential"]}") + .putHeader("Cookie", "${twcMap["user_cookie"]}") + .putHeader("Cookie", "${twcMap["session_cookie"]}") + .send { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 200) { + + val data = ar.result().bodyAsJsonObject() + val resource = Resource(resourceId, workspaceId, data.getJsonArray("ldp:contains").list) + //println(resource) + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.RESOURCE, resource)) + ) + } else { - println("Query Branches failed: ${ar.cause().message}") + println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } - + } else { + println("Query Branches failed: ${ar.cause().message}") + myError() } + + } } private fun getResources(client: WebClient, twcMap: LocalMap, obj: JsonObject) { val workspaceId = obj.getString(DataConstants.WORKSPACE_ID) client.get(port, serverPath, "/osmc/workspaces/${workspaceId}/resources") - .putHeader("content-type", "application/ld+json") - .putHeader("Authorization", "${twcMap.get("credential")}") - .putHeader("Cookie", "${twcMap.get("user_cookie")}") - .putHeader("Cookie", "${twcMap.get("session_cookie")}") - .send { ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 200) { - - val data = ar.result().bodyAsJsonArray() - val workspace = Workspace(workspaceId, data.getJsonObject(0).getJsonArray("ldp:contains").list) - //println(workspace) - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.WORKSPACE, workspace))) - - } else { - println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() - } + .putHeader("content-type", "application/ld+json") + .putHeader("Authorization", "${twcMap["credential"]}") + .putHeader("Cookie", "${twcMap["user_cookie"]}") + .putHeader("Cookie", "${twcMap["session_cookie"]}") + .send { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 200) { + + val data = ar.result().bodyAsJsonArray() + val workspace = Workspace(workspaceId, data.getJsonObject(0).getJsonArray("ldp:contains").list) + //println(workspace) + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.WORKSPACE, workspace)) + ) + } else { - println("Query Resources failed: ${ar.cause().message}") + println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } - + } else { + println("Query Resources failed: ${ar.cause().message}") + myError() } + + } } private fun login(client: WebClient, twcMap: LocalMap) { client.get(port, serverPath, "/osmc/login") - .putHeader("content-type", "application/json") - .putHeader("Accept", "text/html") - .putHeader("Authorization", "${twcMap.get("credential")}") - .send({ ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 204) { - val userCookie = ar.result().cookies()[0].split(';')[0] - val sessionCookie = ar.result().cookies()[1].split(';')[0] - twcMap.put("user_cookie", userCookie) - twcMap.put("session_cookie", sessionCookie) - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.LOGGED_IN, JsonObject()))) - } else { - println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() - } - + .putHeader("content-type", "application/json") + .putHeader("Accept", "text/html") + .putHeader("Authorization", "${twcMap["credential"]}") + .send { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 204) { + val userCookie = ar.result().cookies()[0].split(';')[0] + val sessionCookie = ar.result().cookies()[1].split(';')[0] + twcMap["user_cookie"] = userCookie + twcMap["session_cookie"] = sessionCookie + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.LOGGED_IN, JsonObject())) + ) } else { - ar.cause().printStackTrace() - println("Login failed: ${ar.cause().message}") + println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } - }) + + } else { + ar.cause().printStackTrace() + println("Login failed: ${ar.cause().message}") + myError() + } + } } private fun logout(client: WebClient, twcMap: LocalMap) { client.get(port, serverPath, "/osmc/logout") - .putHeader("content-type", "application/ld+json") - .putHeader("Authorization", "${twcMap.get("credential")}") - .putHeader("Cookie", "${twcMap.get("user_cookie")}") - .putHeader("Cookie", "${twcMap.get("session_cookie")}") - .send { ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 204) { - //twcMap.put("cookies",ar.result().cookies().toString()) - twcMap.remove("cookies") - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.EXIT, JsonObject()))) - } else { - println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() - } - + .putHeader("content-type", "application/ld+json") + .putHeader("Authorization", "${twcMap["credential"]}") + .putHeader("Cookie", "${twcMap["user_cookie"]}") + .putHeader("Cookie", "${twcMap["session_cookie"]}") + .send { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 204) { + //twcMap.put("cookies",ar.result().cookies().toString()) + twcMap.remove("cookies") + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.EXIT, JsonObject())) + ) } else { - ar.cause().printStackTrace() - println("Logout failed: ${ar.cause().message}") + println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } + + } else { + ar.cause().printStackTrace() + println("Logout failed: ${ar.cause().message}") + myError() } + } } //TODO private fun getWorkspaces(client: WebClient, twcMap: LocalMap) { client.get(port, serverPath, "/osmc/workspaces") - .putHeader("content-type", "application/ld+json") - .putHeader("Authorization", "${twcMap.get("credential")}") - .putHeader("Cookie", "${twcMap.get("user_cookie")}") - .putHeader("Cookie", "${twcMap.get("session_cookie")}") - .timeout(2000) - .send { ar -> - if (ar.succeeded()) { - if (ar.result().statusCode() == 200) { - - val data = ar.result().bodyAsJsonObject() - val repoID = data.getString("@id") - val workspaces = data.getJsonArray("ldp:contains") - - val repo = Repo(repoID, workspaces.list) - - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.REPO, JsonObject.mapFrom(repo)))) - } else { - println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") - myError() - } + .putHeader("content-type", "application/ld+json") + .putHeader("Authorization", "${twcMap["credential"]}") + .putHeader("Cookie", "${twcMap["user_cookie"]}") + .putHeader("Cookie", "${twcMap["session_cookie"]}") + .timeout(2000) + .send { ar -> + if (ar.succeeded()) { + if (ar.result().statusCode() == 200) { + + val data = ar.result().bodyAsJsonObject() + val repoID = data.getString("@id") + val workspaces = data.getJsonArray("ldp:contains") + + val repo = Repo(repoID, workspaces.list) + + vertx.eventBus().send( + DataConstants.TWCMAIN_ADDRESS, + JsonObject.mapFrom(Message(DataConstants.REPO, JsonObject.mapFrom(repo))) + ) } else { - println("Query Workspaces failed: ${ar.cause().message}") + println("${ar.result().statusCode()} : ${ar.result().statusMessage()}") myError() } - + } else { + println("Query Workspaces failed: ${ar.cause().message}") + myError() } + + } } private fun myError() { - vertx.eventBus().send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.ERROR, JsonObject()))) + vertx.eventBus() + .send(DataConstants.TWCMAIN_ADDRESS, JsonObject.mapFrom(Message(DataConstants.ERROR, JsonObject()))) } } \ No newline at end of file