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