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

templates.kotlin-micronaut.client.auth.AuthorizationBinder.mustache Maven / Gradle / Ivy

{{>common/licenseInfo}}
package {{invokerPackage}}.auth

import io.micronaut.aop.MethodInvocationContext
import io.micronaut.core.annotation.AnnotationValue
import io.micronaut.core.annotation.NonNull
import io.micronaut.core.naming.NameUtils
import io.micronaut.http.MutableHttpRequest
import io.micronaut.http.client.bind.AnnotatedClientRequestBinder
import io.micronaut.http.client.bind.ClientRequestUriContext
import {{javaxPackage}}.inject.Singleton
{{#generatedAnnotation}}
import {{javaxPackage}}.annotation.Generated
{{/generatedAnnotation}}

{{#generatedAnnotation}}
{{>common/generatedAnnotation}}
{{/generatedAnnotation}}
@Singleton
open class AuthorizationBinder : AnnotatedClientRequestBinder {

    @NonNull
    override fun getAnnotationType(): Class {
        return Authorization::class.java
    }

    override fun bind(@NonNull context: MethodInvocationContext,
             @NonNull uriContext: ClientRequestUriContext,
             @NonNull request: MutableHttpRequest<*>
    ) {
        val annotations: List> = context.annotationMetadata
                .getAnnotationValuesByType(Authorization::class.java)

        if (annotations.isNotEmpty()) {
            val authorizationNames = ArrayList()
            for (ann in annotations) {
                ann.stringValue("value")
                        .filter{ s -> !s.isNullOrEmpty() }
                        .ifPresent { v -> authorizationNames.add(configurationName(v)) }
            }
            request.setAttribute(AUTHORIZATION_NAMES, authorizationNames)
        }
    }

    private fun configurationName(name: String): String {
        return NameUtils.hyphenate(name.replace('.', '_'), true)
    }

    companion object {
        var AUTHORIZATION_NAMES = "micronaut.security.AUTHORIZATION_NAMES"
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy