2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.gui.editors.apex.rest.handling;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonNull;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonPrimitive;
30 import java.io.StringReader;
32 import java.util.Map.Entry;
33 import java.util.TreeMap;
34 import javax.ws.rs.core.MediaType;
35 import javax.xml.bind.JAXBContext;
36 import javax.xml.bind.JAXBElement;
37 import javax.xml.bind.JAXBException;
38 import javax.xml.bind.Unmarshaller;
39 import javax.xml.transform.stream.StreamSource;
40 import org.eclipse.persistence.jaxb.MarshallerProperties;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
42 import org.onap.policy.gui.editors.apex.rest.handling.bean.BeanBase;
45 * Utilities for handling RESTful communication for Apex.
47 * @author Liam Fallon (liam.fallon@ericsson.com)
49 public abstract class RestUtils {
50 // Regular expressions for checking input types
51 private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*"; // starts with <?xml...>
53 * starts with some kind of bracket [ or ( or {, then has something, then has
56 private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s+\\S]*[\\)\\}\\]]";
59 * Constructor, block inheritance.
62 // Private constructor to block subclassing
66 * HTTP POST requests can't send nulls so we interpret blanks as nulls.
68 * @param parameter the parameter to convert from blank to null
69 * @return null if the parameter us blank, otherwise the original parameter
71 private static String blank2null(final String parameter) {
72 return (parameter.length() == 0 ? null : parameter);
76 * HTTP POST requests can't send nulls so we interpret blanks as nulls.
79 * @return null if the parameter us blank, otherwise the original parameter
81 private static JsonElement blank2null(final JsonElement val) {
83 return JsonNull.INSTANCE;
85 if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) {
86 final String v = ((JsonPrimitive) val).getAsString();
87 if (v == null || "".equals(v)) {
88 return JsonNull.INSTANCE;
91 if (val.isJsonArray()) {
92 final JsonArray arr = val.getAsJsonArray();
93 for (int i = 0; i < arr.size(); i++) {
94 arr.set(i, blank2null(arr.get(i)));
97 if (val.isJsonObject()) {
98 final JsonObject o = val.getAsJsonObject();
99 for (final Entry<String, JsonElement> e : o.entrySet()) {
100 e.setValue(blank2null(e.getValue()));
107 * Apex HTTP PUT requests send simple single level JSON strings, this method
108 * reads those strings into a map.
110 * @param jsonString the incoming JSON string
111 * @return a map of the JSON strings
113 public static Map<String, String> getJsonParameters(final String jsonString) {
114 final GsonBuilder gb = new GsonBuilder();
115 gb.serializeNulls().enableComplexMapKeySerialization();
116 final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class);
118 final Map<String, String> jsonMap = new TreeMap<>();
119 for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
120 jsonMap.put(jsonEntry.getKey(),
121 (jsonEntry.getValue() == JsonNull.INSTANCE ? null : blank2null(jsonEntry.getValue().getAsString())));
127 * Apex HTTP PUT requests send simple single level JSON strings, this method
128 * reads those strings into a map.
130 * @param <C> the generic type
131 * @param jsonString the incoming JSON string
133 * @return a map of the JSON strings
135 public static <C extends BeanBase> C getJsonParameters(final String jsonString, final Class<C> clz) {
136 final GsonBuilder gb = new GsonBuilder();
137 gb.serializeNulls().enableComplexMapKeySerialization();
138 final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class);
140 for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
141 final JsonElement val = jsonEntry.getValue();
142 jsonEntry.setValue(blank2null(val));
144 return gb.create().fromJson(jsonObject, clz);
148 * Gets the concept from JSON.
150 * @param <C> the generic type
151 * @param jsonString the json string
153 * @return the concept from JSON
154 * @throws JAXBException the JAXB exception
156 public static <C extends AxConcept> C getConceptFromJson(final String jsonString, final Class<C> clz)
157 throws JAXBException {
158 Unmarshaller unmarshaller = null;
159 final JAXBContext jaxbContext = JAXBContext.newInstance(clz);
160 unmarshaller = jaxbContext.createUnmarshaller();
161 if (jsonString.matches(JSON_INPUT_TYPE_REGEXP)) {
162 unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
163 unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
164 } else if (jsonString.matches(XML_INPUT_TYPE_REGEXP)) {
165 unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
169 final StreamSource source = new StreamSource(new StringReader(jsonString));
170 final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, clz);
171 return rootElement.getValue();
175 * Gets the JSO nfrom concept.
177 * @param object the object
178 * @return the JSO nfrom concept
180 public static String getJsonfromConcept(final Object object) {
181 final GsonBuilder gb = new GsonBuilder();
182 gb.serializeNulls().enableComplexMapKeySerialization();
183 return gb.create().toJson(object);