This is an automated email from the ASF dual-hosted git repository.

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new c56f8a0  [type: refactor] optimize upstream result data processing. 
(#2758)
c56f8a0 is described below

commit c56f8a0d13c9efc2a67be4194906543d84b9aa2b
Author: Qicz <[email protected]>
AuthorDate: Wed Jan 12 21:32:08 2022 +0800

    [type: refactor] optimize upstream result data processing. (#2758)
    
    * [type: refactor] optimize upstream result data processing.
    
    * polish
---
 .../org/apache/shenyu/plugin/api/result/ShenyuResult.java   | 10 +++++-----
 .../apache/shenyu/plugin/api/utils/WebFluxResultUtils.java  | 13 ++++++-------
 2 files changed, 11 insertions(+), 12 deletions(-)

diff --git 
a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/result/ShenyuResult.java
 
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/result/ShenyuResult.java
index b4a54dc..2764ae4 100644
--- 
a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/result/ShenyuResult.java
+++ 
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/result/ShenyuResult.java
@@ -35,7 +35,7 @@ public interface ShenyuResult<T> {
      * The response result.
      *
      * @param exchange the exchange
-     * @param formatted the formatted object
+     * @param formatted the formatted data that is origin data(basic、byte[]) 
or json string
      * @return the result object
      */
     default Object result(ServerWebExchange exchange, Object formatted) {
@@ -43,15 +43,15 @@ public interface ShenyuResult<T> {
     }
 
     /**
-     * format the origin, default is json format.
+     * format the origin, default is json format except the basic and bytes.
      *
      * @param exchange the exchange
      * @param origin the origin
      * @return format origin
      */
     default Object format(ServerWebExchange exchange, Object origin) {
-        // basic data
-        if (ObjectTypeUtils.isBasicType(origin)) {
+        // basic data or upstream data
+        if (ObjectTypeUtils.isBasicType(origin) || (origin instanceof byte[])) 
{
             return origin;
         }
         // error result or rpc origin result.
@@ -62,7 +62,7 @@ public interface ShenyuResult<T> {
      * the response context type, default is application/json.
      *
      * @param exchange the exchange
-     * @param formatted the formatted data that is origin data or byte[] 
convert string
+     * @param formatted the formatted data that is origin data(basic、byte[]) 
or json string
      * @return the context type
      */
     default MediaType contentType(ServerWebExchange exchange, Object 
formatted) {
diff --git 
a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java
 
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java
index f00d40b..37619e0 100644
--- 
a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java
+++ 
b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/utils/WebFluxResultUtils.java
@@ -55,20 +55,19 @@ public final class WebFluxResultUtils {
             return Mono.empty();
         }
         final ShenyuResult<?> shenyuResult = ShenyuResultWrap.shenyuResult();
-        Object resultData = result;
-        // WebClientMessageWriter provide byte[] data, convert to string
-        if (result instanceof byte[]) {
-            resultData = new String((byte[]) result, StandardCharsets.UTF_8);
-        }
-        resultData = shenyuResult.format(exchange, resultData);
+        Object resultData = shenyuResult.format(exchange, result);
         // basic data use text/plain
         MediaType mediaType = MediaType.TEXT_PLAIN;
         if (!ObjectTypeUtils.isBasicType(result)) {
             mediaType = shenyuResult.contentType(exchange, resultData);
         }
         exchange.getResponse().getHeaders().setContentType(mediaType);
+        final Object responseData = shenyuResult.result(exchange, resultData);
+        assert null != responseData;
+        final byte[] bytes = (responseData instanceof byte[])
+                ? (byte[]) responseData : 
responseData.toString().getBytes(StandardCharsets.UTF_8);
         return 
exchange.getResponse().writeWith(Mono.just(exchange.getResponse()
-                        
.bufferFactory().wrap(Objects.requireNonNull(shenyuResult.result(exchange, 
resultData)).toString().getBytes(StandardCharsets.UTF_8)))
+                        .bufferFactory().wrap(bytes))
                 .doOnNext(data -> 
exchange.getResponse().getHeaders().setContentLength(data.readableByteCount())));
     }
 

Reply via email to