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 2022/06/08 08:31:20 UTC
[servicecomb-java-chassis] branch master updated: [SCB-2533] [master] encode invocation context before add to http header (#3045)
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/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 42ebfe4bc [SCB-2533] [master] encode invocation context before add to http header (#3045)
42ebfe4bc is described below
commit 42ebfe4bc0c8a1c1d8348ed73d0b07bae667b46b
Author: demonbug <24...@users.noreply.github.com>
AuthorDate: Wed Jun 8 16:31:14 2022 +0800
[SCB-2533] [master] encode invocation context before add to http header (#3045)
---
dependencies/default/pom.xml | 7 ++
.../common/utils/AbstractRestObjectMapper.java | 8 +++
.../foundation/common/utils/JsonUtils.java | 9 +++
.../foundation/common/utils/RestObjectMapper.java | 10 +++
.../foundation/common/utils/JsonUtilsTest.java | 45 +++++++++++++
.../transport-rest/transport-rest-client/pom.xml | 5 ++
.../rest/client/http/RestClientInvocation.java | 2 +-
.../rest/client/http/TestRestClientInvocation.java | 75 +++++++++++++++++++++-
8 files changed, 159 insertions(+), 2 deletions(-)
diff --git a/dependencies/default/pom.xml b/dependencies/default/pom.xml
index 66cf190cd..c814dfa31 100644
--- a/dependencies/default/pom.xml
+++ b/dependencies/default/pom.xml
@@ -45,6 +45,7 @@
<commons-lang.version>2.6</commons-lang.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<commons-logging.version>1.2</commons-logging.version>
+ <commons-text.version>1.9</commons-text.version>
<esotericsoftware.version>4.0.2</esotericsoftware.version>
<findbugs-jsr305.version>3.0.2</findbugs-jsr305.version>
<governator-annotations.version>1.17.12</governator-annotations.version>
@@ -585,6 +586,12 @@
<version>${commons-lang3.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-text</artifactId>
+ <version>${commons-text.version}</version>
+ </dependency>
+
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/AbstractRestObjectMapper.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/AbstractRestObjectMapper.java
index 0a246962c..43f106270 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/AbstractRestObjectMapper.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/AbstractRestObjectMapper.java
@@ -22,5 +22,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
public abstract class AbstractRestObjectMapper extends ObjectMapper {
private static final long serialVersionUID = 189026839992490564L;
+ public AbstractRestObjectMapper() {
+ super();
+ }
+
+ public AbstractRestObjectMapper(RestObjectMapper src) {
+ super(src);
+ }
+
abstract public String convertToString(Object value) throws Exception;
}
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JsonUtils.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JsonUtils.java
index ed1d1ad12..07269d683 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JsonUtils.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/JsonUtils.java
@@ -22,14 +22,19 @@ import java.io.InputStream;
import java.io.OutputStream;
import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public final class JsonUtils {
public static final ObjectMapper OBJ_MAPPER;
+ private static final ObjectMapper UNICODE_OBJ_MAPPER;
static {
OBJ_MAPPER = new RestObjectMapper();
+
+ UNICODE_OBJ_MAPPER = OBJ_MAPPER.copy();
+ UNICODE_OBJ_MAPPER.enable(JsonWriteFeature.ESCAPE_NON_ASCII.mappedFeature());
}
private JsonUtils() {
@@ -55,6 +60,10 @@ public final class JsonUtils {
return OBJ_MAPPER.writeValueAsString(value);
}
+ public static String writeUnicodeValueAsString(Object value) throws JsonProcessingException {
+ return UNICODE_OBJ_MAPPER.writeValueAsString(value);
+ }
+
public static <T> T convertValue(Object fromValue, Class<T> toValueType) {
return OBJ_MAPPER.convertValue(fromValue, toValueType);
}
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/RestObjectMapper.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/RestObjectMapper.java
index 1f5669d8e..edc609587 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/RestObjectMapper.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/utils/RestObjectMapper.java
@@ -47,6 +47,7 @@ public class RestObjectMapper extends AbstractRestObjectMapper {
private static final JavaType STRING_JAVA_TYPE = TypeFactory.defaultInstance().constructType(String.class);
public RestObjectMapper() {
+ super();
getFactory().disable(Feature.AUTO_CLOSE_SOURCE);
// Enable features that can tolerance errors and not enable those make more constraints for compatible reasons.
// Developers can use validation api to do more checks.
@@ -66,6 +67,15 @@ public class RestObjectMapper extends AbstractRestObjectMapper {
registerModule(new JavaTimeModule());
}
+ public RestObjectMapper(RestObjectMapper src) {
+ super(src);
+ }
+ @Override
+ public RestObjectMapper copy() {
+ this._checkInvalidCopy(RestObjectMapper.class);
+ return new RestObjectMapper(this);
+ }
+
@Override
public String convertToString(Object value) throws Exception {
return convertValue(value, STRING_JAVA_TYPE);
diff --git a/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/JsonUtilsTest.java b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/JsonUtilsTest.java
new file mode 100644
index 000000000..60c5d9c43
--- /dev/null
+++ b/foundations/foundation-common/src/test/java/org/apache/servicecomb/foundation/common/utils/JsonUtilsTest.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.servicecomb.foundation.common.utils;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.core.type.TypeReference;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class JsonUtilsTest {
+
+ @Test
+ void testWriteUnicodeValueAsString() throws JsonProcessingException {
+ String unicodeStr = "测试";
+ Map<String, String> inMap = new HashMap<>();
+ inMap.put("key", unicodeStr);
+
+ Assertions.assertFalse(StringUtils.isAsciiPrintable(JsonUtils.writeValueAsString(inMap)));
+ String jsonStr = JsonUtils.writeUnicodeValueAsString(inMap);
+ Assertions.assertTrue(StringUtils.isAsciiPrintable(jsonStr));
+
+ Map<String, String> outMap = JsonUtils.OBJ_MAPPER.readValue(jsonStr, new TypeReference<Map<String, String>>() {
+ });
+ Assertions.assertEquals(unicodeStr, outMap.get("key"));
+ }
+}
diff --git a/transports/transport-rest/transport-rest-client/pom.xml b/transports/transport-rest/transport-rest-client/pom.xml
index 904dee554..62db05b65 100644
--- a/transports/transport-rest/transport-rest-client/pom.xml
+++ b/transports/transport-rest/transport-rest-client/pom.xml
@@ -72,5 +72,10 @@
<artifactId>swagger-generator-jaxrs</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-text</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/RestClientInvocation.java b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/RestClientInvocation.java
index 4c3651e6a..18339c153 100644
--- a/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/RestClientInvocation.java
+++ b/transports/transport-rest/transport-rest-client/src/main/java/org/apache/servicecomb/transport/rest/client/http/RestClientInvocation.java
@@ -298,7 +298,7 @@ public class RestClientInvocation {
protected void setCseContext() {
try {
- clientRequest.putHeader(Const.CSE_CONTEXT, JsonUtils.writeValueAsString(invocation.getContext()));
+ clientRequest.putHeader(Const.CSE_CONTEXT, JsonUtils.writeUnicodeValueAsString(invocation.getContext()));
} catch (Throwable e) {
invocation.getTraceIdLogger().error(LOGGER, "Failed to encode and set cseContext, message={}."
, ExceptionUtils.getExceptionMessageWithoutTrace(e));
diff --git a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestRestClientInvocation.java b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestRestClientInvocation.java
index 5bfa5286c..5687dcdbe 100644
--- a/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestRestClientInvocation.java
+++ b/transports/transport-rest/transport-rest-client/src/test/java/org/apache/servicecomb/transport/rest/client/http/TestRestClientInvocation.java
@@ -27,8 +27,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.text.StringEscapeUtils;
import org.apache.log4j.Level;
import org.apache.servicecomb.common.rest.RestConst;
+import org.apache.servicecomb.common.rest.VertxRestInvocation;
import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
import org.apache.servicecomb.common.rest.definition.path.URLPathBuilder;
import org.apache.servicecomb.common.rest.filter.HttpClientFilter;
@@ -44,6 +47,7 @@ import org.apache.servicecomb.foundation.common.utils.JsonUtils;
import org.apache.servicecomb.foundation.test.scaffolding.exception.RuntimeExceptionWithoutStackTrace;
import org.apache.servicecomb.foundation.test.scaffolding.log.LogCollector;
import org.apache.servicecomb.foundation.vertx.client.http.HttpClientWithContext;
+import org.apache.servicecomb.foundation.vertx.http.HttpServletRequestEx;
import org.apache.servicecomb.registry.definition.DefinitionConst;
import org.apache.servicecomb.swagger.invocation.AsyncResponse;
import org.apache.servicecomb.swagger.invocation.Response;
@@ -261,7 +265,7 @@ public class TestRestClientInvocation {
new Expectations(JsonUtils.class) {
{
- JsonUtils.writeValueAsString(any);
+ JsonUtils.writeUnicodeValueAsString(any);
result = new RuntimeExceptionWithoutStackTrace();
}
};
@@ -274,6 +278,75 @@ public class TestRestClientInvocation {
logCollector.teardown();
}
+ @Test
+ public void testSetCseContext_enable_unicode() {
+ Map<String, String> contextMap = new HashMap<String, String>();
+ contextMap.put("key", "测试");
+ contextMap.put("encodedKey", StringEscapeUtils.escapeJson("测试"));
+ when(invocation.getContext()).thenReturn(contextMap);
+ restClientInvocation.setCseContext();
+
+ String context = headers.get(org.apache.servicecomb.core.Const.CSE_CONTEXT);
+ HttpServletRequestEx requestEx = new MockUp<HttpServletRequestEx>(){
+ @Mock
+ public String getHeader(String name){
+ if (StringUtils.equals(name, org.apache.servicecomb.core.Const.CSE_CONTEXT)){
+ return context;
+ } else {
+ return null;
+ }
+ }
+ }.getMockInstance();
+
+ VertxRestInvocation vertxRestInvocation = new VertxRestInvocation();
+ Deencapsulation.setField(vertxRestInvocation, "requestEx", requestEx);
+ Deencapsulation.setField(vertxRestInvocation, "invocation", invocation);
+
+ Deencapsulation.invoke(vertxRestInvocation, "setContext");
+
+ Assertions.assertEquals("测试", invocation.getContext().get("key"));
+ Assertions.assertEquals(StringEscapeUtils.escapeJson("测试"), invocation.getContext().get("encodedKey"));
+ }
+
+
+ @Test
+ public void testSetCseContext_disable_unicode() throws JsonProcessingException {
+ Map<String, String> contextMap = new HashMap<String, String>();
+ contextMap.put("key", "测试");
+ contextMap.put("encodedKey", StringEscapeUtils.escapeJson("测试"));
+ when(invocation.getContext()).thenReturn(contextMap);
+
+ new MockUp<JsonUtils>() {
+ @Mock
+ public String writeUnicodeValueAsString(Object value) throws JsonProcessingException {
+ return JsonUtils.writeValueAsString(value);
+ }
+ };
+
+ restClientInvocation.setCseContext();
+ String context = headers.get(org.apache.servicecomb.core.Const.CSE_CONTEXT);
+ HttpServletRequestEx requestEx = new MockUp<HttpServletRequestEx>(){
+ @Mock
+ public String getHeader(String name){
+ if (StringUtils.equals(name, org.apache.servicecomb.core.Const.CSE_CONTEXT)){
+ return context;
+ } else {
+ return null;
+ }
+ }
+ }.getMockInstance();
+
+ VertxRestInvocation vertxRestInvocation = new VertxRestInvocation();
+ Deencapsulation.setField(vertxRestInvocation, "requestEx", requestEx);
+ Deencapsulation.setField(vertxRestInvocation, "invocation", invocation);
+
+ Deencapsulation.invoke(vertxRestInvocation, "setContext");
+
+ Assertions.assertEquals("测试", invocation.getContext().get("key"));
+ Assertions.assertEquals(StringEscapeUtils.escapeJson("测试"), invocation.getContext().get("encodedKey"));
+ }
+
+
@SuppressWarnings("unchecked")
@Test
public void handleResponse() {