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:54 UTC

[camel] branch camel-3.11.x updated (34b2b14 -> 1a7387b)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch camel-3.11.x
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from 34b2b14  CAMEL-17135: camel-debezium - Fixed configuration component level options not working correctly.
     new 8555f44  CAMEL-17156: camel-aws2 - do not interrupt extension (#6362)
     new 1a7387b  [CAMEL-17159] Changes to support multiple valid content types (#6372)

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../camel/component/aws2/sqs/Sqs2Consumer.java     |  19 +++-
 .../jetty/rest/RestJettyContentTypeTest.java       |  20 ++++-
 .../servlet/rest/RestServletContentTypeTest.java   | 100 +++++++++++++++++++++
 .../undertow/rest/RestUndertowContentTypeTest.java |  70 ++++++++++++---
 .../apache/camel/processor/RestBindingAdvice.java  |  17 ++--
 5 files changed, 202 insertions(+), 24 deletions(-)
 create mode 100644 components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletContentTypeTest.java

[camel] 01/02: CAMEL-17156: camel-aws2 - do not interrupt extension (#6362)

Posted by da...@apache.org.
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 8555f44fb67fe98fe621682e8b9856e5257b4cf5
Author: Simon Rasmussen <sr...@viabill.com>
AuthorDate: Tue Nov 2 12:04:48 2021 +0100

    CAMEL-17156: camel-aws2 - do not interrupt extension (#6362)
    
    * CAMEL-17156: camel-aws2 - do not interrupt extension
    
    * CAMEL-17156: camel-aws2 - Catch and swallow SqsException when failing to extend visibility
---
 .../apache/camel/component/aws2/sqs/Sqs2Consumer.java | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Consumer.java b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Consumer.java
index 3d24fa0..a52d77e5 100644
--- a/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Consumer.java
+++ b/components/camel-aws/camel-aws2-sqs/src/main/java/org/apache/camel/component/aws2/sqs/Sqs2Consumer.java
@@ -53,6 +53,7 @@ import software.amazon.awssdk.services.sqs.model.QueueDoesNotExistException;
 import software.amazon.awssdk.services.sqs.model.ReceiptHandleIsInvalidException;
 import software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest;
 import software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse;
+import software.amazon.awssdk.services.sqs.model.SqsException;
 
 /**
  * A Consumer of messages from the Amazon Web Service Simple Queue Service <a href="http://aws.amazon.com/sqs/">AWS
@@ -195,7 +196,7 @@ public class Sqs2Consumer extends ScheduledBatchPollingConsumer {
                         // cancel task as we are done
                         LOG.trace("Processing done so cancelling TimeoutExtender task for exchangeId: {}",
                                 exchange.getExchangeId());
-                        scheduledFuture.cancel(true);
+                        scheduledFuture.cancel(false);
                     }
                 });
             }
@@ -384,12 +385,22 @@ public class Sqs2Consumer extends ScheduledBatchPollingConsumer {
                 // Ignore.
             } catch (MessageNotInflightException e) {
                 // Ignore.
+            } catch (SqsException e) {
+                if (e.getMessage().contains("Message does not exist or is not available for visibility timeout change")) {
+                    // Ignore.
+                } else {
+                    logException(e);
+                }
             } catch (Exception e) {
-                LOG.warn("Extending visibility window failed for exchange " + exchange
-                         + ". Will not attempt to extend visibility further. This exception will be ignored.",
-                        e);
+                logException(e);
             }
         }
+
+        private void logException(Exception e) {
+            LOG.warn("Extending visibility window failed for exchange " + exchange
+                     + ". Will not attempt to extend visibility further. This exception will be ignored.",
+                    e);
+        }
     }
 
 }

[camel] 02/02: [CAMEL-17159] Changes to support multiple valid content types (#6372)

Posted by da...@apache.org.
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;
         }