2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.workflow.activitiext.restservicetask;
18 import java.util.Map.Entry;
20 import org.activiti.engine.delegate.DelegateExecution;
21 import org.onap.workflow.activitiext.common.ConstString;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonPrimitive;
30 public class GsonUtil {
32 private static final Logger logger = LoggerFactory.getLogger(GsonUtil.class);
34 public static JsonObject formatJsonObject(String key, JsonObject obj, DelegateExecution execution) {
36 JsonObject res = new JsonObject();
38 for (Entry<String, JsonElement> entry : obj.entrySet()) {
40 if (entry.getValue() instanceof JsonObject) {
42 JsonObject tempObj = (JsonObject) entry.getValue();
43 JsonElement valueSource = tempObj.get(ConstString.PARAMETER_VALUESOURCE);
44 if(valueSource == null || valueSource.getAsString().equals(ConstString.PARAMETER_VALUE_SOURCE_DEFINITION))
46 res.add(entry.getKey(), formatJsonObject(entry.getKey(), entry.getValue().getAsJsonObject(), execution));
48 res.addProperty(entry.getKey(), getValue(tempObj, execution));
51 } else if (entry.getValue() instanceof JsonArray) {
53 JsonArray objArray = entry.getValue().getAsJsonArray();
54 JsonArray resArray = formatJsonArray(objArray, execution);
55 res.add(entry.getKey(), resArray);
62 public static JsonArray formatJsonArray(JsonArray objArray, DelegateExecution execution) {
64 JsonArray array = new JsonArray();
66 for (JsonElement element : objArray) {
68 if (element.isJsonObject()) {
70 JsonObject tempObj = (JsonObject) element;
71 JsonElement valueSource = tempObj.get(ConstString.PARAMETER_VALUESOURCE);
72 if(valueSource == null || valueSource.getAsString().equals(ConstString.PARAMETER_VALUE_SOURCE_DEFINITION))
74 array.add(formatJsonObject(null, element.getAsJsonObject(), execution));
76 array.add(new JsonPrimitive(getValue(tempObj, execution)));
79 } else if (element.isJsonArray()) {
81 array = formatJsonArray(element.getAsJsonArray(), execution);
89 * obtain jsonObject value
94 public static String getValue(JsonObject obj, DelegateExecution execution)
96 String result = new String();
98 String valueSource = obj.get(ConstString.PARAMETER_VALUESOURCE).getAsString();
99 String value = obj.get(ConstString.PARAMETER_VALUE).getAsString();
100 if(ConstString.PARAMETER_VALUE_SOURCE_STRING.equals(valueSource))
103 }else if(ConstString.PARAMETER_VALUE_SOURCE_PLAN.equals(valueSource))
105 result = HttpUtil.parsePlanExpression(value, execution);
106 }else if(ConstString.PARAMETER_VALUE_SOURCE_TOPOLOGY.equals(valueSource))
108 result = HttpUtil.parseTopologyExpression(value, execution);
110 } catch (Exception e) {
111 logger.error("jsonObject {} is illegal!", obj.toString(), e);