diff --git a/fizz-common/src/main/java/we/util/Consts.java b/fizz-common/src/main/java/we/util/Consts.java index ea76fa4..b6e05f0 100644 --- a/fizz-common/src/main/java/we/util/Consts.java +++ b/fizz-common/src/main/java/we/util/Consts.java @@ -27,6 +27,11 @@ public final class Consts { } public static final class S { + public static final String TRUE = "true"; + public static final String FALSE = "false"; + public static final String TRUE1 = "1"; + public static final String FALSE0 = "0"; + public static final String EMPTY = ""; public static final String SPACE_STR = " "; public static final char SPACE = ' '; diff --git a/fizz-core/src/main/java/we/dedicated_line/DedicatedLineHttpHandler.java b/fizz-core/src/main/java/we/dedicated_line/DedicatedLineHttpHandler.java index aaabf12..8c27b49 100644 --- a/fizz-core/src/main/java/we/dedicated_line/DedicatedLineHttpHandler.java +++ b/fizz-core/src/main/java/we/dedicated_line/DedicatedLineHttpHandler.java @@ -148,14 +148,19 @@ class DedicatedLineHttpHandler implements HttpHandler { log.debug(sb.toString()); } Flux remoteRespBody = remoteResp.body(BodyExtractors.toDataBuffers()); + if (systemConfig.fizzDedicatedLineClientRequestCrypto()) { - respHeaders.remove(HttpHeaders.CONTENT_LENGTH); - return response.writeWith (decrypt(remoteRespBody, dedicatedLineInfo.requestCryptoKey)); - } else { - return response.writeWith (remoteRespBody) - .doOnError ( throwable -> cleanup(remoteResp) ) - .doOnCancel( () -> cleanup(remoteResp) ); + if (response.getStatusCode() == HttpStatus.OK) { + String v = respHeaders.getFirst(WebUtils.BODY_ENCRYPT); + if (org.apache.commons.lang3.StringUtils.isBlank(v) || v.equals(Consts.S.TRUE1)) { + respHeaders.remove(HttpHeaders.CONTENT_LENGTH); + return response.writeWith (decrypt(remoteRespBody, dedicatedLineInfo.requestCryptoKey)); + } + } } + return response.writeWith (remoteRespBody) + .doOnError ( throwable -> cleanup(remoteResp) ) + .doOnCancel( () -> cleanup(remoteResp) ); } ); diff --git a/fizz-core/src/main/java/we/filter/FilterExceptionHandlerConfig.java b/fizz-core/src/main/java/we/filter/FilterExceptionHandlerConfig.java index 82149fa..19f4002 100644 --- a/fizz-core/src/main/java/we/filter/FilterExceptionHandlerConfig.java +++ b/fizz-core/src/main/java/we/filter/FilterExceptionHandlerConfig.java @@ -17,6 +17,7 @@ package we.filter; +import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; @@ -30,6 +31,7 @@ import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.server.WebExceptionHandler; import reactor.core.publisher.Mono; +import we.Fizz; import we.config.SystemConfig; import we.exception.ExecuteScriptException; import we.exception.RedirectException; @@ -37,6 +39,7 @@ import we.exception.StopAndResponseException; import we.fizz.exception.FizzRuntimeException; import we.flume.clients.log4j2appender.LogService; import we.legacy.RespEntity; +import we.util.Consts; import we.util.JacksonUtils; import we.util.ThreadContext; import we.util.WebUtils; @@ -67,10 +70,16 @@ public class FilterExceptionHandlerConfig { } } + HttpHeaders respHeaders = resp.getHeaders(); + String value = Fizz.context.getEnvironment().getProperty(SystemConfig.FIZZ_DEDICATED_LINE_CLIENT_ENABLE); + if (StringUtils.isNotBlank(value) && value.equals(Consts.S.TRUE)) { + respHeaders.set(WebUtils.BODY_ENCRYPT, Consts.S.FALSE0); + } + if (t instanceof StopAndResponseException) { StopAndResponseException ex = (StopAndResponseException) t; if (ex.getData() != null) { - resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + respHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); return resp.writeWith(Mono.just(resp.bufferFactory().wrap(ex.getData().toString().getBytes()))); } } @@ -78,7 +87,7 @@ public class FilterExceptionHandlerConfig { RedirectException ex = (RedirectException) t; if (ex.getRedirectUrl() != null) { resp.setStatusCode(HttpStatus.MOVED_PERMANENTLY); - resp.getHeaders().setLocation(URI.create(ex.getRedirectUrl())); + respHeaders.setLocation(URI.create(ex.getRedirectUrl())); return Mono.empty(); } } @@ -89,7 +98,7 @@ public class FilterExceptionHandlerConfig { } if (t instanceof ExecuteScriptException) { ExecuteScriptException ex = (ExecuteScriptException) t; - resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + respHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); RespEntity rs = null; if (ex.getStepContext() != null && ex.getStepContext().returnContext()) { rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId, ex.getStepContext()); @@ -103,7 +112,7 @@ public class FilterExceptionHandlerConfig { if (t instanceof FizzRuntimeException) { FizzRuntimeException ex = (FizzRuntimeException) t; log.error(traceId + ' ' + tMsg, LogService.BIZ_ID, traceId, ex); - resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + respHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); RespEntity rs = null; if (ex.getStepContext() != null && ex.getStepContext().returnContext()) { rs = new RespEntity(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId, ex.getStepContext()); @@ -121,7 +130,7 @@ public class FilterExceptionHandlerConfig { WebUtils.request2stringBuilder(exchange, b); log.error(b.toString(), LogService.BIZ_ID, traceId, t); String s = WebUtils.jsonRespBody(HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, traceId); - resp.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + respHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); vm = resp.writeWith(Mono.just(resp.bufferFactory().wrap(s.getBytes()))); } else { vm = WebUtils.responseError(exchange, filterExceptionHandler, HttpStatus.INTERNAL_SERVER_ERROR.value(), tMsg, t); diff --git a/fizz-core/src/main/java/we/util/WebUtils.java b/fizz-core/src/main/java/we/util/WebUtils.java index 557c8e4..a528db1 100644 --- a/fizz-core/src/main/java/we/util/WebUtils.java +++ b/fizz-core/src/main/java/we/util/WebUtils.java @@ -113,6 +113,8 @@ public abstract class WebUtils { public static final String FIZZ_REQUEST = "fr@"; + public static final String BODY_ENCRYPT = "b-ecyt"; + private WebUtils() { } @@ -489,6 +491,9 @@ public abstract class WebUtils { // } // ); // } + + exchange.getResponse().getHeaders().set(BODY_ENCRYPT, Consts.S.FALSE0); + String traceId = getTraceId(exchange); // Schedulers.parallel().schedule(() -> { StringBuilder b = ThreadContext.getStringBuilder(); diff --git a/fizz-plugin/src/main/java/we/plugin/dedicatedline/codec/DedicatedLineCodecPluginFilter.java b/fizz-plugin/src/main/java/we/plugin/dedicatedline/codec/DedicatedLineCodecPluginFilter.java index 8066a35..8b66943 100644 --- a/fizz-plugin/src/main/java/we/plugin/dedicatedline/codec/DedicatedLineCodecPluginFilter.java +++ b/fizz-plugin/src/main/java/we/plugin/dedicatedline/codec/DedicatedLineCodecPluginFilter.java @@ -87,7 +87,7 @@ public class DedicatedLineCodecPluginFilter extends RequestBodyPlugin { FizzServerHttpResponseDecorator fizzServerHttpResponseDecorator = new FizzServerHttpResponseDecorator(original) { @Override public Publisher writeWith(DataBuffer remoteResponseBody) { - if (remoteResponseBody == NettyDataBufferUtils.EMPTY_DATA_BUFFER) { + if (remoteResponseBody == null || remoteResponseBody == NettyDataBufferUtils.EMPTY_DATA_BUFFER) { return Mono.empty(); } else { if (StringUtils.isNotBlank(cryptoKey)) {