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 2026/07/20 07:42:38 UTC

(camel) branch fix/CAMEL-24177 created (now 3924ec541e73)

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

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


      at 3924ec541e73 CAMEL-24177: camel-cxfrs - Fix contentLanguage latching first request value

This branch includes the following new commits:

     new 3924ec541e73 CAMEL-24177: camel-cxfrs - Fix contentLanguage latching first request value

The 1 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.



(camel) 01/01: CAMEL-24177: camel-cxfrs - Fix contentLanguage latching first request value

Posted by da...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch fix/CAMEL-24177
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 3924ec541e7311a4c627b02ff8c4a46fcb996d39
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jul 20 09:42:25 2026 +0200

    CAMEL-24177: camel-cxfrs - Fix contentLanguage latching first request value
    
    Co-Authored-By: Claude Opus 4.6 <no...@anthropic.com>
    Signed-off-by: Claus Ibsen <cl...@gmail.com>
---
 .../component/cxf/jaxrs/DefaultCxfRsBinding.java   | 33 +++++-----------------
 .../cxf/jaxrs/DefaultCxfRsBindingTest.java         | 33 ++++++++++++++++++++++
 2 files changed, 40 insertions(+), 26 deletions(-)

diff --git a/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java b/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
index d7fb394f3933..a100f55fa0c7 100644
--- a/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
+++ b/components/camel-cxf/camel-cxf-rest/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
@@ -45,8 +45,6 @@ import org.apache.camel.support.ExchangeHelper;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.HttpHeaderHelper;
-import org.apache.cxf.jaxrs.client.AbstractClient;
-import org.apache.cxf.jaxrs.client.ClientState;
 import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.model.OperationResourceInfoStack;
@@ -64,8 +62,6 @@ public class DefaultCxfRsBinding implements CxfRsBinding, HeaderFilterStrategyAw
 
     private HeaderFilterStrategy headerFilterStrategy;
 
-    private String contentLanguage;
-
     public DefaultCxfRsBinding() {
     }
 
@@ -271,30 +267,15 @@ public class DefaultCxfRsBinding implements CxfRsBinding, HeaderFilterStrategyAw
             contentType = MediaType.WILDCARD;
         }
         String contentEncoding = camelMessage.getHeader(CxfConstants.CONTENT_ENCODING, String.class);
-        if (webClient != null && contentLanguage == null) {
-            try {
-                Method getStateMethod = AbstractClient.class.getDeclaredMethod("getState");
-                getStateMethod.setAccessible(true);
-                ClientState clientState = (ClientState) getStateMethod.invoke(webClient);
-                if (clientState.getRequestHeaders().containsKey(HttpHeaders.CONTENT_LANGUAGE)) {
-                    contentLanguage = clientState.getRequestHeaders()
-                            .getFirst(HttpHeaders.CONTENT_LANGUAGE);
-                    if (contentLanguage != null) {
-                        return Entity.entity(body, new Variant(
-                                MediaType.valueOf(contentType),
-                                // TODO Update once baseline is Java 21
-                                // Locale.of(contentLanguage),
-                                new Locale(contentLanguage),
-                                contentEncoding));
-                    }
-                }
-            } catch (Exception ex) {
-                LOG.warn(
-                        "Cannot retrieve CONTENT_LANGUAGE from WebClient. This exception is ignored, and US Locale will be used",
-                        ex);
+        if (webClient != null) {
+            String contentLanguage = webClient.getHeaders().getFirst(HttpHeaders.CONTENT_LANGUAGE);
+            if (contentLanguage != null) {
+                return Entity.entity(body, new Variant(
+                        MediaType.valueOf(contentType),
+                        new Locale(contentLanguage),
+                        contentEncoding));
             }
         }
-        contentLanguage = Locale.US.getLanguage();
         return Entity.entity(body, new Variant(MediaType.valueOf(contentType), Locale.US, contentEncoding));
     }
 
diff --git a/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBindingTest.java b/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBindingTest.java
index e31191391fad..25466ddd5229 100644
--- a/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBindingTest.java
+++ b/components/camel-cxf/camel-cxf-rest/src/test/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBindingTest.java
@@ -20,8 +20,12 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
+import jakarta.ws.rs.client.Entity;
+import jakarta.ws.rs.core.HttpHeaders;
+
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.impl.DefaultCamelContext;
@@ -29,6 +33,7 @@ import org.apache.camel.support.DefaultExchange;
 import org.apache.camel.support.DefaultHeaderFilterStrategy;
 import org.apache.camel.support.DefaultMessage;
 import org.apache.camel.support.ExchangeHelper;
+import org.apache.cxf.jaxrs.client.WebClient;
 import org.apache.cxf.message.MessageImpl;
 import org.junit.jupiter.api.Test;
 
@@ -55,6 +60,34 @@ public class DefaultCxfRsBindingTest {
         assertEquals("UTF-8", charset, "Get a worng charset name");
     }
 
+    @Test
+    public void testContentLanguagePerRequest() throws Exception {
+        DefaultCxfRsBinding binding = new DefaultCxfRsBinding();
+        Exchange exchange = new DefaultExchange(context);
+        Message camelMessage = exchange.getIn();
+        camelMessage.setBody("test");
+
+        WebClient frClient = WebClient.create("http://localhost");
+        frClient.header(HttpHeaders.CONTENT_LANGUAGE, "fr");
+        Entity<Object> frEntity = binding.bindCamelMessageToRequestEntity("body", camelMessage, exchange, frClient);
+        assertEquals(Locale.FRENCH, frEntity.getVariant().getLanguage());
+
+        WebClient deClient = WebClient.create("http://localhost");
+        deClient.header(HttpHeaders.CONTENT_LANGUAGE, "de");
+        Entity<Object> deEntity = binding.bindCamelMessageToRequestEntity("body", camelMessage, exchange, deClient);
+        assertEquals(Locale.GERMAN, deEntity.getVariant().getLanguage());
+    }
+
+    @Test
+    public void testContentLanguageDefaultsToUS() throws Exception {
+        DefaultCxfRsBinding binding = new DefaultCxfRsBinding();
+        Exchange exchange = new DefaultExchange(context);
+        Message camelMessage = exchange.getIn();
+
+        Entity<Object> entity = binding.bindCamelMessageToRequestEntity("body", camelMessage, exchange, null);
+        assertEquals(Locale.US, entity.getVariant().getLanguage());
+    }
+
     @Test
     public void testCopyProtocolHeader() {
         DefaultCxfRsBinding cxfRsBinding = new DefaultCxfRsBinding();