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 421579fe25f9 CAMEL-26262: camel-alibaba-sls - the putLogs examples
build a LogGroup with a bean instead of setting a text body (#28953)
421579fe25f9 is described below
commit 421579fe25f93132ad6ffd1344af8223f785822e
Author: Andrea Cosentino <[email protected]>
AuthorDate: Mon Nov 2 22:44:18 2026 +0100
CAMEL-26262: camel-alibaba-sls - the putLogs examples build a LogGroup with
a bean instead of setting a text body (#28953)
* CAMEL-26262: camel-alibaba-sls - the putLogs examples build a LogGroup
with a bean instead of setting a text body
Signed-off-by: Andrea Cosentino <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../camel/catalog/docs/alibaba-sls-component.adoc | 37 +++++-
.../src/main/docs/alibaba-sls-component.adoc | 37 +++++-
.../alibaba/sls/PutLogsDocExampleTest.java | 129 +++++++++++++++++++++
3 files changed, 191 insertions(+), 12 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-sls-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-sls-component.adoc
index cd11cd6fa40b..64eb2ca4a75f 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-sls-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/alibaba-sls-component.adoc
@@ -54,7 +54,33 @@ The component supports the following operations:
=== Producer examples
-Put logs, where the message body must be a `LogGroup` or a `PutLogsRequest` of
the SLS SDK (any other body fails):
+Put logs, where the message body must be a `LogGroup` or a `PutLogsRequest` of
the SLS SDK: any other body, such as text, fails.
+Each log of a log group has a time, in seconds since the epoch, and its fields
as `LogContent` keys and values.
+A bean can build the log group, such as this one, which writes the text of the
message as one log, under the key `message`:
+
+[source,java]
+----
+package com.example;
+
+import java.time.Instant;
+import java.util.List;
+
+import com.aliyun.sls20201230.models.LogContent;
+import com.aliyun.sls20201230.models.LogGroup;
+import com.aliyun.sls20201230.models.LogItem;
+
+public class SlsLogs {
+
+ public LogGroup toLogGroup(String message) {
+ LogItem log = new LogItem()
+ .setTime((int) Instant.now().getEpochSecond())
+ .setContents(List.of(new
LogContent().setKey("message").setValue(message)));
+ return new LogGroup().setTopic("camel").setLogItems(List.of(log));
+ }
+}
+----
+
+The route calls the bean before `putLogs`:
[tabs]
====
@@ -63,7 +89,7 @@ Java::
[source,java]
----
from("direct:start")
- .setBody(simple("${body}"))
+ .bean(SlsLogs.class, "toLogGroup")
.to("alibaba-sls:putLogs?project=my-project&logStoreName=my-logstore®ion=cn-hangzhou&endpoint=cn-hangzhou.log.aliyuncs.com&accessKey=RAW(accessKey)&secretKey=RAW(secretKey)");
----
@@ -75,10 +101,9 @@ YAML::
from:
uri: direct:start
steps:
- - setBody:
- expression:
- simple:
- expression: "${body}"
+ - bean:
+ beanType: com.example.SlsLogs
+ method: toLogGroup
- to:
uri: alibaba-sls:putLogs
parameters:
diff --git
a/components/camel-alibaba/camel-alibaba-sls/src/main/docs/alibaba-sls-component.adoc
b/components/camel-alibaba/camel-alibaba-sls/src/main/docs/alibaba-sls-component.adoc
index cd11cd6fa40b..64eb2ca4a75f 100644
---
a/components/camel-alibaba/camel-alibaba-sls/src/main/docs/alibaba-sls-component.adoc
+++
b/components/camel-alibaba/camel-alibaba-sls/src/main/docs/alibaba-sls-component.adoc
@@ -54,7 +54,33 @@ The component supports the following operations:
=== Producer examples
-Put logs, where the message body must be a `LogGroup` or a `PutLogsRequest` of
the SLS SDK (any other body fails):
+Put logs, where the message body must be a `LogGroup` or a `PutLogsRequest` of
the SLS SDK: any other body, such as text, fails.
+Each log of a log group has a time, in seconds since the epoch, and its fields
as `LogContent` keys and values.
+A bean can build the log group, such as this one, which writes the text of the
message as one log, under the key `message`:
+
+[source,java]
+----
+package com.example;
+
+import java.time.Instant;
+import java.util.List;
+
+import com.aliyun.sls20201230.models.LogContent;
+import com.aliyun.sls20201230.models.LogGroup;
+import com.aliyun.sls20201230.models.LogItem;
+
+public class SlsLogs {
+
+ public LogGroup toLogGroup(String message) {
+ LogItem log = new LogItem()
+ .setTime((int) Instant.now().getEpochSecond())
+ .setContents(List.of(new
LogContent().setKey("message").setValue(message)));
+ return new LogGroup().setTopic("camel").setLogItems(List.of(log));
+ }
+}
+----
+
+The route calls the bean before `putLogs`:
[tabs]
====
@@ -63,7 +89,7 @@ Java::
[source,java]
----
from("direct:start")
- .setBody(simple("${body}"))
+ .bean(SlsLogs.class, "toLogGroup")
.to("alibaba-sls:putLogs?project=my-project&logStoreName=my-logstore®ion=cn-hangzhou&endpoint=cn-hangzhou.log.aliyuncs.com&accessKey=RAW(accessKey)&secretKey=RAW(secretKey)");
----
@@ -75,10 +101,9 @@ YAML::
from:
uri: direct:start
steps:
- - setBody:
- expression:
- simple:
- expression: "${body}"
+ - bean:
+ beanType: com.example.SlsLogs
+ method: toLogGroup
- to:
uri: alibaba-sls:putLogs
parameters:
diff --git
a/components/camel-alibaba/camel-alibaba-sls/src/test/java/org/apache/camel/component/alibaba/sls/PutLogsDocExampleTest.java
b/components/camel-alibaba/camel-alibaba-sls/src/test/java/org/apache/camel/component/alibaba/sls/PutLogsDocExampleTest.java
new file mode 100644
index 000000000000..c891dc0e6734
--- /dev/null
+++
b/components/camel-alibaba/camel-alibaba-sls/src/test/java/org/apache/camel/component/alibaba/sls/PutLogsDocExampleTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.alibaba.sls;
+
+import java.time.Instant;
+import java.util.List;
+
+import com.aliyun.sls20201230.Client;
+import com.aliyun.sls20201230.models.LogContent;
+import com.aliyun.sls20201230.models.LogGroup;
+import com.aliyun.sls20201230.models.LogItem;
+import com.aliyun.sls20201230.models.PutLogsRequest;
+import com.aliyun.sls20201230.models.PutLogsResponse;
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * The putLogs example of the docs: a bean builds the LogGroup of the text of
the message, which putLogs writes. A text
+ * body, which the earlier example set, fails.
+ */
+class PutLogsDocExampleTest extends CamelTestSupport {
+
+ private final TestConfiguration testConfiguration = new
TestConfiguration();
+ private final Client slsClient = mock(Client.class);
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ when(slsClient.putLogs(any(), any(),
any(PutLogsRequest.class))).thenReturn(new
PutLogsResponse().setStatusCode(200));
+ context.getRegistry().bind("slsClient", slsClient);
+ return context;
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ String putLogs = "alibaba-sls:putLogs"
+ + "?project=" +
testConfiguration.getProperty("project")
+ + "&logStoreName=" +
testConfiguration.getProperty("logStoreName")
+ + "®ion=" + testConfiguration.getProperty("region")
+ + "&endpoint=" +
testConfiguration.getProperty("endpoint")
+ + "&accessKey=" +
testConfiguration.getProperty("accessKey")
+ + "&secretKey=" +
testConfiguration.getProperty("secretKey")
+ + "&slsClient=#slsClient";
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ // the example of the docs
+ from("direct:start")
+ .bean(SlsLogs.class, "toLogGroup")
+ .to(putLogs);
+
+ // the earlier example of the docs, which set a text body
+ from("direct:text")
+ .setBody(simple("${body}"))
+ .to(putLogs);
+ }
+ };
+ }
+
+ @Test
+ void beanOfTheDocsBuildsTheLogGroup() throws Exception {
+ long before = Instant.now().getEpochSecond();
+
+ template.sendBody("direct:start", "hello sls");
+
+ ArgumentCaptor<PutLogsRequest> request =
ArgumentCaptor.forClass(PutLogsRequest.class);
+ verify(slsClient).putLogs(
+ eq(testConfiguration.getProperty("project")),
eq(testConfiguration.getProperty("logStoreName")),
+ request.capture());
+ LogGroup logGroup = request.getValue().getBody();
+ assertThat(logGroup.getTopic()).isEqualTo("camel");
+ assertThat(logGroup.getLogItems()).hasSize(1);
+ LogItem log = logGroup.getLogItems().get(0);
+ assertThat(log.getTime()).isBetween((int) before, (int)
Instant.now().getEpochSecond());
+ assertThat(log.getContents()).hasSize(1);
+ assertThat(log.getContents().get(0).getKey()).isEqualTo("message");
+ assertThat(log.getContents().get(0).getValue()).isEqualTo("hello sls");
+ }
+
+ @Test
+ void textBodyFails() throws Exception {
+ assertThatThrownBy(() -> template.sendBody("direct:text", "hello sls"))
+ .isInstanceOf(CamelExecutionException.class)
+ .cause()
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("LogGroup or PutLogsRequest");
+ verify(slsClient, never()).putLogs(any(), any(),
any(PutLogsRequest.class));
+ }
+
+ /**
+ * The bean of the docs.
+ */
+ public static class SlsLogs {
+
+ public LogGroup toLogGroup(String message) {
+ LogItem log = new LogItem()
+ .setTime((int) Instant.now().getEpochSecond())
+ .setContents(List.of(new
LogContent().setKey("message").setValue(message)));
+ return new LogGroup().setTopic("camel").setLogItems(List.of(log));
+ }
+ }
+}