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 6ecdd19c270a CAMEL-26988: camel-servlet - Rest DSL consumers build
their binding from all the endpoint options (#30046)
6ecdd19c270a is described below
commit 6ecdd19c270afbbc842db09dc66912eb6f6d69da
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 23:05:17 2026 +0100
CAMEL-26988: camel-servlet - Rest DSL consumers build their binding from
all the endpoint options (#30046)
* CAMEL-26988: camel-servlet - Rest DSL consumers build their binding f…
…rom all the endpoint options
The Rest DSL binding now applies attachmentMultipartBinding (reading the
multipart parts as attachments),
fileNameExtWhitelist, logException, mapHttpMessageBody and the component
deserializationFilter, and a
ServletRestHttpBinding set on the component is used instead of being
replaced.
* Merge remote-tracking branch 'origin/main' into CAMEL-26988-servlet-r…
…est-binding-options
# Conflicts:
# docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
* Merge remote-tracking branch 'origin/main' into CAMEL-26988-servlet-r…
…est-binding-options
# Conflicts:
# docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
* CAMEL-26988: escape the {id} placeholder in the servlet docs and the …
…upgrade guide, and say why the Rest DSL binding does not get
allowJavaSerializedObject
Asciidoctor read `{id}` in backticks as a missing attribute. The Rest DSL
binding keeps not copying
allowJavaSerializedObject, as before: it deserializes a Java serialized
request only with transferException=true, where
the component deserializationFilter applies.
Co-Authored-By: Claude <[email protected]>
---
.../camel/catalog/docs/servlet-component.adoc | 9 ++
.../src/main/docs/servlet-component.adoc | 9 ++
.../component/servlet/AttachmentHttpBinding.java | 16 +++-
.../camel/component/servlet/ServletComponent.java | 14 +++-
.../component/servlet/ServletRestHttpBinding.java | 24 ++++++
.../RestServletAttachmentMultipartBindingTest.java | 96 ++++++++++++++++++++++
.../rest/RestServletComponentHttpBindingTest.java | 63 ++++++++++++++
.../RestServletMapHttpMessageBodyFalseTest.java | 54 ++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 10 +++
9 files changed, 289 insertions(+), 6 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/servlet-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/servlet-component.adoc
index 96ca7980cb45..a596b25122c8 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/servlet-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/servlet-component.adoc
@@ -235,6 +235,15 @@ The full path where the camel-servlet HTTP endpoint is
published depends on:
For example, if the application context path is `/camel` and
`CamelHttpTransportServlet` is configured with a URL mapping of `/services/*`.
Then a Camel route like `from("servlet:hello")` would be published to a path
like http://localhost:8080/camel/services/hello.
+=== Rest DSL
+
+The Rest DSL consumers of the servlet component use the endpoint options set
with
+`restConfiguration().endpointProperty(...)`, or as component options, such as
`attachmentMultipartBinding` and
+`mapHttpMessageBody`.
+A custom `httpBinding` set with `endpointProperty("httpBinding",
"#myBinding")` is used as it is. A custom `httpBinding`
+set on the component is used only when it extends
`org.apache.camel.component.servlet.ServletRestHttpBinding`, which
+maps the path placeholders such as `+{id}+` to headers.
+
=== Servlet asynchronous support
To enable Camel to benefit from Servlet asynchronous support, you must enable
the `async` boolean init parameter by setting it to `true`.
diff --git a/components/camel-servlet/src/main/docs/servlet-component.adoc
b/components/camel-servlet/src/main/docs/servlet-component.adoc
index 96ca7980cb45..a596b25122c8 100644
--- a/components/camel-servlet/src/main/docs/servlet-component.adoc
+++ b/components/camel-servlet/src/main/docs/servlet-component.adoc
@@ -235,6 +235,15 @@ The full path where the camel-servlet HTTP endpoint is
published depends on:
For example, if the application context path is `/camel` and
`CamelHttpTransportServlet` is configured with a URL mapping of `/services/*`.
Then a Camel route like `from("servlet:hello")` would be published to a path
like http://localhost:8080/camel/services/hello.
+=== Rest DSL
+
+The Rest DSL consumers of the servlet component use the endpoint options set
with
+`restConfiguration().endpointProperty(...)`, or as component options, such as
`attachmentMultipartBinding` and
+`mapHttpMessageBody`.
+A custom `httpBinding` set with `endpointProperty("httpBinding",
"#myBinding")` is used as it is. A custom `httpBinding`
+set on the component is used only when it extends
`org.apache.camel.component.servlet.ServletRestHttpBinding`, which
+maps the path placeholders such as `+{id}+` to headers.
+
=== Servlet asynchronous support
To enable Camel to benefit from Servlet asynchronous support, you must enable
the `async` boolean init parameter by setting it to `true`.
diff --git
a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java
b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java
index 0f992f9ff73e..c3367cee0bc8 100644
---
a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java
+++
b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/AttachmentHttpBinding.java
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
+import java.util.function.Predicate;
import jakarta.activation.DataSource;
import jakarta.servlet.http.HttpServletRequest;
@@ -51,6 +52,15 @@ public final class AttachmentHttpBinding extends
DefaultHttpBinding {
@Override
protected void populateAttachments(HttpServletRequest request, Message
message) {
+ populateMultipartAttachments(request, message,
this::isFileNameAccepted, getFileNameExtWhitelist());
+ }
+
+ /**
+ * Binds the parts of a multipart/form-data request as attachments on the
message. Also used by
+ * {@link ServletRestHttpBinding} when attachmentMultipartBinding is
enabled.
+ */
+ static void populateMultipartAttachments(
+ HttpServletRequest request, Message message, Predicate<String>
fileNameAccepted, String fileNameExtWhitelist) {
// the servlet container throws from getParts() when the request is
not multipart/form-data, so a plain
// request (GET, or a POST of text, JSON and so on) has no parts to
read and must not fail
if (!isMultipartFormData(request)) {
@@ -63,7 +73,7 @@ public final class AttachmentHttpBinding extends
DefaultHttpBinding {
// name and not against Part.getName(), which is the multipart
field name
String fileName = part.getSubmittedFileName();
// is the file name accepted
- boolean accepted = isFileNameAccepted(fileName);
+ boolean accepted = fileNameAccepted.test(fileName);
if (accepted) {
DataSource ds = new PartDataSource(part);
@@ -78,7 +88,7 @@ public final class AttachmentHttpBinding extends
DefaultHttpBinding {
} else {
LOG.debug(
"Cannot add file as attachment: {} because the
file is not accepted according to fileNameExtWhitelist: {}",
- HttpHelper.sanitizeLog(fileName),
getFileNameExtWhitelist());
+ HttpHelper.sanitizeLog(fileName),
fileNameExtWhitelist);
}
}
} catch (Exception e) {
@@ -91,7 +101,7 @@ public final class AttachmentHttpBinding extends
DefaultHttpBinding {
return contentType != null && contentType.regionMatches(true, 0,
MULTIPART_FORM_DATA, 0, MULTIPART_FORM_DATA.length());
}
- public final class PartDataSource implements DataSource {
+ public static final class PartDataSource implements DataSource {
private final Part part;
PartDataSource(Part part) {
diff --git
a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
index abe2f0526ddc..361f0c9188d4 100644
---
a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
+++
b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletComponent.java
@@ -315,13 +315,21 @@ public class ServletComponent extends HttpCommonComponent
implements RestConsume
ServletEndpoint endpoint = (ServletEndpoint)
camelContext.getEndpoint(url, parameters);
- if (!map.containsKey("httpBinding")) {
- // use the rest binding, if not using a custom http binding
- HttpBinding binding = new ServletRestHttpBinding();
+ // use the rest binding, if not using a custom http binding on the
endpoint, or a rest binding on the component
+ // (a binding that is not a rest binding does not map the path
placeholders to headers)
+ if (!map.containsKey("httpBinding") && !(getHttpBinding() instanceof
ServletRestHttpBinding)) {
+ ServletRestHttpBinding binding = new ServletRestHttpBinding();
+
binding.setAttachmentMultipartBinding(endpoint.isAttachmentMultipartBinding());
+
binding.setFileNameExtWhitelist(endpoint.getFileNameExtWhitelist());
binding.setHeaderFilterStrategy(endpoint.getHeaderFilterStrategy());
binding.setTransferException(endpoint.isTransferException());
binding.setMuteException(endpoint.isMuteException());
+ binding.setLogException(endpoint.isLogException());
+ // allowJavaSerializedObject is not copied on purpose: Rest DSL
consumers deserialize an
+ // application/x-java-serialized-object request only with
transferException=true, where this filter applies
+ binding.setDeserializationFilter(getDeserializationFilter());
binding.setEagerCheckContentAvailable(endpoint.isEagerCheckContentAvailable());
+ binding.setMapHttpMessageBody(endpoint.isMapHttpMessageBody());
binding.setMapHttpMessageHeaders(endpoint.isMapHttpMessageHeaders());
binding.setMapHttpMessageFormUrlEncodedBody(endpoint.isMapHttpMessageFormUrlEncodedBody());
endpoint.setHttpBinding(binding);
diff --git
a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestHttpBinding.java
b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestHttpBinding.java
index 2b8567a361a4..40237d378b58 100644
---
a/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestHttpBinding.java
+++
b/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/ServletRestHttpBinding.java
@@ -25,9 +25,33 @@ import static
org.apache.camel.http.base.HttpHelper.evalPlaceholders;
public class ServletRestHttpBinding extends DefaultHttpBinding {
+ private boolean attachmentMultipartBinding;
+
public ServletRestHttpBinding() {
}
+ public boolean isAttachmentMultipartBinding() {
+ return attachmentMultipartBinding;
+ }
+
+ /**
+ * Whether to bind the parts of a multipart/form-data request as
attachments on the Camel message, as the servlet
+ * endpoint does with attachmentMultipartBinding=true.
+ */
+ public void setAttachmentMultipartBinding(boolean
attachmentMultipartBinding) {
+ this.attachmentMultipartBinding = attachmentMultipartBinding;
+ }
+
+ @Override
+ protected void populateAttachments(HttpServletRequest request, Message
message) {
+ if (attachmentMultipartBinding) {
+ AttachmentHttpBinding.populateMultipartAttachments(request,
message, this::isFileNameAccepted,
+ getFileNameExtWhitelist());
+ } else {
+ super.populateAttachments(request, message);
+ }
+ }
+
@Override
protected void populateRequestParameters(HttpServletRequest request,
Message message) {
super.populateRequestParameters(request, message);
diff --git
a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletAttachmentMultipartBindingTest.java
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletAttachmentMultipartBindingTest.java
new file mode 100644
index 000000000000..e761bab86866
--- /dev/null
+++
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletAttachmentMultipartBindingTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.servlet.rest;
+
+import java.io.InputStream;
+
+import jakarta.activation.DataHandler;
+import jakarta.servlet.MultipartConfigElement;
+
+import io.undertow.servlet.api.DeploymentInfo;
+import org.apache.camel.Exchange;
+import org.apache.camel.attachment.AttachmentMessage;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.servlet.ServletCamelRouterTestSupport;
+import org.apache.camel.component.servlet.ServletEndpoint;
+import org.apache.camel.component.servlet.ServletRestHttpBinding;
+import org.apache.camel.http.common.HttpBinding;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class RestServletAttachmentMultipartBindingTest extends
ServletCamelRouterTestSupport {
+
+ @Override
+ protected DeploymentInfo getDeploymentInfo() {
+ DeploymentInfo deploymentInfo = super.getDeploymentInfo();
+ deploymentInfo.setDefaultMultipartConfig(new
MultipartConfigElement(System.getProperty("java.io.tmpdir")));
+ return deploymentInfo;
+ }
+
+ @Test
+ void testUploadIsBoundAsAttachment() throws Exception {
+ InputStream body =
context.getTypeConverter().convertTo(InputStream.class, "Hello World");
+ PostMethodWebRequest request = new PostMethodWebRequest(
+ contextUrl + "/services/users/123/upload", body,
"multipart/form-data; boundary=----Boundary");
+ WebResponse response = query(request);
+
+ assertEquals(200, response.getResponseCode());
+ // the path placeholder is mapped to a header, and the uploaded part
is an attachment
+ assertEquals("123:Hello World", response.getText());
+ }
+
+ @Test
+ void testRestBindingHasTheEndpointOptions() {
+ ServletEndpoint endpoint = context.getEndpoints().stream()
+ .filter(ServletEndpoint.class::isInstance)
+ .map(ServletEndpoint.class::cast)
+ .findFirst().orElseThrow();
+ HttpBinding binding = endpoint.getHttpBinding();
+
+ assertInstanceOf(ServletRestHttpBinding.class, binding);
+ assertEquals("txt", binding.getFileNameExtWhitelist());
+ assertTrue(binding.isLogException());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ restConfiguration().component("servlet")
+ .endpointProperty("attachmentMultipartBinding", "true")
+ .endpointProperty("fileNameExtWhitelist", "txt")
+ .endpointProperty("logException", "true");
+
+ rest("/users/").post("{id}/upload").to("direct:upload");
+
+
from("direct:upload").process(RestServletAttachmentMultipartBindingTest::replyWithAttachment);
+ }
+ };
+ }
+
+ private static void replyWithAttachment(Exchange exchange) throws
Exception {
+ DataHandler file =
exchange.getMessage(AttachmentMessage.class).getAttachment("file");
+ String content = file != null
+ ?
exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class,
file.getInputStream())
+ : "no attachment";
+ exchange.getMessage().setBody(exchange.getMessage().getHeader("id") +
":" + content);
+ }
+}
diff --git
a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletComponentHttpBindingTest.java
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletComponentHttpBindingTest.java
new file mode 100644
index 000000000000..f4a7082f2da3
--- /dev/null
+++
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletComponentHttpBindingTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.servlet.rest;
+
+import jakarta.servlet.http.HttpServletRequest;
+
+import org.apache.camel.Message;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.servlet.ServletCamelRouterTestSupport;
+import org.apache.camel.component.servlet.ServletComponent;
+import org.apache.camel.component.servlet.ServletRestHttpBinding;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RestServletComponentHttpBindingTest extends
ServletCamelRouterTestSupport {
+
+ @Test
+ void testComponentRestBindingIsUsed() throws Exception {
+ WebResponse response = query(new GetMethodWebRequest(contextUrl +
"/services/users/123/basic"));
+
+ assertEquals(200, response.getResponseCode());
+ assertEquals("123:true", response.getText());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ getContext().getComponent("servlet",
ServletComponent.class).setHttpBinding(new MarkingRestHttpBinding());
+
+ restConfiguration().component("servlet");
+
+ rest("/users/").get("{id}/basic").to("direct:basic");
+
+
from("direct:basic").transform(simple("${header.id}:${header.marked}"));
+ }
+ };
+ }
+
+ private static final class MarkingRestHttpBinding extends
ServletRestHttpBinding {
+ @Override
+ public void readRequest(HttpServletRequest request, Message message) {
+ super.readRequest(request, message);
+ message.setHeader("marked", "true");
+ }
+ }
+}
diff --git
a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletMapHttpMessageBodyFalseTest.java
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletMapHttpMessageBodyFalseTest.java
new file mode 100644
index 000000000000..a54b024d7596
--- /dev/null
+++
b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletMapHttpMessageBodyFalseTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.servlet.rest;
+
+import java.io.ByteArrayInputStream;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.servlet.ServletCamelRouterTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RestServletMapHttpMessageBodyFalseTest extends
ServletCamelRouterTestSupport {
+
+ @Test
+ void testBodyIsNotMapped() throws Exception {
+ WebRequest req = new PostMethodWebRequest(
+ contextUrl + "/services/users/123/greet",
+ new
ByteArrayInputStream("Hello".getBytes(StandardCharsets.UTF_8)), "text/plain");
+ WebResponse response = query(req);
+
+ assertEquals(200, response.getResponseCode());
+ assertEquals("123:", response.getText());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+
restConfiguration().component("servlet").endpointProperty("mapHttpMessageBody",
"false");
+
+ rest("/users/").post("{id}/greet").to("direct:greet");
+
+ from("direct:greet").transform(simple("${header.id}:${body}"));
+ }
+ };
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 99debe0ba6cd..36f0865ad185 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -488,6 +488,16 @@ Saxon uses the bean by reference, so `secureProcessing` is
set on the bean itsel
`saxonConfiguration` (for example all the endpoints, when it is set on the
component) must use the same
`secureProcessing` value, otherwise the endpoint created last sets it for all
of them.
+=== camel-servlet - Rest DSL consumers use the endpoint options
+
+The Rest DSL consumers of camel-servlet now apply the
`attachmentMultipartBinding`, `fileNameExtWhitelist`,
+`logException` and `mapHttpMessageBody` options set with
`restConfiguration().endpointProperty(...)`, or as component
+options, as `servlet:` routes do. Before, these options were ignored: with
+`attachmentMultipartBinding=true` the uploaded parts were not bound as
attachments, and the request body was always
+read. A custom `httpBinding` set on the component is now used by the Rest DSL
consumers when it extends
+`ServletRestHttpBinding`. Any other binding set on the component is still used
only by `servlet:` routes, as it does
+not map the path placeholders such as `+{id}+` to headers.
+
=== Java
*Breaking change:* Camel 4.24 requires *Java 21* as the minimum runtime
version.