/*
 * Copyright 1999-2019 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.csp.sentinel.adapter.gateway.common.command;

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

import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.command.CommandHandler;
import com.alibaba.csp.sentinel.command.CommandRequest;
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.JSONArray;

/**
 * @author Eric Zhao
 * @since 1.6.0
 */
@CommandMapping(name = "gateway/updateApiDefinitions", desc = "")
public class UpdateGatewayApiDefinitionGroupCommandHandler implements CommandHandler<String> {

    @Override
    public CommandResponse<String> handle(CommandRequest request) {
        String data = request.getParam("data");
        if (StringUtil.isBlank(data)) {
            return CommandResponse.ofFailure(new IllegalArgumentException("Bad data"));
        }
        try {
            data = URLDecoder.decode(data, "utf-8");
        } catch (Exception e) {
            RecordLog.info("Decode gateway API definition data error", e);
            return CommandResponse.ofFailure(e, "decode gateway API definition data error");
        }

        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));
        return CommandResponse.ofSuccess(result);
    }

    private static final String SUCCESS_MSG = "success";
}

@@ -747,19 +747,18 @@
 va.util.
-Lis
+Se
 t;%0A%0Aimpo
@@ -827,16 +827,174 @@
 nition;%0A
+import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem;%0Aimport com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem;%0A
 import c
@@ -1405,16 +1405,50 @@
 ngUtil;%0A
+import com.alibaba.fastjson.JSON;%0A
 import c
@@ -1477,16 +1477,56 @@
 ONArray;
+%0Aimport com.alibaba.fastjson.JSONObject;
 %0A%0A/**%0A *
@@ -2414,24 +2414,25 @@
 SS_MSG;%0A
+%0A
         
 List%3CApi
@@ -2427,11 +2427,10 @@
     
-Lis
+Se
 t%3CAp
@@ -2463,54 +2463,22 @@
 s = 
-JSONArray.parseArray(data, ApiDefinition.class
+parseJson(data
 );%0A 
@@ -2535,22 +2535,8 @@
 ons(
-new HashSet%3C%3E(
 apiD
@@ -2546,18 +2546,18 @@
 nitions)
-)
 ;
+%0A
 %0A       
@@ -2662,11 +2662,1014 @@
 uccess%22;
+%0A%0A    /**%0A     * Parse json data to set of %7B@link ApiDefinition%7D.%0A     *%0A     * Since the predicateItems of %7B@link ApiDefinition%7D is set of interface,%0A     * here we parse predicateItems to %7B@link ApiPathPredicateItem%7D temporarily.%0A     */%0A    private Set%3CApiDefinition%3E parseJson(String data) %7B%0A        Set%3CApiDefinition%3E apiDefinitions = new HashSet%3C%3E();%0A        JSONArray array = JSON.parseArray(data);%0A        for (Object obj : array) %7B%0A            JSONObject o = (JSONObject)obj;%0A            ApiDefinition apiDefinition = new ApiDefinition((o.getString(%22apiName%22)));%0A            Set%3CApiPredicateItem%3E predicateItems = new HashSet%3C%3E();%0A            JSONArray itemArray = o.getJSONArray(%22predicateItems%22);%0A            if (itemArray != null) %7B%0A                predicateItems.addAll(itemArray.toJavaList(ApiPathPredicateItem.class));%0A            %7D%0A            apiDefinition.setPredicateItems(predicateItems);%0A            apiDefinitions.add(apiDefinition);%0A        %7D%0A%0A        return apiDefinitions;%0A    %7D
 %0A%7D%0A

Add parse method for parse ApiPredicateItem from json string in UpdateGatewayApiDefinitionGroupCommandHandler