christophd commented on code in PR #27440: URL: https://github.com/apache/camel/pull/27440#discussion_r4198611766
########## components/camel-openfeature/src/main/java/org/apache/camel/component/openfeature/OpenFeatureEndpoint.java: ########## @@ -0,0 +1,295 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openfeature; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Map; + +import dev.openfeature.contrib.providers.flagd.Config; +import dev.openfeature.contrib.providers.flagd.FlagdOptions; +import dev.openfeature.contrib.providers.flagd.FlagdProvider; +import dev.openfeature.sdk.Client; +import dev.openfeature.sdk.FeatureProvider; +import dev.openfeature.sdk.MutableContext; +import org.apache.camel.Category; +import org.apache.camel.Consumer; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.spi.Metadata; +import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; +import org.apache.camel.spi.UriPath; +import org.apache.camel.support.DefaultEndpoint; +import org.apache.camel.support.ResourceHelper; + +/** Evaluate feature flags using the OpenFeature specification with flagd. */ +@UriEndpoint(firstVersion = "4.23.0", scheme = "openfeature", title = "OpenFeature", syntax = "openfeature:domain", + producerOnly = true, category = { Category.CORE }) +public class OpenFeatureEndpoint extends DefaultEndpoint { + + private static final String DEFAULT_PROVIDER_BEAN = "flags"; + + @UriPath + @Metadata(required = true) + private String domain; + + @UriParam + private OpenFeatureConfiguration configuration; + + private volatile Client client; + private volatile FeatureProvider resolvedProvider; + private volatile File tempFlagFile; + private volatile boolean ownedProvider; + + public OpenFeatureEndpoint(String uri, OpenFeatureComponent component, String domain, + OpenFeatureConfiguration configuration) { + super(uri, component); + this.domain = domain; + this.configuration = configuration; + } + + @Override + public OpenFeatureComponent getComponent() { + return (OpenFeatureComponent) super.getComponent(); + } + + @Override + public Producer createProducer() { + return new OpenFeatureProducer(this); + } + + @Override + public Consumer createConsumer(Processor processor) { + throw new UnsupportedOperationException("OpenFeature is producer only"); + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + configuration.validate(); + + if (domain == null || domain.isBlank()) { + throw new IllegalArgumentException("domain must not be blank"); + } + + FeatureProvider provider = resolveProvider(); + resolvedProvider = provider; + client = getComponent().registerProviderAndGetClient(domain, provider); + } + + @Override + protected void doStop() throws Exception { + client = null; + + if (ownedProvider && resolvedProvider != null) { + resolvedProvider.shutdown(); + } + resolvedProvider = null; + + File tmp = tempFlagFile; + tempFlagFile = null; + if (tmp != null) { + Files.deleteIfExists(tmp.toPath()); + } + + super.doStop(); + } + + @SuppressWarnings("unchecked") + MutableContext buildContext(Exchange exchange) { + MutableContext ctx = new MutableContext(); + + // 1. CamelOpenFeatureEvaluationContext header + Object contextHeader = exchange.getMessage().getHeader(OpenFeatureConstants.EVALUATION_CONTEXT); + if (contextHeader == null) { + contextHeader = exchange.getProperty(OpenFeatureConstants.EVALUATION_CONTEXT); + } + if (contextHeader instanceof Map) { + addMapToContext(ctx, (Map<String, Object>) contextHeader); + } else { + // Fallback to body map entries + Object body = exchange.getMessage().getBody(); + if (body instanceof Map) { Review Comment: Fixed. Map body is now opt-in via the `contextFromBody` option (default `false`). A Map body is only used as evaluation context when `contextFromBody=true` is explicitly set on the endpoint. Added `testMapBodyNotUsedAsContextByDefault` test verifying the default behavior, and updated the existing targeting tests to use `contextFromBody=true`. ########## components/camel-openfeature/src/test/java/org/apache/camel/component/openfeature/OpenFeatureLanguageTest.java: ########## @@ -0,0 +1,184 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.openfeature; + +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Expression; +import org.apache.camel.Predicate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.language.openfeature.OpenFeatureLanguage; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.apache.camel.test.junit6.TestSupport; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class OpenFeatureLanguageTest extends CamelTestSupport { + + private static final String FLAGS_RESOURCE = "classpath:openfeature/flags.json"; + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext camelContext = super.createCamelContext(); + OpenFeatureComponent component = new OpenFeatureComponent(); + component.getConfiguration().setFlagsResource(FLAGS_RESOURCE); + camelContext.addComponent("openfeature", component); + return camelContext; + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:filter-enabled") + .filter().language("openfeature", "enrichment-enabled") + .to("mock:filtered") + .end() + .to("mock:after-filter"); + + from("direct:filter-disabled") + .filter().language("openfeature", "disabled-flag") + .to("mock:filtered-disabled") + .end() + .to("mock:after-filter-disabled"); + + from("direct:choice") + .choice() + .when().language("openfeature", "enrichment-enabled") + .to("mock:choice-enabled") + .otherwise() + .to("mock:choice-disabled") + .end(); + + from("direct:with-context") + .filter().language("openfeature", "enrichment-enabled") + .to("mock:context-filtered") + .end(); + } + }; + } + + @Test + void testFilterPassesWhenFlagEnabled() throws Exception { + MockEndpoint filtered = getMockEndpoint("mock:filtered"); + filtered.expectedMessageCount(1); + MockEndpoint afterFilter = getMockEndpoint("mock:after-filter"); + afterFilter.expectedMessageCount(1); + + template.sendBody("direct:filter-enabled", "test-message"); + + filtered.assertIsSatisfied(); + afterFilter.assertIsSatisfied(); + } + + @Test + void testFilterBlocksWhenFlagDisabled() throws Exception { + MockEndpoint filtered = getMockEndpoint("mock:filtered-disabled"); + filtered.expectedMessageCount(0); + MockEndpoint afterFilter = getMockEndpoint("mock:after-filter-disabled"); + afterFilter.expectedMessageCount(1); + + template.sendBody("direct:filter-disabled", "test-message"); + + filtered.assertIsSatisfied(); + afterFilter.assertIsSatisfied(); + } + + @Test + void testChoiceRoutesToCorrectBranch() throws Exception { + MockEndpoint enabled = getMockEndpoint("mock:choice-enabled"); + enabled.expectedMessageCount(1); + MockEndpoint disabled = getMockEndpoint("mock:choice-disabled"); + disabled.expectedMessageCount(0); + + template.sendBody("direct:choice", "test-message"); + + enabled.assertIsSatisfied(); + disabled.assertIsSatisfied(); + } + + @Test + void testEvaluationContextViaHeaders() throws Exception { Review Comment: Fixed. Added a `targeted-boolean` flag with an `if`/`in` targeting rule on `customer_tier`. Changed the language context tests to use this flag and added tests that assert both outcomes: `testTargetedBooleanWithContextHeaders` (ENTERPRISE → passes filter) and `testTargetedBooleanBlocksForStandard` (STANDARD → blocked). Also added `testLanguageDoesNotLeakIntoProducer` which mixes a language evaluation followed by a producer `to("openfeature:...")` to verify no property leakage, and `testCreatePredicateBlocksForStandard` as a mirror of the existing predicate test. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
