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

io.getunleash.strategy.RemoteAddressStrategy Maven / Gradle / Ivy

There is a newer version: 9.2.4
Show newest version
package io.getunleash.strategy;

import io.getunleash.UnleashContext;
import io.getunleash.util.IpAddressMatcher;
import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public final class RemoteAddressStrategy implements Strategy {
    static final String PARAM = "IPs";
    private static final String STRATEGY_NAME = "remoteAddress";
    private static final Pattern SPLITTER = Pattern.compile(",");

    @Override
    public String getName() {
        return STRATEGY_NAME;
    }

    @Override
    public boolean isEnabled(Map parameters) {
        return false;
    }

    @Override
    public boolean isEnabled(Map parameters, UnleashContext context) {
        return Optional.ofNullable(parameters.get(PARAM))
                .map(ips -> Arrays.asList(SPLITTER.split(ips, -1)))
                .map(
                        ips ->
                                ips.stream()
                                        .flatMap(
                                                ipAddress ->
                                                        buildIpAddressMatcher(ipAddress)
                                                                .map(Stream::of)
                                                                .orElseGet(Stream::empty))
                                        .map(
                                                subnet ->
                                                        context.getRemoteAddress()
                                                                .map(subnet::matches)
                                                                .orElse(false))
                                        .anyMatch(Boolean.TRUE::equals))
                .orElse(false);
    }

    private Optional buildIpAddressMatcher(String ipAddress) {
        try {
            return Optional.of(new IpAddressMatcher(ipAddress));
        } catch (IllegalArgumentException ex) {
            return Optional.empty();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy