diff --git a/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/AbstractGetHandler.kt b/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/AbstractGetHandler.kt new file mode 100644 index 000000000..bfae38b82 --- /dev/null +++ b/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/AbstractGetHandler.kt @@ -0,0 +1,225 @@ +package io.sirix.rest.crud + +import io.brackit.query.jdm.StructuredItemCollection +import io.brackit.query.jdm.node.TemporalNodeCollection +import io.sirix.access.Databases +import io.sirix.api.Database +import io.sirix.api.NodeCursor +import io.sirix.api.NodeReadOnlyTrx +import io.sirix.api.ResourceSession +import io.sirix.api.json.JsonResourceSession +import io.sirix.api.xml.XmlResourceSession +import io.sirix.query.SirixQueryContext +import io.vertx.ext.auth.User +import io.vertx.ext.web.Route +import io.vertx.ext.web.RoutingContext +import io.vertx.kotlin.coroutines.await + +import io.sirix.query.json.* +import io.sirix.query.node.BasicXmlDBStore +import io.sirix.query.node.XmlDBCollection +import io.sirix.rest.crud.json.JsonSessionDBStore +import io.sirix.rest.crud.xml.XmlSessionDBStore +import io.vertx.core.Context +import io.vertx.core.Promise +import io.vertx.core.json.JsonObject +import io.vertx.ext.auth.authorization.AuthorizationProvider +import io.vertx.kotlin.coroutines.dispatcher +import kotlinx.coroutines.withContext +import java.nio.file.Path + +abstract class AbstractGetHandler , + W: AutoCloseable, R: NodeCursor>( +private val location: Path, +private val authz: AuthorizationProvider){ + suspend fun handle(ctx: RoutingContext): Route { + val context = ctx.vertx().orCreateContext + val databaseName: String = ctx.pathParam("database") + val resource: String = ctx.pathParam("resource") + val jsonBody = ctx.body().asJsonObject() + val query: String? = ctx.queryParam("query").getOrElse(0) { + jsonBody?.getString("query") + } + + withContext(context.dispatcher()) { + get(databaseName, ctx, resource, query, context, ctx.get("user") as User, jsonBody) + } + + return ctx.currentRoute() + } + + suspend fun get( + databaseName: String, ctx: RoutingContext, resource: String?, query: String?, + vertxContext: Context, user: User, jsonBody: JsonObject? + ) { + val revision: String? = ctx.queryParam("revision").getOrNull(0) + val revisionTimestamp: String? = ctx.queryParam("revision-timestamp").getOrNull(0) + val startRevision: String? = ctx.queryParam("start-revision").getOrNull(0) + val endRevision: String? = ctx.queryParam("end-revision").getOrNull(0) + val startRevisionTimestamp: String? = ctx.queryParam("start-revision-timestamp").getOrNull(0) + val endRevisionTimestamp: String? = ctx.queryParam("end-revision-timestamp").getOrNull(0) + + val nodeId: String? = ctx.queryParam("nodeId").getOrNull(0) + + var body: String? + + val database = openDatabase(location.resolve(databaseName)) + + database.use { + val manager = database.beginResourceSession(resource) + + manager.use { + body = if (query != null && query.isNotEmpty()) { + queryResource( + databaseName, database, revision, revisionTimestamp, manager, ctx, nodeId, query, + vertxContext, user, jsonBody + ) + } else { + val revisions: IntArray = + Revisions.getRevisionsToSerialize( + startRevision, endRevision, startRevisionTimestamp, + endRevisionTimestamp, manager, revision, revisionTimestamp + ) + + serializeResource(manager, revisions, nodeId?.toLongOrNull(), ctx, vertxContext) + } + } + } + + if (body != null) { + ctx.response().end(body) + } else { + ctx.response().end() + } + } + + suspend fun queryResource( + databaseName: String?, database: Database, revision: String?, + revisionTimestamp: String?, manager: T, ctx: RoutingContext, + nodeId: String?, query: String, vertxContext: Context, user: User, jsonBody: JsonObject? + ): String? { + val dbCollection = getDBCollection(databaseName, database) + + dbCollection.use { + val revisionNumber = Revisions.getRevisionNumber(revision, revisionTimestamp, manager) + val startResultSeqIndex = ctx.queryParam("startResultSeqIndex").getOrElse(0) { null } + val endResultSeqIndex = ctx.queryParam("endResultSeqIndex").getOrElse(0) { null } + + return xquery( + manager, + dbCollection, + nodeId, + revisionNumber, + query, + ctx, + vertxContext, + user, + startResultSeqIndex?.toLong(), + endResultSeqIndex?.toLong(), + jsonBody + ) + } + } + + suspend fun xquery( + manager: T?, + dbCollection: W?, + nodeId: String?, + revisionNumber: IntArray?, query: String, routingContext: RoutingContext, vertxContext: Context, + user: User, startResultSeqIndex: Long?, endResultSeqIndex: Long?, jsonBody: JsonObject? + ): String? { + return vertxContext.executeBlocking { promise: Promise -> + // Initialize queryResource context and store. + val jsonDBStore = JsonSessionDBStore( + routingContext, + BasicJsonDBStore.newBuilder().storeDeweyIds(true).build(), + user, + authz + ) + val xmlDBStore = + XmlSessionDBStore(routingContext, BasicXmlDBStore.newBuilder().storeDeweyIds(true).build(), user, authz) + + val commitMessage = routingContext.queryParam("commitMessage").getOrElse(0) { + jsonBody?.getString("commitMessage") + } + val commitTimestampAsString = routingContext.queryParam("commitTimestamp").getOrElse(0) { + jsonBody?.getString("commitTimestamp") + } + val commitTimestamp = if (commitTimestampAsString == null) { + null + } else { + Revisions.parseRevisionTimestamp(commitTimestampAsString).toInstant() + } + + val queryCtx = SirixQueryContext.createWithJsonStoreAndNodeStoreAndCommitStrategy( + xmlDBStore, + jsonDBStore, + SirixQueryContext.CommitStrategy.AUTO, + commitMessage, + commitTimestamp + ) + + var body: String? + + queryCtx.use { + if (manager != null && dbCollection != null && revisionNumber != null) { + val rtx = manager.beginNodeReadOnlyTrx(revisionNumber[0]) as R + + rtx.use { + if (nodeId == null) { + rtx.moveToFirstChild() + } else { + rtx.moveTo(nodeId.toLong()) + } + + handleQueryExtra(rtx, dbCollection, queryCtx, jsonDBStore) + + body = query( + xmlDBStore, + jsonDBStore, + startResultSeqIndex, + query, + queryCtx, + endResultSeqIndex, + routingContext + ) + } + + } else { + body = query( + xmlDBStore, + jsonDBStore, + startResultSeqIndex, + query, + queryCtx, + endResultSeqIndex, + routingContext + ) + } + } + + promise.complete(body) + }.await() + } + + abstract suspend fun serializeResource( + manager: T, revisions: IntArray, nodeId: Long?, + ctx: RoutingContext, + vertxContext: Context + ): String + + abstract suspend fun openDatabase(dbFile: Path): Database + + abstract suspend fun getDBCollection(databaseName: String?, database: Database): W + + abstract fun handleQueryExtra(rtx: R, dbCollection: W, queryContext: SirixQueryContext, jsonSessionDBStore: JsonSessionDBStore) + abstract fun query( + xmlDBStore: XmlSessionDBStore, + jsonDBStore: JsonSessionDBStore, + startResultSeqIndex: Long?, + query: String, + queryCtx: SirixQueryContext, + endResultSeqIndex: Long?, + routingContext: RoutingContext + ): String +} diff --git a/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/json/JsonGet.kt b/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/json/JsonGet.kt index a727a1afd..cb62e70fb 100644 --- a/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/json/JsonGet.kt +++ b/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/json/JsonGet.kt @@ -1,5 +1,7 @@ package io.sirix.rest.crud.json +import io.brackit.query.jdm.StructuredItemCollection +import io.brackit.query.jdm.node.TemporalNodeCollection import io.vertx.core.Context import io.vertx.core.Promise import io.vertx.core.http.HttpHeaders @@ -12,7 +14,10 @@ import io.vertx.ext.web.RoutingContext import io.vertx.kotlin.coroutines.await import io.sirix.access.Databases import io.sirix.api.Database +import io.sirix.api.json.JsonNodeReadOnlyTrx +import io.sirix.api.json.JsonNodeTrx import io.sirix.api.json.JsonResourceSession +import io.sirix.api.xml.XmlResourceSession import io.sirix.rest.crud.PermissionCheckingQuery import io.sirix.rest.crud.QuerySerializer import io.sirix.rest.crud.Revisions @@ -21,223 +26,97 @@ import io.sirix.service.json.serialize.JsonRecordSerializer import io.sirix.service.json.serialize.JsonSerializer import io.sirix.query.JsonDBSerializer import io.sirix.query.SirixCompileChain +import io.sirix.rest.crud.AbstractGetHandler import io.sirix.query.SirixQueryContext import io.sirix.query.json.* import io.sirix.query.node.BasicXmlDBStore +import io.sirix.query.node.XmlDBCollection +import io.vertx.core.json.Json import java.io.StringWriter import java.nio.file.Path -class JsonGet(private val location: Path, private val keycloak: OAuth2Auth, private val authz: AuthorizationProvider) { - suspend fun handle(ctx: RoutingContext): Route { - val context = ctx.vertx().orCreateContext - val databaseName = ctx.pathParam("database") - val resource = ctx.pathParam("resource") - val jsonBody = ctx.body().asJsonObject() - val query: String? = ctx.queryParam("query").getOrElse(0) { - jsonBody?.getString("query") - } - - get(databaseName, ctx, resource, query, context, ctx.get("user") as User, jsonBody) - - return ctx.currentRoute() - } - - private suspend fun get( - databaseName: String, ctx: RoutingContext, resource: String?, query: String?, - vertxContext: Context, user: User, jsonBody: JsonObject? - ) { - val revision: String? = ctx.queryParam("revision").getOrNull(0) - val revisionTimestamp: String? = ctx.queryParam("revision-timestamp").getOrNull(0) - val startRevision: String? = ctx.queryParam("start-revision").getOrNull(0) - val endRevision: String? = ctx.queryParam("end-revision").getOrNull(0) - val startRevisionTimestamp: String? = ctx.queryParam("start-revision-timestamp").getOrNull(0) - val endRevisionTimestamp: String? = ctx.queryParam("end-revision-timestamp").getOrNull(0) - - val nodeId: String? = ctx.queryParam("nodeId").getOrNull(0) - - var body: String? - - val database = Databases.openJsonDatabase(location.resolve(databaseName)) - - database.use { - val manager = database.beginResourceSession(resource) - - manager.use { - body = if (query.isNullOrEmpty()) { - val revisions: IntArray = - Revisions.getRevisionsToSerialize( - startRevision, endRevision, startRevisionTimestamp, - endRevisionTimestamp, manager, revision, revisionTimestamp - ) - - serializeResource(manager, revisions, nodeId?.toLongOrNull(), ctx, vertxContext) - } else { - queryResource( - databaseName, database, revision, revisionTimestamp, manager, ctx, nodeId, query, - vertxContext, user, jsonBody - ) - } - } - } - - if (body != null) { - ctx.response().end(body) - } else { - ctx.response().end() - } - } - - private suspend fun queryResource( - databaseName: String?, database: Database, revision: String?, - revisionTimestamp: String?, manager: JsonResourceSession, ctx: RoutingContext, - nodeId: String?, query: String, vertxContext: Context, user: User, jsonBody: JsonObject? - ): String? { - val dbCollection = JsonDBCollection(databaseName, database) - - dbCollection.use { - val revisionNumber = Revisions.getRevisionNumber(revision, revisionTimestamp, manager) - - val startResultSeqIndex = ctx.queryParam("startResultSeqIndex").getOrElse(0) { null } - val endResultSeqIndex = ctx.queryParam("endResultSeqIndex").getOrElse(0) { null } - - return xquery( - manager, - dbCollection, - nodeId, - revisionNumber, - query, - ctx, - vertxContext, - user, - startResultSeqIndex?.toLong(), - endResultSeqIndex?.toLong(), - jsonBody - ) - } - } - - suspend fun xquery( - manager: JsonResourceSession?, - dbCollection: JsonDBCollection?, - nodeId: String?, - revisionNumber: IntArray?, query: String, routingContext: RoutingContext, vertxContext: Context, - user: User, startResultSeqIndex: Long?, endResultSeqIndex: Long?, jsonBody: JsonObject? - ): String? { - return vertxContext.executeBlocking { promise: Promise -> - // Initialize queryResource context and store. - val jsonDBStore = JsonSessionDBStore( - routingContext, - BasicJsonDBStore.newBuilder().storeDeweyIds(true).build(), - user, - authz - ) - val xmlDBStore = - XmlSessionDBStore(routingContext, BasicXmlDBStore.newBuilder().storeDeweyIds(true).build(), user, authz) - - val commitMessage = routingContext.queryParam("commitMessage").getOrElse(0) { - jsonBody?.getString("commitMessage") - } - val commitTimestampAsString = routingContext.queryParam("commitTimestamp").getOrElse(0) { - jsonBody?.getString("commitTimestamp") - } - val commitTimestamp = if (commitTimestampAsString == null) { - null - } else { - Revisions.parseRevisionTimestamp(commitTimestampAsString).toInstant() - } - - val queryCtx = SirixQueryContext.createWithJsonStoreAndNodeStoreAndCommitStrategy( - xmlDBStore, - jsonDBStore, - SirixQueryContext.CommitStrategy.AUTO, - commitMessage, - commitTimestamp - ) - - var body: String? = null - - queryCtx.use { - if (manager != null && dbCollection != null && revisionNumber != null) { - val rtx = manager.beginNodeReadOnlyTrx(revisionNumber[0]) - - rtx.use { - if (nodeId == null) { - rtx.moveToFirstChild() - } else { - rtx.moveTo(nodeId.toLong()) - } - - val jsonItem = JsonItemFactory() - .getSequence(rtx, dbCollection) - - if (jsonItem != null) { - queryCtx.contextItem = jsonItem - - when (jsonItem) { - is AbstractJsonDBArray<*> -> { - jsonItem.collection.setJsonDBStore(jsonDBStore) - jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) - } - - is JsonDBObject -> { - jsonItem.collection.setJsonDBStore(jsonDBStore) - jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) - } - - is AtomicBooleanJsonDBItem -> { - jsonItem.collection.setJsonDBStore(jsonDBStore) - jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) - } - - is AtomicStrJsonDBItem -> { - jsonItem.collection.setJsonDBStore(jsonDBStore) - jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) - } - - is AtomicNullJsonDBItem -> { - jsonItem.collection.setJsonDBStore(jsonDBStore) - jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) - } - - is NumericJsonDBItem -> { - jsonItem.collection.setJsonDBStore(jsonDBStore) - jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) - } - - else -> { - throw IllegalStateException("Node type not known.") - } - } - } - - body = query( - xmlDBStore, - jsonDBStore, - startResultSeqIndex, - query, - queryCtx, - endResultSeqIndex, - routingContext - ) - } - } else { - body = query( - xmlDBStore, - jsonDBStore, - startResultSeqIndex, - query, - queryCtx, - endResultSeqIndex, - routingContext - ) - } - } - - promise.complete(body) - }.await() - } - - private fun query( +class JsonGet(private val location: Path, private val keycloak: OAuth2Auth, private val authz: AuthorizationProvider): AbstractGetHandler (location, authz) { +// override suspend fun xquery( +// manager: JsonResourceSession?, +// dbCollection: JsonDBCollection?, +// nodeId: String?, +// revisionNumber: IntArray?, query: String, routingContext: RoutingContext, vertxContext: Context, +// user: User, startResultSeqIndex: Long?, endResultSeqIndex: Long?, jsonBody: JsonObject? +// ): String? { +// return vertxContext.executeBlocking { promise: Promise -> +// // Initialize queryResource context and store. +// val jsonDBStore = JsonSessionDBStore( +// routingContext, +// BasicJsonDBStore.newBuilder().storeDeweyIds(true).build(), +// user, +// authz +// ) +// val xmlDBStore = +// XmlSessionDBStore(routingContext, BasicXmlDBStore.newBuilder().storeDeweyIds(true).build(), user, authz) +// +// val commitMessage = routingContext.queryParam("commitMessage").getOrElse(0) { +// jsonBody?.getString("commitMessage") +// } +// val commitTimestampAsString = routingContext.queryParam("commitTimestamp").getOrElse(0) { +// jsonBody?.getString("commitTimestamp") +// } +// val commitTimestamp = if (commitTimestampAsString == null) { +// null +// } else { +// Revisions.parseRevisionTimestamp(commitTimestampAsString).toInstant() +// } +// +// val queryCtx = SirixQueryContext.createWithJsonStoreAndNodeStoreAndCommitStrategy( +// xmlDBStore, +// jsonDBStore, +// SirixQueryContext.CommitStrategy.AUTO, +// commitMessage, +// commitTimestamp +// ) +// +// var body: String? = null +// +// queryCtx.use { +// if (manager != null && dbCollection != null && revisionNumber != null) { +// val rtx = manager.beginNodeReadOnlyTrx(revisionNumber[0]) +// +// rtx.use { +// if (nodeId == null) { +// rtx.moveToFirstChild() +// } else { +// rtx.moveTo(nodeId.toLong()) +// } +// +// handleQueryExtra(rtx, dbCollection, queryCtx, jsonDBStore) +// +// body = query( +// xmlDBStore, +// jsonDBStore, +// startResultSeqIndex, +// query, +// queryCtx, +// endResultSeqIndex, +// routingContext +// ) +// } +// } else { +// body = query( +// xmlDBStore, +// jsonDBStore, +// startResultSeqIndex, +// query, +// queryCtx, +// endResultSeqIndex, +// routingContext +// ) +// } +// } +// +// promise.complete(body) +// }.await() +// } + + override fun query( xmlDBStore: XmlSessionDBStore, jsonDBStore: JsonSessionDBStore, startResultSeqIndex: Long?, @@ -303,7 +182,7 @@ class JsonGet(private val location: Path, private val keycloak: OAuth2Auth, priv } } - private suspend fun serializeResource( + override suspend fun serializeResource( manager: JsonResourceSession, revisions: IntArray, nodeId: Long?, ctx: RoutingContext, vertxContext: Context @@ -398,4 +277,57 @@ class JsonGet(private val location: Path, private val keycloak: OAuth2Auth, priv return serializedString!! } + + override suspend fun openDatabase(dbFile: Path): Database { + return Databases.openJsonDatabase(dbFile) + } + + override suspend fun getDBCollection(databaseName: String?, database: Database): JsonDBCollection { + return JsonDBCollection(databaseName, database) + } + + override fun handleQueryExtra(rtx: JsonNodeReadOnlyTrx, dbCollection: JsonDBCollection, queryCtx: SirixQueryContext, jsonDBStore: JsonSessionDBStore) { + val jsonItem = JsonItemFactory() + .getSequence(rtx, dbCollection) + + if (jsonItem != null) { + queryCtx.contextItem = jsonItem + + when (jsonItem) { + is AbstractJsonDBArray<*> -> { + jsonItem.collection.setJsonDBStore(jsonDBStore) + jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) + } + + is JsonDBObject -> { + jsonItem.collection.setJsonDBStore(jsonDBStore) + jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) + } + + is AtomicBooleanJsonDBItem -> { + jsonItem.collection.setJsonDBStore(jsonDBStore) + jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) + } + + is AtomicStrJsonDBItem -> { + jsonItem.collection.setJsonDBStore(jsonDBStore) + jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) + } + + is AtomicNullJsonDBItem -> { + jsonItem.collection.setJsonDBStore(jsonDBStore) + jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) + } + + is NumericJsonDBItem -> { + jsonItem.collection.setJsonDBStore(jsonDBStore) + jsonDBStore.addDatabase(jsonItem.collection, jsonItem.collection.database) + } + + else -> { + throw IllegalStateException("Node type not known.") + } + } + } + } } diff --git a/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/xml/XmlGet.kt b/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/xml/XmlGet.kt index ee7becd2f..558280740 100644 --- a/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/xml/XmlGet.kt +++ b/bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/xml/XmlGet.kt @@ -1,5 +1,7 @@ package io.sirix.rest.crud.xml +import io.brackit.query.jdm.StructuredItemCollection +import io.brackit.query.jdm.node.TemporalNodeCollection import io.vertx.core.Context import io.vertx.core.Promise import io.vertx.core.http.HttpHeaders @@ -14,6 +16,8 @@ import io.vertx.kotlin.coroutines.dispatcher import kotlinx.coroutines.withContext import io.sirix.access.Databases import io.sirix.api.Database +import io.sirix.api.json.JsonNodeReadOnlyTrx +import io.sirix.api.xml.XmlNodeReadOnlyTrx import io.sirix.api.xml.XmlResourceSession import io.sirix.rest.crud.PermissionCheckingQuery import io.sirix.rest.crud.QuerySerializer @@ -23,7 +27,8 @@ import io.sirix.service.xml.serialize.XmlSerializer import io.sirix.query.SirixCompileChain import io.sirix.query.SirixQueryContext import io.sirix.query.XmlDBSerializer -import io.sirix.query.json.BasicJsonDBStore +import io.sirix.query.json.* +import io.sirix.rest.crud.AbstractGetHandler import io.sirix.query.node.BasicXmlDBStore import io.sirix.query.node.XmlDBCollection import io.sirix.query.node.XmlDBNode @@ -31,180 +36,89 @@ import java.io.ByteArrayOutputStream import java.io.PrintStream import java.nio.file.Path -class XmlGet(private val location: Path, private val keycloak: OAuth2Auth, private val authz: AuthorizationProvider) { - suspend fun handle(ctx: RoutingContext): Route { - val context = ctx.vertx().orCreateContext - val databaseName: String = ctx.pathParam("database") - val resource: String = ctx.pathParam("resource") - val jsonBody = ctx.body().asJsonObject() - val query: String? = ctx.queryParam("query").getOrElse(0) { - jsonBody?.getString("query") - } - - withContext(context.dispatcher()) { - get(databaseName, ctx, resource, query, context, ctx.get("user") as User, jsonBody) - } - - return ctx.currentRoute() - } - - private suspend fun get( - databaseName: String, ctx: RoutingContext, resource: String, query: String?, - vertxContext: Context, user: User, jsonBody: JsonObject? - ) { - val revision: String? = ctx.queryParam("revision").getOrNull(0) - val revisionTimestamp: String? = ctx.queryParam("revision-timestamp").getOrNull(0) - val startRevision: String? = ctx.queryParam("start-revision").getOrNull(0) - val endRevision: String? = ctx.queryParam("end-revision").getOrNull(0) - val startRevisionTimestamp: String? = ctx.queryParam("start-revision-timestamp").getOrNull(0) - val endRevisionTimestamp: String? = ctx.queryParam("end-revision-timestamp").getOrNull(0) - - val nodeId: String? = ctx.queryParam("nodeId").getOrNull(0) - - var body: String? - - val database = Databases.openXmlDatabase(location.resolve(databaseName)) - - database.use { - val manager = database.beginResourceSession(resource) - - manager.use { - body = if (query != null && query.isNotEmpty()) { - queryResource( - databaseName, database, revision, revisionTimestamp, manager, ctx, nodeId, query, - vertxContext, user, jsonBody - ) - } else { - val revisions: IntArray = - Revisions.getRevisionsToSerialize( - startRevision, endRevision, startRevisionTimestamp, - endRevisionTimestamp, manager, revision, revisionTimestamp - ) - - serializeResource(manager, revisions, nodeId?.toLongOrNull(), ctx) - } - } - } - - if (body != null) { - ctx.response().end(body) - } else { - ctx.response().end() - } - } - - private suspend fun queryResource( - databaseName: String?, database: Database, revision: String?, - revisionTimestamp: String?, manager: XmlResourceSession, ctx: RoutingContext, - nodeId: String?, query: String, vertxContext: Context, user: User, jsonBody: JsonObject? - ): String? { - val dbCollection = XmlDBCollection(databaseName, database) - - dbCollection.use { - val revisionNumber = Revisions.getRevisionNumber(revision, revisionTimestamp, manager) - val startResultSeqIndex = ctx.queryParam("startResultSeqIndex").getOrElse(0) { null } - val endResultSeqIndex = ctx.queryParam("endResultSeqIndex").getOrElse(0) { null } - - return xquery( - manager, - dbCollection, - nodeId, - revisionNumber, - query, - ctx, - vertxContext, - user, - startResultSeqIndex?.toLong(), - endResultSeqIndex?.toLong(), - jsonBody - ) - } - } - - suspend fun xquery( - manager: XmlResourceSession?, - dbCollection: XmlDBCollection?, - nodeId: String?, - revisionNumber: IntArray?, query: String, routingContext: RoutingContext, vertxContext: Context, - user: User, startResultSeqIndex: Long?, endResultSeqIndex: Long?, jsonBody: JsonObject? - ): String? { - return vertxContext.executeBlocking { promise: Promise -> - // Initialize queryResource context and store. - val jsonDBStore = JsonSessionDBStore( - routingContext, - BasicJsonDBStore.newBuilder().storeDeweyIds(true).build(), - user, - authz - ) - val xmlDBStore = - XmlSessionDBStore(routingContext, BasicXmlDBStore.newBuilder().storeDeweyIds(true).build(), user, authz) - - val commitMessage = routingContext.queryParam("commitMessage").getOrElse(0) { - jsonBody?.getString("commitMessage") - } - val commitTimestampAsString = routingContext.queryParam("commitTimestamp").getOrElse(0) { - jsonBody?.getString("commitTimestamp") - } - val commitTimestamp = if (commitTimestampAsString == null) { - null - } else { - Revisions.parseRevisionTimestamp(commitTimestampAsString).toInstant() - } - - val queryCtx = SirixQueryContext.createWithJsonStoreAndNodeStoreAndCommitStrategy( - xmlDBStore, - jsonDBStore, - SirixQueryContext.CommitStrategy.AUTO, - commitMessage, - commitTimestamp - ) - - var body: String? - - queryCtx.use { - if (manager != null && dbCollection != null && revisionNumber != null) { - val rtx = manager.beginNodeReadOnlyTrx(revisionNumber[0]) - - rtx.use { - if (nodeId == null) { - rtx.moveToFirstChild() - } else { - rtx.moveTo(nodeId.toLong()) - } - - val dbNode = XmlDBNode(rtx, dbCollection) - - queryCtx.contextItem = dbNode - - body = query( - xmlDBStore, - jsonDBStore, - startResultSeqIndex, - query, - queryCtx, - endResultSeqIndex, - routingContext - ) - } - - } else { - body = query( - xmlDBStore, - jsonDBStore, - startResultSeqIndex, - query, - queryCtx, - endResultSeqIndex, - routingContext - ) - } - } - - promise.complete(body) - }.await() - } - - private fun query( +class XmlGet(private val location: Path, private val keycloak: OAuth2Auth, private val authz: AuthorizationProvider): AbstractGetHandler (location, authz) { +// override suspend fun xquery( +// manager: XmlResourceSession?, +// dbCollection: XmlDBCollection?, +// nodeId: String?, +// revisionNumber: IntArray?, query: String, routingContext: RoutingContext, vertxContext: Context, +// user: User, startResultSeqIndex: Long?, endResultSeqIndex: Long?, jsonBody: JsonObject? +// ): String? { +// return vertxContext.executeBlocking { promise: Promise -> +// // Initialize queryResource context and store. +// val jsonDBStore = JsonSessionDBStore( +// routingContext, +// BasicJsonDBStore.newBuilder().storeDeweyIds(true).build(), +// user, +// authz +// ) +// val xmlDBStore = +// XmlSessionDBStore(routingContext, BasicXmlDBStore.newBuilder().storeDeweyIds(true).build(), user, authz) +// +// val commitMessage = routingContext.queryParam("commitMessage").getOrElse(0) { +// jsonBody?.getString("commitMessage") +// } +// val commitTimestampAsString = routingContext.queryParam("commitTimestamp").getOrElse(0) { +// jsonBody?.getString("commitTimestamp") +// } +// val commitTimestamp = if (commitTimestampAsString == null) { +// null +// } else { +// Revisions.parseRevisionTimestamp(commitTimestampAsString).toInstant() +// } +// +// val queryCtx = SirixQueryContext.createWithJsonStoreAndNodeStoreAndCommitStrategy( +// xmlDBStore, +// jsonDBStore, +// SirixQueryContext.CommitStrategy.AUTO, +// commitMessage, +// commitTimestamp +// ) +// +// var body: String? +// +// queryCtx.use { +// if (manager != null && dbCollection != null && revisionNumber != null) { +// val rtx = manager.beginNodeReadOnlyTrx(revisionNumber[0]) +// +// rtx.use { +// if (nodeId == null) { +// rtx.moveToFirstChild() +// } else { +// rtx.moveTo(nodeId.toLong()) +// } +// +// handleQueryExtra(rtx, dbCollection, queryCtx, jsonDBStore) +// +// body = query( +// xmlDBStore, +// jsonDBStore, +// startResultSeqIndex, +// query, +// queryCtx, +// endResultSeqIndex, +// routingContext +// ) +// } +// +// } else { +// body = query( +// xmlDBStore, +// jsonDBStore, +// startResultSeqIndex, +// query, +// queryCtx, +// endResultSeqIndex, +// routingContext +// ) +// } +// } +// +// promise.complete(body) +// }.await() +// } + + override fun query( xmlDBStore: XmlSessionDBStore, jsonDBStore: JsonSessionDBStore, startResultSeqIndex: Long?, @@ -274,9 +188,10 @@ class XmlGet(private val location: Path, private val keycloak: OAuth2Auth, priva } } - private fun serializeResource( + override suspend fun serializeResource( manager: XmlResourceSession, revisions: IntArray, nodeId: Long?, - ctx: RoutingContext + ctx: RoutingContext, + vertxContext: Context ): String { val out = ByteArrayOutputStream() @@ -291,4 +206,19 @@ class XmlGet(private val location: Path, private val keycloak: OAuth2Auth, priva return XmlSerializeHelper().serializeXml(serializer, out, ctx, manager, nodeId) } + + override fun handleQueryExtra(rtx: XmlNodeReadOnlyTrx, dbCollection: XmlDBCollection, queryCtx: SirixQueryContext, jsonDBStore: JsonSessionDBStore) { + val dbNode = XmlDBNode(rtx, dbCollection) + + queryCtx.contextItem = dbNode + } + + + override suspend fun openDatabase(dbFile: Path): Database { + return Databases.openXmlDatabase(dbFile) + } + + override suspend fun getDBCollection(databaseName: String?, database: Database): XmlDBCollection { + return XmlDBCollection(databaseName, database) + } } \ No newline at end of file