From e02615f33d88734c1a2527129171ffde6d01484e Mon Sep 17 00:00:00 2001 From: jaguililla Date: Sun, 17 Mar 2024 17:05:42 +0100 Subject: [PATCH] Simplify callbacks usage --- http/http_server/api/http_server.api | 2 ++ .../kotlin/com/hexagonkt/http/server/callbacks/FileCallback.kt | 3 +++ .../kotlin/com/hexagonkt/http/server/callbacks/UrlCallback.kt | 3 +++ .../com/hexagonkt/http/server/callbacks/FileCallbackTest.kt | 2 +- .../com/hexagonkt/http/server/callbacks/UrlCallbackTest.kt | 2 +- 5 files changed, 10 insertions(+), 2 deletions(-) diff --git a/http/http_server/api/http_server.api b/http/http_server/api/http_server.api index 4ddbc452cf..9b77380eb0 100644 --- a/http/http_server/api/http_server.api +++ b/http/http_server/api/http_server.api @@ -104,6 +104,7 @@ public final class com/hexagonkt/http/server/callbacks/DateCallback : kotlin/jvm public final class com/hexagonkt/http/server/callbacks/FileCallback : kotlin/jvm/functions/Function1 { public fun (Ljava/io/File;)V + public fun (Ljava/lang/String;)V public fun invoke (Lcom/hexagonkt/http/handlers/HttpContext;)Lcom/hexagonkt/http/handlers/HttpContext; public synthetic fun invoke (Ljava/lang/Object;)Ljava/lang/Object; } @@ -117,6 +118,7 @@ public final class com/hexagonkt/http/server/callbacks/LoggingCallback : kotlin/ } public final class com/hexagonkt/http/server/callbacks/UrlCallback : kotlin/jvm/functions/Function1 { + public fun (Ljava/lang/String;)V public fun (Ljava/net/URL;)V public fun invoke (Lcom/hexagonkt/http/handlers/HttpContext;)Lcom/hexagonkt/http/handlers/HttpContext; public synthetic fun invoke (Ljava/lang/Object;)Ljava/lang/Object; diff --git a/http/http_server/src/main/kotlin/com/hexagonkt/http/server/callbacks/FileCallback.kt b/http/http_server/src/main/kotlin/com/hexagonkt/http/server/callbacks/FileCallback.kt index f42f466fca..b1735fb9bd 100644 --- a/http/http_server/src/main/kotlin/com/hexagonkt/http/server/callbacks/FileCallback.kt +++ b/http/http_server/src/main/kotlin/com/hexagonkt/http/server/callbacks/FileCallback.kt @@ -18,6 +18,9 @@ import java.io.File * @param file Base file used to resolve paths passed on the request. */ class FileCallback(private val file: File) : (HttpContext) -> HttpContext { + + constructor(file: String) : this(File(file)) + private companion object { val logger: Logger = Logger(FileCallback::class) } diff --git a/http/http_server/src/main/kotlin/com/hexagonkt/http/server/callbacks/UrlCallback.kt b/http/http_server/src/main/kotlin/com/hexagonkt/http/server/callbacks/UrlCallback.kt index 167a56d5d6..7b09606d2c 100644 --- a/http/http_server/src/main/kotlin/com/hexagonkt/http/server/callbacks/UrlCallback.kt +++ b/http/http_server/src/main/kotlin/com/hexagonkt/http/server/callbacks/UrlCallback.kt @@ -10,6 +10,9 @@ import com.hexagonkt.http.handlers.HttpContext import java.net.URL class UrlCallback(private val url: URL) : (HttpContext) -> HttpContext { + + constructor(url: String) : this(urlOf(url)) + private companion object { val logger: Logger = Logger(UrlCallback::class) } diff --git a/http/http_server/src/test/kotlin/com/hexagonkt/http/server/callbacks/FileCallbackTest.kt b/http/http_server/src/test/kotlin/com/hexagonkt/http/server/callbacks/FileCallbackTest.kt index 015501ba99..6308785ecb 100644 --- a/http/http_server/src/test/kotlin/com/hexagonkt/http/server/callbacks/FileCallbackTest.kt +++ b/http/http_server/src/test/kotlin/com/hexagonkt/http/server/callbacks/FileCallbackTest.kt @@ -70,7 +70,7 @@ internal class FileCallbackTest { requestPath: String, pathPattern: String ): HttpResponsePort = - FileCallback(File(filePath))( + FileCallback(filePath)( HttpContext( request = HttpRequest(path = requestPath), predicate = HttpPredicate(pathPattern = TemplatePathPattern(pathPattern)), diff --git a/http/http_server/src/test/kotlin/com/hexagonkt/http/server/callbacks/UrlCallbackTest.kt b/http/http_server/src/test/kotlin/com/hexagonkt/http/server/callbacks/UrlCallbackTest.kt index fbce9e168f..23bb0f5d4c 100644 --- a/http/http_server/src/test/kotlin/com/hexagonkt/http/server/callbacks/UrlCallbackTest.kt +++ b/http/http_server/src/test/kotlin/com/hexagonkt/http/server/callbacks/UrlCallbackTest.kt @@ -70,7 +70,7 @@ internal class UrlCallbackTest { requestPath: String, pathPattern: String ): HttpResponsePort = - UrlCallback(urlOf(url))( + UrlCallback(url)( HttpContext( HttpCall(HttpRequest(path = requestPath)), HttpPredicate(pathPattern = TemplatePathPattern(pathPattern))