aboutsummaryrefslogtreecommitdiffstats
path: root/profiles/http/src/main/java/org/onap/cli/fw/http/utils/OnapCommandHttpUtils.java
blob: dcc3082d65e1e897d54062c9ff6c10c321675ee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright 2017 Huawei Technologies Co., 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
 *
 *     http://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 org.onap.cli.fw.http.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.onap.cli.fw.error.OnapCommandException;
import org.onap.cli.fw.error.OnapCommandInvalidParameterValue;
import org.onap.cli.fw.error.OnapCommandParameterNotFound;
import org.onap.cli.fw.error.OnapCommandResultEmpty;
import org.onap.cli.fw.error.OnapCommandResultMapProcessingFailed;
import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
import org.onap.cli.fw.http.connect.HttpInput;
import org.onap.cli.fw.http.connect.HttpResult;
import org.onap.cli.fw.http.error.OnapCommandHttpHeaderNotFound;
import org.onap.cli.fw.http.error.OnapCommandHttpInvalidRequestBody;
import org.onap.cli.fw.http.error.OnapCommandHttpInvalidResponseBody;
import org.onap.cli.fw.input.OnapCommandParameter;
import org.onap.cli.fw.input.OnapCommandParameterType;
import org.onap.cli.fw.utils.OnapCommandUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;

import net.minidev.json.JSONArray;

public class OnapCommandHttpUtils {

    static Logger log = LoggerFactory.getLogger(OnapCommandHttpUtils.class);
    private static Gson gson = new GsonBuilder().serializeNulls().create();
    private OnapCommandHttpUtils() {
        throw new IllegalStateException("Utility class");
    }
    /**
     * Set argument to param value.
     *
     * @param params
     *            map
     * @param input
     *            HttpInput
     * @return HttpInput
     * @throws OnapCommandParameterNotFound
     *             exception
     * @throws OnapCommandInvalidParameterValue
     *             exception
     */
    public static HttpInput populateParameters(Map<String, OnapCommandParameter> params, HttpInput input)
            throws OnapCommandException {

        HttpInput inp = new HttpInput();

        for (OnapCommandParameter param : params.values()) {
            if (OnapCommandParameterType.BINARY.equals(param.getParameterType())) {
                inp.setBinaryData(true);
                break;
            }
        }

        inp.setUri(OnapCommandUtils.replaceLineForSpecialValues(input.getUri()));
        inp.setUri(OnapCommandUtils.replaceLineFromInputParameters(input.getUri(), params));

        inp.setMethod(input.getMethod().toLowerCase());

        boolean isRemoveEmptyNodes = Boolean.parseBoolean(input.getContext().getOrDefault(OnapCommandHttpConstants.CONTEXT_REMOVE_EMPTY_JSON_NODES, "false"));

        //Process for md5
        Map <String, String> values = new HashMap<>();
        for (Map.Entry<String, OnapCommandParameter> param: params.entrySet()) {
            values.put(param.getKey(), param.getValue().getValue().toString());
        }

        if (!input.getMultiparts().isEmpty()) {
            for (HttpInput.Part part: input.getMultiparts()) {
                part.setContent(OnapCommandUtils.replaceLineForSpecialValues(part.getContent(), values));
                part.setContent(OnapCommandUtils.replaceLineFromInputParameters(part.getContent(), params));
                if (isRemoveEmptyNodes) {
                    part.setContent(OnapCommandHttpUtils.normalizeJson(part.getContent()));
                }
            }

            inp.setMultiparts(input.getMultiparts());
        } else {
            inp.setMultipartEntityName(input.getMultipartEntityName());
            inp.setBody(OnapCommandUtils.replaceLineForSpecialValues(input.getBody(), values));
            inp.setBody(OnapCommandUtils.replaceLineFromInputParameters(inp.getBody(), params));
            if (isRemoveEmptyNodes) {
                inp.setBody(OnapCommandHttpUtils.normalizeJson(inp.getBody()));
            }
        }

        //consider __body__ spl entry
        values.put(OnapCommandHttpConstants.__BODY__, inp.getBody());

        for (String h : input.getReqHeaders().keySet()) {
            String value = input.getReqHeaders().get(h);
            value = OnapCommandUtils.replaceLineForSpecialValues(value, values);
            inp.getReqHeaders().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params));
        }

        for (String h : input.getReqQueries().keySet()) {
            String value = input.getReqQueries().get(h);
            value = OnapCommandUtils.replaceLineForSpecialValues(value, values);
            inp.getReqQueries().put(h, OnapCommandUtils.replaceLineFromInputParameters(value, params));
        }

        return inp;
    }

    /**
     * Populate result.
     *
     * @param resultMap
     *            map
     * @param resultHttp
     *            HttpResult
     * @return map
     * @throws OnapCommandHttpHeaderNotFound
     *             header not found exception
     * @throws OnapCommandHttpInvalidResponseBody
     *             invalid response body exception
     * @throws OnapCommandResultMapProcessingFailed
     *             map processing failed exception
     */
    public static Map<String, List<String>> populateOutputs(Map<String, String> resultMap, HttpResult resultHttp)
            throws OnapCommandException {
        Map<String, List<String>> resultsProcessed = new HashMap<>();

        for (Entry<String, String> entry : resultMap.entrySet()) {
            String key = entry.getKey();
            try {
                resultsProcessed.put(key, OnapCommandHttpUtils.replaceLineFromOutputResults(resultMap.get(key), resultHttp));
            } catch(OnapCommandResultEmpty e) {  // NOSONAR
                // pass
            }
        }

        return resultsProcessed;
    }

    public static List<String> replaceLineFromOutputResults(String line, HttpResult resultHttp)
            throws OnapCommandHttpHeaderNotFound, OnapCommandHttpInvalidResponseBody,
            OnapCommandResultMapProcessingFailed, OnapCommandResultEmpty {
        StringBuilder headerProcessedLine = new StringBuilder();

        ArrayList<String> result = new ArrayList<>();
        if (!line.contains("$b{") && !line.contains("$h{")) {
            result.add(line);
            return result;
        }

        /**
         * In case of empty response body [] or {}
         **/
        if (resultHttp.getBody() != null && resultHttp.getBody().length() <= 2) {
            return result;
        }

        /**
         * Process headers macros : line: $h{abc}-$b{$.[*].xyz} , After processing line will be [abc's
         * value]-$b{$.[*].xyz}
         **/
        int currentIdx = 0;
        while (currentIdx < line.length()) {
            int idxS = line.indexOf("$h{", currentIdx);
            if (idxS == -1) {
                headerProcessedLine.append(line.substring(currentIdx));
                break;
            }
            int idxE = line.indexOf('}', idxS);
            String headerName = line.substring(idxS + 3, idxE);
            headerName = headerName.trim();
            if (!resultHttp.getRespHeaders().containsKey(headerName)) {
                throw new OnapCommandHttpHeaderNotFound(headerName);
            }
            String value = resultHttp.getRespHeaders().get(headerName);

            headerProcessedLine.append(line.substring(currentIdx, idxS) + value);
            currentIdx = idxE + 1;
        }

        // Process body jsonpath macros
        List<Object> values = new ArrayList<>();
        StringBuilder bodyProcessedPattern = new StringBuilder();
        currentIdx = 0;
        int maxRows = 1; // in normal case, only one row will be there
        while (currentIdx < headerProcessedLine.length()) {
            int idxS = headerProcessedLine.indexOf("$b{", currentIdx);
            if (idxS == -1) {
                bodyProcessedPattern.append(headerProcessedLine.substring(currentIdx));
                break;
            }
            int idxE = headerProcessedLine.indexOf("}", idxS);
            String jsonPath = headerProcessedLine.substring(idxS + 3, idxE);
            jsonPath = jsonPath.trim();
            Object value = new Object();
            try {
                // JSONArray or String
                value = JsonPath.read(resultHttp.getBody(), jsonPath);
            } catch (PathNotFoundException e1) { // NOSONAR
                //set to blank for those entries which are missing from the output json
                value = "";
            } catch (Exception e) {
                throw new OnapCommandHttpInvalidResponseBody(jsonPath, e);
            }

            if (value instanceof JSONArray) {
                JSONArray arr = (JSONArray) value;
                if (arr.size() > maxRows) {
                    maxRows = arr.size();
                }
            }
            bodyProcessedPattern.append(headerProcessedLine.substring(currentIdx, idxS) + "%s");
            values.add(value);
            currentIdx = idxE + 1;
        }

        if (bodyProcessedPattern.toString().isEmpty()) {
            result.add(headerProcessedLine.toString());
            return result;
        } else {
            for (int i = 0; i < maxRows; i++) {
                currentIdx = 0;
                StringBuilder bodyProcessedLine = new StringBuilder();
                int positionalIdx = 0; // %s positional idx
                while (currentIdx < bodyProcessedPattern.length()) {
                    int idxS = bodyProcessedPattern.indexOf("%s", currentIdx);
                    if (idxS == -1) {
                        bodyProcessedLine.append(bodyProcessedPattern.substring(currentIdx));
                        break;
                    }
                    int idxE = idxS + 2; // %s
                    try {
                        Object value = values.get(positionalIdx);
                        String valueS = String.valueOf(value);
                        if (value instanceof JSONArray) {
                            JSONArray arr = (JSONArray) value;
                            if (!arr.isEmpty()) {
                                valueS = arr.get(i).toString();
                            } else {
                                throw new OnapCommandResultEmpty();
                            }
                        }

                        bodyProcessedLine.append(bodyProcessedPattern.substring(currentIdx, idxS) + valueS);
                        currentIdx = idxE;
                        positionalIdx++;
                    } catch (OnapCommandResultEmpty e) {
                        throw e;
                    } catch (Exception e) {
                        throw new OnapCommandResultMapProcessingFailed(line, e);
                    }
                }
                result.add(bodyProcessedLine.toString());
            }

            return result;
        }
    }

    public static void normalizeJson(JsonElement node) {
        Iterator<Entry<String, JsonElement>> it = node.getAsJsonObject().entrySet().iterator();

        while (it.hasNext()) {
            JsonElement child = it.next().getValue();
            if (child.isJsonPrimitive() && child.getAsJsonPrimitive().isString() && child.getAsJsonPrimitive().getAsString().equals(""))
                it.remove();
            else  if (child.isJsonNull())
                it.remove();
            else if (child.isJsonObject())
                normalizeJson(child);
            else if (child.isJsonArray()) {
                for (JsonElement ele:child.getAsJsonArray()) {
                    if (ele.isJsonObject())
                        normalizeJson(ele);
                }
            }
        }
    }

    public static String normalizeJson(String json) throws OnapCommandHttpInvalidRequestBody {
        JsonParser jsonParser = new JsonParser();
        JsonElement node;
        try {
            node = jsonParser.parse(json);
            normalizeJson(node);
            return gson.toJson(node);
        } catch (Exception e) {  //NOSONAR
            throw new OnapCommandHttpInvalidRequestBody(e);
        }

    }
}