-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AbstractHeadHandler and Refactor [Xml/Json]/Head.kt (#662)
- Loading branch information
1 parent
62d22e9
commit 45b12fe
Showing
3 changed files
with
101 additions
and
147 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
bundles/sirix-rest-api/src/main/kotlin/io/sirix/rest/crud/AbstractHeadHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package io.sirix.rest.crud | ||
|
||
import io.sirix.access.Databases | ||
import io.sirix.access.trx.node.HashType | ||
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.vertx.core.http.HttpHeaders | ||
import io.vertx.ext.web.Route | ||
import io.vertx.ext.web.RoutingContext | ||
import io.vertx.kotlin.coroutines.await | ||
import java.nio.file.Path | ||
import java.time.LocalDateTime | ||
import java.time.ZoneId | ||
|
||
abstract class AbstractHeadHandler< T : ResourceSession<*, *>> ( | ||
private val location: Path){ | ||
suspend fun handle(ctx: RoutingContext): Route { | ||
val databaseName = ctx.pathParam("database") | ||
val resource = ctx.pathParam("resource") | ||
|
||
if (databaseName == null || resource == null) { | ||
throw IllegalStateException("Database name and resource name must be given.") | ||
} | ||
|
||
ctx.vertx().executeBlocking<Unit> { | ||
head(databaseName, ctx, resource) | ||
}.await() | ||
|
||
return ctx.currentRoute() | ||
} | ||
fun head(databaseName: String, ctx: RoutingContext, resource: String) { | ||
val revision = ctx.queryParam("revision").getOrNull(0) | ||
val revisionTimestamp = ctx.queryParam("revision-timestamp").getOrNull(0) | ||
|
||
val nodeId = ctx.queryParam("nodeId").getOrNull(0) | ||
|
||
val database = openDatabase(location.resolve(databaseName)) | ||
|
||
database.use { | ||
val manager = database.beginResourceSession(resource) | ||
|
||
manager.use { | ||
if (manager.resourceConfig.hashType == HashType.NONE) { | ||
ctx.response().putHeader(HttpHeaders.ETAG, "") | ||
} else { | ||
val revisionNumber = getRevisionNumber(revision, revisionTimestamp, manager) | ||
|
||
val rtx = manager.beginNodeReadOnlyTrx(revisionNumber) | ||
|
||
rtx.use { | ||
if (nodeId != null) { | ||
if (!rtx.moveTo(nodeId.toLong())) { | ||
throw IllegalStateException("Node with ID $nodeId doesn't exist.") | ||
} | ||
} else if (rtx.isDocumentRoot) { | ||
(rtx as NodeCursor).moveToFirstChild() | ||
} | ||
|
||
ctx.response().putHeader(HttpHeaders.ETAG, rtx.hash.toString()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
ctx.response().end() | ||
} | ||
private fun getRevisionNumber(rev: String?, revTimestamp: String?, manager: T): Int { | ||
return rev?.toInt() | ||
?: if (revTimestamp != null) { | ||
var revision = getRevisionNumber(manager, revTimestamp) | ||
if (revision == 0) { | ||
++revision | ||
} else { | ||
revision | ||
} | ||
} else { | ||
manager.mostRecentRevisionNumber | ||
} | ||
} | ||
|
||
private fun getRevisionNumber(manager: T, revision: String): Int { | ||
val revisionDateTime = LocalDateTime.parse(revision) | ||
val zdt = revisionDateTime.atZone(ZoneId.systemDefault()) | ||
return manager.getRevisionNumber(zdt.toInstant()) | ||
} | ||
abstract fun openDatabase(dbFile: Path): Database<T> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters