This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch 3.3.0-beta.1-release
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.3.0-beta.1-release by this
push:
new dad3b070cd fix pb class not found (#12997)
dad3b070cd is described below
commit dad3b070cd6a92c917ad335f002ccdca059333d7
Author: icodening <[email protected]>
AuthorDate: Mon Sep 4 10:53:51 2023 +0800
fix pb class not found (#12997)
---
.../dubbo/remoting/http12/message/JsonCodec.java | 21 -----
.../remoting/http12/message/JsonCodecFactory.java | 2 +
.../dubbo/remoting/http12/message/JsonPbCodec.java | 101 +++++++++++++++++++++
...onCodecFactory.java => JsonPbCodecFactory.java} | 15 ++-
...remoting.http12.message.HttpMessageCodecFactory | 1 +
5 files changed, 116 insertions(+), 24 deletions(-)
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 819de6f420..5ee8dfb13e 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,11 +17,8 @@
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;
@@ -48,11 +45,6 @@ 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 {
@@ -87,11 +79,6 @@ 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();
@@ -136,12 +123,4 @@ public class JsonCodec implements HttpMessageCodec {
throw new DecodeException(e);
}
}
-
- private boolean isProtobuf(Class<?> targetType) {
- if (targetType == null) {
- return false;
- }
- return Message.class.isAssignableFrom(targetType);
- }
-
}
diff --git
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodecFactory.java
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodecFactory.java
index 8f7d2f356c..64a7cd2f55 100644
---
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodecFactory.java
+++
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodecFactory.java
@@ -23,6 +23,8 @@ import org.apache.dubbo.rpc.model.FrameworkModel;
@Activate
public class JsonCodecFactory implements HttpMessageCodecFactory {
+ public static final String NAME = "json";
+
@Override
public HttpMessageCodec createCodec(URL url, FrameworkModel
frameworkModel) {
return new JsonCodec();
diff --git
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodec.java
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodec.java
new file mode 100644
index 0000000000..4ade704e97
--- /dev/null
+++
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodec.java
@@ -0,0 +1,101 @@
+/*
+ * 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.dubbo.remoting.http12.message;
+
+import com.google.protobuf.Message;
+import com.google.protobuf.util.JsonFormat;
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.utils.ClassUtils;
+import org.apache.dubbo.common.utils.MethodUtils;
+import org.apache.dubbo.remoting.http12.exception.DecodeException;
+import org.apache.dubbo.remoting.http12.exception.EncodeException;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
+
+@Activate
+public class JsonPbCodec implements HttpMessageCodec {
+
+ private HttpMessageCodec jsonCodec;
+
+ public void setJsonCodec(HttpMessageCodec jsonCodec) {
+ this.jsonCodec = jsonCodec;
+ }
+
+ @Override
+ public MediaType contentType() {
+ return jsonCodec.contentType();
+ }
+
+ @Override
+ public boolean support(String contentType) {
+ return HttpMessageCodec.super.support(contentType) &&
ClassUtils.isPresent("com.google.protobuf.Message",
getClass().getClassLoader());
+ }
+
+ @Override
+ public void encode(OutputStream outputStream, Object unSerializedBody)
throws EncodeException {
+ try {
+ if (unSerializedBody instanceof Message) {
+ String jsonString = JsonFormat.printer().print((Message)
unSerializedBody);
+
outputStream.write(jsonString.getBytes(StandardCharsets.UTF_8));
+ return;
+ }
+ } catch (IOException e) {
+ throw new EncodeException(e);
+ }
+ jsonCodec.encode(outputStream, unSerializedBody);
+ }
+
+ @Override
+ public void encode(OutputStream outputStream, Object[] data) throws
EncodeException {
+ jsonCodec.encode(outputStream, data);
+ }
+
+ @Override
+ public Object decode(InputStream body, Class<?> targetType) throws
DecodeException {
+ try {
+ if (isProtobuf(targetType)) {
+ int len;
+ byte[] data = new byte[4096];
+ StringBuilder builder = new StringBuilder(4096);
+ while ((len = body.read(data)) != -1) {
+ builder.append(new String(data, 0, len));
+ }
+ Message.Builder newBuilder = (Message.Builder)
MethodUtils.findMethod(targetType, "newBuilder").invoke(null);
+
JsonFormat.parser().ignoringUnknownFields().merge(builder.toString(),
newBuilder);
+ return newBuilder.build();
+ }
+ } catch (Throwable e) {
+ throw new DecodeException(e);
+ }
+ return jsonCodec.decode(body, targetType);
+ }
+
+ @Override
+ public Object[] decode(InputStream dataInputStream, Class<?>[]
targetTypes) throws DecodeException {
+ return jsonCodec.decode(dataInputStream, targetTypes);
+ }
+
+ private boolean isProtobuf(Class<?> targetType) {
+ if (targetType == null) {
+ return false;
+ }
+ return Message.class.isAssignableFrom(targetType);
+ }
+}
diff --git
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodecFactory.java
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodecFactory.java
similarity index 64%
copy from
dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodecFactory.java
copy to
dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodecFactory.java
index 8f7d2f356c..3b7cf19d0a 100644
---
a/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonCodecFactory.java
+++
b/dubbo-remoting/dubbo-remoting-http12/src/main/java/org/apache/dubbo/remoting/http12/message/JsonPbCodecFactory.java
@@ -18,18 +18,27 @@ package org.apache.dubbo.remoting.http12.message;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.rpc.model.FrameworkModel;
-@Activate
-public class JsonCodecFactory implements HttpMessageCodecFactory {
+@Activate(order = -100)
+public class JsonPbCodecFactory implements HttpMessageCodecFactory {
@Override
public HttpMessageCodec createCodec(URL url, FrameworkModel
frameworkModel) {
- return new JsonCodec();
+ HttpMessageCodec codec =
frameworkModel.getExtensionLoader(HttpMessageCodecFactory.class).getExtension(JsonCodecFactory.NAME).createCodec(url,
frameworkModel);
+ JsonPbCodec jsonPbCodec = new JsonPbCodec();
+ jsonPbCodec.setJsonCodec(codec);
+ return jsonPbCodec;
}
@Override
public MediaType contentType() {
return MediaType.APPLICATION_JSON_VALUE;
}
+
+ @Override
+ public boolean support(String contentType) {
+ return HttpMessageCodecFactory.super.support(contentType) &&
ClassUtils.isPresent("com.google.protobuf.Message",
getClass().getClassLoader());
+ }
}
diff --git
a/dubbo-remoting/dubbo-remoting-http12/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.message.HttpMessageCodecFactory
b/dubbo-remoting/dubbo-remoting-http12/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.message.HttpMessageCodecFactory
index f72f51f4b6..0fcf5fb19a 100644
---
a/dubbo-remoting/dubbo-remoting-http12/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.message.HttpMessageCodecFactory
+++
b/dubbo-remoting/dubbo-remoting-http12/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.http12.message.HttpMessageCodecFactory
@@ -1 +1,2 @@
json=org.apache.dubbo.remoting.http12.message.JsonCodecFactory
+jsonpb=org.apache.dubbo.remoting.http12.message.JsonPbCodecFactory