From 7d93ffdef8c70c2ff8fc563953aba22e32fc5b88 Mon Sep 17 00:00:00 2001 From: Tim Linschoten Date: Wed, 6 Nov 2024 15:43:04 +0100 Subject: [PATCH 1/4] Exposed the parseEventRequest method to the deserialisation so it can be reused (#1799) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Exposed the parseEventRequest method so the desirialisation can be reused --------- Co-authored-by: “Tim <“Tim.Linschoten@ing.com”> --- .../javadsl/BakerWithHttpResponse.scala | 87 ++++++++++++++----- 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/http/baker-http-server/src/main/scala/com/ing/baker/http/server/javadsl/BakerWithHttpResponse.scala b/http/baker-http-server/src/main/scala/com/ing/baker/http/server/javadsl/BakerWithHttpResponse.scala index 36d3215a8..4c2bd3078 100644 --- a/http/baker-http-server/src/main/scala/com/ing/baker/http/server/javadsl/BakerWithHttpResponse.scala +++ b/http/baker-http-server/src/main/scala/com/ing/baker/http/server/javadsl/BakerWithHttpResponse.scala @@ -36,7 +36,7 @@ class BakerWithHttpResponse(val baker: Baker, ec: ExecutionContext) extends Lazy json <- parse(recipe).toOption encodedRecipe <- json.as[EncodedRecipe].toOption } yield RecipeLoader.fromBytes(encodedRecipe.base64.getBytes(Charset.forName("UTF-8"))).unsafeToFuture()) - .map(_.flatMap(recipe => baker.addRecipe(recipe, validate = false).toBakerResultScalaFuture)) + .map(_.flatMap(recipe => baker.addRecipe(recipe, validate = false).mapToBakerResult)) .getOrElse(Future.failed(new IllegalStateException("Error adding recipe"))) }.toJava.toCompletableFuture @@ -87,6 +87,41 @@ class BakerWithHttpResponse(val baker: Baker, ec: ExecutionContext) extends Lazy } } + def parseEventRequest(eventJson: String): Either[BakerException, EventInstance] = { + parse(eventJson) match { + case Left(_) => + logger.error("Failure parsing of EventInstance") + Left(BakerException.UnexpectedException("Failure parsing of EventInstance")) + case Right(json: io.circe.Json) => + json.as[EventInstance] match { + case Left(_) => + logger.error("Failure decoding json to EventInstance") + Left(BakerException.UnexpectedException("Failure decoding json to EventInstance")) + case Right(eventInstance: EventInstance) => + Right(eventInstance) + } + } + } + + def toBakerResult[A](f: Future[A])(implicit encoder: Encoder[A]): Future[String] = { + f.map { + case () => BakerResult.Ack + case a => BakerResult(a) + }.recover { + case e: BakerException => BakerResult(e) + case e: Throwable => + val errorId = UUID.randomUUID().toString + logger.error(s"Unexpected exception happened when calling Baker (id='$errorId').", e) + BakerResult(BakerException.UnexpectedException(errorId)) + }.map(bakerResultEncoder.apply(_).noSpaces) + } + + def toBakerResultJFuture[A](f: Future[A])(implicit encoder: Encoder[A]): JFuture[String] = { + toBakerResult(f)(encoder) + .toJava + .toCompletableFuture + } + /** * Do calls for a specific instance. */ @@ -124,34 +159,44 @@ class BakerWithHttpResponse(val baker: Baker, ec: ExecutionContext) extends Lazy def resolveInteraction(interactionName: String, eventJson: String): JFuture[String] = parseEventAndExecute(eventJson, baker.resolveInteraction(recipeInstanceId, interactionName, _)) + + def fireAndResolveWhenReceivedFromEventInstance(eventInstance: EventInstance, maybeCorrelationId: Optional[String]): JFuture[String] = + eventInstanceExecute(eventInstance, baker.fireEventAndResolveWhenReceived(recipeInstanceId, _, toOption(maybeCorrelationId))) + + def fireAndResolveWhenCompletedFromEventInstance(eventInstance: EventInstance, maybeCorrelationId: Optional[String]): JFuture[String] = + eventInstanceExecute(eventInstance, baker.fireEventAndResolveWhenCompleted(recipeInstanceId, _, toOption(maybeCorrelationId))) + + def fireAndResolveOnEventFromEventInstance(eventInstance: EventInstance, event: String, maybeCorrelationId: Optional[String]): JFuture[String] = + eventInstanceExecute(eventInstance, baker.fireEventAndResolveOnEvent(recipeInstanceId, _, event, toOption(maybeCorrelationId))) + + def resolveInteractionFromEventInstance(interactionName: String, eventInstance: EventInstance): JFuture[String] = + eventInstanceExecute(eventInstance, baker.resolveInteraction(recipeInstanceId, interactionName, _)) } private def toOption[T](opt: Optional[T]): Option[T] = if (opt.isPresent) Some(opt.get()) else None - private def parseEventAndExecute[A](eventJson: String, f: EventInstance => Future[A])(implicit encoder: Encoder[A]): JFuture[String] = ( - for { - json <- parse(eventJson) - eventInstance <- json.as[EventInstance] - } yield { - f(eventInstance).toBakerResultScalaFuture - }).getOrElse(Future.failed(new IllegalArgumentException("Can't process event"))).toJava.toCompletableFuture + private def eventInstanceExecute[A](eventInstance: EventInstance, f: EventInstance => Future[A])(implicit encoder: Encoder[A]): JFuture[String] = + f(eventInstance).mapToBakerResultJFuture + + private def parseEventAndExecute[A](eventJson: String, f: EventInstance => Future[A])(implicit encoder: Encoder[A]): JFuture[String] = + parseEventRequest(eventJson) match { + case Right(eventInstance: EventInstance) => + f(eventInstance).toBakerResult + case Left(bakerException: BakerException) => + Future(bakerResultEncoder.apply(BakerResult(bakerException)).noSpaces) + .toJava + .toCompletableFuture + } private implicit class BakerResultHelperJavaFuture[A](f: => Future[A])(implicit encoder: Encoder[A]) { - def toBakerResult: JFuture[String] = f.toBakerResultScalaFuture.toJava.toCompletableFuture + def toBakerResult: JFuture[String] = f.mapToBakerResultJFuture } private implicit class BakerResultHelperScalaFuture[A](f: => Future[A])(implicit encoder: Encoder[A]) { - def toBakerResultScalaFuture(implicit encoder: Encoder[A]): Future[String] = { - f.map { - case () => BakerResult.Ack - case a => BakerResult(a) - }.recover { - case e: BakerException => BakerResult(e) - case e: Throwable => - val errorId = UUID.randomUUID().toString - logger.error(s"Unexpected exception happened when calling Baker (id='$errorId').", e) - BakerResult(BakerException.UnexpectedException(errorId)) - }.map(bakerResultEncoder.apply(_).noSpaces) - } + def mapToBakerResult(implicit encoder: Encoder[A]): Future[String] = + toBakerResult(f)(encoder) + + def mapToBakerResultJFuture(implicit encoder: Encoder[A]): JFuture[String] = + toBakerResultJFuture(f)(encoder) } } From c69ff5a75a0b20a7dbdfd48c91f72e65e6007dcd Mon Sep 17 00:00:00 2001 From: Scala Steward <43047562+scala-steward@users.noreply.github.com> Date: Mon, 11 Nov 2024 10:36:51 +0100 Subject: [PATCH 2/4] Update sbt to 1.10.5 (#1801) --- project/build.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/build.properties b/project/build.properties index 09feeeed5..db1723b08 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.10.4 +sbt.version=1.10.5 From c24fc758d15fc37868698a055202c8e75ad7c509 Mon Sep 17 00:00:00 2001 From: Scala Steward <43047562+scala-steward@users.noreply.github.com> Date: Mon, 18 Nov 2024 09:32:12 +0100 Subject: [PATCH 3/4] Update netty-handler to 4.1.115.Final (#1802) --- project/Dependencies.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index 022b3da77..f23e5bff1 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -141,7 +141,7 @@ object Dependencies { val jacksonDatabind = "com.fasterxml.jackson.core" % "jackson-databind" % "2.18.1" val jacksonCore = "com.fasterxml.jackson.core" % "jackson-core" % "2.18.1" val jawnParser = "org.typelevel" %% "jawn-parser" % "1.6.0" - val nettyHandler = "io.netty" % "netty-handler" % "4.1.114.Final" + val nettyHandler = "io.netty" % "netty-handler" % "4.1.115.Final" private val bouncycastleVersion = "1.79" From b0b03b936db7ed8987494356eaa92fdc071fe512 Mon Sep 17 00:00:00 2001 From: Scala Steward <43047562+scala-steward@users.noreply.github.com> Date: Mon, 18 Nov 2024 09:33:03 +0100 Subject: [PATCH 4/4] Update spring-context to 6.1.15 (#1803) --- project/Dependencies.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/Dependencies.scala b/project/Dependencies.scala index f23e5bff1..b2c031487 100644 --- a/project/Dependencies.scala +++ b/project/Dependencies.scala @@ -14,7 +14,7 @@ object Dependencies { val catsEffectVersion = "2.5.5" val catsCoreVersion = "2.12.0" val scalapbVersion = scalapb.compiler.Version.scalapbVersion - val springVersion = "6.1.14" + val springVersion = "6.1.15" val springBootVersion = "2.6.1" val akkaInmemoryJournal = ("com.github.dnvriend" %% "akka-persistence-inmemory" % "2.5.15.2")