aboutsummaryrefslogtreecommitdiffstats
path: root/gui-editors/gui-editor-apex/src/main/java/org/onap/policy/gui/editors/apex/rest/handling/RestUtils.java
blob: cd9c3d25e0a8762f193258dc6e4b0ba18ca79583 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020,2023 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 *  Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.gui.editors.apex.rest.handling;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import java.io.StringReader;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanBase;

/**
 * Utilities for handling RESTful communication for Apex.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public abstract class RestUtils {
    // Regular expressions for checking input types
    private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*"; // starts with <?xml...>
    /**
     * starts with some kind of bracket [ or ( or {, then has something, then has
     * bracket.
     */
    private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s\\S]*[\\)\\}\\]]";

    /**
     * Constructor, block inheritance.
     */
    private RestUtils() {
        // Private constructor to block subclassing
    }

    /**
     * HTTP POST requests can't send nulls so we interpret blanks as nulls.
     *
     * @param parameter the parameter to convert from blank to null
     * @return null if the parameter us blank, otherwise the original parameter
     */
    private static String blank2null(final String parameter) {
        return (parameter.length() == 0 ? null : parameter);
    }

    /**
     * HTTP POST requests can't send nulls so we interpret blanks as nulls.
     *
     * @param val the val
     * @return null if the parameter us blank, otherwise the original parameter
     */
    private static JsonElement blank2null(final JsonElement val) {
        if (val == null) {
            return JsonNull.INSTANCE;
        }
        if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) {
            final var v = ((JsonPrimitive) val).getAsString();
            if (v == null || "".equals(v)) {
                return JsonNull.INSTANCE;
            }
        }
        if (val.isJsonArray()) {
            final var arr = val.getAsJsonArray();
            for (var i = 0; i < arr.size(); i++) {
                arr.set(i, blank2null(arr.get(i)));
            }
        }
        if (val.isJsonObject()) {
            final var o = val.getAsJsonObject();
            for (final Entry<String, JsonElement> e : o.entrySet()) {
                e.setValue(blank2null(e.getValue()));
            }
        }
        return val;
    }

    /**
     * Apex HTTP PUT requests send simple single level JSON strings, this method
     * reads those strings into a map.
     *
     * @param jsonString the incoming JSON string
     * @return a map of the JSON strings
     */
    public static Map<String, String> getJsonParameters(final String jsonString) {
        final var gb = new GsonBuilder();
        gb.serializeNulls().enableComplexMapKeySerialization();
        final var jsonObject = gb.create().fromJson(jsonString, JsonObject.class);

        final Map<String, String> jsonMap = new TreeMap<>();
        for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
            jsonMap.put(jsonEntry.getKey(),
                (jsonEntry.getValue() == JsonNull.INSTANCE ? null : blank2null(jsonEntry.getValue().getAsString())));
        }
        return jsonMap;
    }

    /**
     * Apex HTTP PUT requests send simple single level JSON strings, this method
     * reads those strings into a map.
     *
     * @param <C>        the generic type
     * @param jsonString the incoming JSON string
     * @param clz        the clz
     * @return a map of the JSON strings
     */
    public static <C extends BeanBase> C getJsonParameters(final String jsonString, final Class<C> clz) {
        final var gb = new GsonBuilder();
        gb.serializeNulls().enableComplexMapKeySerialization();
        final var jsonObject = gb.create().fromJson(jsonString, JsonObject.class);

        for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
            final JsonElement val = jsonEntry.getValue();
            jsonEntry.setValue(blank2null(val));
        }
        return gb.create().fromJson(jsonObject, clz);
    }

    /**
     * Gets the concept from JSON.
     *
     * @param <C>        the generic type
     * @param jsonString the json string
     * @param clz        the clz
     * @return the concept from JSON
     * @throws JAXBException the JAXB exception
     */
    public static <C extends AxConcept> C getConceptFromJson(final String jsonString, final Class<C> clz)
        throws JAXBException {
        Unmarshaller unmarshaller = null;
        final var jaxbContext = JAXBContext.newInstance(clz);
        unmarshaller = jaxbContext.createUnmarshaller();
        if (jsonString.matches(JSON_INPUT_TYPE_REGEXP)) {
            unmarshaller.setProperty("media-type", MediaType.APPLICATION_JSON);
        } else if (jsonString.matches(XML_INPUT_TYPE_REGEXP)) {
            unmarshaller.setProperty("media-type", MediaType.APPLICATION_XML);
        } else {
            return null;
        }
        final var source = new StreamSource(new StringReader(jsonString));
        final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, clz);
        return rootElement.getValue();
    }

    /**
     * Gets the JSON from concept.
     *
     * @param object the object
     * @return the JSON from concept
     */
    public static String getJsonFromConcept(final Object object) {
        final var gb = new GsonBuilder();
        gb.serializeNulls().enableComplexMapKeySerialization();
        return gb.create().toJson(object);
    }
}