Skip to content

Commit

Permalink
Add settings to Java :HTTP :client :adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Aug 15, 2024
1 parent 9a21b66 commit 495daf4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class HttpClient(
path("*", listOf(it, sendHandler))
}

private val noRequestSettings =
settings.contentType == null
&& settings.authorization == null
&& settings.accept.isEmpty()
&& settings.headers.isEmpty()

var cookies: List<Cookie> = emptyList()

override fun close() {
Expand Down Expand Up @@ -65,12 +71,15 @@ class HttpClient(
?: adapter.send(request.setUp())

private fun HttpRequest.setUp(): HttpRequest {
return copy(
contentType = contentType ?: settings.contentType,
accept = accept.ifEmpty(settings::accept),
headers = settings.headers + headers,
authorization = authorization ?: settings.authorization,
)
return if (noRequestSettings)
this
else
copy(
contentType = contentType ?: settings.contentType,
accept = accept.ifEmpty(settings::accept),
headers = settings.headers + headers,
authorization = authorization ?: settings.authorization,
)
}

fun sse(request: HttpRequest): Publisher<ServerEvent> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.hexagonkt.http.SslSettings
import com.hexagonkt.http.model.*
import java.net.URL

// TODO Add proxy configuration
// TODO Add proxy configuration and timeouts
data class HttpClientSettings(
val baseUrl: URL? = null,
val contentType: ContentType? = null,
Expand Down
2 changes: 2 additions & 0 deletions http/http_client_java/api/http_client_java.api
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
public final class com/hexagonkt/http/client/java/JavaClientAdapter : com/hexagonkt/http/client/HttpClientPort {
public fun <init> ()V
public fun <init> (Lcom/hexagonkt/http/model/HttpProtocol;Ljava/util/concurrent/Executor;)V
public synthetic fun <init> (Lcom/hexagonkt/http/model/HttpProtocol;Ljava/util/concurrent/Executor;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun send (Lcom/hexagonkt/http/model/HttpRequestPort;)Lcom/hexagonkt/http/model/HttpResponsePort;
public fun shutDown ()V
public fun sse (Lcom/hexagonkt/http/model/HttpRequestPort;)Ljava/util/concurrent/Flow$Publisher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.hexagonkt.http.client.HttpClientSettings
import com.hexagonkt.http.formatQueryString
import com.hexagonkt.http.handlers.bodyToBytes
import com.hexagonkt.http.model.*
import com.hexagonkt.http.model.HttpProtocol.H2C
import com.hexagonkt.http.model.HttpProtocol.HTTP2
import com.hexagonkt.http.model.HttpResponse
import com.hexagonkt.http.model.ws.WsSession
import com.hexagonkt.http.parseContentType
Expand All @@ -18,12 +20,14 @@ import java.net.HttpCookie
import java.net.URI
import java.net.http.HttpClient.Redirect.ALWAYS
import java.net.http.HttpClient.Redirect.NEVER
import java.net.http.HttpClient.Version.HTTP_1_1
import java.net.http.HttpClient.Version.HTTP_2
import java.net.http.HttpHeaders
import java.net.http.HttpRequest.BodyPublishers
import java.net.http.HttpResponse.BodyHandlers
import java.security.SecureRandom
import java.security.cert.X509Certificate
import java.util.concurrent.Executor
import java.util.concurrent.Flow.Publisher
import javax.net.ssl.KeyManagerFactory
import javax.net.ssl.SSLContext
Expand All @@ -36,7 +40,10 @@ import java.net.http.HttpResponse as JavaHttpResponse
/**
* Client to use other REST services.
*/
class JavaClientAdapter : HttpClientPort {
class JavaClientAdapter(
private val protocol: HttpProtocol = HTTP2,
private val executor: Executor? = null,
) : HttpClientPort {

private companion object {
object TrustAll : X509TrustManager {
Expand All @@ -62,12 +69,15 @@ class JavaClientAdapter : HttpClientPort {
httpSettings = settings
val javaClientBuilder = JavaHttpClient
.newBuilder()
.version(HTTP_2)
.version(if (protocol == HTTP2 || protocol == H2C) HTTP_2 else HTTP_1_1)
.followRedirects(if (settings.followRedirects) ALWAYS else NEVER)

if (settings.useCookies)
javaClientBuilder.cookieHandler(cookieManager)

if (executor != null)
javaClientBuilder.executor(executor)

settings.sslSettings?.let { javaClientBuilder.sslContext(sslContext(it)) }

javaClient = javaClientBuilder.build()
Expand Down

0 comments on commit 495daf4

Please sign in to comment.