feat: 修改context

This commit is contained in:
zengxiaobo 2024-05-11 14:12:01 +08:00
parent cdebd9a4b3
commit 8967e18511

View File

@ -221,7 +221,7 @@ public class RequestFilterHook implements ProxyHook {
return response;
}
JSONObject jsonResp = (JSONObject) response;
Object content = jsonResp.get("content");
Object content = jsonResp.get("data");
if (!(content instanceof JSONObject) && !(content instanceof JSONArray)) {
return response;
}
@ -229,16 +229,16 @@ public class RequestFilterHook implements ProxyHook {
if (content instanceof JSONArray) {
List<JSONObject> records = ((JSONArray) content).toJavaList(JSONObject.class);
JSONArray filtered = new JSONArray().fluentAddAll(filterRecords(reqContext, records));
return jsonResp.fluentPut("content", filtered);
return jsonResp.fluentPut("data", filtered);
}
JSONObject page = (JSONObject) content;
JSONArray records = page.getJSONArray("records");
JSONArray records = page.getJSONArray("data");
if (CollectionUtils.isEmpty(records)) {
return response;
}
List<JSONObject> filtered = filterRecords(reqContext, records.toJavaList(JSONObject.class));
page.put("records", new JSONArray().fluentAddAll(filtered));
page.put("data", new JSONArray().fluentAddAll(filtered));
return response;
}
@ -257,7 +257,7 @@ public class RequestFilterHook implements ProxyHook {
return response;
}
JSONObject jsonResp = (JSONObject) response;
Object content = jsonResp.get("content");
Object content = jsonResp.get("data");
if (!(content instanceof JSONObject) && !(content instanceof JSONArray)) {
return response;
}
@ -268,16 +268,16 @@ public class RequestFilterHook implements ProxyHook {
return response;
}
JSONArray filtered = new JSONArray().fluentAddAll(filterRecords(reqContext, records, config));
return jsonResp.fluentPut("content", filtered);
return jsonResp.fluentPut("data", filtered);
}
JSONObject page = (JSONObject) content;
JSONArray records = page.getJSONArray("records");
JSONArray records = page.getJSONArray("data");
if (CollectionUtils.isEmpty(records)) {
return response;
}
List<JSONObject> filtered = filterRecords(reqContext, records.toJavaList(JSONObject.class), config);
page.put("records", new JSONArray().fluentAddAll(filtered));
page.put("data", new JSONArray().fluentAddAll(filtered));
return response;
}
@ -296,12 +296,12 @@ public class RequestFilterHook implements ProxyHook {
return response;
}
JSONObject jsonResp = (JSONObject) response;
Object content = jsonResp.get("content");
Object content = jsonResp.get("data");
if (content == null) {
return response;
}
Preconditions.checkArgument(content instanceof JSON, "ConvertContentFilter不支持原始response.content非json的情况");
return jsonResp.fluentPut("content", filterContent(reqContext, (JSON) content, config));
return jsonResp.fluentPut("data", filterContent(reqContext, (JSON) content, config));
}
public abstract JSON filterContent(RequestContext reqContext, JSON content, JSONObject config);
@ -319,13 +319,13 @@ public class RequestFilterHook implements ProxyHook {
return response;
}
JSONObject jsonResp = (JSONObject) response;
Object content = jsonResp.get("content");
Object content = jsonResp.get("data");
if (!(content instanceof JSONObject)) {
return response;
}
JSONArray records = ((JSONObject) content).getJSONArray("records");
return jsonResp.fluentPut("content", Optional.ofNullable(records).orElseGet(JSONArray::new));
JSONArray records = ((JSONObject) content).getJSONArray("data");
return jsonResp.fluentPut("data", Optional.ofNullable(records).orElseGet(JSONArray::new));
}
}
@ -341,13 +341,13 @@ public class RequestFilterHook implements ProxyHook {
return response;
}
JSONObject jsonResp = (JSONObject) response;
Object content = jsonResp.get("content");
Object content = jsonResp.get("data");
JSONArray arrayContent = null;
if (content instanceof JSONArray) {
arrayContent = (JSONArray) content;
} else if (content instanceof JSONObject) {
JSONArray records = ((JSONObject) content).getJSONArray("records");
JSONArray records = ((JSONObject) content).getJSONArray("data");
arrayContent = records == null ? new JSONArray() : records;
} else {
return response;
@ -356,7 +356,7 @@ public class RequestFilterHook implements ProxyHook {
.mapToObj(arrayContent::getJSONObject)
.findFirst()
.orElseGet(JSONObject::new);
return jsonResp.fluentPut("content", filterObject(reqContext, obj, config));
return jsonResp.fluentPut("data", filterObject(reqContext, obj, config));
}
public JSONObject filterObject(RequestContext reqContext, JSONObject obj) {
@ -380,7 +380,7 @@ public class RequestFilterHook implements ProxyHook {
return response;
}
JSONObject jsonResp = (JSONObject) response;
Object content = jsonResp.get("content");
Object content = jsonResp.get("data");
if (!(content instanceof JSONArray)) {
return response;
}
@ -397,7 +397,7 @@ public class RequestFilterHook implements ProxyHook {
.limit(page.getSize())
.collect(Collectors.toList()));
return jsonResp.fluentPut("content", page);
return jsonResp.fluentPut("data", page);
}
}
@ -484,7 +484,7 @@ public class RequestFilterHook implements ProxyHook {
* 仅支持:
* 1.content是JSONObject
* 2.content是JSONArray且其中是JSONObject
* 3.content是分页返回对象{"records":[{}]}
* 3.content是分页返回对象{"data":[{}]}
*/
public static class CropContentFilter extends ConvertContentFilter implements RequestFilter {
@Override
@ -510,9 +510,9 @@ public class RequestFilterHook implements ProxyHook {
if (content instanceof JSONObject) {
JSONObject contentJSONObject = (JSONObject) content;
// 可能是分页content此时支持裁剪分页列表中的json对象
if (contentJSONObject.containsKey("records")) {
contentJSONObject.put("records",
cropJSONArrayContent(contentJSONObject.getJSONArray("records"), config));
if (contentJSONObject.containsKey("data")) {
contentJSONObject.put("data",
cropJSONArrayContent(contentJSONObject.getJSONArray("data"), config));
}
return doCrop(contentJSONObject, config);
}