This is an automated email from the ASF dual-hosted git repository.
icodening pushed a commit to branch fix_h2_curl
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/fix_h2_curl by this push:
new fead06d853 support json to pb
fead06d853 is described below
commit fead06d853d45a483a92718b50ad98b3ffa910fc
Author: icodening <[email protected]>
AuthorDate: Fri Sep 1 23:42:51 2023 +0800
support json to pb
---
dubbo-remoting/dubbo-remoting-http12/pom.xml | 4 ++++
.../dubbo/remoting/http12/message/JsonCodec.java | 20 ++++++++++++++++++++
.../tri/h12/AbstractServerTransportListener.java | 4 ++++
3 files changed, 28 insertions(+)
diff --git a/dubbo-remoting/dubbo-remoting-http12/pom.xml
b/dubbo-remoting/dubbo-remoting-http12/pom.xml
index 514786699c..ad702991e9 100644
--- a/dubbo-remoting/dubbo-remoting-http12/pom.xml
+++ b/dubbo-remoting/dubbo-remoting-http12/pom.xml
@@ -46,6 +46,10 @@
<artifactId>dubbo-remoting-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
+ <dependency>
+ <groupId>com.google.protobuf</groupId>
+ <artifactId>protobuf-java-util</artifactId>
+ </dependency>
<dependency>
<groupId>io.netty</groupId>
diff --git
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodec.java
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodec.java
index fb6a6f6faa..819de6f420 100644
---
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodec.java
+++
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodec.java
@@ -17,8 +17,11 @@
package org.apache.dubbo.remoting.http12.message;
import com.alibaba.fastjson2.JSONObject;
+import com.google.protobuf.Message;
+import com.google.protobuf.util.JsonFormat;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.JsonUtils;
+import org.apache.dubbo.common.utils.MethodUtils;
import org.apache.dubbo.remoting.http12.exception.DecodeException;
import org.apache.dubbo.remoting.http12.exception.EncodeException;
@@ -45,6 +48,11 @@ public class JsonCodec implements HttpMessageCodec {
public void encode(OutputStream outputStream, Object unSerializedBody)
throws EncodeException {
try {
try {
+ if (unSerializedBody instanceof Message) {
+ String jsonString = JsonFormat.printer().print((Message)
unSerializedBody);
+
outputStream.write(jsonString.getBytes(StandardCharsets.UTF_8));
+ return;
+ }
String jsonString = JsonUtils.toJson(unSerializedBody);
outputStream.write(jsonString.getBytes(StandardCharsets.UTF_8));
} finally {
@@ -79,6 +87,11 @@ public class JsonCodec implements HttpMessageCodec {
while ((len = body.read(data)) != -1) {
builder.append(new String(data, 0, len));
}
+ if (isProtobuf(targetType)) {
+ Message.Builder newBuilder = (Message.Builder)
MethodUtils.findMethod(targetType, "newBuilder").invoke(null);
+
JsonFormat.parser().ignoringUnknownFields().merge(builder.toString(),
newBuilder);
+ return newBuilder.build();
+ }
return JsonUtils.toJavaObject(builder.toString(), targetType);
} finally {
body.close();
@@ -124,4 +137,11 @@ public class JsonCodec implements HttpMessageCodec {
}
}
+ private boolean isProtobuf(Class<?> targetType) {
+ if (targetType == null) {
+ return false;
+ }
+ return Message.class.isAssignableFrom(targetType);
+ }
+
}
diff --git
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/AbstractServerTransportListener.java
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/AbstractServerTransportListener.java
index 1e442d1e5c..a80785ab13 100644
---
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/AbstractServerTransportListener.java
+++
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/h12/AbstractServerTransportListener.java
@@ -118,10 +118,12 @@ public abstract class
AbstractServerTransportListener<HEADER extends RequestMeta
try {
this.executor = initializeExecutor(metadata);
} catch (Throwable throwable) {
+ LOGGER.error("initialize executor fail.", throwable);
onError(throwable);
return;
}
if (this.executor == null) {
+ LOGGER.error("executor must be not null.");
onError(new NullPointerException("initializeExecutor return
null"));
return;
}
@@ -129,6 +131,7 @@ public abstract class
AbstractServerTransportListener<HEADER extends RequestMeta
try {
doOnMetadata(metadata);
} catch (Throwable throwable) {
+ LOGGER.error("server internal error", throwable);
onError(throwable);
}
});
@@ -174,6 +177,7 @@ public abstract class
AbstractServerTransportListener<HEADER extends RequestMeta
try {
doOnData(message);
} catch (Throwable e) {
+ LOGGER.error("server internal error", e);
onError(e);
}
});