Refactor Editor REST servlet
[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
64     /**
65      * HTTP POST requests can't send nulls so we interpret blanks as nulls.
66      *
67      * @param parameter the parameter to convert from blank to null
68      * @return null if the parameter us blank, otherwise the original parameter
69      */
70     private static String blank2null(final String parameter) {
71         return (parameter.length() == 0 ? null : parameter);
72     }
73
74     /**
75      * HTTP POST requests can't send nulls so we interpret blanks as nulls.
76      *
77      * @param val the val
78      * @return null if the parameter us blank, otherwise the original parameter
79      */
80     private static JsonElement blank2null(final JsonElement val) {
81         if (val == null) {
82             return JsonNull.INSTANCE;
83         }
84         if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) {
85             final String v = ((JsonPrimitive) val).getAsString();
86             if (v == null || "".equals(v)) {
87                 return JsonNull.INSTANCE;
88             }
89         }
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)));
94             }
95         }
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()));
100             }
101         }
102         return val;
103     }
104
105     /**
106      * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings
107      * into a map.
108      *
109      * @param jsonString the incoming JSON string
110      * @return a map of the JSON strings
111      */
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);
116
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())));
121         }
122         return jsonMap;
123     }
124
125     /**
126      * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings
127      * 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 }