|
| 1 | +package dev.openfeature.contrib.providers.ofrep; |
| 2 | + |
| 3 | +import dev.openfeature.contrib.providers.ofrep.internal.Resolver; |
| 4 | +import dev.openfeature.sdk.EvaluationContext; |
| 5 | +import dev.openfeature.sdk.FeatureProvider; |
| 6 | +import dev.openfeature.sdk.Metadata; |
| 7 | +import dev.openfeature.sdk.ProviderEvaluation; |
| 8 | +import dev.openfeature.sdk.Value; |
| 9 | +import java.util.concurrent.Executor; |
| 10 | +import java.util.concurrent.ExecutorService; |
| 11 | +import java.util.concurrent.TimeUnit; |
| 12 | +import lombok.extern.slf4j.Slf4j; |
| 13 | +import org.apache.commons.validator.routines.UrlValidator; |
| 14 | + |
| 15 | +/** |
| 16 | + * OpenFeature provider for OFREP. |
| 17 | + */ |
| 18 | +@Slf4j |
| 19 | +public final class OfrepProvider implements FeatureProvider { |
| 20 | + |
| 21 | + private static final String OFREP_PROVIDER = "ofrep"; |
| 22 | + private static final long DEFAULT_EXECUTOR_SHUTDOWN_TIMEOUT = 1000; |
| 23 | + |
| 24 | + private final Resolver ofrepResolver; |
| 25 | + private Executor executor; |
| 26 | + |
| 27 | + public static OfrepProvider constructProvider() { |
| 28 | + return new OfrepProvider(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructs an OfrepProvider with the specified options. |
| 33 | + * |
| 34 | + * @param options The options for configuring the provider. |
| 35 | + * @return An instance of OfrepProvider configured with the provided options. |
| 36 | + * @throws IllegalArgumentException if any of the options are invalid. |
| 37 | + */ |
| 38 | + public static OfrepProvider constructProvider(OfrepProviderOptions options) { |
| 39 | + if (!isValidUrl(options.getBaseUrl())) { |
| 40 | + throw new IllegalArgumentException("Invalid base URL: " + options.getBaseUrl()); |
| 41 | + } |
| 42 | + |
| 43 | + if (options.getHeaders() == null) { |
| 44 | + throw new IllegalArgumentException("Headers cannot be null"); |
| 45 | + } |
| 46 | + |
| 47 | + if (options.getRequestTimeout() == null |
| 48 | + || options.getRequestTimeout().isNegative() |
| 49 | + || options.getRequestTimeout().isZero()) { |
| 50 | + throw new IllegalArgumentException("Request timeout must be a positive duration"); |
| 51 | + } |
| 52 | + |
| 53 | + if (options.getConnectTimeout() == null |
| 54 | + || options.getConnectTimeout().isNegative() |
| 55 | + || options.getConnectTimeout().isZero()) { |
| 56 | + throw new IllegalArgumentException("Connect timeout must be a positive duration"); |
| 57 | + } |
| 58 | + |
| 59 | + if (options.getProxySelector() == null) { |
| 60 | + throw new IllegalArgumentException("ProxySelector cannot be null"); |
| 61 | + } |
| 62 | + |
| 63 | + if (options.getExecutor() == null) { |
| 64 | + throw new IllegalArgumentException("Executor cannot be null"); |
| 65 | + } |
| 66 | + |
| 67 | + return new OfrepProvider(options); |
| 68 | + } |
| 69 | + |
| 70 | + private OfrepProvider() { |
| 71 | + this(new OfrepProviderOptions.Builder().build()); |
| 72 | + } |
| 73 | + |
| 74 | + private OfrepProvider(OfrepProviderOptions options) { |
| 75 | + this.executor = options.getExecutor(); |
| 76 | + this.ofrepResolver = new Resolver( |
| 77 | + options.getBaseUrl(), |
| 78 | + options.getHeaders(), |
| 79 | + options.getRequestTimeout(), |
| 80 | + options.getConnectTimeout(), |
| 81 | + options.getProxySelector(), |
| 82 | + options.getExecutor()); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Metadata getMetadata() { |
| 87 | + return () -> OFREP_PROVIDER; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void shutdown() { |
| 92 | + if (executor instanceof ExecutorService) { |
| 93 | + ExecutorService executorService = (ExecutorService) executor; |
| 94 | + try { |
| 95 | + executorService.shutdown(); |
| 96 | + |
| 97 | + if (!executorService.awaitTermination(DEFAULT_EXECUTOR_SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS)) { |
| 98 | + executorService.shutdownNow(); |
| 99 | + if (!executorService.awaitTermination(DEFAULT_EXECUTOR_SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS)) { |
| 100 | + log.error("Provider couldn't shutdown gracefully."); |
| 101 | + } |
| 102 | + } |
| 103 | + } catch (InterruptedException e) { |
| 104 | + executorService.shutdownNow(); |
| 105 | + Thread.currentThread().interrupt(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) { |
| 112 | + return ofrepResolver.resolveBoolean(key, defaultValue, ctx); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) { |
| 117 | + return ofrepResolver.resolveString(key, defaultValue, ctx); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) { |
| 122 | + return ofrepResolver.resolveInteger(key, defaultValue, ctx); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double defaultValue, EvaluationContext ctx) { |
| 127 | + return ofrepResolver.resolveDouble(key, defaultValue, ctx); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultValue, EvaluationContext ctx) { |
| 132 | + return ofrepResolver.resolveObject(key, defaultValue, ctx); |
| 133 | + } |
| 134 | + |
| 135 | + private static boolean isValidUrl(String url) { |
| 136 | + UrlValidator validator = new UrlValidator(new String[] {"http", "https"}, UrlValidator.ALLOW_LOCAL_URLS); |
| 137 | + return validator.isValid(url); |
| 138 | + } |
| 139 | +} |
0 commit comments