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/01/25 10:02:10 UTC

[camel] branch master updated: CAMEL-16077: camel-undertow - not respect content-type specified by REST DSL produces when body returns null (#4923)

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

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new e347eb5  CAMEL-16077: camel-undertow - not respect content-type specified by REST DSL produces when body returns null (#4923)
e347eb5 is described below

commit e347eb57cab515df5d2a47442a6a25502b9ebfdb
Author: Tadayoshi Sato <sa...@gmail.com>
AuthorDate: Mon Jan 25 19:01:49 2021 +0900

    CAMEL-16077: camel-undertow - not respect content-type specified by REST DSL produces when body returns null (#4923)
---
 .../camel/component/undertow/UndertowConsumer.java |  5 +-
 .../undertow/rest/RestUndertowContentTypeTest.java | 53 ++++++++++++++++++++++
 2 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
index d07e0b1..aa20c20 100644
--- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
+++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java
@@ -246,7 +246,10 @@ public class UndertowConsumer extends DefaultConsumer implements HttpHandler, Su
         if (body == null) {
             String message = httpExchange.getStatusCode() == 500 ? "Exception" : "No response available";
             LOG.trace("No payload to send as reply for exchange: {}", camelExchange);
-            httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_TYPE, MimeMappings.DEFAULT_MIME_MAPPINGS.get("txt"));
+            // respect Content-Type assigned from HttpBinding if any
+            String contentType = camelExchange.getIn().getHeader(Exchange.CONTENT_TYPE,
+                    MimeMappings.DEFAULT_MIME_MAPPINGS.get("txt"), String.class);
+            httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_TYPE, contentType);
             httpExchange.getResponseSender().send(message);
             return;
         }
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
new file mode 100644
index 0000000..3ca9a4e
--- /dev/null
+++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowContentTypeTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.undertow.rest;
+
+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.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+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));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                restConfiguration().component("undertow").host("localhost").port(getPort());
+
+                rest("/users")
+                        .get("/null").produces("application/json")
+                        .route()
+                        .transform().constant(null);
+            }
+        };
+    }
+}