All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.core.RequestContext.kt Maven / Gradle / Ivy

package org.http4k.core

import org.http4k.lens.Header
import org.http4k.lens.LensInjectorExtractor
import java.util.UUID

class RequestContext internal constructor(val id: UUID = UUID.randomUUID()) {
    private val objects = mutableMapOf()

    companion object {
        fun lensForStore(storeId: String?) = object : LensInjectorExtractor {
            private val X_HTTP4K_CONTEXT = Header.map(UUID::fromString, UUID::toString)
                .required("x-http4k-context" + (storeId?.let { "-$it" } ?: ""))

            override fun  invoke(value: UUID, target: R): R = X_HTTP4K_CONTEXT(value, target)

            override fun invoke(target: Request): UUID = X_HTTP4K_CONTEXT(target)
        }
    }

    @Suppress("UNCHECKED_CAST")
    operator fun  get(key: String): T? = objects[key] as T?

    operator fun set(key: String, value: Any?) {
        value?.let { objects[key] = value } ?: objects.remove(key)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy