Repository: camel
Updated Branches:
  refs/heads/camel-2.13.x 1c6f5b713 -> 0781f00e9
  refs/heads/camel-2.14.x 4134f18d4 -> f527e0376


CAMEL-8035 CXFRS sets the exchange charset if the content type provides one.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/29350e32
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/29350e32
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/29350e32

Branch: refs/heads/camel-2.14.x
Commit: 29350e32bdd8eec33c357a69f5e026ba53b37d94
Parents: 4134f18
Author: Willem Jiang <willem.ji...@gmail.com>
Authored: Wed Nov 12 17:04:18 2014 +0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Fri Nov 14 10:46:47 2014 +0800

----------------------------------------------------------------------
 .../cxf/jaxrs/DefaultCxfRsBinding.java          | 18 ++++++++
 .../cxf/jaxrs/DefaultCxfRsBindingTest.java      | 45 ++++++++++++++++++++
 2 files changed, 63 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/29350e32/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBinding.java
----------------------------------------------------------------------
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 867536f..82a2ed7 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
@@ -18,6 +18,7 @@
 package org.apache.camel.component.cxf.jaxrs;
 
 import java.lang.reflect.Method;
+import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -32,6 +33,8 @@ import 
org.apache.camel.component.cxf.common.message.CxfConstants;
 import org.apache.camel.component.cxf.util.CxfUtils;
 import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.camel.spi.HeaderFilterStrategyAware;
+import org.apache.camel.util.ExchangeHelper;
+import org.apache.cxf.helpers.HttpHeaderHelper;
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.model.OperationResourceInfoStack;
 import org.apache.cxf.message.MessageContentsList;
@@ -106,6 +109,9 @@ public class DefaultCxfRsBinding implements CxfRsBinding, 
HeaderFilterStrategyAw
         // TODO use header filter strategy and cxfToCamelHeaderMap
         CxfUtils.copyHttpHeadersFromCxfToCamel(cxfMessage, camelMessage);
         
+        // setup the charset from content-type header
+        setCharsetWithContentType(camelExchange);
+        
         //copy the protocol header
         copyProtocolHeader(cxfMessage, camelMessage, 
camelMessage.getExchange());
         
@@ -129,6 +135,18 @@ public class DefaultCxfRsBinding implements CxfRsBinding, 
HeaderFilterStrategyAw
             camelExchange.getIn().getHeaders().put(Exchange.AUTHENTICATION, 
subject);
         }
     }
+    
+    protected void setCharsetWithContentType(Exchange camelExchange) {
+        // setup the charset from content-type header
+        String contentTypeHeader = 
ExchangeHelper.getContentType(camelExchange);
+        if (contentTypeHeader != null) {
+            String charset = HttpHeaderHelper.findCharset(contentTypeHeader);
+            String normalizedEncoding = HttpHeaderHelper.mapCharset(charset, 
Charset.forName("UTF-8").name());
+            if (normalizedEncoding != null) {
+                camelExchange.setProperty(Exchange.CHARSET_NAME, 
normalizedEncoding);
+            }
+        }
+    }
 
     
     public MultivaluedMap<String, String> 
bindCamelHeadersToRequestHeaders(Map<String, Object> camelHeaders,

http://git-wip-us.apache.org/repos/asf/camel/blob/29350e32/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBindingTest.java
----------------------------------------------------------------------
diff --git 
a/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBindingTest.java
 
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBindingTest.java
new file mode 100644
index 0000000..40ddb47
--- /dev/null
+++ 
b/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/DefaultCxfRsBindingTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.cxf.jaxrs;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.util.IOHelper;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class DefaultCxfRsBindingTest extends Assert {
+    private DefaultCamelContext context = new DefaultCamelContext();
+    
+    @Test
+    public void testSetCharsetWithContentType() {
+        DefaultCxfRsBinding cxfRsBinding = new DefaultCxfRsBinding();
+        Exchange exchange = new DefaultExchange(context);
+        exchange.getIn().setHeader(Exchange.CONTENT_TYPE, 
"text/xml;charset=ISO-8859-1");
+        cxfRsBinding.setCharsetWithContentType(exchange);
+        
+        String charset = IOHelper.getCharsetName(exchange);
+        assertEquals("Get a wrong charset", "ISO-8859-1", charset);
+        
+        exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "text/xml");
+        cxfRsBinding.setCharsetWithContentType(exchange);
+        charset = IOHelper.getCharsetName(exchange);
+        assertEquals("Get a worng charset name", "UTF-8", charset);
+    }
+
+}

Reply via email to