You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/06/04 06:22:18 UTC
[incubator-servicecomb-java-chassis] 02/03: [SCB-637] support
getCharsetFromContentType in HttpUtils
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 48db26ca9f701f0b6603442c19c339ebb5ff7437
Author: wujimin <wu...@huawei.com>
AuthorDate: Mon Jun 4 00:12:06 2018 +0800
[SCB-637] support getCharsetFromContentType in HttpUtils
---
.../foundation/common/http/HttpUtils.java | 28 ++++++++++++
.../foundation/common/http/TestHttpUtils.java | 51 ++++++++++++++++++++++
2 files changed, 79 insertions(+)
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/http/HttpUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/http/HttpUtils.java
index 6edcee4..ed94d76 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/http/HttpUtils.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/http/HttpUtils.java
@@ -81,4 +81,32 @@ public final class HttpUtils {
fileName = uriDecodePath(fileName);
return new File(fileName).getName();
}
+
+ /**
+ * Parse the character encoding from the specified content type header.
+ * If the content type is null, or there is no explicit character encoding,
+ * <code>null</code> is returned.
+ *
+ * @param contentType a content type header
+ */
+ public static String getCharsetFromContentType(String contentType) {
+ if (contentType == null) {
+ return null;
+ }
+ int start = contentType.indexOf("charset=");
+ if (start < 0) {
+ return null;
+ }
+ String encoding = contentType.substring(start + 8);
+ int end = encoding.indexOf(';');
+ if (end >= 0) {
+ encoding = encoding.substring(0, end);
+ }
+ encoding = encoding.trim();
+ if ((encoding.length() > 2) && (encoding.startsWith("\""))
+ && (encoding.endsWith("\""))) {
+ encoding = encoding.substring(1, encoding.length() - 1);
+ }
+ return encoding.trim();
+ }
}
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/http/TestHttpUtils.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/http/TestHttpUtils.java
index 5fbd603..6fc0c8c 100644
--- a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/http/TestHttpUtils.java
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/http/TestHttpUtils.java
@@ -18,6 +18,8 @@ package org.apache.servicecomb.foundation.common.http;
import java.net.URISyntaxException;
+import javax.ws.rs.core.MediaType;
+
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Rule;
@@ -111,4 +113,53 @@ public class TestHttpUtils {
public void parseFileNameFromHeaderValue_ignorePath() {
Assert.assertEquals("a.txt", HttpUtils.parseFileNameFromHeaderValue("xx;filename=../../a.txt"));
}
+
+ @Test
+ public void getCharsetFromContentType_noContentType() {
+ String character = HttpUtils.getCharsetFromContentType(null);
+
+ Assert.assertNull(character);
+ }
+
+ @Test
+ public void getCharsetFromContentType_noCharset() {
+ String character = HttpUtils.getCharsetFromContentType(MediaType.APPLICATION_JSON);
+
+ Assert.assertNull(character);
+ }
+
+ @Test
+ public void getCharsetFromContentType_noSemicolonEnd() {
+ String character = HttpUtils.getCharsetFromContentType(MediaType.APPLICATION_JSON + ";charset=utf-8");
+
+ Assert.assertEquals("utf-8", character);
+ }
+
+ @Test
+ public void getCharsetFromContentType_semicolonEnd() {
+ String character = HttpUtils.getCharsetFromContentType(MediaType.APPLICATION_JSON + ";charset=utf-8;");
+
+ Assert.assertEquals("utf-8", character);
+ }
+
+ @Test
+ public void getCharsetFromContentType_needTrim() {
+ String character = HttpUtils.getCharsetFromContentType(MediaType.APPLICATION_JSON + ";charset= utf-8 ;");
+
+ Assert.assertEquals("utf-8", character);
+ }
+
+ @Test
+ public void getCharsetFromContentType_quotationMarks() {
+ String character = HttpUtils.getCharsetFromContentType(MediaType.APPLICATION_JSON + ";charset=\"utf-8\";");
+
+ Assert.assertEquals("utf-8", character);
+ }
+
+ @Test
+ public void getCharsetFromContentType_quotationMarks_needTrim() {
+ String character = HttpUtils.getCharsetFromContentType(MediaType.APPLICATION_JSON + ";charset=\" utf-8 \";");
+
+ Assert.assertEquals("utf-8", character);
+ }
}
--
To stop receiving notification emails like this one, please contact
liubao@apache.org.