You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2024/06/05 02:15:37 UTC

(james-project) branch master updated: [REFACTORING] JMAP: Problem details already entails the status code

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git


The following commit(s) were added to refs/heads/master by this push:
     new 476a19c140 [REFACTORING] JMAP: Problem details already entails the status code
476a19c140 is described below

commit 476a19c140e24193c3a84d1b3f681447e1e070b9
Author: Benoit TELLIER <bt...@linagora.com>
AuthorDate: Tue Jun 4 09:47:49 2024 +0200

    [REFACTORING] JMAP: Problem details already entails the status code
    
    Avoid duplicating it.
---
 .../apache/james/jmap/routes/DownloadRoutes.scala    | 20 ++++++--------------
 .../org/apache/james/jmap/routes/SessionRoutes.scala | 10 ++++------
 .../org/apache/james/jmap/routes/UploadRoutes.scala  | 18 +++++-------------
 3 files changed, 15 insertions(+), 33 deletions(-)

diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/DownloadRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/DownloadRoutes.scala
index ff3074d629..40e1119709 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/DownloadRoutes.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/DownloadRoutes.scala
@@ -271,23 +271,15 @@ class DownloadRoutes @Inject()(@Named(InjectionKeys.RFC_8621) val authenticator:
       .flatMap(mailboxSession => getIfOwner(request, response, mailboxSession))
       .onErrorResume {
         case _: ForbiddenException | _: AccountNotFoundException =>
-          respondDetails(response,
-            ProblemDetails(status = FORBIDDEN, detail = "You cannot download in others accounts"),
-            FORBIDDEN)
+          respondDetails(response, ProblemDetails(status = FORBIDDEN, detail = "You cannot download in others accounts"))
         case e: UnauthorizedException =>
           LOGGER.warn("Unauthorized", e)
-          respondDetails(e.addHeaders(response),
-            ProblemDetails(status = UNAUTHORIZED, detail = e.getMessage),
-            UNAUTHORIZED)
+          respondDetails(e.addHeaders(response), ProblemDetails(status = UNAUTHORIZED, detail = e.getMessage))
         case _: BlobNotFoundException =>
-          respondDetails(response,
-            ProblemDetails(status = NOT_FOUND, detail = "The resource could not be found"),
-            NOT_FOUND)
+          respondDetails(response, ProblemDetails(status = NOT_FOUND, detail = "The resource could not be found"))
         case e =>
           LOGGER.error("Unexpected error upon download {}", request.uri(), e)
-          respondDetails(response,
-            ProblemDetails(status = INTERNAL_SERVER_ERROR, detail = e.getMessage),
-            INTERNAL_SERVER_ERROR)
+          respondDetails(response, ProblemDetails(status = INTERNAL_SERVER_ERROR, detail = e.getMessage))
       }
       .subscribeOn(ReactorUtils.BLOCKING_CALL_WRAPPER)
       .asJava()
@@ -369,12 +361,12 @@ class DownloadRoutes @Inject()(@Named(InjectionKeys.RFC_8621) val authenticator:
       .flatMap(_.asScala)
       .headOption
 
-  private def respondDetails(httpServerResponse: HttpServerResponse, details: ProblemDetails, statusCode: HttpResponseStatus = BAD_REQUEST): SMono[Unit] =
+  private def respondDetails(httpServerResponse: HttpServerResponse, details: ProblemDetails): SMono[Unit] =
     SMono.fromCallable(() => ResponseSerializer.serialize(details))
       .map(Json.stringify)
       .map(_.getBytes(StandardCharsets.UTF_8))
       .flatMap(bytes =>
-        SMono.fromPublisher(httpServerResponse.status(statusCode)
+        SMono.fromPublisher(httpServerResponse.status(details.status)
           .header(CONTENT_TYPE, JSON_CONTENT_TYPE)
           .header(CONTENT_LENGTH, Integer.toString(bytes.length))
           .sendByteArray(SMono.just(bytes))
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/SessionRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/SessionRoutes.scala
index f3f3dc4a25..f1fdafdd3e 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/SessionRoutes.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/SessionRoutes.scala
@@ -108,22 +108,20 @@ class SessionRoutes @Inject()(@Named(InjectionKeys.RFC_8621) val authenticator:
         .sendByteArray(SMono.just(bytes))
         .`then`()))
 
-  def errorHandling(throwable: Throwable, response: HttpServerResponse): Mono[Void] =
+  private def errorHandling(throwable: Throwable, response: HttpServerResponse): Mono[Void] =
     throwable match {
       case e: UnauthorizedException =>
         LOGGER.warn("Unauthorized", e)
         respondDetails(e.addHeaders(response),
-          ProblemDetails(status = UNAUTHORIZED, detail = e.getMessage),
-          UNAUTHORIZED)
+          ProblemDetails(status = UNAUTHORIZED, detail = e.getMessage))
       case e =>
         LOGGER.error("Unexpected error upon requesting session", e)
         respondDetails(response,
-          ProblemDetails(status = INTERNAL_SERVER_ERROR, detail = e.getMessage),
-          INTERNAL_SERVER_ERROR)
+          ProblemDetails(status = INTERNAL_SERVER_ERROR, detail = e.getMessage))
     }
 
 
-  private def respondDetails(httpServerResponse: HttpServerResponse, details: ProblemDetails, statusCode: HttpResponseStatus = BAD_REQUEST): Mono[Void] =
+  private def respondDetails(httpServerResponse: HttpServerResponse, details: ProblemDetails): Mono[Void] =
     SMono.fromCallable(() => ResponseSerializer.serialize(details).toString)
       .map(_.getBytes(StandardCharsets.UTF_8))
       .flatMap(bytes =>
diff --git a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/UploadRoutes.scala b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/UploadRoutes.scala
index b80d6ce2a4..3b73a1401c 100644
--- a/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/UploadRoutes.scala
+++ b/server/protocols/jmap-rfc-8621/src/main/scala/org/apache/james/jmap/routes/UploadRoutes.scala
@@ -91,22 +91,14 @@ class UploadRoutes @Inject()(@Named(InjectionKeys.RFC_8621) val authenticator: A
         .onErrorResume {
           case e: UnauthorizedException =>
             LOGGER.warn("Unauthorized", e)
-            respondDetails(e.addHeaders(response),
-              ProblemDetails(status = UNAUTHORIZED, detail = e.getMessage),
-              UNAUTHORIZED)
+            respondDetails(e.addHeaders(response), ProblemDetails(status = UNAUTHORIZED, detail = e.getMessage))
           case _: TooBigUploadException =>
-            respondDetails(response,
-              ProblemDetails(status = BAD_REQUEST, detail = "Attempt to upload exceed max size"),
-              BAD_REQUEST)
+            respondDetails(response, ProblemDetails(status = BAD_REQUEST, detail = "Attempt to upload exceed max size"))
           case _: ForbiddenException | _: AccountNotFoundException =>
-            respondDetails(response,
-              ProblemDetails(status = FORBIDDEN, detail = "Upload to other accounts is forbidden"),
-              FORBIDDEN)
+            respondDetails(response, ProblemDetails(status = FORBIDDEN, detail = "Upload to other accounts is forbidden"))
           case e =>
             LOGGER.error("Unexpected error upon upload {}", request.uri(), e)
-            respondDetails(response,
-              ProblemDetails(status = INTERNAL_SERVER_ERROR, detail = e.getMessage),
-              INTERNAL_SERVER_ERROR)
+            respondDetails(response, ProblemDetails(status = INTERNAL_SERVER_ERROR, detail = e.getMessage))
         }
         .asJava()
         .subscribeOn(ReactorUtils.BLOCKING_CALL_WRAPPER)
@@ -168,7 +160,7 @@ class UploadRoutes @Inject()(@Named(InjectionKeys.RFC_8621) val authenticator: A
 
   private def asBlobId(uploadId: UploadId): BlobId = BlobId.of(s"uploads-${uploadId.asString()}" ).get
 
-  private def respondDetails(httpServerResponse: HttpServerResponse, details: ProblemDetails, statusCode: HttpResponseStatus = BAD_REQUEST): SMono[Void] =
+  private def respondDetails(httpServerResponse: HttpServerResponse, details: ProblemDetails): SMono[Void] =
     SMono.fromCallable(() => ResponseSerializer.serialize(details).toString)
       .map(_.getBytes(StandardCharsets.UTF_8))
       .flatMap(bytes =>


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org