You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/11/03 08:55:56 UTC
[camel] 02/02: [CAMEL-17159] Changes to support multiple valid
content types (#6372)
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-3.11.x
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 1a7387b707c7536b4e8ed4bbc25ebfb6527c5f8f
Author: henka-rl <67...@users.noreply.github.com>
AuthorDate: Wed Nov 3 09:53:58 2021 +0100
[CAMEL-17159] Changes to support multiple valid content types (#6372)
* [CAMEL-17159] Changes to support multiple valid content types
* CAMEL-17159 Code style adjustments
---
.../jetty/rest/RestJettyContentTypeTest.java | 20 ++++-
.../servlet/rest/RestServletContentTypeTest.java | 100 +++++++++++++++++++++
.../undertow/rest/RestUndertowContentTypeTest.java | 70 ++++++++++++---
.../apache/camel/processor/RestBindingAdvice.java | 17 ++--
4 files changed, 187 insertions(+), 20 deletions(-)
diff --git a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyContentTypeTest.java b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyContentTypeTest.java
index bbb0316..9d34914 100644
--- a/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyContentTypeTest.java
+++ b/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/rest/RestJettyContentTypeTest.java
@@ -61,6 +61,15 @@ public class RestJettyContentTypeTest extends BaseJettyTest {
assertEquals("", cause.getResponseBody());
}
+ @Test
+ public void testJettyMultiProducerContentTypeValid() throws Exception {
+ String out = fluentTemplate.withHeader("Accept", "application/csv")
+ .withHeader(Exchange.HTTP_METHOD, "get")
+ .to("http://localhost:" + getPort() + "/users").request(String.class);
+
+ assertEquals("Email,FirstName,LastName\ndonald.duck@disney.com,Donald,Duck", out);
+ }
+
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@@ -72,8 +81,17 @@ public class RestJettyContentTypeTest extends BaseJettyTest {
.clientRequestValidation(true);
// use the rest DSL to define the rest services
- rest("/users/").post("{id}/update").consumes("application/json").produces("application/json").route()
+ rest("/users").post("{id}/update").consumes("application/json").produces("application/json").route()
.setBody(constant("{ \"status\": \"ok\" }"));
+
+ rest("/users").get().produces("application/json,application/csv").route()
+ .choice()
+ .when(simple("${header.Accept} == 'application/csv'"))
+ .setBody(constant("Email,FirstName,LastName\ndonald.duck@disney.com,Donald,Duck"))
+ .setHeader(Exchange.CONTENT_TYPE, constant("application/csv"))
+ .otherwise()
+ .setBody(constant("{\"email\": \"donald.duck@disney.com\", \"firstname\": \"Donald\", \"lastname\": \"Duck\"}"));
+
}
};
}
diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletContentTypeTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletContentTypeTest.java
new file mode 100644
index 0000000..1ac5288
--- /dev/null
+++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletContentTypeTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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 org.apache.camel.BindToRegistry;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.servlet.ServletCamelRouterTestSupport;
+import org.apache.camel.component.servlet.ServletRestHttpBinding;
+import org.apache.commons.io.IOUtils;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class RestServletContentTypeTest extends ServletCamelRouterTestSupport {
+
+ @BindToRegistry("myBinding")
+ private ServletRestHttpBinding restHttpBinding = new ServletRestHttpBinding();
+
+ @Test
+ public void testProducerNoContentType() throws Exception {
+ WebRequest req = new PostMethodWebRequest(
+ contextUrl + "/services/users/123/update",
+ IOUtils.toInputStream("{ \"name\": \"Donald Duck\" }", "UTF-8"),
+ null);
+ WebResponse response = query(req, false);
+
+ assertEquals("{ \"status\": \"ok\" }", response.getText());
+ }
+
+ @Test
+ public void testProducerContentTypeValid() throws Exception {
+ WebRequest req = new PostMethodWebRequest(
+ contextUrl + "/services/users/123/update",
+ IOUtils.toInputStream("{ \"name\": \"Donald Duck\" }", "UTF-8"),
+ "application/json");
+ WebResponse response = query(req, false);
+
+ assertEquals("{ \"status\": \"ok\" }", response.getText());
+ }
+
+ @Test
+ public void testProducerContentTypeInvalid() throws Exception {
+ WebRequest req = new PostMethodWebRequest(
+ contextUrl + "/services/users/123/update",
+ IOUtils.toInputStream("<name>Donald Duck</name>", "UTF-8"),
+ "application/xml");
+ WebResponse response = query(req, false);
+
+ assertEquals(415, response.getResponseCode());
+ }
+
+ @Test
+ public void testProducerMultiContentTypeValid() throws Exception {
+ WebRequest req = new GetMethodWebRequest(contextUrl + "/services/users");
+ req.setHeaderField("Accept", "application/csv");
+ WebResponse response = query(req, false);
+
+ assertEquals("Email,FirstName,LastName\ndonald.duck@disney.com,Donald,Duck", response.getText());
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ restConfiguration().component("servlet").host("localhost")
+ // turn on client request validation
+ .clientRequestValidation(true);
+
+ // use the rest DSL to define the rest services
+ rest("/users").post("/{id}/update").consumes("application/json").produces("application/json").route()
+ .setBody(constant("{ \"status\": \"ok\" }"));
+
+ rest("/users").get().produces("application/json,application/csv").route()
+ .choice()
+ .when(simple("${header.Accept} == 'application/csv'"))
+ .setBody(constant("Email,FirstName,LastName\ndonald.duck@disney.com,Donald,Duck"))
+ .setHeader(Exchange.CONTENT_TYPE, constant("application/csv"))
+ .otherwise()
+ .setBody(constant("{\"email\": \"donald.duck@disney.com\", \"firstname\": \"Donald\", \"lastname\": \"Duck\"}"));
+
+ }
+ };
+ }
+}
diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowContentTypeTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowContentTypeTest.java
index 3ca9a4e..a248872 100644
--- a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowContentTypeTest.java
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowContentTypeTest.java
@@ -16,24 +16,58 @@
*/
package org.apache.camel.component.undertow.rest;
+import org.apache.camel.CamelExecutionException;
import org.apache.camel.Exchange;
-import org.apache.camel.Message;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.undertow.BaseUndertowTest;
+import org.apache.camel.http.base.HttpOperationFailedException;
import org.junit.jupiter.api.Test;
+import static org.apache.camel.test.junit5.TestSupport.assertIsInstanceOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class RestUndertowContentTypeTest extends BaseUndertowTest {
@Test
- public void contentTypeWithNullBody() {
- Exchange in = createExchangeWithBody(null);
- Exchange out = template.send("http://localhost:{{port}}/users/null", in);
- Message message = out.getMessage();
- assertNull(message.getBody());
- assertEquals("application/json", message.getHeader(Exchange.CONTENT_TYPE));
+ public void testProducerNoContentType() throws Exception {
+ String out = fluentTemplate.withHeader(Exchange.HTTP_METHOD, "post").withBody("{ \"name\": \"Donald Duck\" }")
+ .to("http://localhost:" + getPort() + "/users/123/update")
+ .request(String.class);
+
+ assertEquals("{ \"status\": \"ok\" }", out);
+ }
+
+ @Test
+ public void testProducerContentTypeValid() throws Exception {
+ String out = fluentTemplate.withHeader(Exchange.CONTENT_TYPE, "application/json")
+ .withHeader(Exchange.HTTP_METHOD, "post").withBody("{ \"name\": \"Donald Duck\" }")
+ .to("http://localhost:" + getPort() + "/users/123/update").request(String.class);
+
+ assertEquals("{ \"status\": \"ok\" }", out);
+ }
+
+ @Test
+ public void testProducerContentTypeInvalid() {
+ fluentTemplate = fluentTemplate.withHeader(Exchange.CONTENT_TYPE, "application/xml")
+ .withHeader(Exchange.HTTP_METHOD, "post")
+ .withBody("<name>Donald Duck</name>")
+ .to("http://localhost:" + getPort() + "/users/123/update");
+
+ Exception ex = assertThrows(CamelExecutionException.class, () -> fluentTemplate.request(String.class));
+
+ HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, ex.getCause());
+ assertEquals(415, cause.getStatusCode());
+ assertEquals("No response available", cause.getResponseBody());
+ }
+
+ @Test
+ public void testProducerMultiContentTypeValid() throws Exception {
+ String out = fluentTemplate.withHeader("Accept", "application/csv")
+ .withHeader(Exchange.HTTP_METHOD, "get")
+ .to("http://localhost:" + getPort() + "/users").request(String.class);
+
+ assertEquals("Email,FirstName,LastName\ndonald.duck@disney.com,Donald,Duck", out);
}
@Override
@@ -41,12 +75,22 @@ public class RestUndertowContentTypeTest extends BaseUndertowTest {
return new RouteBuilder() {
@Override
public void configure() {
- restConfiguration().component("undertow").host("localhost").port(getPort());
+ restConfiguration().component("undertow").host("localhost").port(getPort())
+ // turn on client request validation
+ .clientRequestValidation(true);
+
+ // use the rest DSL to define the rest services
+ rest("/users").post("/{id}/update").consumes("application/json").produces("application/json").route()
+ .setBody(constant("{ \"status\": \"ok\" }"));
+
+ rest("/users").get().produces("application/json,application/csv").route()
+ .choice()
+ .when(simple("${header.Accept} == 'application/csv'"))
+ .setBody(constant("Email,FirstName,LastName\ndonald.duck@disney.com,Donald,Duck"))
+ .setHeader(Exchange.CONTENT_TYPE, constant("application/csv"))
+ .otherwise()
+ .setBody(constant("{\"email\": \"donald.duck@disney.com\", \"firstname\": \"Donald\", \"lastname\": \"Duck\"}"));
- rest("/users")
- .get("/null").produces("application/json")
- .route()
- .transform().constant(null);
}
};
}
diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RestBindingAdvice.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RestBindingAdvice.java
index e47976d..8abe6ce 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/RestBindingAdvice.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/RestBindingAdvice.java
@@ -525,19 +525,24 @@ public class RestBindingAdvice implements CamelInternalProcessorAdvice<Map<Strin
// Any MIME type
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept#Directives
- if ("*/*".equals(target)) {
+ if (target.contains("*/*")) {
return true;
}
- boolean isXml = valid.toLowerCase(Locale.ENGLISH).contains("xml");
- boolean isJson = valid.toLowerCase(Locale.ENGLISH).contains("json");
+ valid = valid.toLowerCase(Locale.ENGLISH);
+ target = target.toLowerCase(Locale.ENGLISH);
- String type = target.toLowerCase(Locale.ENGLISH);
+ if (valid.contains(target)) {
+ return true;
+ }
+
+ boolean isXml = valid.contains("xml");
+ boolean isJson = valid.contains("json");
- if (isXml && !type.contains("xml")) {
+ if (isXml && !target.contains("xml")) {
return false;
}
- if (isJson && !type.contains("json")) {
+ if (isJson && !target.contains("json")) {
return false;
}