2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.gui.editors.apex.rest.handling;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonNull;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonPrimitive;
31 import java.io.StringReader;
33 import java.util.Map.Entry;
34 import java.util.TreeMap;
35 import javax.ws.rs.core.MediaType;
36 import javax.xml.bind.JAXBContext;
37 import javax.xml.bind.JAXBElement;
38 import javax.xml.bind.JAXBException;
39 import javax.xml.bind.Unmarshaller;
40 import javax.xml.transform.stream.StreamSource;
41 import org.eclipse.persistence.jaxb.MarshallerProperties;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
43 import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanBase;
46 * Utilities for handling RESTful communication for Apex.
48 * @author Liam Fallon (liam.fallon@ericsson.com)
50 public abstract class RestUtils {
51 // Regular expressions for checking input types
52 private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*"; // starts with <?xml...>
54 * starts with some kind of bracket [ or ( or {, then has something, then has
57 private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s\\S]*[\\)\\}\\]]";
60 * Constructor, block inheritance.
63 // Private constructor to block subclassing
67 * HTTP POST requests can't send nulls so we interpret blanks as nulls.
69 * @param parameter the parameter to convert from blank to null
70 * @return null if the parameter us blank, otherwise the original parameter
72 private static String blank2null(final String parameter) {
73 return (parameter.length() == 0 ? null : parameter);
77 * HTTP POST requests can't send nulls so we interpret blanks as nulls.
80 * @return null if the parameter us blank, otherwise the original parameter
82 private static JsonElement blank2null(final JsonElement val) {
84 return JsonNull.INSTANCE;
86 if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) {
87 final var v = ((JsonPrimitive) val).getAsString();
88 if (v == null || "".equals(v)) {
89 return JsonNull.INSTANCE;
92 if (val.isJsonArray()) {
93 final var arr = val.getAsJsonArray();
94 for (var i = 0; i < arr.size(); i++) {
95 arr.set(i, blank2null(arr.get(i)));
98 if (val.isJsonObject()) {
99 final var o = val.getAsJsonObject();
100 for (final Entry<String, JsonElement> e : o.entrySet()) {
101 e.setValue(blank2null(e.getValue()));
108 * Apex HTTP PUT requests send simple single level JSON strings, this method
109 * reads those strings into a map.
111 * @param jsonString the incoming JSON string
112 * @return a map of the JSON strings
114 public static Map<String, String> getJsonParameters(final String jsonString) {
115 final var gb = new GsonBuilder();
116 gb.serializeNulls().enableComplexMapKeySerialization();
117 final var jsonObject = gb.create().fromJson(jsonString, JsonObject.class);
119 final Map<String, String> jsonMap = new TreeMap<>();
120 for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
121 jsonMap.put(jsonEntry.getKey(),
122 (jsonEntry.getValue() == JsonNull.INSTANCE ? null : blank2null(jsonEntry.getValue().getAsString())));
128 * Apex HTTP PUT requests send simple single level JSON strings, this method
129 * reads those strings into a map.
131 * @param <C> the generic type
132 * @param jsonString the incoming JSON string
134 * @return a map of the JSON strings
136 public static <C extends BeanBase> C getJsonParameters(final String jsonString, final Class<C> clz) {
137 final var gb = new GsonBuilder();
138 gb.serializeNulls().enableComplexMapKeySerialization();
139 final var jsonObject = gb.create().fromJson(jsonString, JsonObject.class);
141 for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
142 final JsonElement val = jsonEntry.getValue();
143 jsonEntry.setValue(blank2null(val));
145 return gb.create().fromJson(jsonObject, clz);
149 * Gets the concept from JSON.
151 * @param <C> the generic type
152 * @param jsonString the json string
154 * @return the concept from JSON
155 * @throws JAXBException the JAXB exception
157 public static <C extends AxConcept> C getConceptFromJson(final String jsonString, final Class<C> clz)
158 throws JAXBException {
159 Unmarshaller unmarshaller = null;
160 final var jaxbContext = JAXBContext.newInstance(clz);
161 unmarshaller = jaxbContext.createUnmarshaller();
162 if (jsonString.matches(JSON_INPUT_TYPE_REGEXP)) {
163 unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
164 unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
165 } else if (jsonString.matches(XML_INPUT_TYPE_REGEXP)) {
166 unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
170 final var source = new StreamSource(new StringReader(jsonString));
171 final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, clz);
172 return rootElement.getValue();
176 * Gets the JSON from concept.
178 * @param object the object
179 * @return the JSON from concept
181 public static String getJsonFromConcept(final Object object) {
182 final var gb = new GsonBuilder();
183 gb.serializeNulls().enableComplexMapKeySerialization();
184 return gb.create().toJson(object);