819657f2f70dfa158fa637d526592117a83d56bc
[policy/gui.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.gui.editors.apex.rest.handling;
23
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;
31 import java.util.Map;
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;
43
44 /**
45  * Utilities for handling RESTful communication for Apex.
46  *
47  * @author Liam Fallon (liam.fallon@ericsson.com)
48  */
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...>
52     /**
53      * starts with some kind of bracket [ or ( or {, then has something, then has
54      * bracket.
55      */
56     private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s+\\S]*[\\)\\}\\]]";
57
58     /**
59      * Constructor, block inheritance.
60      */
61     private RestUtils() {
62         // Private constructor to block subclassing
63     }
64
65     /**
66      * HTTP POST requests can't send nulls so we interpret blanks as nulls.
67      *
68      * @param parameter the parameter to convert from blank to null
69      * @return null if the parameter us blank, otherwise the original parameter
70      */
71     private static String blank2null(final String parameter) {
72         return (parameter.length() == 0 ? null : parameter);
73     }
74
75     /**
76      * HTTP POST requests can't send nulls so we interpret blanks as nulls.
77      *
78      * @param val the val
79      * @return null if the parameter us blank, otherwise the original parameter
80      */
81     private static JsonElement blank2null(final JsonElement val) {
82         if (val == null) {
83             return JsonNull.INSTANCE;
84         }
85         if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) {
86             final String v = ((JsonPrimitive) val).getAsString();
87             if (v == null || "".equals(v)) {
88                 return JsonNull.INSTANCE;
89             }
90         }
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)));
95             }
96         }
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()));
101             }
102         }
103         return val;
104     }
105
106     /**
107      * Apex HTTP PUT requests send simple single level JSON strings, this method
108      * reads those strings into a map.
109      *
110      * @param jsonString the incoming JSON string
111      * @return a map of the JSON strings
112      */
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);
117
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())));
122         }
123         return jsonMap;
124     }
125
126     /**
127      * Apex HTTP PUT requests send simple single level JSON strings, this method
128      * reads those strings into a map.
129      *
130      * @param <C>        the generic type
131      * @param jsonString the incoming JSON string
132      * @param clz        the clz
133      * @return a map of the JSON strings
134      */
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);
139
140         for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
141             final JsonElement val = jsonEntry.getValue();
142             jsonEntry.setValue(blank2null(val));
143         }
144         return gb.create().fromJson(jsonObject, clz);
145     }
146
147     /**
148      * Gets the concept from JSON.
149      *
150      * @param <C>        the generic type
151      * @param jsonString the json string
152      * @param clz        the clz
153      * @return the concept from JSON
154      * @throws JAXBException the JAXB exception
155      */
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);
166         } else {
167             return null;
168         }
169         final StreamSource source = new StreamSource(new StringReader(jsonString));
170         final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, clz);
171         return rootElement.getValue();
172     }
173
174     /**
175      * Gets the JSO nfrom concept.
176      *
177      * @param object the object
178      * @return the JSO nfrom concept
179      */
180     public static String getJsonfromConcept(final Object object) {
181         final GsonBuilder gb = new GsonBuilder();
182         gb.serializeNulls().enableComplexMapKeySerialization();
183         return gb.create().toJson(object);
184     }
185 }