Merge remote-tracking branch 'upstream/3.3.0-beta.1-release' into 3.3.0-beta.1-release
This commit is contained in:
commit
fd04ef711b
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue