From b9b2246682e22ff15a971f6a17cbcfc33fa6279b Mon Sep 17 00:00:00 2001 From: Raitis Veinbahs Date: Mon, 12 Feb 2024 16:49:44 +0100 Subject: [PATCH] fix TriplestoreServiceInMemory, simplify loadRdfObject along with it --- .../impl/TriplestoreServiceLive.scala | 27 +++++-------------- .../api/TriplestoreServiceInMemory.scala | 12 ++++++--- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/webapi/src/main/scala/org/knora/webapi/store/triplestore/impl/TriplestoreServiceLive.scala b/webapi/src/main/scala/org/knora/webapi/store/triplestore/impl/TriplestoreServiceLive.scala index c812057c36..21a71a9107 100644 --- a/webapi/src/main/scala/org/knora/webapi/store/triplestore/impl/TriplestoreServiceLive.scala +++ b/webapi/src/main/scala/org/knora/webapi/store/triplestore/impl/TriplestoreServiceLive.scala @@ -259,12 +259,13 @@ case class TriplestoreServiceLive( uriBuilder } - inputEntity <- loadRdfObject(elem.path) + rdfContents <- loadRdfObject(elem.path) httpPost <- ZIO.attemptBlocking { - val httpPost = new HttpPost(uriBuilder.build()) - httpPost.setEntity(inputEntity) + val httpPost = new HttpPost(uriBuilder.build()) + val turtleContentType = ContentType.create(mimeTypeTextTurtle, "UTF-8") + httpPost.setEntity(new StringEntity(rdfContents, turtleContentType)) httpPost } responseHandler <- ZIO.attempt(returnInsertGraphDataResponse(graphName)(_)) @@ -278,23 +279,9 @@ case class TriplestoreServiceLive( /** * Load the RdfDataObject into some AbstractHttpEntity from either a relative ../$path or through a resource. */ - private def loadRdfObject(path: String): Task[AbstractHttpEntity] = { - val turtleContentType = ContentType.create(mimeTypeTextTurtle, "UTF-8") - - val relativeFileEntity = { - // Add the input file to the body of the request. - // here we need to tweak the base directory path from "webapi" - // to the parent folder where the files can be found - val inputFile = Paths.get("..", path) - ZIO.attemptBlocking { - if (!Files.exists(inputFile)) - throw BadRequestException(s"File ${inputFile.toAbsolutePath} does not exist") - new FileEntity(inputFile.toFile, turtleContentType) - } - } - - val resourceStringEntity = - ZIO.attemptBlocking(new StringEntity(Source.fromResource(path).mkString, turtleContentType)) + private def loadRdfObject(path: String): Task[String] = { + val relativeFileEntity = ZIO.readFile(Paths.get("..", path)) + val resourceStringEntity = ZIO.attemptBlocking(Source.fromResource(path).mkString) relativeFileEntity.orElse(resourceStringEntity) } diff --git a/webapi/src/test/scala/org/knora/webapi/store/triplestore/api/TriplestoreServiceInMemory.scala b/webapi/src/test/scala/org/knora/webapi/store/triplestore/api/TriplestoreServiceInMemory.scala index b1c00d0e9d..912433bd4b 100644 --- a/webapi/src/test/scala/org/knora/webapi/store/triplestore/api/TriplestoreServiceInMemory.scala +++ b/webapi/src/test/scala/org/knora/webapi/store/triplestore/api/TriplestoreServiceInMemory.scala @@ -22,6 +22,7 @@ import zio.ZIO import zio.ZLayer import zio.macros.accessible +import java.io.InputStream import java.nio.charset.StandardCharsets import java.nio.file.Path import java.nio.file.Paths @@ -210,18 +211,21 @@ final case class TriplestoreServiceInMemory(datasetRef: Ref[Dataset], implicit v } } - private def insertRdfDataObject(elem: RdfDataObject): ZIO[Any, Throwable, Unit] = { - val inputFile = Paths.get("..", elem.path) + private def insertRdfDataObject(elem: RdfDataObject): ZIO[Any, Throwable, Unit] = ZIO.scoped { for { graphName <- checkGraphName(elem) - in <- fileInputStream(inputFile) + in <- loadRdfUrl(elem.path) ds <- getDataSetWithTransaction(ReadWrite.WRITE) model = ds.getNamedModel(graphName) _ = model.read(in, null, "TURTLE") } yield () } - } + + private def loadRdfUrl(path: String): ZIO[Any & Scope, Throwable, InputStream] = + ZIO + .attemptBlocking(Option(getClass.getClassLoader.getResourceAsStream(path)).get) + .orElse(fileInputStream(Paths.get("..", path))) private def checkGraphName(elem: RdfDataObject): Task[String] = checkGraphName(elem.name)