2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.client.editor.rest.handling;
23 import com.google.gson.GsonBuilder;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonNull;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonPrimitive;
30 import java.io.StringReader;
32 import java.util.Map.Entry;
33 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;
42 import org.eclipse.persistence.jaxb.MarshallerProperties;
43 import org.onap.policy.apex.client.editor.rest.handling.bean.BeanBase;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
47 * Utilities for handling RESTful communication for Apex.
49 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public abstract class RestUtils {
52 // Regular expressions for checking input types
53 private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*"; //starts with <?xml...>
55 * starts with some kind of bracket [ or ( or {, then has something, then has bracket.
57 private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s+\\S]*[\\)\\}\\]]";
60 * Constructor, block inheritance.
62 private RestUtils() {}
65 * HTTP POST requests can't send nulls so we interpret blanks as nulls.
67 * @param parameter the parameter to convert from blank to null
68 * @return null if the parameter us blank, otherwise the original parameter
70 private static String blank2null(final String parameter) {
71 return (parameter.length() == 0 ? null : parameter);
75 * HTTP POST requests can't send nulls so we interpret blanks as nulls.
78 * @return null if the parameter us blank, otherwise the original parameter
80 private static JsonElement blank2null(final JsonElement val) {
82 return JsonNull.INSTANCE;
84 if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) {
85 final String v = ((JsonPrimitive) val).getAsString();
86 if (v == null || "".equals(v)) {
87 return JsonNull.INSTANCE;
90 if (val.isJsonArray()) {
91 final JsonArray arr = val.getAsJsonArray();
92 for (int i = 0; i < arr.size(); i++) {
93 arr.set(i, blank2null(arr.get(i)));
96 if (val.isJsonObject()) {
97 final JsonObject o = val.getAsJsonObject();
98 for (final Entry<String, JsonElement> e : o.entrySet()) {
99 e.setValue(blank2null(e.getValue()));
106 * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings
109 * @param jsonString the incoming JSON string
110 * @return a map of the JSON strings
112 public static Map<String, String> getJsonParameters(final String jsonString) {
113 final GsonBuilder gb = new GsonBuilder();
114 gb.serializeNulls().enableComplexMapKeySerialization();
115 final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class);
117 final Map<String, String> jsonMap = new TreeMap<>();
118 for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
119 jsonMap.put(jsonEntry.getKey(), (jsonEntry.getValue() == JsonNull.INSTANCE ? null
120 : blank2null(jsonEntry.getValue().getAsString())));
126 * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings
129 * @param <C> the generic type
130 * @param jsonString the incoming JSON string
132 * @return a map of the JSON strings
134 public static <C extends BeanBase> C getJsonParameters(final String jsonString, final Class<C> clz) {
135 final GsonBuilder gb = new GsonBuilder();
136 gb.serializeNulls().enableComplexMapKeySerialization();
137 final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class);
139 for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
140 final JsonElement val = jsonEntry.getValue();
141 jsonEntry.setValue(blank2null(val));
143 return gb.create().fromJson(jsonObject, clz);
147 * Gets the concept from JSON.
149 * @param <C> the generic type
150 * @param jsonString the json string
152 * @return the concept from JSON
153 * @throws JAXBException the JAXB exception
155 public static <C extends AxConcept> C getConceptFromJson(final String jsonString, final Class<C> clz)
156 throws JAXBException {
157 Unmarshaller unmarshaller = null;
158 final JAXBContext jaxbContext = JAXBContext.newInstance(clz);
159 unmarshaller = jaxbContext.createUnmarshaller();
160 if (jsonString.matches(JSON_INPUT_TYPE_REGEXP)) {
161 unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
162 unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
163 } else if (jsonString.matches(XML_INPUT_TYPE_REGEXP)) {
164 unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
168 final StreamSource source = new StreamSource(new StringReader(jsonString));
169 final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, clz);
170 return rootElement.getValue();
174 * Gets the JSO nfrom concept.
176 * @param object the object
177 * @return the JSO nfrom concept
179 public static String getJsonfromConcept(final Object object) {
180 final GsonBuilder gb = new GsonBuilder();
181 gb.serializeNulls().enableComplexMapKeySerialization();
182 return gb.create().toJson(object);