Changes for checkstyle 8.32
[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 import java.io.StringReader;
30 import java.util.Map;
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;
42
43 /**
44  * Utilities for handling RESTful communication for Apex.
45  *
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  */
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...>
51     /**
52      * starts with some kind of bracket [ or ( or {, then has something, then has bracket.
53      */
54     private static final String JSON_INPUT_TYPE_REGEXP = "^\\s*[\\(\\{\\[][\\s+\\S]*[\\)\\}\\]]";
55
56     /**
57      * Constructor, block inheritance.
58      */
59     private RestUtils() {
60         // Private constructor to block subclassing
61     }
62
63     /**
64      * HTTP POST requests can't send nulls so we interpret blanks as nulls.
65      *
66      * @param parameter the parameter to convert from blank to null
67      * @return null if the parameter us blank, otherwise the original parameter
68      */
69     private static String blank2null(final String parameter) {
70         return (parameter.length() == 0 ? null : parameter);
71     }
72
73     /**
74      * HTTP POST requests can't send nulls so we interpret blanks as nulls.
75      *
76      * @param val the val
77      * @return null if the parameter us blank, otherwise the original parameter
78      */
79     private static JsonElement blank2null(final JsonElement val) {
80         if (val == null) {
81             return JsonNull.INSTANCE;
82         }
83         if (val.isJsonPrimitive() && ((JsonPrimitive) val).isString()) {
84             final String v = ((JsonPrimitive) val).getAsString();
85             if (v == null || "".equals(v)) {
86                 return JsonNull.INSTANCE;
87             }
88         }
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)));
93             }
94         }
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()));
99             }
100         }
101         return val;
102     }
103
104     /**
105      * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings into a map.
106      *
107      * @param jsonString the incoming JSON string
108      * @return a map of the JSON strings
109      */
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);
114
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())));
119         }
120         return jsonMap;
121     }
122
123     /**
124      * Apex HTTP PUT requests send simple single level JSON strings, this method reads those strings into a map.
125      *
126      * @param <C> the generic type
127      * @param jsonString the incoming JSON string
128      * @param clz the clz
129      * @return a map of the JSON strings
130      */
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);
135
136         for (final Entry<String, JsonElement> jsonEntry : jsonObject.entrySet()) {
137             final JsonElement val = jsonEntry.getValue();
138             jsonEntry.setValue(blank2null(val));
139         }
140         return gb.create().fromJson(jsonObject, clz);
141     }
142
143     /**
144      * Gets the concept from JSON.
145      *
146      * @param <C> the generic type
147      * @param jsonString the json string
148      * @param clz the clz
149      * @return the concept from JSON
150      * @throws JAXBException the JAXB exception
151      */
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);
162         } else {
163             return null;
164         }
165         final StreamSource source = new StreamSource(new StringReader(jsonString));
166         final JAXBElement<C> rootElement = unmarshaller.unmarshal(source, clz);
167         return rootElement.getValue();
168     }
169
170     /**
171      * Gets the JSO nfrom concept.
172      *
173      * @param object the object
174      * @return the JSO nfrom concept
175      */
176     public static String getJsonfromConcept(final Object object) {
177         final GsonBuilder gb = new GsonBuilder();
178         gb.serializeNulls().enableComplexMapKeySerialization();
179         return gb.create().toJson(object);
180     }
181 }