738f33cb10e1f93b5d40ab96c7268c4dd088a2ea
[policy/apex-pdp.git] / client / client-editor / src / main / java / org / onap / policy / apex / client / editor / rest / handling / RestUtils.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.client.editor.rest.handling;
22
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
30 import java.io.StringReader;
31 import java.util.Map;
32 import java.util.Map.Entry;
33 import java.util.TreeMap;
34
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
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;
45
46 /**
47  * Utilities for handling RESTful communication for Apex.
48  *
49  * @author Liam Fallon (liam.fallon@ericsson.com)
50  */
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...>
54     /**
55      * starts with some kind of bracket [ or ( or {, then has something, then has 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 String v = ((JsonPrimitive) val).getAsString();
88             if (v == null || "".equals(v)) {
89                 return JsonNull.INSTANCE;
90             }
91         }
92         if (val.isJsonArray()) {
93             final JsonArray arr = val.getAsJsonArray();
94             for (int i = 0; i < arr.size(); i++) {
95                 arr.set(i, blank2null(arr.get(i)));
96             }
97         }
98         if (val.isJsonObject()) {
99             final JsonObject 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 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(), (jsonEntry.getValue() == JsonNull.INSTANCE ? null
121                     : blank2null(jsonEntry.getValue().getAsString())));
122         }
123         return jsonMap;
124     }
125
126     /**
127      * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings into a map.
128      *
129      * @param <C> the generic type
130      * @param jsonString the incoming JSON string
131      * @param clz the clz
132      * @return a map of the JSON strings
133      */
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);
138
139         for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
140             final JsonElement val = jsonEntry.getValue();
141             jsonEntry.setValue(blank2null(val));
142         }
143         return gb.create().fromJson(jsonObject, clz);
144     }
145
146     /**
147      * Gets the concept from JSON.
148      *
149      * @param <C> the generic type
150      * @param jsonString the json string
151      * @param clz the clz
152      * @return the concept from JSON
153      * @throws JAXBException the JAXB exception
154      */
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);
165         } else {
166             return null;
167         }
168         final StreamSource source = new StreamSource(new StringReader(jsonString));
169         final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, clz);
170         return rootElement.getValue();
171     }
172
173     /**
174      * Gets the JSO nfrom concept.
175      *
176      * @param object the object
177      * @return the JSO nfrom concept
178      */
179     public static String getJsonfromConcept(final Object object) {
180         final GsonBuilder gb = new GsonBuilder();
181         gb.serializeNulls().enableComplexMapKeySerialization();
182         return gb.create().toJson(object);
183     }
184 }