You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2014/11/12 10:15:32 UTC
[3/5] camel git commit: CAMEL-8035 CXFRS sets the exchange charset if
the content type provides one.
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/11a05498
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/11a05498
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/11a05498
Branch: refs/heads/master
Commit: 11a0549893720a7a71652bcec74e8d1a75faffcb
Parents: 49ab630
Author: Willem Jiang <wi...@gmail.com>
Authored: Wed Nov 12 17:04:18 2014 +0800
Committer: Willem Jiang <wi...@gmail.com>
Committed: Wed Nov 12 17:14:38 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/11a05498/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/11a05498/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);
+ }
+
+}