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;
29 import java.io.StringReader;
31 import java.util.Map.Entry;
32 import java.util.TreeMap;
33 import javax.ws.rs.core.MediaType;
34 import javax.xml.bind.JAXBContext;
35 import javax.xml.bind.JAXBElement;
36 import javax.xml.bind.JAXBException;
37 import javax.xml.bind.Unmarshaller;
38 import javax.xml.transform.stream.StreamSource;
39 import org.eclipse.persistence.jaxb.MarshallerProperties;
40 import org.onap.policy.apex.client.editor.rest.handling.bean.BeanBase;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
44 * Utilities for handling RESTful communication for Apex.
46 * @author Liam Fallon (liam.fallon@ericsson.com)
48 public abstract class RestUtils {
49 // Regular expressions for checking input types
50 private static final String XML_INPUT_TYPE_REGEXP = "^\\s*<\\?xml.*>\\s*"; // starts with <?xml...>
52 * starts with some kind of bracket [ or ( or {, then has something, then has bracket.
54 private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s+\\S]*[\\)\\}\\]]";
57 * Constructor, block inheritance.
60 // Private constructor to block subclassing
64 * HTTP POST requests can't send nulls so we interpret blanks as nulls.
66 * @param parameter the parameter to convert from blank to null
67 * @return null if the parameter us blank, otherwise the original parameter
69 private static String blank2null(final String parameter) {
70 return (parameter.length() == 0 ? null : parameter);
74 * HTTP POST requests can't send nulls so we interpret blanks as nulls.
77 * @return null if the parameter us blank, otherwise the original parameter
79 private static JsonElement blank2null(final JsonElement val) {
81 return JsonNull.INSTANCE;
83 if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) {
84 final String v = ((JsonPrimitive) val).getAsString();
85 if (v == null || "".equals(v)) {
86 return JsonNull.INSTANCE;
89 if (val.isJsonArray()) {
90 final JsonArray arr = val.getAsJsonArray();
91 for (int i = 0; i < arr.size(); i++) {
92 arr.set(i, blank2null(arr.get(i)));
95 if (val.isJsonObject()) {
96 final JsonObject o = val.getAsJsonObject();
97 for (final Entry<String, JsonElement> e : o.entrySet()) {
98 e.setValue(blank2null(e.getValue()));
105 * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings into a map.
107 * @param jsonString the incoming JSON string
108 * @return a map of the JSON strings
110 public static Map<String, String> getJsonParameters(final String jsonString) {
111 final GsonBuilder gb = new GsonBuilder();
112 gb.serializeNulls().enableComplexMapKeySerialization();
113 final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class);
115 final Map<String, String> jsonMap = new TreeMap<>();
116 for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
117 jsonMap.put(jsonEntry.getKey(), (jsonEntry.getValue() == JsonNull.INSTANCE ? null
118 : blank2null(jsonEntry.getValue().getAsString())));
124 * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings into a map.
126 * @param <C> the generic type
127 * @param jsonString the incoming JSON string
129 * @return a map of the JSON strings
131 public static <C extends BeanBase> C getJsonParameters(final String jsonString, final Class<C> clz) {
132 final GsonBuilder gb = new GsonBuilder();
133 gb.serializeNulls().enableComplexMapKeySerialization();
134 final JsonObject jsonObject = gb.create().fromJson(jsonString, JsonObject.class);
136 for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
137 final JsonElement val = jsonEntry.getValue();
138 jsonEntry.setValue(blank2null(val));
140 return gb.create().fromJson(jsonObject, clz);
144 * Gets the concept from JSON.
146 * @param <C> the generic type
147 * @param jsonString the json string
149 * @return the concept from JSON
150 * @throws JAXBException the JAXB exception
152 public static <C extends AxConcept> C getConceptFromJson(final String jsonString, final Class<C> clz)
153 throws JAXBException {
154 Unmarshaller unmarshaller = null;
155 final JAXBContext jaxbContext = JAXBContext.newInstance(clz);
156 unmarshaller = jaxbContext.createUnmarshaller();
157 if (jsonString.matches(JSON_INPUT_TYPE_REGEXP)) {
158 unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);
159 unmarshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
160 } else if (jsonString.matches(XML_INPUT_TYPE_REGEXP)) {
161 unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, MediaType.APPLICATION_XML);
165 final StreamSource source = new StreamSource(new StringReader(jsonString));
166 final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, clz);
167 return rootElement.getValue();
171 * Gets the JSO nfrom concept.
173 * @param object the object
174 * @return the JSO nfrom concept
176 public static String getJsonfromConcept(final Object object) {
177 final GsonBuilder gb = new GsonBuilder();
178 gb.serializeNulls().enableComplexMapKeySerialization();
179 return gb.create().toJson(object);