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

templates.java-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.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.client.bind.AnnotatedClientRequestBinder;
import io.micronaut.http.client.bind.ClientRequestUriContext;
import {{javaxPackage}}.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
{{#generatedAnnotation}}
import {{javaxPackage}}.annotation.Generated;
{{/generatedAnnotation}}

{{#generatedAnnotation}}
{{>common/generatedAnnotation}}
{{/generatedAnnotation}}
@Singleton
public class AuthorizationBinder implements AnnotatedClientRequestBinder {

    public static final CharSequence AUTHORIZATION_NAMES = "micronaut.security.AUTHORIZATION_NAMES";

    @NonNull
    @Override
    public Class getAnnotationType() {
        return Authorization.class;
    }

    @Override
    public void bind(@NonNull MethodInvocationContext context,
                     @NonNull ClientRequestUriContext uriContext,
                     @NonNull MutableHttpRequest request
    ) {
        List> annotations = context.getAnnotationMetadata()
                .getAnnotationValuesByType(Authorization.class);

        if (CollectionUtils.isNotEmpty(annotations)) {
            var authorizationNames = new ArrayList();
            for (AnnotationValue annotation : annotations) {
                annotation.stringValue("name")
                        .filter(StringUtils::isNotEmpty)
                        .ifPresent(v -> authorizationNames.add(configurationName(v)));
            }
            request.setAttribute(AUTHORIZATION_NAMES, authorizationNames);
        }
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy