You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ff...@apache.org on 2021/04/06 15:58:27 UTC

[camel] branch master updated: [CAMEL-16460]The Content-Language of CxfRsProducer should be configurable

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

ffang 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 cf35b03  [CAMEL-16460]The Content-Language of CxfRsProducer should be configurable
cf35b03 is described below

commit cf35b03b852a58ac5ca1bfc6a948b28fad7a841b
Author: Freeman Fang <fr...@gmail.com>
AuthorDate: Tue Apr 6 11:57:41 2021 -0400

    [CAMEL-16460]The Content-Language of CxfRsProducer should be configurable
---
 .../camel/component/cxf/jaxrs/CxfRsBinding.java    | 16 ++++++++++++
 .../camel/component/cxf/jaxrs/CxfRsProducer.java   |  2 +-
 .../component/cxf/jaxrs/DefaultCxfRsBinding.java   | 30 ++++++++++++++++++++++
 .../CxfRsBindingConfigurationSelectionTest.java    |  9 +++++++
 4 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsBinding.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsBinding.java
index 9307877..ceb0269 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsBinding.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsBinding.java
@@ -123,4 +123,20 @@ public interface CxfRsBinding {
     Entity<Object> bindCamelMessageToRequestEntity(
             Object body, org.apache.camel.Message camelMessage, org.apache.camel.Exchange camelExchange)
             throws Exception;
+
+    /**
+     * Bind the Camel message to a request {@link Entity} that gets passed to
+     * {@link AsyncInvoker#method(java.lang.String, javax.ws.rs.client.Entity, javax.ws.rs.client.InvocationCallback)}.
+     *
+     * @param  camelMessage  the source message
+     * @param  camelExchange the Camel exchange
+     * @param  body          the message body
+     * @param  webClient     the CXF JAXRS WebClient
+     * @throws Exception     can be thrown if error in the binding process
+     * @return               the {@link Entity} to use
+     */
+    Entity<Object> bindCamelMessageToRequestEntity(
+            Object body, org.apache.camel.Message camelMessage, org.apache.camel.Exchange camelExchange,
+            org.apache.cxf.jaxrs.client.WebClient webClient)
+            throws Exception;
 }
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
index 3c2b2e5..b823671 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/CxfRsProducer.java
@@ -174,7 +174,7 @@ public class CxfRsProducer extends DefaultAsyncProducer {
         }
 
         //Build message entity
-        Entity<Object> entity = binding.bindCamelMessageToRequestEntity(body, inMessage, exchange);
+        Entity<Object> entity = binding.bindCamelMessageToRequestEntity(body, inMessage, exchange, client);
 
         // handle cookies
         CookieHandler cookieHandler = ((CxfRsEndpoint) getEndpoint()).getCookieHandler();
diff --git a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
index 44e88d4..56e3104 100644
--- a/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
+++ b/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
@@ -27,6 +27,7 @@ import java.util.TreeMap;
 
 import javax.security.auth.Subject;
 import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
@@ -43,6 +44,9 @@ 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;
 import org.apache.cxf.message.MessageContentsList;
@@ -244,6 +248,14 @@ public class DefaultCxfRsBinding implements CxfRsBinding, HeaderFilterStrategyAw
     @Override
     public Entity<Object> bindCamelMessageToRequestEntity(Object body, Message camelMessage, Exchange camelExchange)
             throws Exception {
+        return bindCamelMessageToRequestEntity(body, camelMessage, camelExchange, null);
+    }
+
+    @Override
+    public Entity<Object> bindCamelMessageToRequestEntity(
+            Object body, Message camelMessage, Exchange camelExchange,
+            WebClient webClient)
+            throws Exception {
         if (body == null) {
             return null;
         }
@@ -252,6 +264,24 @@ public class DefaultCxfRsBinding implements CxfRsBinding, HeaderFilterStrategyAw
             contentType = MediaType.WILDCARD;
         }
         String contentEncoding = camelMessage.getHeader(Exchange.CONTENT_ENCODING, String.class);
+        if (webClient != null) {
+            try {
+                Method getStateMethod = AbstractClient.class.getDeclaredMethod("getState");
+                getStateMethod.setAccessible(true);
+                ClientState clientState = (ClientState) getStateMethod.invoke(webClient);
+                if (clientState.getRequestHeaders().containsKey(HttpHeaders.CONTENT_LANGUAGE)) {
+                    String contentLanguage = clientState.getRequestHeaders()
+                            .getFirst(HttpHeaders.CONTENT_LANGUAGE);
+                    if (contentLanguage != null) {
+                        return Entity.entity(body, new Variant(
+                                MediaType.valueOf(contentType),
+                                new Locale(contentLanguage), contentEncoding));
+                    }
+                }
+            } catch (Exception ex) {
+                LOG.warn("Cannot retrieve CONTENT_LANGUAGE from WebClient", ex);
+            }
+        }
         return Entity.entity(body, new Variant(MediaType.valueOf(contentType), Locale.US, contentEncoding));
     }
 
diff --git a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsBindingConfigurationSelectionTest.java b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsBindingConfigurationSelectionTest.java
index d5682dc..f8cc734 100644
--- a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsBindingConfigurationSelectionTest.java
+++ b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsBindingConfigurationSelectionTest.java
@@ -28,6 +28,7 @@ import org.apache.camel.Message;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.cxf.CXFTestSupport;
 import org.apache.camel.test.junit5.CamelTestSupport;
+import org.apache.cxf.jaxrs.client.WebClient;
 import org.junit.jupiter.api.Test;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -134,6 +135,14 @@ public class CxfRsBindingConfigurationSelectionTest extends CamelTestSupport {
                 throws Exception {
             return null;
         }
+
+        @Override
+        public Entity<Object> bindCamelMessageToRequestEntity(
+                Object body, Message camelMessage,
+                Exchange camelExchange, WebClient webClient)
+                throws Exception {
+            return null;
+        }
     }
 
 }