You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/11/29 04:27:53 UTC

(camel) branch main updated: CAMEL-20163: consolidate duplicated charset extract/set code (#12242)

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

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new ffcb160b426 CAMEL-20163: consolidate duplicated charset extract/set code (#12242)
ffcb160b426 is described below

commit ffcb160b4269dcfe7fa061862a8020cfccbb4370
Author: Otavio Rodolfo Piske <or...@users.noreply.github.com>
AuthorDate: Wed Nov 29 05:27:47 2023 +0100

    CAMEL-20163: consolidate duplicated charset extract/set code (#12242)
---
 .../org/apache/camel/http/base/HttpHelper.java     | 20 ------------
 .../org/apache/camel/http/common/HttpHelper.java   |  5 +--
 .../component/netty/http/NettyHttpConverter.java   |  3 +-
 .../component/netty/http/NettyHttpHelper.java      | 24 --------------
 .../http/handlers/HttpServerChannelHandler.java    |  2 +-
 .../org/apache/camel/support/http/HttpUtil.java    | 38 ++++++++++++++++++++++
 6 files changed, 44 insertions(+), 48 deletions(-)

diff --git a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpHelper.java b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpHelper.java
index b53cef30a58..e13b4059774 100644
--- a/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpHelper.java
+++ b/components/camel-http-base/src/main/java/org/apache/camel/http/base/HttpHelper.java
@@ -77,26 +77,6 @@ public final class HttpHelper {
         }
     }
 
-    /**
-     * @deprecated use {@link IOHelper#getCharsetNameFromContentType(String)}
-     */
-    @Deprecated
-    public static String getCharsetFromContentType(String contentType) {
-        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);
-                // there may be another parameter after a semi colon, so skip that
-                if (charset.contains(";")) {
-                    charset = StringHelper.before(charset, ";");
-                }
-                return IOHelper.normalizeCharset(charset);
-            }
-        }
-        return null;
-    }
-
     /**
      * Appends the key/value to the headers.
      * <p/>
diff --git a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
index 0035908bc0c..2c90b767921 100644
--- a/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
+++ b/components/camel-http-common/src/main/java/org/apache/camel/http/common/HttpHelper.java
@@ -35,6 +35,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.RuntimeExchangeException;
 import org.apache.camel.converter.stream.CachedOutputStream;
 import org.apache.camel.support.CamelObjectInputStream;
+import org.apache.camel.support.http.HttpUtil;
 import org.apache.camel.util.CollectionHelper;
 import org.apache.camel.util.IOHelper;
 import org.apache.camel.util.URISupport;
@@ -55,11 +56,11 @@ public final class HttpHelper {
     }
 
     public static void setCharsetFromContentType(String contentType, Exchange exchange) {
-        org.apache.camel.http.base.HttpHelper.setCharsetFromContentType(contentType, exchange);
+        HttpUtil.setCharsetFromContentType(contentType, exchange);
     }
 
     public static String getCharsetFromContentType(String contentType) {
-        return org.apache.camel.http.base.HttpHelper.getCharsetFromContentType(contentType);
+        return HttpUtil.getCharsetFromContentType(contentType);
     }
 
     /**
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java
index f7afabf4bbd..7ccd8cc8c27 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpConverter.java
@@ -30,6 +30,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePropertyKey;
 import org.apache.camel.component.netty.NettyConverter;
 import org.apache.camel.spi.TypeConverterRegistry;
+import org.apache.camel.support.http.HttpUtil;
 
 @Converter(generateLoader = true)
 public final class NettyHttpConverter {
@@ -87,7 +88,7 @@ public final class NettyHttpConverter {
     @Converter
     public static String toString(FullHttpResponse response, Exchange exchange) {
         String contentType = response.headers().get(NettyHttpConstants.CONTENT_TYPE);
-        String charset = NettyHttpHelper.getCharsetFromContentType(contentType);
+        String charset = HttpUtil.getCharsetFromContentType(contentType);
         if (charset == null && exchange != null) {
             charset = exchange.getProperty(ExchangePropertyKey.CHARSET_NAME, String.class);
         }
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
index 29e41d7792d..2ce85dd4a23 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/NettyHttpHelper.java
@@ -26,7 +26,6 @@ import java.util.Map;
 import io.netty.handler.codec.http.FullHttpResponse;
 import io.netty.handler.codec.http.HttpMethod;
 import org.apache.camel.Exchange;
-import org.apache.camel.ExchangePropertyKey;
 import org.apache.camel.Message;
 import org.apache.camel.RuntimeExchangeException;
 import org.apache.camel.util.CollectionHelper;
@@ -44,29 +43,6 @@ public final class NettyHttpHelper {
     private NettyHttpHelper() {
     }
 
-    public static void setCharsetFromContentType(String contentType, Exchange exchange) {
-        String charset = getCharsetFromContentType(contentType);
-        if (charset != null) {
-            exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, IOHelper.normalizeCharset(charset));
-        }
-    }
-
-    public static String getCharsetFromContentType(String contentType) {
-        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);
-                // there may be another parameter after a semi colon, so skip that
-                if (charset.contains(";")) {
-                    charset = StringHelper.before(charset, ";");
-                }
-                return IOHelper.normalizeCharset(charset);
-            }
-        }
-        return null;
-    }
-
     /**
      * Appends the key/value to the headers.
      * <p/>
diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
index a969904a804..e641b1ad9ba 100644
--- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
+++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java
@@ -360,7 +360,7 @@ public class HttpServerChannelHandler extends ServerChannelHandler {
 
         // honor the character encoding
         String contentType = in.getHeader(NettyHttpConstants.CONTENT_TYPE, String.class);
-        String charset = NettyHttpHelper.getCharsetFromContentType(contentType);
+        String charset = org.apache.camel.support.http.HttpUtil.getCharsetFromContentType(contentType);
         if (charset != null) {
             exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, charset);
             in.setHeader(NettyHttpConstants.HTTP_CHARACTER_ENCODING, charset);
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java b/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java
index d0f54109f8b..f055473cd09 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/http/HttpUtil.java
@@ -18,7 +18,10 @@
 package org.apache.camel.support.http;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePropertyKey;
 import org.apache.camel.Message;
+import org.apache.camel.util.IOHelper;
+import org.apache.camel.util.StringHelper;
 
 public final class HttpUtil {
     private static final int INTERNAL_SERVER_ERROR = 500;
@@ -53,4 +56,39 @@ public final class HttpUtil {
         return codeToUse;
     }
 
+    /**
+     * Deprecated way to extract the charset value from the content type string
+     * @deprecated use {@link IOHelper#getCharsetNameFromContentType(String)}
+     * @param contentType the content type string
+     * @return the charset value or null if there is nothing to extract
+     */
+    @Deprecated
+    public static String getCharsetFromContentType(String contentType) {
+        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);
+                // there may be another parameter after a semi colon, so skip that
+                if (charset.contains(";")) {
+                    charset = StringHelper.before(charset, ";");
+                }
+                return IOHelper.normalizeCharset(charset);
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Extracts the charset value from the content type string and sets it on the given exchange
+     * @param contentType the content type string
+     * @param exchange the exchange to set the charset value
+     */
+    public static void setCharsetFromContentType(String contentType, Exchange exchange) {
+        String charset = HttpUtil.getCharsetFromContentType(contentType);
+        if (charset != null) {
+            exchange.setProperty(ExchangePropertyKey.CHARSET_NAME, IOHelper.normalizeCharset(charset));
+        }
+    }
+
 }