Repository: camel
Updated Branches:
  refs/heads/master f922a139e -> 308c9d4e6


 CAMEL-7217 set up the charset name of the response message according to the 
http content-type header


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

Branch: refs/heads/master
Commit: 308c9d4e67fa20aed5930283c12ba984e27d2b97
Parents: f922a13
Author: Willem Jiang <willem.ji...@gmail.com>
Authored: Fri Feb 28 20:41:36 2014 +0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Fri Feb 28 20:42:03 2014 +0800

----------------------------------------------------------------------
 .../java/org/apache/camel/util/IOHelper.java    | 23 ++++++++++++++++++++
 .../org/apache/camel/util/IOHelperTest.java     | 11 +++++++++-
 .../camel/component/ahc/helper/AhcHelper.java   |  7 +-----
 .../camel/component/http/HttpProducer.java      |  2 ++
 .../camel/component/http4/HttpProducer.java     |  1 +
 .../jetty/DefaultJettyHttpBinding.java          |  2 ++
 6 files changed, 39 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/308c9d4e/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/IOHelper.java 
b/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
index ed2f793..7d8a836 100644
--- a/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/util/IOHelper.java
@@ -493,4 +493,27 @@ public final class IOHelper {
             close(isr, in);
         }
     }
+    
+    /**
+     * Get the charset name from the content type string
+     * @param contentType
+     * @return the charset name, or <tt>UTF-8</tt> if no found
+     */
+    public static String getCharsetNameFromContentType(String contentType) {
+        String[] values = contentType.split(";"); 
+        String charset = "";
+
+        for (String value : values) {
+            value = value.trim();
+            if (value.toLowerCase().startsWith("charset=")) {
+                // Take the charset name
+                charset = value.substring(8);
+            }
+        }
+        if ("".equals(charset)) {
+            charset = "UTF-8"; 
+        }
+        return IOHelper.normalizeCharset(charset);
+
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/308c9d4e/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java 
b/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
index ac2e5ff..da2aecf 100644
--- a/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/IOHelperTest.java
@@ -96,7 +96,7 @@ public class IOHelperTest extends TestCase {
     private File tempFile(String testname) throws Exception {
         return File.createTempFile(testname, "");
     }
-
+ 
     private void write(File file, String text) throws Exception {
         PrintWriter out = new PrintWriter(file);
         out.print(text);
@@ -114,5 +114,14 @@ public class IOHelperTest extends TestCase {
         exchange.getIn().removeHeader(Exchange.CHARSET_NAME);
         exchange.setProperty(Exchange.CHARSET_NAME, "iso-8859-1");
         assertEquals("iso-8859-1", IOHelper.getCharsetName(exchange, false));
+        
+    }
+    
+    public void testGetCharsetNameFromContentType() throws Exception {
+        String charsetName = 
IOHelper.getCharsetNameFromContentType("text/html; charset=iso-8859-1");
+        assertEquals("iso-8859-1", charsetName);
+        
+        charsetName = IOHelper.getCharsetNameFromContentType("text/html");
+        assertEquals("UTF-8", charsetName);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/308c9d4e/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/helper/AhcHelper.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/helper/AhcHelper.java
 
b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/helper/AhcHelper.java
index f64067b..208b2da 100644
--- 
a/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/helper/AhcHelper.java
+++ 
b/components/camel-ahc/src/main/java/org/apache/camel/component/ahc/helper/AhcHelper.java
@@ -81,12 +81,7 @@ public final class AhcHelper {
 
     public static void setCharsetFromContentType(String contentType, Exchange 
exchange) {
         if (contentType != null) {
-            // find the charset and set it to the Exchange
-            int index = contentType.indexOf("charset=");
-            if (index > 0) {
-                String charset = contentType.substring(index + 8);
-                exchange.setProperty(Exchange.CHARSET_NAME, 
IOHelper.normalizeCharset(charset));
-            }
+            exchange.setProperty(Exchange.CHARSET_NAME, 
IOHelper.getCharsetNameFromContentType(contentType));
         }
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/308c9d4e/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
 
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
index 6787a6b..9774118 100644
--- 
a/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
+++ 
b/components/camel-http/src/main/java/org/apache/camel/component/http/HttpProducer.java
@@ -181,7 +181,9 @@ public class HttpProducer extends DefaultProducer {
             String value = header.getValue();
             if (name.toLowerCase().equals("content-type")) {
                 name = Exchange.CONTENT_TYPE;
+                exchange.setProperty(Exchange.CHARSET_NAME, 
IOHelper.getCharsetNameFromContentType(value));
             }
+            
             // use http helper to extract parameter value as it may contain 
multiple values
             Object extracted = HttpHelper.extractHttpParameterValue(value);
             if (strategy != null && 
!strategy.applyFilterToExternalHeaders(name, extracted, exchange)) {

http://git-wip-us.apache.org/repos/asf/camel/blob/308c9d4e/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
 
b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
index 3480aad..2fb8380 100644
--- 
a/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
+++ 
b/components/camel-http4/src/main/java/org/apache/camel/component/http4/HttpProducer.java
@@ -196,6 +196,7 @@ public class HttpProducer extends DefaultProducer {
             String value = header.getValue();
             if (name.toLowerCase().equals("content-type")) {
                 name = Exchange.CONTENT_TYPE;
+                exchange.setProperty(Exchange.CHARSET_NAME, 
IOHelper.getCharsetNameFromContentType(value));
             }
             // use http helper to extract parameter value as it may contain 
multiple values
             Object extracted = HttpHelper.extractHttpParameterValue(value);

http://git-wip-us.apache.org/repos/asf/camel/blob/308c9d4e/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
----------------------------------------------------------------------
diff --git 
a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
 
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
index 37dd6d4..a69c82c 100644
--- 
a/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
+++ 
b/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/DefaultJettyHttpBinding.java
@@ -29,6 +29,7 @@ import 
org.apache.camel.component.http.HttpHeaderFilterStrategy;
 import org.apache.camel.component.http.HttpOperationFailedException;
 import org.apache.camel.component.http.helper.HttpHelper;
 import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.MessageHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -104,6 +105,7 @@ public class DefaultJettyHttpBinding implements 
JettyHttpBinding {
 
                 if (name.toLowerCase().equals("content-type")) {
                     name = Exchange.CONTENT_TYPE;
+                    exchange.setProperty(Exchange.CHARSET_NAME, 
IOHelper.getCharsetNameFromContentType(value));
                 }
                 if (strategy != null && 
!strategy.applyFilterToExternalHeaders(name, value, exchange)) {
                     HttpHelper.appendHeader(answer.getHeaders(), name, value);

Reply via email to