diff options
| author | Linnea Gräf <nea@nea.moe> | 2025-01-08 19:25:29 +0100 |
|---|---|---|
| committer | Linnea Gräf <nea@nea.moe> | 2025-01-08 19:25:29 +0100 |
| commit | d1e16a47819509ed645bb93e1a173e0a97025cef (patch) | |
| tree | efbe886d9ac1ab4ea01788cb4842812fd0af9079 /src/main/kotlin/moe/nea/ledger/utils/network | |
| parent | f694daf322bbb4ff530a9332547c5c8337c3e0c0 (diff) | |
| download | LocalTransactionLedger-d1e16a47819509ed645bb93e1a173e0a97025cef.tar.gz LocalTransactionLedger-d1e16a47819509ed645bb93e1a173e0a97025cef.tar.bz2 LocalTransactionLedger-d1e16a47819509ed645bb93e1a173e0a97025cef.zip | |
build: Move mod to subproject
Diffstat (limited to 'src/main/kotlin/moe/nea/ledger/utils/network')
3 files changed, 0 insertions, 122 deletions
diff --git a/src/main/kotlin/moe/nea/ledger/utils/network/Request.kt b/src/main/kotlin/moe/nea/ledger/utils/network/Request.kt deleted file mode 100644 index ddf2fcc..0000000 --- a/src/main/kotlin/moe/nea/ledger/utils/network/Request.kt +++ /dev/null @@ -1,40 +0,0 @@ -package moe.nea.ledger.utils.network - -import com.google.gson.JsonElement -import java.net.URL - -data class Request( - val url: URL, - val method: Method, - val body: String?, - val headers: Map<String, String>, -) { - enum class Method { - GET, POST - } - - enum class MediaType(val text: String) { - JSON("application/json"), - TEXT("text/plain"), - HTML("text/html"), - ANY("*/*"), - } - - fun withHeaders(map: Map<String, String>): Request { - // TODO: enforce caselessness? - return this.copy(headers = headers + map) - } - - fun post() = copy(method = Method.POST) - fun get() = copy(method = Method.GET) - - fun json(element: JsonElement) = copy( - headers = headers + mapOf("content-type" to "application/json"), - body = element.toString()) - - fun accept(request: MediaType) = withHeaders(mapOf("accept" to request.text)) - - fun acceptJson() = accept(MediaType.JSON) - - fun execute(requestUtil: RequestUtil) = requestUtil.executeRequest(this) -}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt b/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt deleted file mode 100644 index a49c65a..0000000 --- a/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt +++ /dev/null @@ -1,63 +0,0 @@ -package moe.nea.ledger.utils.network - -import moe.nea.ledger.utils.ErrorUtil -import moe.nea.ledger.utils.di.Inject -import java.net.URL -import java.net.URLConnection -import java.security.KeyStore -import java.util.zip.GZIPInputStream -import javax.net.ssl.HttpsURLConnection -import javax.net.ssl.KeyManagerFactory -import javax.net.ssl.SSLContext -import javax.net.ssl.TrustManagerFactory - -class RequestUtil @Inject constructor(val errorUtil: ErrorUtil) { - - private fun createSSLContext(): SSLContext? = errorUtil.catch { - val keyStorePath = RequestUtil::class.java.getResourceAsStream("/ledgerkeystore.jks") - ?: error("Could not locate keystore") - val keyStore = KeyStore.getInstance("JKS") - keyStore.load(keyStorePath, "neuneu".toCharArray()) - val kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()) - val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()) - kmf.init(keyStore, null) - tmf.init(keyStore) - val ctx = SSLContext.getInstance("TLS") - ctx.init(kmf.keyManagers, tmf.trustManagers, null) - return@catch ctx - } - - val sslContext = createSSLContext() - - fun enhanceConnection(connection: URLConnection) { - if (connection is HttpsURLConnection && sslContext != null) { - connection.sslSocketFactory = sslContext.socketFactory - } - } - - fun createRequest(url: String) = createRequest(URL(url)) - fun createRequest(url: URL) = Request(url, Request.Method.GET, null, mapOf()) - - fun executeRequest(request: Request): Response { - val connection = request.url.openConnection() - enhanceConnection(connection) - connection.setRequestProperty("accept-encoding", "gzip") - request.headers.forEach { (k, v) -> - connection.setRequestProperty(k, v) - } - if (request.body != null) { - connection.getOutputStream().write(request.body.encodeToByteArray()) - connection.getOutputStream().close() - } - var stream = connection.getInputStream() - if (connection.contentEncoding == "gzip") { - stream = GZIPInputStream(stream) - } - val text = stream.bufferedReader().readText() - stream.close() - // Do NOT call connection.disconnect() to allow for connection reuse - return Response(request, text, connection.headerFields) - } - - -}
\ No newline at end of file diff --git a/src/main/kotlin/moe/nea/ledger/utils/network/Response.kt b/src/main/kotlin/moe/nea/ledger/utils/network/Response.kt deleted file mode 100644 index daae7f7..0000000 --- a/src/main/kotlin/moe/nea/ledger/utils/network/Response.kt +++ /dev/null @@ -1,19 +0,0 @@ -package moe.nea.ledger.utils.network - -import com.google.gson.reflect.TypeToken -import moe.nea.ledger.Ledger - -data class Response( - val source: Request, - // TODO: allow other body processors, to avoid loading everything as strings - val response: String, - val headers: Map<String, List<String>>, -) { - fun <T : Any> json(typ: TypeToken<T>): T { - return Ledger.gson.fromJson(response, typ.type) - } - - fun <T : Any> json(clazz: Class<T>): T { - return Ledger.gson.fromJson(response, clazz) - } -}
\ No newline at end of file |
