Browse Source

Add parse method for parse ApiPredicateItem from json string in UpdateGatewayApiDefinitionGroupCommandHandler

master
cdfive Eric Zhao 5 years ago
parent
commit
eda7fdc5c8
1 changed files with 33 additions and 3 deletions
  1. +33
    -3
      sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/command/UpdateGatewayApiDefinitionGroupCommandHandler.java

+ 33
- 3
sentinel-adapter/sentinel-api-gateway-adapter-common/src/main/java/com/alibaba/csp/sentinel/adapter/gateway/common/command/UpdateGatewayApiDefinitionGroupCommandHandler.java View File

@@ -17,9 +17,11 @@ package com.alibaba.csp.sentinel.adapter.gateway.common.command;

import java.net.URLDecoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.command.CommandRequest;
@@ -27,7 +29,9 @@ import com.alibaba.csp.sentinel.command.CommandResponse;
import com.alibaba.csp.sentinel.command.annotation.CommandMapping;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
* @author Eric Zhao
@@ -52,10 +56,36 @@ public class UpdateGatewayApiDefinitionGroupCommandHandler implements CommandHan
RecordLog.info("[API Server] Receiving data change (type: gateway API definition): {0}", data);

String result = SUCCESS_MSG;
List<ApiDefinition> apiDefinitions = JSONArray.parseArray(data, ApiDefinition.class);
GatewayApiDefinitionManager.loadApiDefinitions(new HashSet<>(apiDefinitions));

Set<ApiDefinition> apiDefinitions = parseJson(data);
GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions);

return CommandResponse.ofSuccess(result);
}

private static final String SUCCESS_MSG = "success";

/**
* Parse json data to set of {@link ApiDefinition}.
*
* Since the predicateItems of {@link ApiDefinition} is set of interface,
* here we parse predicateItems to {@link ApiPathPredicateItem} temporarily.
*/
private Set<ApiDefinition> parseJson(String data) {
Set<ApiDefinition> apiDefinitions = new HashSet<>();
JSONArray array = JSON.parseArray(data);
for (Object obj : array) {
JSONObject o = (JSONObject)obj;
ApiDefinition apiDefinition = new ApiDefinition((o.getString("apiName")));
Set<ApiPredicateItem> predicateItems = new HashSet<>();
JSONArray itemArray = o.getJSONArray("predicateItems");
if (itemArray != null) {
predicateItems.addAll(itemArray.toJavaList(ApiPathPredicateItem.class));
}
apiDefinition.setPredicateItems(predicateItems);
apiDefinitions.add(apiDefinition);
}

return apiDefinitions;
}
}

Loading…
Cancel
Save