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

com.github.lontime.extpac4j.configuration.OptionResolver Maven / Gradle / Ivy

The newest version!
package com.github.lontime.extpac4j.configuration;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

import com.github.lontime.extconfig.ConfigHelper;
import com.github.lontime.extpac4j.common.SessionStoreKind;
import com.github.lontime.shaded.com.google.common.base.Suppliers;
import com.github.lontime.shaded.io.helidon.config.Config;

/**
 * OptionResolver.
 * @author lontime
 * @since 1.0
 */
public class OptionResolver {

    private static final String NAME = "pac4j";

    /*------------------singleton------------------*/
    private static Supplier supplier = Suppliers.memoize(OptionResolver::new);

    public static OptionResolver getInstance() {
        return supplier.get();
    }
    /*------------------singleton------------------*/

    private final Options options;

    public OptionResolver() {
        this.options = ConfigHelper.resolve(NAME)
                .as(new Mapper()).orElse(new Options());
    }

    public Options getOptions() {
        return options;
    }

    static class Mapper implements Function {

        @Override
        public Options apply(Config config) {
            final Options option = new Options();
            config.get("callbackUrl").asString().ifPresent(option::setCallbackUrl);
            final Map map = config.get("properties").detach().asMap().orElse(Collections.emptyMap());
            final Map newMap = new HashMap<>();
            for (Map.Entry entry : map.entrySet()) {
                newMap.put(Config.Key.unescapeName(entry.getKey()), entry.getValue());
            }
            option.setProperties(newMap);
            config.get("clients").asString().map(String::toLowerCase).ifPresent(option::setClients);
            config.get("authorizers").asString().ifPresent(option::setAuthorizers);
            config.get("matchers").asString().ifPresent(option::setMatchers);
            config.get("multiProfile").asBoolean().ifPresent(option::setMultiProfile);
            config.get("storeKind").asString().map(String::toUpperCase)
                    .map(SessionStoreKind::valueOf).ifPresent(option::setStoreKind);
            config.get("logout").as(new LogoutMapper()).ifPresent(option::setLogout);
            config.get("redissonName").asString().ifPresent(option::setRedissonName);
            config.get("callback").as(new CallbackMapper()).ifPresent(option::setCallback);
            config.get("defaultSecurityClients").asString().ifPresent(option::setDefaultSecurityClients);
            return option;
        }
    }

    static class LogoutMapper implements Function {

        @Override
        public LogoutOption apply(Config config) {
            final LogoutOption option = new LogoutOption();
            config.get("defaultUrl").asString().ifPresent(option::setDefaultUrl);
            config.get("logoutUrlPattern").asString().ifPresent(option::setLogoutUrlPattern);
            config.get("localLogout").asBoolean().ifPresent(option::setLocalLogout);
            config.get("destroySession").asBoolean().ifPresent(option::setDestroySession);
            config.get("centralLogout").asBoolean().ifPresent(option::setCentralLogout);
            return option;
        }
    }

    static class CallbackMapper implements Function {

        @Override
        public CallbackOption apply(Config config) {
            final CallbackOption option = new CallbackOption();
            config.get("defaultUrl").asString().ifPresent(option::setDefaultUrl);
            config.get("multiProfile").asBoolean().ifPresent(option::setMultiProfile);
            config.get("saveInSession").asBoolean().ifPresent(option::setSaveInSession);
            config.get("renewSession").asBoolean().ifPresent(option::setRenewSession);
            config.get("defaultClient").asString().ifPresent(option::setDefaultClient);
            return option;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy