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

java-micronaut.client.auth.AuthorizationBinder.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>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 jakarta.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import {{javaxPackage}}.annotation.Generated;


{{>common/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)) {
            List authorizationNames = new ArrayList<>();
            for (AnnotationValue annotation: annotations) {
                annotation.get("name", String.class)
                        .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 - 2024 Weber Informatics LLC | Privacy Policy