diff --git a/fizz-core/src/main/java/we/filter/AggregateFilter.java b/fizz-core/src/main/java/we/filter/AggregateFilter.java index 849d4e7..5e3050c 100644 --- a/fizz-core/src/main/java/we/filter/AggregateFilter.java +++ b/fizz-core/src/main/java/we/filter/AggregateFilter.java @@ -185,10 +185,13 @@ public class AggregateFilter implements WebFilter { } return result.subscribeOn(Schedulers.elastic()).flatMap(aggResult -> { LogService.setBizId(traceId); + if (aggResult.getHttpStatus() != null) { + serverHttpResponse.setRawStatusCode(aggResult.getHttpStatus()); + } String jsonString = null; - if(aggResult.getBody() instanceof String) { + if (aggResult.getBody() instanceof String) { jsonString = (String) aggResult.getBody(); - }else { + } else { if (this.aggregateFilterProperties.isWriteMapNullValue()) { jsonString = JSON.toJSONString(aggResult.getBody(), SerializerFeature.WriteMapNullValue); } else { diff --git a/fizz-core/src/main/java/we/fizz/AggregateResult.java b/fizz-core/src/main/java/we/fizz/AggregateResult.java index c41c9b7..489021b 100644 --- a/fizz-core/src/main/java/we/fizz/AggregateResult.java +++ b/fizz-core/src/main/java/we/fizz/AggregateResult.java @@ -25,6 +25,8 @@ import org.springframework.util.MultiValueMap; * */ public class AggregateResult { + + private Integer httpStatus; private MultiValueMap headers; @@ -56,4 +58,12 @@ public class AggregateResult { this.stepContext = stepContext; } + public Integer getHttpStatus() { + return httpStatus; + } + + public void setHttpStatus(Integer httpStatus) { + this.httpStatus = httpStatus; + } + } diff --git a/fizz-core/src/main/java/we/fizz/Pipeline.java b/fizz-core/src/main/java/we/fizz/Pipeline.java index 0b22468..bdbc522 100644 --- a/fizz-core/src/main/java/we/fizz/Pipeline.java +++ b/fizz-core/src/main/java/we/fizz/Pipeline.java @@ -50,6 +50,8 @@ import we.fizz.component.ComponentResult; import we.fizz.component.IComponent; import we.fizz.component.StepContextPosition; import we.fizz.exception.FizzRuntimeException; +import we.fizz.field.FieldConfig; +import we.fizz.field.ValueTypeEnum; import we.fizz.input.ClientInputConfig; import we.fizz.input.Input; import we.fizz.input.InputConfig; @@ -332,6 +334,7 @@ public class Pipeline { } response = group.get("response"); String respContentType = null; + int statusCode = 200; if (input != null && input.getConfig() != null && input.getConfig().getDataMapping() != null) { Map responseMapping = (Map) input.getConfig().getDataMapping() .get("response"); @@ -341,7 +344,28 @@ public class Pipeline { if (!CollectionUtils.isEmpty(responseMapping)) { respContentType = (String) responseMapping.get("contentType"); ONode ctxNode = PathMapping.toONode(stepContext); - + // HttpStatus + if (validateResponse == null) { + if (responseMapping.get("httpStatus") != null) { + Map fcMap = (Map) responseMapping.get("httpStatus"); + FieldConfig fc = new FieldConfig(fcMap); + if (ValueTypeEnum.FIXED.equals(fc.getType()) && fc.getValue() != null) { + statusCode = Integer.valueOf(fc.getValue().toString()); + } else if ((ValueTypeEnum.REF.equals(fc.getType()) || ValueTypeEnum.FUNC.equals(fc.getType())) + && fc.getValue() != null) { + String dataType = null; + if (fc.getRefDataType() != null) { + dataType = fc.getRefDataType().getCode(); + } + Object statusCodeObj = PathMapping.getValueByPath(ctxNode, dataType, + (String) fc.getValue()); + statusCode = (statusCodeObj == null) ? statusCode + : Integer.valueOf(statusCodeObj.toString()); + } + } + response.put("httpStatus", statusCode); + } + // headers Map headers = PathMapping.transform(ctxNode, stepContext, MapUtil.upperCaseKey((Map) responseMapping.get("fixedHeaders")), @@ -399,6 +423,7 @@ public class Pipeline { t.put(stepContext.CONTEXT_FIELD, stepContext); } + aggResult.setHttpStatus(statusCode); aggResult.setBody(response.get("body")); aggResult.setHeaders(httpHeaders); return aggResult; diff --git a/fizz-core/src/main/java/we/fizz/component/circle/Circle.java b/fizz-core/src/main/java/we/fizz/component/circle/Circle.java index 57ec2c3..c99027c 100644 --- a/fizz-core/src/main/java/we/fizz/component/circle/Circle.java +++ b/fizz-core/src/main/java/we/fizz/component/circle/Circle.java @@ -33,9 +33,9 @@ import we.fizz.component.ComponentResult; import we.fizz.component.ComponentTypeEnum; import we.fizz.component.IComponent; import we.fizz.component.StepContextPosition; -import we.fizz.component.ValueTypeEnum; import we.fizz.component.condition.Condition; import we.fizz.exception.FizzRuntimeException; +import we.fizz.field.ValueTypeEnum; import we.fizz.input.PathMapping; /** diff --git a/fizz-core/src/main/java/we/fizz/component/condition/Condition.java b/fizz-core/src/main/java/we/fizz/component/condition/Condition.java index df93ee0..9a3990d 100644 --- a/fizz-core/src/main/java/we/fizz/component/condition/Condition.java +++ b/fizz-core/src/main/java/we/fizz/component/condition/Condition.java @@ -34,9 +34,9 @@ import lombok.NoArgsConstructor; import we.fizz.component.ComponentTypeEnum; import we.fizz.component.IComponent; import we.fizz.component.OperatorEnum; -import we.fizz.component.RefDataTypeEnum; -import we.fizz.component.ValueTypeEnum; import we.fizz.exception.FizzRuntimeException; +import we.fizz.field.RefDataTypeEnum; +import we.fizz.field.ValueTypeEnum; import we.fizz.input.PathMapping; import we.fizz.input.extension.request.RequestInput; diff --git a/fizz-core/src/main/java/we/fizz/component/condition/ConditionValue.java b/fizz-core/src/main/java/we/fizz/component/condition/ConditionValue.java index ad4cfdb..f89c49b 100644 --- a/fizz-core/src/main/java/we/fizz/component/condition/ConditionValue.java +++ b/fizz-core/src/main/java/we/fizz/component/condition/ConditionValue.java @@ -20,9 +20,9 @@ package we.fizz.component.condition; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import we.fizz.component.FixedDataTypeEnum; -import we.fizz.component.RefDataTypeEnum; -import we.fizz.component.ValueTypeEnum; +import we.fizz.field.FixedDataTypeEnum; +import we.fizz.field.RefDataTypeEnum; +import we.fizz.field.ValueTypeEnum; /** * Condition value diff --git a/fizz-core/src/main/java/we/fizz/field/FieldConfig.java b/fizz-core/src/main/java/we/fizz/field/FieldConfig.java new file mode 100644 index 0000000..047a565 --- /dev/null +++ b/fizz-core/src/main/java/we/fizz/field/FieldConfig.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2021 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package we.fizz.field; + +import java.util.Map; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * + * @author Francis Dong + * + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +public class FieldConfig { + + private ValueTypeEnum type; + + private FixedDataTypeEnum fixedDataType; + + private RefDataTypeEnum refDataType; + + private Object value; + + public FieldConfig(Map configMap) { + if (configMap != null && !configMap.isEmpty()) { + if (configMap.containsKey("type")) { + this.type = ValueTypeEnum.getEnumByCode(configMap.get("type").toString()); + } + if (configMap.containsKey("fixedDataType")) { + this.fixedDataType = FixedDataTypeEnum.getEnumByCode(configMap.get("fixedDataType").toString()); + } + if (configMap.containsKey("refDataType")) { + this.refDataType = RefDataTypeEnum.getEnumByCode(configMap.get("refDataType").toString()); + } + if (configMap.containsKey("value")) { + this.value = configMap.get("value"); + } + } + } +} diff --git a/fizz-core/src/main/java/we/fizz/component/FixedDataTypeEnum.java b/fizz-core/src/main/java/we/fizz/field/FixedDataTypeEnum.java similarity index 82% rename from fizz-core/src/main/java/we/fizz/component/FixedDataTypeEnum.java rename to fizz-core/src/main/java/we/fizz/field/FixedDataTypeEnum.java index cf881c7..9aa6950 100644 --- a/fizz-core/src/main/java/we/fizz/component/FixedDataTypeEnum.java +++ b/fizz-core/src/main/java/we/fizz/field/FixedDataTypeEnum.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package we.fizz.component; +package we.fizz.field; /** * Data type of fixed value @@ -41,4 +41,13 @@ public enum FixedDataTypeEnum{ this.code = code; } + public static FixedDataTypeEnum getEnumByCode(String code) { + for (FixedDataTypeEnum e : FixedDataTypeEnum.values()) { + if (e.getCode().equals(code)) { + return e; + } + } + return null; + } + } diff --git a/fizz-core/src/main/java/we/fizz/component/RefDataTypeEnum.java b/fizz-core/src/main/java/we/fizz/field/RefDataTypeEnum.java similarity index 83% rename from fizz-core/src/main/java/we/fizz/component/RefDataTypeEnum.java rename to fizz-core/src/main/java/we/fizz/field/RefDataTypeEnum.java index d212a82..3ef1c53 100644 --- a/fizz-core/src/main/java/we/fizz/component/RefDataTypeEnum.java +++ b/fizz-core/src/main/java/we/fizz/field/RefDataTypeEnum.java @@ -15,7 +15,7 @@ * along with this program. If not, see . */ -package we.fizz.component; +package we.fizz.field; /** * Data type of reference value @@ -40,4 +40,14 @@ public enum RefDataTypeEnum { public void setCode(String code) { this.code = code; } + + public static RefDataTypeEnum getEnumByCode(String code) { + for (RefDataTypeEnum e : RefDataTypeEnum.values()) { + if (e.getCode().equals(code)) { + return e; + } + } + return null; + } + } diff --git a/fizz-core/src/main/java/we/fizz/component/ValueTypeEnum.java b/fizz-core/src/main/java/we/fizz/field/ValueTypeEnum.java similarity index 78% rename from fizz-core/src/main/java/we/fizz/component/ValueTypeEnum.java rename to fizz-core/src/main/java/we/fizz/field/ValueTypeEnum.java index 9bb64ac..9500bb4 100644 --- a/fizz-core/src/main/java/we/fizz/component/ValueTypeEnum.java +++ b/fizz-core/src/main/java/we/fizz/field/ValueTypeEnum.java @@ -15,8 +15,7 @@ * along with this program. If not, see . */ -package we.fizz.component; - +package we.fizz.field; /** * Value Type @@ -26,7 +25,7 @@ package we.fizz.component; */ public enum ValueTypeEnum { - FIXED("fixed"), REF("ref"); + FIXED("fixed"), REF("ref"), FUNC("func"); private String code; @@ -38,4 +37,13 @@ public enum ValueTypeEnum { return code; } + public static ValueTypeEnum getEnumByCode(String code) { + for (ValueTypeEnum e : ValueTypeEnum.values()) { + if (e.getCode().equals(code)) { + return e; + } + } + return null; + } + } diff --git a/fizz-core/src/main/java/we/fizz/input/PathMapping.java b/fizz-core/src/main/java/we/fizz/input/PathMapping.java index b94af30..0046aea 100644 --- a/fizz-core/src/main/java/we/fizz/input/PathMapping.java +++ b/fizz-core/src/main/java/we/fizz/input/PathMapping.java @@ -333,6 +333,19 @@ public class PathMapping { * @return */ public static Object getValueByPath(ONode ctxNode, String path) { + return getValueByPath(ctxNode, null, path); + } + + /** + * Returns value of path, return default value if no value matched by path + * + * @param ctxNode + * @param type + * @param path e.g: step1.request1.headers.abc or + * step1.request1.headers.abc|123 (default value separate by "|") + * @return + */ + public static Object getValueByPath(ONode ctxNode, String type, String path) { // if (StringUtils.isBlank(path)) { // return null; // } @@ -352,7 +365,7 @@ public class PathMapping { // return val.toData(); // } // return defaultValue; - Object val = getRefValue(ctxNode, null, path); + Object val = getRefValue(ctxNode, type, path); if (val != null && val instanceof ONode) { ONode oval = (ONode)val; if (!oval.isNull()) { diff --git a/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInput.java b/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInput.java index 22c91c7..fb1c724 100644 --- a/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInput.java +++ b/fizz-core/src/main/java/we/fizz/input/extension/request/RequestInput.java @@ -436,6 +436,11 @@ public class RequestInput extends RPCInput implements IInput{ } }); headers.put("ELAPSEDTIME", elapsedMillis + "ms"); + + RequestRPCResponse reqCr = (RequestRPCResponse) cr; + if (reqCr.getStatusCode() != null) { + this.response.put("httpStatus", reqCr.getStatusCode().value()); + } this.response.put("headers", headers); this.respContentType = httpHeaders.getFirst(CONTENT_TYPE); inputContext.getStepContext().addElapsedTime(prefix + request.get("url"), diff --git a/fizz-core/src/test/java/we/fizz/component/CircleTests.java b/fizz-core/src/test/java/we/fizz/component/CircleTests.java index ff09c68..59f9174 100644 --- a/fizz-core/src/test/java/we/fizz/component/CircleTests.java +++ b/fizz-core/src/test/java/we/fizz/component/CircleTests.java @@ -30,6 +30,9 @@ import we.fizz.component.circle.Circle; import we.fizz.component.circle.CircleItem; import we.fizz.component.condition.Condition; import we.fizz.component.condition.ConditionValue; +import we.fizz.field.FixedDataTypeEnum; +import we.fizz.field.RefDataTypeEnum; +import we.fizz.field.ValueTypeEnum; /** * * @author Francis Dong diff --git a/fizz-core/src/test/java/we/fizz/component/ConditionTests.java b/fizz-core/src/test/java/we/fizz/component/ConditionTests.java index 0aa7742..7216dcd 100644 --- a/fizz-core/src/test/java/we/fizz/component/ConditionTests.java +++ b/fizz-core/src/test/java/we/fizz/component/ConditionTests.java @@ -34,6 +34,9 @@ import org.noear.snack.ONode; import we.fizz.component.condition.Condition; import we.fizz.component.condition.ConditionValue; +import we.fizz.field.FixedDataTypeEnum; +import we.fizz.field.RefDataTypeEnum; +import we.fizz.field.ValueTypeEnum; import we.fizz.input.PathMapping; /**