Author: ningjiang
Date: Thu Mar 21 04:14:01 2013
New Revision: 1459149
URL: http://svn.apache.org/r1459149
Log:
CAMEL-6188 set the exchange charset in CXF consumer if content type is there
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
Modified:
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java?rev=1459149&r1=1459148&r2=1459149&view=diff
==============================================================================
---
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
(original)
+++
camel/trunk/components/camel-cxf/src/main/java/org/apache/camel/component/cxf/DefaultCxfBinding.java
Thu Mar 21 04:14:01 2013
@@ -18,6 +18,7 @@ package org.apache.camel.component.cxf;
import java.io.InputStream;
import java.lang.reflect.Method;
+import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -44,6 +45,7 @@ import org.apache.camel.ExchangePattern;
import org.apache.camel.component.cxf.common.message.CxfConstants;
import org.apache.camel.spi.HeaderFilterStrategy;
import org.apache.camel.spi.HeaderFilterStrategyAware;
+import org.apache.camel.util.ExchangeHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.cxf.attachment.AttachmentImpl;
import org.apache.cxf.binding.soap.Soap11;
@@ -56,6 +58,7 @@ import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.helpers.HttpHeaderHelper;
import org.apache.cxf.helpers.XMLUtils;
import org.apache.cxf.jaxws.context.WrappedMessageContext;
import org.apache.cxf.message.Attachment;
@@ -253,6 +256,9 @@ public class DefaultCxfBinding implement
camelExchange.getIn().setHeader(Client.REQUEST_CONTEXT, value);
LOG.trace("Populate context from CXF message {} value={}",
Client.REQUEST_CONTEXT, value);
}
+
+ // setup the charset from content-type header
+ setCharsetWithContentType(camelExchange);
// set body
Object body = DefaultCxfBinding.getContentFromCxf(cxfMessage,
@@ -386,7 +392,18 @@ public class DefaultCxfBinding implement
// HeaderFilterStrategyAware Methods
//
-------------------------------------------------------------------------
-
+ 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 HeaderFilterStrategy getHeaderFilterStrategy() {
return headerFilterStrategy;
}
Modified:
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
URL:
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java?rev=1459149&r1=1459148&r2=1459149&view=diff
==============================================================================
---
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
(original)
+++
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/DefaultCxfBindingTest.java
Thu Mar 21 04:14:01 2013
@@ -31,10 +31,12 @@ import javax.activation.FileDataSource;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.component.cxf.common.message.CxfConstants;
+import org.apache.camel.converter.IOConverter;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.impl.DefaultHeaderFilterStrategy;
import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.camel.util.IOHelper;
import org.apache.cxf.attachment.AttachmentImpl;
import org.apache.cxf.binding.Binding;
import org.apache.cxf.endpoint.Client;
@@ -63,6 +65,23 @@ public class DefaultCxfBindingTest exten
}
@Test
+ public void testSetCharsetWithContentType() {
+ DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
+ cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());
+ Exchange exchange = new DefaultExchange(context);
+ exchange.getIn().setHeader(Exchange.CONTENT_TYPE,
"text/xml;charset=ISO-8859-1");
+ cxfBinding.setCharsetWithContentType(exchange);
+
+ String charset = IOHelper.getCharsetName(exchange);
+ assertEquals("Get a wrong charset", "ISO-8859-1", charset);
+
+ exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "text/xml");
+ cxfBinding.setCharsetWithContentType(exchange);
+ charset = IOHelper.getCharsetName(exchange);
+ assertEquals("Get a worng charset name", "UTF-8", charset);
+ }
+
+ @Test
public void testPopulateCxfRequestFromExchange() {
DefaultCxfBinding cxfBinding = new DefaultCxfBinding();
cxfBinding.setHeaderFilterStrategy(new DefaultHeaderFilterStrategy());