This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch re in repository https://gitbox.apache.org/repos/asf/camel.git
commit f36597b2075c27c0a26affb10577a367d13ed5bb Author: Claus Ibsen <[email protected]> AuthorDate: Thu Jun 26 10:16:03 2025 +0200 CAMEL-22200: components that are resource endpoint based should have content cache true as default and make it configurable on component level. --- .../jsonpatch/JsonPatchComponentConfigurer.java | 12 ++++++++ .../jsonpatch/JsonPatchEndpointConfigurer.java | 6 ++++ .../jsonpatch/JsonPatchEndpointUriFactory.java | 3 +- .../camel/component/jsonpatch/json-patch.json | 11 ++++--- .../component/jsonpatch/JsonPatchComponent.java | 36 +++++++++++++++++++++- .../component/jsonpatch/JsonPatchEndpoint.java | 17 ++++++++++ .../component/jsonpatch/JsonPatchProducer.java | 5 ++- .../component/jsonpatch/JsonPatchRefTest.java} | 33 ++++++++++++++------ 8 files changed, 107 insertions(+), 16 deletions(-) diff --git a/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchComponentConfigurer.java b/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchComponentConfigurer.java index 5a79d7c8f44..bd42525728a 100644 --- a/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchComponentConfigurer.java +++ b/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchComponentConfigurer.java @@ -23,8 +23,12 @@ public class JsonPatchComponentConfigurer extends PropertyConfigurerSupport impl public boolean configure(CamelContext camelContext, Object obj, String name, Object value, boolean ignoreCase) { JsonPatchComponent target = (JsonPatchComponent) obj; switch (ignoreCase ? name.toLowerCase() : name) { + case "allowtemplatefromheader": + case "allowTemplateFromHeader": target.setAllowTemplateFromHeader(property(camelContext, boolean.class, value)); return true; case "autowiredenabled": case "autowiredEnabled": target.setAutowiredEnabled(property(camelContext, boolean.class, value)); return true; + case "contentcache": + case "contentCache": target.setContentCache(property(camelContext, boolean.class, value)); return true; case "lazystartproducer": case "lazyStartProducer": target.setLazyStartProducer(property(camelContext, boolean.class, value)); return true; default: return false; @@ -34,8 +38,12 @@ public class JsonPatchComponentConfigurer extends PropertyConfigurerSupport impl @Override public Class<?> getOptionType(String name, boolean ignoreCase) { switch (ignoreCase ? name.toLowerCase() : name) { + case "allowtemplatefromheader": + case "allowTemplateFromHeader": return boolean.class; case "autowiredenabled": case "autowiredEnabled": return boolean.class; + case "contentcache": + case "contentCache": return boolean.class; case "lazystartproducer": case "lazyStartProducer": return boolean.class; default: return null; @@ -46,8 +54,12 @@ public class JsonPatchComponentConfigurer extends PropertyConfigurerSupport impl public Object getOptionValue(Object obj, String name, boolean ignoreCase) { JsonPatchComponent target = (JsonPatchComponent) obj; switch (ignoreCase ? name.toLowerCase() : name) { + case "allowtemplatefromheader": + case "allowTemplateFromHeader": return target.isAllowTemplateFromHeader(); case "autowiredenabled": case "autowiredEnabled": return target.isAutowiredEnabled(); + case "contentcache": + case "contentCache": return target.isContentCache(); case "lazystartproducer": case "lazyStartProducer": return target.isLazyStartProducer(); default: return null; diff --git a/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchEndpointConfigurer.java b/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchEndpointConfigurer.java index e688d44319d..48f40882034 100644 --- a/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchEndpointConfigurer.java +++ b/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchEndpointConfigurer.java @@ -25,6 +25,8 @@ public class JsonPatchEndpointConfigurer extends PropertyConfigurerSupport imple switch (ignoreCase ? name.toLowerCase() : name) { case "allowcontextmapall": case "allowContextMapAll": target.setAllowContextMapAll(property(camelContext, boolean.class, value)); return true; + case "allowtemplatefromheader": + case "allowTemplateFromHeader": target.setAllowTemplateFromHeader(property(camelContext, boolean.class, value)); return true; case "contentcache": case "contentCache": target.setContentCache(property(camelContext, boolean.class, value)); return true; case "lazystartproducer": @@ -38,6 +40,8 @@ public class JsonPatchEndpointConfigurer extends PropertyConfigurerSupport imple switch (ignoreCase ? name.toLowerCase() : name) { case "allowcontextmapall": case "allowContextMapAll": return boolean.class; + case "allowtemplatefromheader": + case "allowTemplateFromHeader": return boolean.class; case "contentcache": case "contentCache": return boolean.class; case "lazystartproducer": @@ -52,6 +56,8 @@ public class JsonPatchEndpointConfigurer extends PropertyConfigurerSupport imple switch (ignoreCase ? name.toLowerCase() : name) { case "allowcontextmapall": case "allowContextMapAll": return target.isAllowContextMapAll(); + case "allowtemplatefromheader": + case "allowTemplateFromHeader": return target.isAllowTemplateFromHeader(); case "contentcache": case "contentCache": return target.isContentCache(); case "lazystartproducer": diff --git a/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchEndpointUriFactory.java b/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchEndpointUriFactory.java index f8718fdd2d0..2a69efee625 100644 --- a/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchEndpointUriFactory.java +++ b/components/camel-json-patch/src/generated/java/org/apache/camel/component/jsonpatch/JsonPatchEndpointUriFactory.java @@ -23,8 +23,9 @@ public class JsonPatchEndpointUriFactory extends org.apache.camel.support.compon private static final Set<String> SECRET_PROPERTY_NAMES; private static final Set<String> MULTI_VALUE_PREFIXES; static { - Set<String> props = new HashSet<>(4); + Set<String> props = new HashSet<>(5); props.add("allowContextMapAll"); + props.add("allowTemplateFromHeader"); props.add("contentCache"); props.add("lazyStartProducer"); props.add("resourceUri"); diff --git a/components/camel-json-patch/src/generated/resources/META-INF/org/apache/camel/component/jsonpatch/json-patch.json b/components/camel-json-patch/src/generated/resources/META-INF/org/apache/camel/component/jsonpatch/json-patch.json index f357b56c2d7..b3a716845c8 100644 --- a/components/camel-json-patch/src/generated/resources/META-INF/org/apache/camel/component/jsonpatch/json-patch.json +++ b/components/camel-json-patch/src/generated/resources/META-INF/org/apache/camel/component/jsonpatch/json-patch.json @@ -24,8 +24,10 @@ "remote": false }, "componentProperties": { - "lazyStartProducer": { "index": 0, "kind": "property", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail [...] - "autowiredEnabled": { "index": 1, "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching t [...] + "allowTemplateFromHeader": { "index": 0, "kind": "property", "displayName": "Allow Template From Header", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether to allow to use resource template from header or not (default false). Enabling this allows to specify dynamic templates via message header. However this can be seen as a potential se [...] + "contentCache": { "index": 1, "kind": "property", "displayName": "Content Cache", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Sets whether to use resource content cache or not" }, + "lazyStartProducer": { "index": 2, "kind": "property", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail [...] + "autowiredEnabled": { "index": 3, "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching t [...] }, "headers": { "CamelJsonPatchResourceUri": { "index": 0, "kind": "header", "displayName": "", "group": "producer", "label": "producer", "required": false, "javaType": "String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "The resource URI", "constantName": "org.apache.camel.component.jsonpatch.JsonPatchConstants#JSON_PATCH_RESOURCE_URI" } @@ -33,7 +35,8 @@ "properties": { "resourceUri": { "index": 0, "kind": "path", "displayName": "Resource Uri", "group": "producer", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "supportFileReference": true, "description": "Path to the resource. You can prefix with: classpath, file, http, ref, or bean. classpath, file and http loads the resource using these protocols (classpath is default). ref will look [...] "allowContextMapAll": { "index": 1, "kind": "parameter", "displayName": "Allow Context Map All", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Sets whether the context map should allow access to all details. By default only the message body and headers can be accessed. This option can be enabled for full access to the current Exchange and C [...] - "contentCache": { "index": 2, "kind": "parameter", "displayName": "Content Cache", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Sets whether to use resource content cache or not" }, - "lazyStartProducer": { "index": 3, "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer (advanced)", "label": "producer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a produc [...] + "allowTemplateFromHeader": { "index": 2, "kind": "parameter", "displayName": "Allow Template From Header", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether to allow to use resource template from header or not (default false). Enabling this allows to specify dynamic templates via message header. However this can be seen as a potential s [...] + "contentCache": { "index": 3, "kind": "parameter", "displayName": "Content Cache", "group": "producer", "label": "", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Sets whether to use resource content cache or not" }, + "lazyStartProducer": { "index": 4, "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer (advanced)", "label": "producer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a produc [...] } } diff --git a/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchComponent.java b/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchComponent.java index 851cef89f86..6c1a4fb0bd6 100644 --- a/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchComponent.java +++ b/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchComponent.java @@ -19,14 +19,48 @@ package org.apache.camel.component.jsonpatch; import java.util.Map; import org.apache.camel.Endpoint; +import org.apache.camel.spi.Metadata; import org.apache.camel.support.DefaultComponent; @org.apache.camel.spi.annotations.Component("json-patch") public class JsonPatchComponent extends DefaultComponent { + @Metadata(defaultValue = "true", description = "Sets whether to use resource content cache or not") + private boolean contentCache; + @Metadata(defaultValue = "false") + private boolean allowTemplateFromHeader; + protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - Endpoint endpoint = new JsonPatchEndpoint(uri, this, remaining); + JsonPatchEndpoint endpoint = new JsonPatchEndpoint(uri, this, remaining); + endpoint.setContentCache(contentCache); + endpoint.setAllowTemplateFromHeader(allowTemplateFromHeader); setProperties(endpoint, parameters); return endpoint; } + + public boolean isContentCache() { + return contentCache; + } + + /** + * Sets whether to use resource content cache or not + */ + public void setContentCache(boolean contentCache) { + this.contentCache = contentCache; + } + + public boolean isAllowTemplateFromHeader() { + return allowTemplateFromHeader; + } + + /** + * Whether to allow to use resource template from header or not (default false). + * + * Enabling this allows to specify dynamic templates via message header. However this can be seen as a potential + * security vulnerability if the header is coming from a malicious user, so use this with care. + */ + public void setAllowTemplateFromHeader(boolean allowTemplateFromHeader) { + this.allowTemplateFromHeader = allowTemplateFromHeader; + } + } diff --git a/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchEndpoint.java b/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchEndpoint.java index b1ad062a191..985c139d941 100644 --- a/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchEndpoint.java +++ b/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchEndpoint.java @@ -23,6 +23,7 @@ import org.apache.camel.Processor; import org.apache.camel.Producer; import org.apache.camel.component.ResourceEndpoint; import org.apache.camel.spi.UriEndpoint; +import org.apache.camel.spi.UriParam; /** * Transforms JSON using JSON patch (RFC 6902). @@ -32,6 +33,9 @@ import org.apache.camel.spi.UriEndpoint; category = { Category.TRANSFORMATION }, headersClass = JsonPatchConstants.class) public class JsonPatchEndpoint extends ResourceEndpoint { + @UriParam(defaultValue = "false") + private boolean allowTemplateFromHeader; + public JsonPatchEndpoint() { } @@ -59,4 +63,17 @@ public class JsonPatchEndpoint extends ResourceEndpoint { throw new UnsupportedOperationException("You cannot consume from this endpoint"); } + public boolean isAllowTemplateFromHeader() { + return allowTemplateFromHeader; + } + + /** + * Whether to allow to use resource template from header or not (default false). + * + * Enabling this allows to specify dynamic templates via message header. However this can be seen as a potential + * security vulnerability if the header is coming from a malicious user, so use this with care. + */ + public void setAllowTemplateFromHeader(boolean allowTemplateFromHeader) { + this.allowTemplateFromHeader = allowTemplateFromHeader; + } } diff --git a/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchProducer.java b/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchProducer.java index 9d32e188ca8..618d8543417 100644 --- a/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchProducer.java +++ b/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchProducer.java @@ -39,7 +39,10 @@ public class JsonPatchProducer extends DefaultProducer { } public void process(Exchange exchange) throws Exception { - String resourceUri = exchange.getIn().getHeader(JsonPatchConstants.JSON_PATCH_RESOURCE_URI, String.class); + String resourceUri = null; + if (endpoint.isAllowTemplateFromHeader()) { + resourceUri = exchange.getIn().getHeader(JsonPatchConstants.JSON_PATCH_RESOURCE_URI, String.class); + } if (resourceUri == null || resourceUri.isEmpty()) { resourceUri = endpoint.getResourceUri(); } diff --git a/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchComponent.java b/components/camel-json-patch/src/test/java/org/apache/camel/component/jsonpatch/JsonPatchRefTest.java similarity index 50% copy from components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchComponent.java copy to components/camel-json-patch/src/test/java/org/apache/camel/component/jsonpatch/JsonPatchRefTest.java index 851cef89f86..e611130077e 100644 --- a/components/camel-json-patch/src/main/java/org/apache/camel/component/jsonpatch/JsonPatchComponent.java +++ b/components/camel-json-patch/src/test/java/org/apache/camel/component/jsonpatch/JsonPatchRefTest.java @@ -16,17 +16,32 @@ */ package org.apache.camel.component.jsonpatch; -import java.util.Map; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; -import org.apache.camel.Endpoint; -import org.apache.camel.support.DefaultComponent; +import static org.junit.jupiter.api.Assertions.assertEquals; [email protected]("json-patch") -public class JsonPatchComponent extends DefaultComponent { +public class JsonPatchRefTest extends CamelTestSupport { - protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - Endpoint endpoint = new JsonPatchEndpoint(uri, this, remaining); - setProperties(endpoint, parameters); - return endpoint; + private static final String TEMP = "[ { \"op\": \"move\", \"from\": \"/a\", \"path\": \"/c\" } ]"; + + @Test + public void testRef() { + String out = template.requestBody("direct:a", "{ \"a\": \"b\" }", String.class); + assertEquals("{\"c\":\"b\"}", out); } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + context.getRegistry().bind("mytemp", TEMP); + + from("direct:a").to( + "json-patch:ref:mytemp"); + } + }; + } + }
