This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 4ef926862c57 camel-core - simple languge improve docs
4ef926862c57 is described below
commit 4ef926862c57da71ad2c82ba0a785d3cc765f85c
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Jan 26 20:10:38 2026 +0100
camel-core - simple languge improve docs
---
.../attachment/AttachmentExpressionBuilder.java | 91 +++++++++++++++-------
.../camel/attachment/SimpleAttachmentTest.java | 9 +++
.../modules/languages/pages/simple-language.adoc | 14 ++++
3 files changed, 86 insertions(+), 28 deletions(-)
diff --git
a/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentExpressionBuilder.java
b/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentExpressionBuilder.java
index fbdda092088c..b6acbc2ad54d 100644
---
a/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentExpressionBuilder.java
+++
b/components/camel-attachments/src/main/java/org/apache/camel/attachment/AttachmentExpressionBuilder.java
@@ -16,6 +16,10 @@
*/
package org.apache.camel.attachment;
+import java.util.Iterator;
+
+import jakarta.activation.DataHandler;
+
import org.apache.camel.CamelContext;
import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
@@ -64,12 +68,14 @@ public class AttachmentExpressionBuilder {
@Override
public Object evaluate(Exchange exchange) {
- Object answer;
- var dh = toAttachmentMessage(exchange).getAttachment(key);
- try {
- answer = dh.getContent();
- } catch (Exception e) {
- throw new RuntimeException(e);
+ Object answer = null;
+ var dh = lookupDataHandlerByKey(exchange, key);
+ if (dh != null) {
+ try {
+ answer = dh.getContent();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
}
if (answer != null && clazz != null) {
try {
@@ -101,7 +107,7 @@ public class AttachmentExpressionBuilder {
@Override
public Object evaluate(Exchange exchange) {
Object answer = null;
- var ao =
toAttachmentMessage(exchange).getAttachmentObject(key);
+ var ao = lookupAttachmentObjectByKey(exchange, key);
if (ao != null) {
answer = ao.getHeader(name);
if (answer != null && clazz != null) {
@@ -132,7 +138,7 @@ public class AttachmentExpressionBuilder {
return new ExpressionAdapter() {
@Override
public Object evaluate(Exchange exchange) {
- var dh = toAttachmentMessage(exchange).getAttachment(key);
+ var dh = lookupDataHandlerByKey(exchange, key);
if (dh != null) {
return dh.getContentType();
}
@@ -163,7 +169,7 @@ public class AttachmentExpressionBuilder {
@Override
public Object evaluate(Exchange exchange) {
String key = attachmentName.evaluate(exchange, String.class);
- Object answer =
toAttachmentMessage(exchange).getAttachment(key);
+ Object answer = lookupAttachmentObjectByKey(exchange, key);
if (mandatory && answer == null) {
throw RuntimeCamelException.wrapRuntimeCamelException(new
NoSuchAttachmentException(exchange, key));
}
@@ -193,26 +199,55 @@ public class AttachmentExpressionBuilder {
return new SimpleExpressionBuilder.KeyedOgnlExpressionAdapter(
ognl, "attachmentOgnl(" + ognl + ")",
(exchange, exp) -> {
- String text = exp.evaluate(exchange, String.class);
- var am = toAttachmentMessage(exchange);
- var dh = am.getAttachment(text);
- if (dh == null && ObjectHelper.isNumber(text)) {
- try {
- // fallback to lookup by numeric index
- int idx = Integer.parseInt(text);
- if (idx < am.getAttachments().size()) {
- var it =
am.getAttachments().values().iterator();
- for (int i = 0; i < idx; i++) {
- it.next();
- }
- dh = it.next();
- }
- } catch (NumberFormatException e) {
- // ignore
- }
- }
- return dh;
+ String key = exp.evaluate(exchange, String.class);
+ return lookupDataHandlerByKey(exchange, key);
});
}
+ private static DataHandler lookupDataHandlerByKey(Exchange exchange,
String key) {
+ AttachmentMessage am = toAttachmentMessage(exchange);
+ var dh = am.getAttachment(key);
+ if (dh == null && ObjectHelper.isNumber(key)) {
+ Integer idx =
exchange.getContext().getTypeConverter().tryConvertTo(Integer.class, key);
+ if (idx != null) {
+ Iterator<?> it =
ObjectHelper.createIterator(am.getAttachments().keySet());
+ for (int i = 0; i <= idx && it.hasNext(); i++) {
+ if (i == idx) {
+ key = it.next().toString();
+ } else {
+ key = null;
+ it.next();
+ }
+ }
+ if (key != null) {
+ dh = am.getAttachment(key);
+ }
+ }
+ }
+ return dh;
+ }
+
+ private static Attachment lookupAttachmentObjectByKey(Exchange exchange,
String key) {
+ AttachmentMessage am = toAttachmentMessage(exchange);
+ var ao = am.getAttachmentObject(key);
+ if (ao == null && ObjectHelper.isNumber(key)) {
+ Integer idx =
exchange.getContext().getTypeConverter().tryConvertTo(Integer.class, key);
+ if (idx != null) {
+ Iterator<?> it =
ObjectHelper.createIterator(am.getAttachments().keySet());
+ for (int i = 0; i <= idx && it.hasNext(); i++) {
+ if (i == idx) {
+ key = it.next().toString();
+ } else {
+ key = null;
+ it.next();
+ }
+ }
+ if (key != null) {
+ ao = am.getAttachmentObject(key);
+ }
+ }
+ }
+ return ao;
+ }
+
}
diff --git
a/components/camel-attachments/src/test/java/org/apache/camel/attachment/SimpleAttachmentTest.java
b/components/camel-attachments/src/test/java/org/apache/camel/attachment/SimpleAttachmentTest.java
index 865e9a5cc039..d3c28535f65e 100644
---
a/components/camel-attachments/src/test/java/org/apache/camel/attachment/SimpleAttachmentTest.java
+++
b/components/camel-attachments/src/test/java/org/apache/camel/attachment/SimpleAttachmentTest.java
@@ -133,5 +133,14 @@ public class SimpleAttachmentTest extends
LanguageTestSupport {
assertExpression("${attachment[2].name}", "123.txt");
assertExpression("${attachment[2].contentType}", "text/plain");
assertExpression("${attachment[2].content}", "456");
+
+ var map =
exchange.getMessage(AttachmentMessage.class).getAttachments();
+ Object is1 = map.get("message1.xml").getContent();
+ String xml1 = context.getTypeConverter().convertTo(String.class, is1);
+ assertExpression("${attachmentContent(0)}", xml1);
+
+ Object is2 = map.get("message2.xml").getContent();
+ String xml2 = context.getTypeConverter().convertTo(String.class, is2);
+ assertExpression("${attachmentContent(1)}", xml2);
}
}
diff --git
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
index 865c0eac8cb8..4f1eb6113473 100644
---
a/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
+++
b/core/camel-core-languages/src/main/docs/modules/languages/pages/simple-language.adoc
@@ -646,6 +646,20 @@ This requires having `camel-attachments` JAR on the
classpath.
|`attachments` | `Map` | All the attachments as a
`Map<String,jakarta.activation.DataHandler>`.
|====
+The attachment functions are only used with components that supports
`javax.attachments` APIs
+such as `camel-cxf`, `camel-http`, `camel-mail`, `camel-platform-http`,
`camel-spring-ws`, etc.
+
+NOTE: The _key_ is the attachment filename such as `mydata.xml` or an `int` to
lookup by index (0 = first).
+
+Suppose you are using `camel-platform-http` to expose a HTTP endpoint that
support file uploads via http multipart,
+then you can use `${attachments.size}` to know how many files are attached.
The other functions are used
+to access attachment content, headers and metadata.
+
+For example if a XML file was uploaded as `mydata.xml`, then you can access
metadata such as
+`${attachment[0].name` returns `mydata.xml`, and `${attachment[0].contentType`
return `text/xml`, and
+`${attachment[2].content}` returns the XML payload. You can also use by
filename `${attachmentContent(mydata.xml)}`
+or by index `${attachmentContent(0)}`.
+
=== Base64 Functions