You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by jo...@apache.org on 2021/11/12 17:09:06 UTC

[isis] 05/07: ISIS-2348 amend with specific handling for HTTP 401

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

joergrade pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 1a6b4a871de2a0c7d267d46b2f188ce4e128b33e
Author: Jörg Rade <jo...@kuehne-nagel.com>
AuthorDate: Fri Nov 12 18:00:48 2021 +0100

    ISIS-2348 amend with specific handling for HTTP 401
---
 .../kroviz/core/aggregator/ErrorDispatcher.kt      |  5 +--
 .../Http401ErrorHandler.kt}                        | 26 +++++++--------
 .../isis/client/kroviz/handler/ResponseHandler.kt  |  4 ++-
 .../isis/client/kroviz/to/PlainTransferObjects.kt  | 38 +++++++++++++++++++---
 .../isis/client/kroviz/ui/dialog/ErrorDialog.kt    |  8 +++--
 .../kroviz/snapshots/demo2_0_0/HTTP_ERROR_401.kt}  | 27 ++++++---------
 .../apache/isis/client/kroviz/to/HttpErrorTest.kt  | 38 ++++++++++++++--------
 7 files changed, 92 insertions(+), 54 deletions(-)

diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt
index 35add14..5b595c7 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt
@@ -21,15 +21,16 @@ package org.apache.isis.client.kroviz.core.aggregator
 import org.apache.isis.client.kroviz.core.event.LogEntry
 import org.apache.isis.client.kroviz.core.event.ResourceSpecification
 import org.apache.isis.client.kroviz.to.HttpError
+import org.apache.isis.client.kroviz.to.HttpResponse
 import org.apache.isis.client.kroviz.ui.core.UiManager
 import org.apache.isis.client.kroviz.ui.dialog.ErrorDialog
 
 class ErrorDispatcher : BaseAggregator() {
 
     override fun update(logEntry: LogEntry, subType: String) {
-        val error = logEntry.getTransferObject() as HttpError
+        val error = logEntry.getTransferObject() as HttpResponse
         val url = logEntry.url
-        val message = error.message
+        val message = error.getMessage()
         val reSpec = ResourceSpecification(url)
         UiManager.getEventStore().fault(reSpec, message)
         ErrorDialog(logEntry).open()
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/handler/Http401ErrorHandler.kt
similarity index 53%
copy from incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt
copy to incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/handler/Http401ErrorHandler.kt
index 35add14..2d9333b 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/handler/Http401ErrorHandler.kt
@@ -16,23 +16,21 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-package org.apache.isis.client.kroviz.core.aggregator
+package org.apache.isis.client.kroviz.handler
 
-import org.apache.isis.client.kroviz.core.event.LogEntry
-import org.apache.isis.client.kroviz.core.event.ResourceSpecification
-import org.apache.isis.client.kroviz.to.HttpError
-import org.apache.isis.client.kroviz.ui.core.UiManager
-import org.apache.isis.client.kroviz.ui.dialog.ErrorDialog
+import kotlinx.serialization.json.Json
+import org.apache.isis.client.kroviz.core.aggregator.ErrorDispatcher
+import org.apache.isis.client.kroviz.to.Http401Error
+import org.apache.isis.client.kroviz.to.TransferObject
 
-class ErrorDispatcher : BaseAggregator() {
+class Http401ErrorHandler : BaseHandler() {
 
-    override fun update(logEntry: LogEntry, subType: String) {
-        val error = logEntry.getTransferObject() as HttpError
-        val url = logEntry.url
-        val message = error.message
-        val reSpec = ResourceSpecification(url)
-        UiManager.getEventStore().fault(reSpec, message)
-        ErrorDialog(logEntry).open()
+    override fun doHandle() {
+        logEntry.addAggregator(ErrorDispatcher())
+        update()
     }
 
+    override fun parse(response: String): TransferObject {
+        return Json.decodeFromString(Http401Error.serializer(), response)
+    }
 }
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/handler/ResponseHandler.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/handler/ResponseHandler.kt
index 7391cf0..a02a1d4 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/handler/ResponseHandler.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/handler/ResponseHandler.kt
@@ -42,6 +42,7 @@ object ResponseHandler {
     private var _7a = CollectionHandler()
     private var _8 = MemberHandler()
     private var _9 = HttpErrorHandler()
+    private var _9a = Http401ErrorHandler()
     private var _10 = UserHandler()
     private var _11 = VersionHandler()
     private var _12 = DomainTypesHandler()
@@ -65,7 +66,8 @@ object ResponseHandler {
         _7.successor = _7a
         _7a.successor = _8
         _8.successor = _9
-        _9.successor = _10
+        _9.successor = _9a
+        _9a.successor = _10
         _10.successor = _11
         _11.successor = _12
         _12.successor = _13
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/to/PlainTransferObjects.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/to/PlainTransferObjects.kt
index 2d05e0d..c9e6d14 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/to/PlainTransferObjects.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/to/PlainTransferObjects.kt
@@ -34,12 +34,25 @@ data class DomainTypes(override val links: List<Link> = emptyList(),
                        val extensions: Extensions? = null
 ) : TransferObject, HasLinks
 
+interface HttpResponse {
+    val detail: HttpErrorDetail?
+    fun getMessage(): String
+    fun getStatusCode(): Int
+}
+
 @Serializable
 data class HttpError(
-        val httpStatusCode: Int,
-        val message: String,
-        val detail: HttpErrorDetail? = null
-) : TransferObject
+    private val httpStatusCode: Int,
+    private val message: String,
+    override val detail: HttpErrorDetail? = null
+) : TransferObject, HttpResponse {
+    override fun getMessage() : String {
+        return message
+    }
+    override fun getStatusCode() : Int {
+        return httpStatusCode
+    }
+}
 
 @Serializable
 data class HttpErrorDetail(
@@ -50,6 +63,23 @@ data class HttpErrorDetail(
 ) : TransferObject
 
 @Serializable
+data class Http401Error(
+    private val timestamp: String,
+    private val status: Int,
+    private val error: String,
+    private val path: String,
+    override val detail: HttpErrorDetail? = null
+) : TransferObject, HttpResponse {
+    override fun getMessage(): String {
+        return error + " / " + path + " / " + timestamp
+    }
+    override fun getStatusCode() : Int {
+        return status
+    }
+
+}
+
+@Serializable
 data class Links(
         @SerialName("links") val content: List<Link> = emptyList()
 ) : TransferObject
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/ui/dialog/ErrorDialog.kt b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/ui/dialog/ErrorDialog.kt
index 066ccc9..1015516 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/ui/dialog/ErrorDialog.kt
+++ b/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/ui/dialog/ErrorDialog.kt
@@ -19,7 +19,9 @@
 package org.apache.isis.client.kroviz.ui.dialog
 
 import org.apache.isis.client.kroviz.core.event.LogEntry
+import org.apache.isis.client.kroviz.to.Http401Error
 import org.apache.isis.client.kroviz.to.HttpError
+import org.apache.isis.client.kroviz.to.HttpResponse
 import org.apache.isis.client.kroviz.to.ValueType
 import org.apache.isis.client.kroviz.ui.core.FormItem
 import org.apache.isis.client.kroviz.ui.core.RoDialog
@@ -27,16 +29,16 @@ import org.apache.isis.client.kroviz.ui.core.RoDialog
 class ErrorDialog(val logEntry: LogEntry) : Command() {
 
     fun open() {
-        val error = logEntry.getTransferObject() as HttpError
+        val error = logEntry.getTransferObject() as HttpResponse
         val formItems = mutableListOf<FormItem>()
         formItems.add(FormItem("URL", ValueType.TEXT, logEntry.url))
-        formItems.add(FormItem("Message", ValueType.TEXT, error.message))
+        formItems.add(FormItem("Message", ValueType.TEXT, error.getMessage()))
         val detail = error.detail
         if (detail != null) {
             formItems.add(FormItem("StackTrace", ValueType.TEXT_AREA, toString(detail.element), 10))
             formItems.add(FormItem("Caused by", ValueType.TEXT, detail.causedBy))
         }
-        val label = "HttpError " + error.httpStatusCode.toString()
+        val label = "HttpError " + error.getStatusCode().toString()
         RoDialog(
             caption = label,
             items = formItems,
diff --git a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt b/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/snapshots/demo2_0_0/HTTP_ERROR_401.kt
similarity index 53%
copy from incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt
copy to incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/snapshots/demo2_0_0/HTTP_ERROR_401.kt
index 35add14..1f1d982 100644
--- a/incubator/clients/kroviz/src/main/kotlin/org/apache/isis/client/kroviz/core/aggregator/ErrorDispatcher.kt
+++ b/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/snapshots/demo2_0_0/HTTP_ERROR_401.kt
@@ -16,23 +16,16 @@
  *  specific language governing permissions and limitations
  *  under the License.
  */
-package org.apache.isis.client.kroviz.core.aggregator
+package org.apache.isis.client.kroviz.snapshots.demo2_0_0
 
-import org.apache.isis.client.kroviz.core.event.LogEntry
-import org.apache.isis.client.kroviz.core.event.ResourceSpecification
-import org.apache.isis.client.kroviz.to.HttpError
-import org.apache.isis.client.kroviz.ui.core.UiManager
-import org.apache.isis.client.kroviz.ui.dialog.ErrorDialog
-
-class ErrorDispatcher : BaseAggregator() {
-
-    override fun update(logEntry: LogEntry, subType: String) {
-        val error = logEntry.getTransferObject() as HttpError
-        val url = logEntry.url
-        val message = error.message
-        val reSpec = ResourceSpecification(url)
-        UiManager.getEventStore().fault(reSpec, message)
-        ErrorDialog(logEntry).open()
-    }
+import org.apache.isis.client.kroviz.snapshots.Response
 
+object HTTP_ERROR_401 : Response() {
+    override val url = ""
+    override val str = """{
+        "timestamp": "2021-11-12T15:39:15.039+00:00",
+        "status": 401,
+        "error": "Unauthorized",
+        "path": "/restful/"
+}"""
 }
diff --git a/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/to/HttpErrorTest.kt b/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/to/HttpErrorTest.kt
index f68d730..7aacdd4 100644
--- a/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/to/HttpErrorTest.kt
+++ b/incubator/clients/kroviz/src/test/kotlin/org/apache/isis/client/kroviz/to/HttpErrorTest.kt
@@ -18,9 +18,11 @@
  */
 package org.apache.isis.client.kroviz.to
 
+import org.apache.isis.client.kroviz.handler.Http401ErrorHandler
 import org.apache.isis.client.kroviz.handler.HttpErrorHandler
-import org.apache.isis.client.kroviz.snapshots.demo2_0_0.HTTP_ERROR_500
+import org.apache.isis.client.kroviz.snapshots.demo2_0_0.HTTP_ERROR_401
 import org.apache.isis.client.kroviz.snapshots.demo2_0_0.HTTP_ERROR_405
+import org.apache.isis.client.kroviz.snapshots.demo2_0_0.HTTP_ERROR_500
 import org.apache.isis.client.kroviz.snapshots.simpleapp1_16_0.HTTP_ERROR
 import org.apache.isis.client.kroviz.snapshots.simpleapp1_16_0.HTTP_ERROR_500_UNIQUE_CONSTRAINT_VIOLATION
 import kotlin.test.Test
@@ -34,15 +36,15 @@ class HttpErrorTest {
     fun testDemo500() {
         val jsonStr = HTTP_ERROR_500.str
         val error = HttpErrorHandler().parse(jsonStr) as HttpError
-        val code = error.httpStatusCode
+        val code = error.getStatusCode()
         assertEquals(500, code)
-        assertNotNull(error.message)
+        assertNotNull(error.getMessage())
 
         val detail = error.detail
         assertNotNull(detail)
         assertNotNull(detail.className)
         assertNotNull(detail.message)
-        assertEquals(error.message, detail.message)
+        assertEquals(error.getMessage(), detail.message)
         assertNotNull(detail.element)
         assertTrue(detail.element.size > 0)
     }
@@ -51,15 +53,15 @@ class HttpErrorTest {
     fun test405() {
         val jsonStr = HTTP_ERROR_405.str
         val error = HttpErrorHandler().parse(jsonStr) as HttpError
-        val code = error.httpStatusCode
+        val code = error.getStatusCode()
         assertEquals(405, code)
-        assertNotNull(error.message)
+        assertNotNull(error.getMessage())
 
         val detail = error.detail
         assertNotNull(detail)
         assertNotNull(detail.className)
         assertNotNull(detail.message)
-        assertEquals(error.message, detail.message)
+        assertEquals(error.getMessage(), detail.message)
         assertNotNull(detail.element)
         assertTrue(detail.element.size > 0)
     }
@@ -68,32 +70,42 @@ class HttpErrorTest {
     fun test400() {
         val jsonStr = HTTP_ERROR.str
         val error = HttpErrorHandler().parse(jsonStr) as HttpError
-        val code = error.httpStatusCode
+        val code = error.getStatusCode()
         assertEquals(400, code)
-        assertNotNull(error.message)
+        assertNotNull(error.getMessage())
 
         val detail = error.detail
         assertNotNull(detail)
         assertNotNull(detail.className)
         assertNotNull(detail.message)
-        assertEquals(error.message, detail.message)
+        assertEquals(error.getMessage(), detail.message)
         assertNotNull(detail.element)
         assertTrue(detail.element.size > 0)
     }
 
+    @Test
+    fun test401() {
+        val jsonStr = HTTP_ERROR_401.str
+        val error = Http401ErrorHandler().parse(jsonStr) as Http401Error
+
+        assertEquals(401, error.getStatusCode())
+        assertTrue(error.getMessage().startsWith("Unauthorized"))
+        assertTrue(error.getMessage().contains("/restful/"))
+    }
+
     //@Test //TODO handle nested causedBy's
     fun test500() {
         val jsonStr = HTTP_ERROR_500_UNIQUE_CONSTRAINT_VIOLATION.str
         val error = HttpErrorHandler().parse(jsonStr) as HttpError
-        val code = error.httpStatusCode
+        val code = error.getStatusCode()
         assertEquals(400, code)
-        assertNotNull(error.message)
+        assertNotNull(error.getMessage())
 
         val detail = error.detail
         assertNotNull(detail)
         assertNotNull(detail.className)
         assertNotNull(detail.message)
-        assertEquals(error.message, detail.message)
+        assertEquals(error.getMessage(), detail.message)
         assertNotNull(detail.element)
         assertTrue(detail.element.size > 0)
     }