Support configuring http status in aggregation #377

This commit is contained in:
Francis Dong
2021-11-29 10:30:09 +08:00
committed by dxfeng10
parent 0d8b8a1668
commit 0d0706dcd6
14 changed files with 163 additions and 15 deletions

View File

@@ -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 {

View File

@@ -26,6 +26,8 @@ import org.springframework.util.MultiValueMap;
*/
public class AggregateResult {
private Integer httpStatus;
private MultiValueMap<String, String> headers;
private Object body;
@@ -56,4 +58,12 @@ public class AggregateResult {
this.stepContext = stepContext;
}
public Integer getHttpStatus() {
return httpStatus;
}
public void setHttpStatus(Integer httpStatus) {
this.httpStatus = httpStatus;
}
}

View File

@@ -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<String, Object> responseMapping = (Map<String, Object>) input.getConfig().getDataMapping()
.get("response");
@@ -341,6 +344,27 @@ 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<String, Object> fcMap = (Map<String, Object>) 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<String, Object> headers = PathMapping.transform(ctxNode, stepContext,
@@ -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;

View File

@@ -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;
/**

View File

@@ -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;

View File

@@ -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

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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<String, Object> 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");
}
}
}
}

View File

@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@@ -15,8 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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;
}
}

View File

@@ -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()) {

View File

@@ -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"),

View File

@@ -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

View File

@@ -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;
/**