You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2021/09/15 03:02:00 UTC

[incubator-shenyu] branch master updated: fix "modify response" plugin order bug and optimize the "clientResponse" (#2075)

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 18ce26c  fix "modify response" plugin order bug and optimize the "clientResponse" (#2075)
18ce26c is described below

commit 18ce26c1a811cf91f6a7dbcda4c40f516c9db0d2
Author: Qicz <qi...@gmail.com>
AuthorDate: Wed Sep 15 11:01:54 2021 +0800

    fix "modify response" plugin order bug and optimize the "clientResponse" (#2075)
    
    * fix "modify response" plugin order bug and optimize the "clientResponse".
    
    * Remove unnecessary judgments
---
 .../org/apache/shenyu/common/enums/PluginEnum.java | 22 +++++++++++-----------
 .../modify/response/ModifyResponsePlugin.java      | 16 ++++++----------
 2 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java b/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
index e9f8b1c..587957b 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
@@ -88,27 +88,27 @@ public enum PluginEnum {
     /**
      * Hystrix plugin enum.
      */
-    HYSTRIX(140, 0, "hystrix"),
+    HYSTRIX(130, 0, "hystrix"),
     
     /**
      * Sentinel plugin enum.
      */
-    SENTINEL(150, 0, "sentinel"),
+    SENTINEL(140, 0, "sentinel"),
     
     /**
      * Resilence4J plugin enum.
      */
-    RESILIENCE4J(160, 0, "resilience4j"),
+    RESILIENCE4J(150, 0, "resilience4j"),
     
     /**
      * Logging plugin enum.
      */
-    LOGGING(170, 0, "logging"),
+    LOGGING(160, 0, "logging"),
     
     /**
      * Monitor plugin enum.
      */
-    MONITOR(180, 0, "monitor"),
+    MONITOR(170, 0, "monitor"),
     
     /**
      * Divide plugin enum.
@@ -134,6 +134,11 @@ public enum PluginEnum {
      * Netty http client plugin enum.
      */
     NETTY_HTTP_CLIENT(210, 0, "nettyHttpClient"),
+
+    /**
+     * ModifyResponse plugin enum.
+     */
+    MODIFY_RESPONSE(220, 0, "modifyResponse"),
     
     /**
      * Param transform plugin enum.
@@ -173,12 +178,7 @@ public enum PluginEnum {
     /**
      * Response plugin enum.
      */
-    RESPONSE(420, 0, "response"),
-    
-    /**
-     * ModifyResponse plugin enum.
-     */
-    MODIFY_RESPONSE(430, 0, "modifyResponse");
+    RESPONSE(420, 0, "response");
 
     private final int code;
 
diff --git a/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java b/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java
index 625840b..16ff0d2 100644
--- a/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java
+++ b/shenyu-plugin/shenyu-plugin-modify-response/src/main/java/org/apache/shenyu/plugin/modify/response/ModifyResponsePlugin.java
@@ -31,14 +31,13 @@ import org.apache.shenyu.plugin.api.context.ShenyuContext;
 import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
 import org.apache.shenyu.plugin.base.support.BodyInserterContext;
 import org.apache.shenyu.plugin.base.support.CachedBodyOutputMessage;
-import org.apache.shenyu.plugin.modify.response.handler.ModifyResponsePluginDataHandler;
 import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
+import org.apache.shenyu.plugin.modify.response.handler.ModifyResponsePluginDataHandler;
 import org.reactivestreams.Publisher;
 import org.springframework.core.io.buffer.DataBuffer;
 import org.springframework.core.io.buffer.DataBufferUtils;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
 import org.springframework.http.ReactiveHttpOutputMessage;
 import org.springframework.http.codec.ServerCodecConfigurer;
 import org.springframework.http.server.reactive.ServerHttpResponse;
@@ -63,9 +62,6 @@ public class ModifyResponsePlugin extends AbstractShenyuPlugin {
     
     @Override
     protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
-        if (Objects.isNull(rule)) {
-            return Mono.empty();
-        }
         final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT);
         assert shenyuContext != null;
         final ModifyResponseRuleHandle modifyResponseRuleHandle = ModifyResponsePluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
@@ -129,10 +125,10 @@ public class ModifyResponsePlugin extends AbstractShenyuPlugin {
         @Override
         @NonNull
         public Mono<Void> writeWith(@NonNull final Publisher<? extends DataBuffer> body) {
-            HttpHeaders httpHeaders = new HttpHeaders();
-            httpHeaders.setContentType(MediaType.APPLICATION_JSON);
-
-            ClientResponse clientResponse = prepareClientResponse(body, httpHeaders);
+            ClientResponse clientResponse = exchange.getAttribute(Constants.CLIENT_RESPONSE_ATTR);
+            if (Objects.isNull(clientResponse)) {
+                clientResponse = prepareClientResponse(body, this.getHeaders());
+            }
             Mono<byte[]> modifiedBody = clientResponse.bodyToMono(byte[].class)
                     .flatMap(originalBody -> Mono.just(updateResponse(originalBody)));
 
@@ -181,7 +177,7 @@ public class ModifyResponsePlugin extends AbstractShenyuPlugin {
 
         private ClientResponse prepareClientResponse(final Publisher<? extends DataBuffer> body, final HttpHeaders httpHeaders) {
             ClientResponse.Builder builder;
-            builder = ClientResponse.create(exchange.getResponse().getStatusCode(), ServerCodecConfigurer.create().getReaders());
+            builder = ClientResponse.create(this.getDelegate().getStatusCode(), ServerCodecConfigurer.create().getReaders());
             return builder.headers(headers -> headers.putAll(httpHeaders)).body(Flux.from(body)).build();
         }
     }