Skip to content

Commit

Permalink
Merge pull request #667 from hexagonkt/develop
Browse files Browse the repository at this point in the history
Update dependencies, fix HTTP client errors and rename Nima adapter
  • Loading branch information
jaguililla committed Oct 18, 2023
2 parents d7bef96 + f3b5850 commit 44fe263
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 139 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ plugins {
id("idea")
id("eclipse")
id("project-report")
id("org.jetbrains.dokka") version("1.9.0")
id("org.jetbrains.dokka") version("1.9.10")
id("com.github.jk1.dependency-license-report") version("2.5")
id("org.jetbrains.kotlinx.binary-compatibility-validator") version("0.13.2")
id("org.graalvm.buildtools.native") version("0.9.27") apply(false)
Expand Down
24 changes: 11 additions & 13 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.warning.mode=all
org.gradle.console=plain

# Gradle
version=3.3.0
version=3.3.1
group=com.hexagonkt
description=The atoms of your platform

Expand All @@ -32,37 +32,35 @@ iconsDirectory=content

# VERSIONS
kotlinVersion=1.9.10
dokkaVersion=1.9.0
dokkaVersion=1.9.10
mockkVersion=1.13.8
junitVersion=5.10.0
gatlingVersion=3.9.5
jmhVersion=1.37
mkdocsMaterialVersion=9.4.4
mkdocsMaterialVersion=9.4.6
mermaidDokkaVersion=0.4.4
nativeToolsVersion=0.9.27

# http_server_netty
nettyVersion=4.1.99.Final
nettyTcNativeVersion=2.0.61.Final
nettyVersion=4.1.100.Final
nettyTcNativeVersion=2.0.62.Final

# http_server_nima
nimaVersion=4.0.0-RC1
# http_server_helidon
helidonVersion=4.0.0-RC1

# http_server_servlet
servletVersion=6.0.0
# TODO Failing with HTTP client gzip encoding
#jettyVersion=12.0.1
jettyVersion=12.0.0
jettyVersion=12.0.2

# rest_tools
swaggerValidatorVersion=2.37.0
swaggerValidatorVersion=2.38.0

# logging
slf4jVersion=2.0.9
logbackVersion=1.4.11

# serialization
jacksonVersion=2.15.2
jacksonVersion=2.15.3
dslJsonVersion=2.0.2

# templates_freemarker
Expand All @@ -72,4 +70,4 @@ freemarkerVersion=2.3.32
pebbleVersion=3.2.1

# templates_rocker
rockerVersion=1.3.0
rockerVersion=1.4.0
2 changes: 1 addition & 1 deletion gradle/kotlin.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ tasks.withType(Test) { testTask ->
}

jacoco {
toolVersion = "0.8.10"
toolVersion = "0.8.11"
}

tasks.jacocoTestReport {
Expand Down
1 change: 1 addition & 0 deletions http/http/api/http.api
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ public final class com/hexagonkt/http/model/HttpProtocol : java/lang/Enum {
public static final field HTTPS Lcom/hexagonkt/http/model/HttpProtocol;
public static fun getEntries ()Lkotlin/enums/EnumEntries;
public final fun getSchema ()Ljava/lang/String;
public final fun getSecure ()Z
public static fun valueOf (Ljava/lang/String;)Lcom/hexagonkt/http/model/HttpProtocol;
public static fun values ()[Lcom/hexagonkt/http/model/HttpProtocol;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package com.hexagonkt.http.model
/**
* Supported HTTP protocols.
*/
enum class HttpProtocol(val schema: String) {
enum class HttpProtocol(val schema: String, val secure: Boolean) {
/** HTTP. */
HTTP("http"),
HTTP("http", false),
/** HTTPS. */
HTTPS("https"),
HTTPS("https", true),
/** HTTP/2. */
HTTP2("https"),
HTTP2("https", true),
/** HTTP/2 clear text. */
H2C("http"),
H2C("http", false),
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.hexagonkt.http.model
import com.hexagonkt.http.model.HttpProtocol.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

internal class HttpProtocolTest {

Expand All @@ -11,5 +13,9 @@ internal class HttpProtocolTest {
assertEquals("https", HTTPS.schema)
assertEquals("https", HTTP2.schema)
assertEquals("http", H2C.schema)
assertFalse(HTTP.secure)
assertTrue(HTTPS.secure)
assertTrue(HTTP2.secure)
assertFalse(H2C.secure)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

# Module http_server_nima
[Helidon Nima] adapter for the [http_server] port.
# Module http_server_helidon
[Helidon] adapter for the [http_server] port.

## This adapter is in ALPHA state!

[Helidon Nima]: https://helidon.io/nima
[Helidon]: https://helidon.io
[http_server]: /http_server

### Install the Dependency
Expand All @@ -17,7 +17,7 @@
}

dependencies {
implementation("com.hexagonkt:http_server_nima:$hexagonVersion")
implementation("com.hexagonkt:http_server_helidon:$hexagonVersion")
}
```

Expand All @@ -26,10 +26,10 @@
```xml
<dependency>
<groupId>com.hexagonkt</groupId>
<artifactId>http_server_nima</artifactId>
<artifactId>http_server_helidon</artifactId>
<version>$hexagonVersion</version>
</dependency>
```

# Package com.hexagonkt.http.server.nima
Code implementing the Helidon Nima HTTP server adapter.
# Package com.hexagonkt.http.server.helidon
Code implementing the Helidon HTTP server adapter.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
public final class com/hexagonkt/http/server/nima/NimaKt {
public final class com/hexagonkt/http/server/helidon/HelidonKt {
public static final fun serve (Lcom/hexagonkt/http/server/HttpServerSettings;Lcom/hexagonkt/http/handlers/HttpHandler;)Lcom/hexagonkt/http/server/HttpServer;
public static final fun serve (Lcom/hexagonkt/http/server/HttpServerSettings;Lkotlin/jvm/functions/Function1;)Lcom/hexagonkt/http/server/HttpServer;
public static synthetic fun serve$default (Lcom/hexagonkt/http/server/HttpServerSettings;Lcom/hexagonkt/http/handlers/HttpHandler;ILjava/lang/Object;)Lcom/hexagonkt/http/server/HttpServer;
public static synthetic fun serve$default (Lcom/hexagonkt/http/server/HttpServerSettings;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lcom/hexagonkt/http/server/HttpServer;
}

public final class com/hexagonkt/http/server/nima/NimaRequestAdapter : com/hexagonkt/http/model/HttpRequestPort {
public final class com/hexagonkt/http/server/helidon/HelidonRequestAdapter : com/hexagonkt/http/model/HttpRequestPort {
public fun <init> (Lio/helidon/http/Method;Lio/helidon/webserver/http/ServerRequest;)V
public fun authorization ()Lcom/hexagonkt/http/model/Authorization;
public fun bodyString ()Ljava/lang/String;
Expand Down Expand Up @@ -44,7 +44,7 @@ public final class com/hexagonkt/http/server/nima/NimaRequestAdapter : com/hexag
public fun with (Ljava/lang/Object;Lcom/hexagonkt/http/model/Headers;Lcom/hexagonkt/http/model/ContentType;Lcom/hexagonkt/http/model/HttpMethod;Lcom/hexagonkt/http/model/HttpProtocol;Ljava/lang/String;ILjava/lang/String;Lcom/hexagonkt/http/model/QueryParameters;Ljava/util/List;Lcom/hexagonkt/http/model/FormParameters;Ljava/util/List;Ljava/util/List;Lcom/hexagonkt/http/model/Authorization;Ljava/util/List;)Lcom/hexagonkt/http/model/HttpRequestPort;
}

public final class com/hexagonkt/http/server/nima/NimaServerAdapter : com/hexagonkt/http/server/HttpServerPort {
public final class com/hexagonkt/http/server/helidon/HelidonServerAdapter : com/hexagonkt/http/server/HttpServerPort {
public static final field START_ERROR_MESSAGE Ljava/lang/String;
public fun <init> ()V
public fun options ()Ljava/util/Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ apply(from = "$rootDir/gradle/dokka.gradle")
apply(from = "$rootDir/gradle/native.gradle")
apply(from = "$rootDir/gradle/detekt.gradle")

description = "HTTP server adapter for Helidon Nima (using Java Virtual Threads)."
description = "HTTP server adapter for Helidon (using Java Virtual Threads)."

dependencies {
val nimaVersion = properties["nimaVersion"]
val helidonVersion = properties["helidonVersion"]

"api"(project(":http:http_server"))
"api"("io.helidon.webserver:helidon-webserver-http2:$nimaVersion")
"api"("io.helidon.http.encoding:helidon-http-encoding-gzip:$nimaVersion")
"api"("io.helidon.http.media:helidon-http-media-multipart:$nimaVersion")
"api"("io.helidon.webserver:helidon-webserver-http2:$helidonVersion")
"api"("io.helidon.http.encoding:helidon-http-encoding-gzip:$helidonVersion")
"api"("io.helidon.http.media:helidon-http-media-multipart:$helidonVersion")

"testImplementation"(project(":http:http_test"))
"testImplementation"(project(":http:http_client_jetty"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.hexagonkt.http.server.nima
package com.hexagonkt.http.server.helidon

import com.hexagonkt.http.server.*
import com.hexagonkt.http.handlers.HandlerBuilder
import com.hexagonkt.http.handlers.HttpHandler

/**
* Create a Helidon Nima server and start it. It is a shortcut to avoid passing the adapter.
* Create a Helidon server and start it. It is a shortcut to avoid passing the adapter.
*
* @param settings Server settings info .
* @param handlers List of [HttpHandler] handlers used in this server instance.
Expand All @@ -15,10 +15,10 @@ import com.hexagonkt.http.handlers.HttpHandler
fun serve(
settings: HttpServerSettings = HttpServerSettings(), handlers: HttpHandler
): HttpServer =
HttpServer(NimaServerAdapter(), handlers, settings).apply { start() }
HttpServer(HelidonServerAdapter(), handlers, settings).apply { start() }

/**
* Create a Helidon Nima server and start it. It is a shortcut to avoid passing the adapter.
* Create a Helidon server and start it. It is a shortcut to avoid passing the adapter.
*
* @param settings Server settings info.
* @param block Lambda to be used to create the list of [HttpHandler] handlers used in the server.
Expand All @@ -28,5 +28,5 @@ fun serve(
fun serve(
settings: HttpServerSettings = HttpServerSettings(), block: HandlerBuilder.() -> Unit
): HttpServer =
HttpServer(NimaServerAdapter(), HandlerBuilder().apply { block() }.handler(), settings)
HttpServer(HelidonServerAdapter(), HandlerBuilder().apply { block() }.handler(), settings)
.apply { start() }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hexagonkt.http.server.nima
package com.hexagonkt.http.server.helidon

import com.hexagonkt.core.media.MediaType
import com.hexagonkt.core.media.MediaTypeGroup
Expand All @@ -11,7 +11,7 @@ import io.helidon.webserver.http.ServerRequest
import java.security.cert.X509Certificate
import kotlin.jvm.optionals.getOrNull

class NimaRequestAdapter(
class HelidonRequestAdapter(
methodName: Method,
req: ServerRequest,
) : HttpRequestPort {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hexagonkt.http.server.nima
package com.hexagonkt.http.server.helidon

import com.hexagonkt.core.fieldsMapOf
import com.hexagonkt.core.security.loadKeyStore
Expand Down Expand Up @@ -29,22 +29,22 @@ import javax.net.ssl.SSLParameters
import javax.net.ssl.TrustManagerFactory

/**
* Implements [HttpServerPort] using Helidon Nima.
* Implements [HttpServerPort] using Helidon.
*/
class NimaServerAdapter : HttpServerPort {
class HelidonServerAdapter : HttpServerPort {

private companion object {
const val START_ERROR_MESSAGE = "Nima server not started correctly"
const val START_ERROR_MESSAGE = "Helidon server not started correctly"
}

private var nimaServer: WebServer? = null
private var helidonServer: WebServer? = null

override fun runtimePort(): Int {
return nimaServer?.port() ?: error(START_ERROR_MESSAGE)
return helidonServer?.port() ?: error(START_ERROR_MESSAGE)
}

override fun started() =
nimaServer?.isRunning ?: false
helidonServer?.isRunning ?: false

override fun startUp(server: HttpServer) {
val settings = server.settings
Expand All @@ -60,11 +60,11 @@ class NimaServerAdapter : HttpServerPort {
.host(settings.bindAddress.hostName)
.port(settings.bindPort)
.routing {
it.any({ nimaRequest, nimaResponse ->
val method = nimaRequest.prologue().method()
val request = NimaRequestAdapter(method, nimaRequest)
it.any({ helidonRequest, helidonResponse ->
val method = helidonRequest.prologue().method()
val request = HelidonRequestAdapter(method, helidonRequest)
val response = handlers[method]?.process(request)?.response ?: HttpResponse()
setResponse(response, nimaResponse)
setResponse(request.protocol.secure, response, helidonResponse)
})
}

Expand All @@ -76,13 +76,13 @@ class NimaServerAdapter : HttpServerPort {
.sslContext(sslContext(sslSettings))
}

nimaServer = serverBuilder.build()
helidonServer = serverBuilder.build()

nimaServer?.start() ?: error(START_ERROR_MESSAGE)
helidonServer?.start() ?: error(START_ERROR_MESSAGE)
}

override fun shutDown() {
nimaServer?.stop() ?: error(START_ERROR_MESSAGE)
helidonServer?.stop() ?: error(START_ERROR_MESSAGE)
}

override fun supportedProtocols(): Set<HttpProtocol> =
Expand All @@ -92,41 +92,47 @@ class NimaServerAdapter : HttpServerPort {
setOf(ZIP)

override fun options(): Map<String, *> =
fieldsMapOf<NimaServerAdapter>()
fieldsMapOf<HelidonServerAdapter>()

private fun setResponse(response: HttpResponsePort, nimaResponse: ServerResponse) {
private fun setResponse(
secureRequest: Boolean,
response: HttpResponsePort,
helidonResponse: ServerResponse
) {
try {
nimaResponse.status(Status.create(response.status.code))
helidonResponse.status(Status.create(response.status.code))

response.headers.values.forEach {
nimaResponse.header(HeaderNames.create(it.name), *it.strings().toTypedArray())
helidonResponse.header(HeaderNames.create(it.name), *it.strings().toTypedArray())
}

val headers = nimaResponse.headers()
response.cookies.forEach {
val cookie = SetCookie
.builder(it.name, it.value)
.maxAge(Duration.ofSeconds(it.maxAge))
.path(it.path)
.httpOnly(it.httpOnly)
.secure(it.secure)

if (it.expires != null)
cookie.expires(it.expires)

if (it.deleted)
headers.clearCookie(it.name)
else
headers.addCookie(cookie.build())
}
val headers = helidonResponse.headers()
response.cookies
.filter { if (secureRequest) true else !it.secure }
.forEach {
val cookie = SetCookie
.builder(it.name, it.value)
.maxAge(Duration.ofSeconds(it.maxAge))
.path(it.path)
.httpOnly(it.httpOnly)
.secure(it.secure)

if (it.expires != null)
cookie.expires(it.expires)

if (it.deleted)
headers.clearCookie(it.name)
else
headers.addCookie(cookie.build())
}

response.contentType?.let { ct -> headers.contentType(HttpMediaType.create(ct.text)) }

nimaResponse.send(bodyToBytes(response.body))
helidonResponse.send(bodyToBytes(response.body))
}
catch (e: Exception) {
nimaResponse.status(Status.INTERNAL_SERVER_ERROR_500)
nimaResponse.send(bodyToBytes(e.toText()))
helidonResponse.status(Status.INTERNAL_SERVER_ERROR_500)
helidonResponse.send(bodyToBytes(e.toText()))
}
}

Expand Down

0 comments on commit 44fe263

Please sign in to comment.